@@ -38,14 +38,15 @@ def forward(self, input: Tensor) -> Tensor:
38
38
class InvertedResidualConfig :
39
39
40
40
def __init__ (self , input_channels : int , kernel : int , expanded_channels : int , out_channels : int , use_se : bool ,
41
- activation : str , stride : int , width_mult : float ):
41
+ activation : str , stride : int , dilation : int , width_mult : float ):
42
42
self .input_channels = self .adjust_channels (input_channels , width_mult )
43
43
self .kernel = kernel
44
44
self .expanded_channels = self .adjust_channels (expanded_channels , width_mult )
45
45
self .out_channels = self .adjust_channels (out_channels , width_mult )
46
46
self .use_se = use_se
47
47
self .use_hs = activation == "HS"
48
48
self .stride = stride
49
+ self .dilation = dilation
49
50
50
51
@staticmethod
51
52
def adjust_channels (channels : int , width_mult : float ):
@@ -70,9 +71,10 @@ def __init__(self, cnf: InvertedResidualConfig, norm_layer: Callable[..., nn.Mod
70
71
norm_layer = norm_layer , activation_layer = activation_layer ))
71
72
72
73
# depthwise
74
+ stride = 1 if cnf .dilation > 1 else cnf .stride
73
75
layers .append (ConvBNActivation (cnf .expanded_channels , cnf .expanded_channels , kernel_size = cnf .kernel ,
74
- stride = cnf . stride , groups = cnf .expanded_channels , norm_layer = norm_layer ,
75
- activation_layer = activation_layer ))
76
+ stride = stride , dilation = cnf .dilation , groups = cnf . expanded_channels ,
77
+ norm_layer = norm_layer , activation_layer = activation_layer ))
76
78
if cnf .use_se :
77
79
layers .append (SqueezeExcitation (cnf .expanded_channels ))
78
80
@@ -82,7 +84,7 @@ def __init__(self, cnf: InvertedResidualConfig, norm_layer: Callable[..., nn.Mod
82
84
83
85
self .block = nn .Sequential (* layers )
84
86
self .out_channels = cnf .out_channels
85
- self .is_strided = cnf .stride > 1
87
+ self ._is_cn = cnf .stride > 1
86
88
87
89
def forward (self , input : Tensor ) -> Tensor :
88
90
result = self .block (input )
@@ -194,78 +196,74 @@ def _mobilenet_v3(
194
196
return model
195
197
196
198
197
- def mobilenet_v3_large (pretrained : bool = False , progress : bool = True , reduced_tail : bool = False ,
198
- ** kwargs : Any ) -> MobileNetV3 :
199
+ def mobilenet_v3_large (pretrained : bool = False , progress : bool = True , ** kwargs : Any ) -> MobileNetV3 :
199
200
"""
200
201
Constructs a large MobileNetV3 architecture from
201
202
`"Searching for MobileNetV3" <https://arxiv.org/abs/1905.02244>`_.
202
203
203
204
Args:
204
205
pretrained (bool): If True, returns a model pre-trained on ImageNet
205
206
progress (bool): If True, displays a progress bar of the download to stderr
206
- reduced_tail (bool): If True, reduces the channel counts of all feature layers
207
- between C4 and C5 by 2. It is used to reduce the channel redundancy in the
208
- backbone for Detection and Segmentation.
209
207
"""
208
+ # non-public config parameters
209
+ reduce_divider = 2 if kwargs .pop ('_reduced_tail' , False ) else 1
210
+ dilation = 2 if kwargs .pop ('_dilated' , False ) else 1
210
211
width_mult = 1.0
212
+
211
213
bneck_conf = partial (InvertedResidualConfig , width_mult = width_mult )
212
214
adjust_channels = partial (InvertedResidualConfig .adjust_channels , width_mult = width_mult )
213
215
214
- reduce_divider = 2 if reduced_tail else 1
215
-
216
216
inverted_residual_setting = [
217
- bneck_conf (16 , 3 , 16 , 16 , False , "RE" , 1 ),
218
- bneck_conf (16 , 3 , 64 , 24 , False , "RE" , 2 ), # C1
219
- bneck_conf (24 , 3 , 72 , 24 , False , "RE" , 1 ),
220
- bneck_conf (24 , 5 , 72 , 40 , True , "RE" , 2 ), # C2
221
- bneck_conf (40 , 5 , 120 , 40 , True , "RE" , 1 ),
222
- bneck_conf (40 , 5 , 120 , 40 , True , "RE" , 1 ),
223
- bneck_conf (40 , 3 , 240 , 80 , False , "HS" , 2 ), # C3
224
- bneck_conf (80 , 3 , 200 , 80 , False , "HS" , 1 ),
225
- bneck_conf (80 , 3 , 184 , 80 , False , "HS" , 1 ),
226
- bneck_conf (80 , 3 , 184 , 80 , False , "HS" , 1 ),
227
- bneck_conf (80 , 3 , 480 , 112 , True , "HS" , 1 ),
228
- bneck_conf (112 , 3 , 672 , 112 , True , "HS" , 1 ),
229
- bneck_conf (112 , 5 , 672 , 160 // reduce_divider , True , "HS" , 2 ), # C4
230
- bneck_conf (160 // reduce_divider , 5 , 960 // reduce_divider , 160 // reduce_divider , True , "HS" , 1 ),
231
- bneck_conf (160 // reduce_divider , 5 , 960 // reduce_divider , 160 // reduce_divider , True , "HS" , 1 ),
217
+ bneck_conf (16 , 3 , 16 , 16 , False , "RE" , 1 , 1 ),
218
+ bneck_conf (16 , 3 , 64 , 24 , False , "RE" , 2 , 1 ), # C1
219
+ bneck_conf (24 , 3 , 72 , 24 , False , "RE" , 1 , 1 ),
220
+ bneck_conf (24 , 5 , 72 , 40 , True , "RE" , 2 , 1 ), # C2
221
+ bneck_conf (40 , 5 , 120 , 40 , True , "RE" , 1 , 1 ),
222
+ bneck_conf (40 , 5 , 120 , 40 , True , "RE" , 1 , 1 ),
223
+ bneck_conf (40 , 3 , 240 , 80 , False , "HS" , 2 , 1 ), # C3
224
+ bneck_conf (80 , 3 , 200 , 80 , False , "HS" , 1 , 1 ),
225
+ bneck_conf (80 , 3 , 184 , 80 , False , "HS" , 1 , 1 ),
226
+ bneck_conf (80 , 3 , 184 , 80 , False , "HS" , 1 , 1 ),
227
+ bneck_conf (80 , 3 , 480 , 112 , True , "HS" , 1 , 1 ),
228
+ bneck_conf (112 , 3 , 672 , 112 , True , "HS" , 1 , 1 ),
229
+ bneck_conf (112 , 5 , 672 , 160 // reduce_divider , True , "HS" , 2 , dilation ), # C4
230
+ bneck_conf (160 // reduce_divider , 5 , 960 // reduce_divider , 160 // reduce_divider , True , "HS" , 1 , dilation ),
231
+ bneck_conf (160 // reduce_divider , 5 , 960 // reduce_divider , 160 // reduce_divider , True , "HS" , 1 , dilation ),
232
232
]
233
233
last_channel = adjust_channels (1280 // reduce_divider ) # C5
234
234
235
235
return _mobilenet_v3 ("mobilenet_v3_large" , inverted_residual_setting , last_channel , pretrained , progress , ** kwargs )
236
236
237
237
238
- def mobilenet_v3_small (pretrained : bool = False , progress : bool = True , reduced_tail : bool = False ,
239
- ** kwargs : Any ) -> MobileNetV3 :
238
+ def mobilenet_v3_small (pretrained : bool = False , progress : bool = True , ** kwargs : Any ) -> MobileNetV3 :
240
239
"""
241
240
Constructs a small MobileNetV3 architecture from
242
241
`"Searching for MobileNetV3" <https://arxiv.org/abs/1905.02244>`_.
243
242
244
243
Args:
245
244
pretrained (bool): If True, returns a model pre-trained on ImageNet
246
245
progress (bool): If True, displays a progress bar of the download to stderr
247
- reduced_tail (bool): If True, reduces the channel counts of all feature layers
248
- between C4 and C5 by 2. It is used to reduce the channel redundancy in the
249
- backbone for Detection and Segmentation.
250
246
"""
247
+ # non-public config parameters
248
+ reduce_divider = 2 if kwargs .pop ('_reduced_tail' , False ) else 1
249
+ dilation = 2 if kwargs .pop ('_dilated' , False ) else 1
251
250
width_mult = 1.0
251
+
252
252
bneck_conf = partial (InvertedResidualConfig , width_mult = width_mult )
253
253
adjust_channels = partial (InvertedResidualConfig .adjust_channels , width_mult = width_mult )
254
254
255
- reduce_divider = 2 if reduced_tail else 1
256
-
257
255
inverted_residual_setting = [
258
- bneck_conf (16 , 3 , 16 , 16 , True , "RE" , 2 ), # C1
259
- bneck_conf (16 , 3 , 72 , 24 , False , "RE" , 2 ), # C2
260
- bneck_conf (24 , 3 , 88 , 24 , False , "RE" , 1 ),
261
- bneck_conf (24 , 5 , 96 , 40 , True , "HS" , 2 ), # C3
262
- bneck_conf (40 , 5 , 240 , 40 , True , "HS" , 1 ),
263
- bneck_conf (40 , 5 , 240 , 40 , True , "HS" , 1 ),
264
- bneck_conf (40 , 5 , 120 , 48 , True , "HS" , 1 ),
265
- bneck_conf (48 , 5 , 144 , 48 , True , "HS" , 1 ),
266
- bneck_conf (48 , 5 , 288 , 96 // reduce_divider , True , "HS" , 2 ), # C4
267
- bneck_conf (96 // reduce_divider , 5 , 576 // reduce_divider , 96 // reduce_divider , True , "HS" , 1 ),
268
- bneck_conf (96 // reduce_divider , 5 , 576 // reduce_divider , 96 // reduce_divider , True , "HS" , 1 ),
256
+ bneck_conf (16 , 3 , 16 , 16 , True , "RE" , 2 , 1 ), # C1
257
+ bneck_conf (16 , 3 , 72 , 24 , False , "RE" , 2 , 1 ), # C2
258
+ bneck_conf (24 , 3 , 88 , 24 , False , "RE" , 1 , 1 ),
259
+ bneck_conf (24 , 5 , 96 , 40 , True , "HS" , 2 , 1 ), # C3
260
+ bneck_conf (40 , 5 , 240 , 40 , True , "HS" , 1 , 1 ),
261
+ bneck_conf (40 , 5 , 240 , 40 , True , "HS" , 1 , 1 ),
262
+ bneck_conf (40 , 5 , 120 , 48 , True , "HS" , 1 , 1 ),
263
+ bneck_conf (48 , 5 , 144 , 48 , True , "HS" , 1 , 1 ),
264
+ bneck_conf (48 , 5 , 288 , 96 // reduce_divider , True , "HS" , 2 , dilation ), # C4
265
+ bneck_conf (96 // reduce_divider , 5 , 576 // reduce_divider , 96 // reduce_divider , True , "HS" , 1 , dilation ),
266
+ bneck_conf (96 // reduce_divider , 5 , 576 // reduce_divider , 96 // reduce_divider , True , "HS" , 1 , dilation ),
269
267
]
270
268
last_channel = adjust_channels (1024 // reduce_divider ) # C5
271
269
0 commit comments