4
4
import torch .nn as nn
5
5
import torch .nn .functional as F
6
6
7
-
8
7
from torch .nn import Conv2d , MaxPool2d
9
8
from ppdettorch .core .workspace import register , serializable
10
9
from ..shape_spec import ShapeSpec
@@ -34,21 +33,18 @@ def __init__(self,
34
33
padding = 1 )
35
34
self .conv_out_list = []
36
35
for i in range (1 , groups ):
37
- conv_out = self .add_sublayer (
38
- 'conv{}' .format (i ),
39
- Conv2d (
40
- in_channels = out_channels ,
41
- out_channels = out_channels ,
42
- kernel_size = 3 ,
43
- stride = 1 ,
44
- padding = 1 ))
36
+ conv_out = Conv2d (in_channels = out_channels ,
37
+ out_channels = out_channels ,
38
+ kernel_size = 3 ,
39
+ stride = 1 ,
40
+ padding = 1 )
41
+ self .add_module ('conv{}' .format (i ), conv_out )
45
42
self .conv_out_list .append (conv_out )
46
43
47
- self .pool = MaxPool2d (
48
- kernel_size = pool_size ,
49
- stride = pool_stride ,
50
- padding = pool_padding ,
51
- ceil_mode = True )
44
+ self .pool = MaxPool2d (kernel_size = pool_size ,
45
+ stride = pool_stride ,
46
+ padding = pool_padding ,
47
+ ceil_mode = True )
52
48
53
49
def forward (self , inputs ):
54
50
out = self .conv0 (inputs )
@@ -95,12 +91,10 @@ def forward(self, inputs):
95
91
class L2NormScale (nn .Module ):
96
92
def __init__ (self , num_channels , scale = 1.0 ):
97
93
super (L2NormScale , self ).__init__ ()
98
- self .scale = self .create_parameter (
99
- attr = ParamAttr (initializer = paddle .nn .initializer .Constant (scale )),
100
- shape = [num_channels ])
94
+ self .scale = nn .Parameter (torch .ones ([num_channels ]), requires_grad = False )
101
95
102
96
def forward (self , inputs ):
103
- out = F .normalize (inputs , axis = 1 , epsilon = 1e-10 )
97
+ out = F .normalize (inputs , dim = 1 , eps = 1e-10 )
104
98
# out = self.scale.unsqueeze(0).unsqueeze(2).unsqueeze(3).expand_as(
105
99
# out) * out
106
100
out = self .scale .unsqueeze (0 ).unsqueeze (2 ).unsqueeze (3 ) * out
@@ -119,7 +113,7 @@ def __init__(self,
119
113
super (VGG , self ).__init__ ()
120
114
121
115
assert depth in [16 , 19 ], \
122
- "depth as 16/19 supported currently, but got {}" .format (depth )
116
+ "depth as 16/19 supported currently, but got {}" .format (depth )
123
117
self .depth = depth
124
118
self .groups = VGG_cfg [depth ]
125
119
self .normalizations = normalizations
@@ -159,19 +153,18 @@ def __init__(self,
159
153
last_channels = 1024
160
154
for i , v in enumerate (self .extra_block_filters ):
161
155
assert len (v ) == 5 , "extra_block_filters size not fix"
162
- extra_conv = self . add_sublayer ( "conv{}" . format ( 6 + i ) ,
163
- ExtraBlock ( last_channels , v [0 ], v [1 ],
164
- v [ 2 ], v [ 3 ], v [ 4 ]) )
156
+ extra_conv = ExtraBlock ( last_channels , v [ 0 ], v [ 1 ] ,
157
+ v [ 2 ] , v [3 ], v [4 ])
158
+ self . add_module ( "conv{}" . format ( 6 + i ), extra_conv )
165
159
last_channels = v [1 ]
166
160
self .extra_convs .append (extra_conv )
167
161
self ._out_channels .append (last_channels )
168
162
169
163
self .norms = []
170
164
for i , n in enumerate (self .normalizations ):
171
165
if n != - 1 :
172
- norm = self .add_sublayer ("norm{}" .format (i ),
173
- L2NormScale (
174
- self .extra_block_filters [i ][1 ], n ))
166
+ norm = L2NormScale (self .extra_block_filters [i ][1 ], n )
167
+ self .add_module ("norm{}" .format (i ), norm )
175
168
else :
176
169
norm = None
177
170
self .norms .append (norm )
0 commit comments