@@ -118,8 +118,11 @@ def _cfg(url='', **kwargs):
118
118
'ecaresnet101d_pruned' : _cfg (
119
119
url = 'https://imvl-automl-sh.oss-cn-shanghai.aliyuncs.com/darts/hyperml/hyperml/job_45610/outputs/ECAResNet101D_P_75a3370e.pth' ,
120
120
interpolation = 'bicubic' ),
121
- 'resnetblur18' : _cfg (),
122
- 'resnetblur50' : _cfg ()
121
+ 'resnetblur18' : _cfg (
122
+ interpolation = 'bicubic' ),
123
+ 'resnetblur50' : _cfg (
124
+ url = 'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/resnetblur50-84f4748f.pth' ,
125
+ interpolation = 'bicubic' )
123
126
}
124
127
125
128
@@ -133,21 +136,22 @@ class BasicBlock(nn.Module):
133
136
134
137
def __init__ (self , inplanes , planes , stride = 1 , downsample = None , cardinality = 1 , base_width = 64 ,
135
138
reduce_first = 1 , dilation = 1 , first_dilation = None , act_layer = nn .ReLU , norm_layer = nn .BatchNorm2d ,
136
- attn_layer = None , drop_block = None , drop_path = None , blur = False ):
139
+ attn_layer = None , aa_layer = None , drop_block = None , drop_path = None ):
137
140
super (BasicBlock , self ).__init__ ()
138
141
139
142
assert cardinality == 1 , 'BasicBlock only supports cardinality of 1'
140
143
assert base_width == 64 , 'BasicBlock does not support changing base width'
141
144
first_planes = planes // reduce_first
142
145
outplanes = planes * self .expansion
143
146
first_dilation = first_dilation or dilation
147
+ use_aa = aa_layer is not None
144
148
145
149
self .conv1 = nn .Conv2d (
146
- inplanes , first_planes , kernel_size = 3 , stride = 1 if blur else stride , padding = first_dilation ,
150
+ inplanes , first_planes , kernel_size = 3 , stride = 1 if use_aa else stride , padding = first_dilation ,
147
151
dilation = first_dilation , bias = False )
148
152
self .bn1 = norm_layer (first_planes )
149
153
self .act1 = act_layer (inplace = True )
150
- self .blurpool = BlurPool2d (channels = first_planes ) if stride == 2 and blur else None
154
+ self .aa = aa_layer (channels = first_planes ) if stride == 2 and use_aa else None
151
155
152
156
self .conv2 = nn .Conv2d (
153
157
first_planes , outplanes , kernel_size = 3 , padding = dilation , dilation = dilation , bias = False )
@@ -173,8 +177,8 @@ def forward(self, x):
173
177
if self .drop_block is not None :
174
178
x = self .drop_block (x )
175
179
x = self .act1 (x )
176
- if self .blurpool is not None :
177
- x = self .blurpool (x )
180
+ if self .aa is not None :
181
+ x = self .aa (x )
178
182
179
183
x = self .conv2 (x )
180
184
x = self .bn2 (x )
@@ -201,25 +205,25 @@ class Bottleneck(nn.Module):
201
205
202
206
def __init__ (self , inplanes , planes , stride = 1 , downsample = None , cardinality = 1 , base_width = 64 ,
203
207
reduce_first = 1 , dilation = 1 , first_dilation = None , act_layer = nn .ReLU , norm_layer = nn .BatchNorm2d ,
204
- attn_layer = None , drop_block = None , drop_path = None , blur = False ):
208
+ attn_layer = None , aa_layer = None , drop_block = None , drop_path = None ):
205
209
super (Bottleneck , self ).__init__ ()
206
210
207
211
width = int (math .floor (planes * (base_width / 64 )) * cardinality )
208
212
first_planes = width // reduce_first
209
213
outplanes = planes * self .expansion
210
214
first_dilation = first_dilation or dilation
211
- self . blur = blur
215
+ use_aa = aa_layer is not None
212
216
213
217
self .conv1 = nn .Conv2d (inplanes , first_planes , kernel_size = 1 , bias = False )
214
218
self .bn1 = norm_layer (first_planes )
215
219
self .act1 = act_layer (inplace = True )
216
220
217
221
self .conv2 = nn .Conv2d (
218
- first_planes , width , kernel_size = 3 , stride = 1 if blur else stride ,
222
+ first_planes , width , kernel_size = 3 , stride = 1 if use_aa else stride ,
219
223
padding = first_dilation , dilation = first_dilation , groups = cardinality , bias = False )
220
224
self .bn2 = norm_layer (width )
221
225
self .act2 = act_layer (inplace = True )
222
- self .blurpool = BlurPool2d (channels = width ) if stride == 2 and blur else None
226
+ self .aa = aa_layer (channels = width ) if stride == 2 and use_aa else None
223
227
224
228
self .conv3 = nn .Conv2d (width , outplanes , kernel_size = 1 , bias = False )
225
229
self .bn3 = norm_layer (outplanes )
@@ -250,8 +254,8 @@ def forward(self, x):
250
254
if self .drop_block is not None :
251
255
x = self .drop_block (x )
252
256
x = self .act2 (x )
253
- if self .blurpool is not None :
254
- x = self .blurpool (x )
257
+ if self .aa is not None :
258
+ x = self .aa (x )
255
259
256
260
x = self .conv3 (x )
257
261
x = self .bn3 (x )
@@ -365,25 +369,19 @@ class ResNet(nn.Module):
365
369
Whether to use average pooling for projection skip connection between stages/downsample.
366
370
output_stride : int, default 32
367
371
Set the output stride of the network, 32, 16, or 8. Typically used in segmentation.
368
- act_layer : class, activation layer
369
- norm_layer : class, normalization layer
372
+ act_layer : nn.Module, activation layer
373
+ norm_layer : nn.Module, normalization layer
374
+ aa_layer : nn.Module, anti-aliasing layer
370
375
drop_rate : float, default 0.
371
376
Dropout probability before classifier, for training
372
377
global_pool : str, default 'avg'
373
378
Global pooling type. One of 'avg', 'max', 'avgmax', 'catavgmax'
374
- blur : str, default ''
375
- Location of Blurring:
376
- * '', default - Not applied
377
- * 'max' - only stem layer MaxPool will be blurred
378
- * 'strided' - only strided convolutions in the downsampling blocks (assembled-cnn style)
379
- * 'max_strided' - on both stem MaxPool and strided convolutions (zhang2019shiftinvar style for ResNets)
380
-
381
379
"""
382
380
def __init__ (self , block , layers , num_classes = 1000 , in_chans = 3 ,
383
381
cardinality = 1 , base_width = 64 , stem_width = 64 , stem_type = '' ,
384
382
block_reduce_first = 1 , down_kernel_size = 1 , avg_down = False , output_stride = 32 ,
385
- act_layer = nn .ReLU , norm_layer = nn .BatchNorm2d , drop_rate = 0.0 , drop_path_rate = 0. ,
386
- drop_block_rate = 0. , global_pool = 'avg' , blur = '' , zero_init_last_bn = True , block_args = None ):
383
+ act_layer = nn .ReLU , norm_layer = nn .BatchNorm2d , aa_layer = None , drop_rate = 0.0 , drop_path_rate = 0. ,
384
+ drop_block_rate = 0. , global_pool = 'avg' , zero_init_last_bn = True , block_args = None ):
387
385
block_args = block_args or dict ()
388
386
self .num_classes = num_classes
389
387
deep_stem = 'deep' in stem_type
@@ -392,7 +390,6 @@ def __init__(self, block, layers, num_classes=1000, in_chans=3,
392
390
self .base_width = base_width
393
391
self .drop_rate = drop_rate
394
392
self .expansion = block .expansion
395
- self .blur = 'strided' in blur
396
393
super (ResNet , self ).__init__ ()
397
394
398
395
# Stem
@@ -414,12 +411,12 @@ def __init__(self, block, layers, num_classes=1000, in_chans=3,
414
411
self .bn1 = norm_layer (self .inplanes )
415
412
self .act1 = act_layer (inplace = True )
416
413
# Stem Pooling
417
- if 'max' in blur :
414
+ if aa_layer is not None :
418
415
self .maxpool = nn .Sequential (* [
419
416
nn .MaxPool2d (kernel_size = 3 , stride = 1 , padding = 1 ),
420
- BlurPool2d (channels = self .inplanes , stride = 2 )
417
+ aa_layer (channels = self .inplanes , stride = 2 )
421
418
])
422
- else :
419
+ else :
423
420
self .maxpool = nn .MaxPool2d (kernel_size = 3 , stride = 2 , padding = 1 )
424
421
425
422
# Feature Blocks
@@ -437,7 +434,7 @@ def __init__(self, block, layers, num_classes=1000, in_chans=3,
437
434
assert output_stride == 32
438
435
layer_args = list (zip (channels , layers , strides , dilations ))
439
436
layer_kwargs = dict (
440
- reduce_first = block_reduce_first , act_layer = act_layer , norm_layer = norm_layer ,
437
+ reduce_first = block_reduce_first , act_layer = act_layer , norm_layer = norm_layer , aa_layer = aa_layer ,
441
438
avg_down = avg_down , down_kernel_size = down_kernel_size , drop_path = dp , ** block_args )
442
439
self .layer1 = self ._make_layer (block , * layer_args [0 ], ** layer_kwargs )
443
440
self .layer2 = self ._make_layer (block , * layer_args [1 ], ** layer_kwargs )
@@ -472,7 +469,7 @@ def _make_layer(self, block, planes, blocks, stride=1, dilation=1, reduce_first=
472
469
473
470
block_kwargs = dict (
474
471
cardinality = self .cardinality , base_width = self .base_width , reduce_first = reduce_first ,
475
- dilation = dilation , blur = self . blur , ** kwargs )
472
+ dilation = dilation , ** kwargs )
476
473
layers = [block (self .inplanes , planes , stride , downsample , first_dilation = first_dilation , ** block_kwargs )]
477
474
self .inplanes = planes * block .expansion
478
475
layers += [block (self .inplanes , planes , ** block_kwargs ) for _ in range (1 , blocks )]
@@ -1148,18 +1145,21 @@ def resnetblur18(pretrained=False, num_classes=1000, in_chans=3, **kwargs):
1148
1145
"""Constructs a ResNet-18 model with blur anti-aliasing
1149
1146
"""
1150
1147
default_cfg = default_cfgs ['resnetblur18' ]
1151
- model = ResNet (BasicBlock , [2 , 2 , 2 , 2 ], num_classes = num_classes , in_chans = in_chans , blur = 'max_strided' ,** kwargs )
1148
+ model = ResNet (
1149
+ BasicBlock , [2 , 2 , 2 , 2 ], num_classes = num_classes , in_chans = in_chans , aa_layer = BlurPool2d , ** kwargs )
1152
1150
model .default_cfg = default_cfg
1153
1151
if pretrained :
1154
1152
load_pretrained (model , default_cfg , num_classes , in_chans )
1155
1153
return model
1156
1154
1155
+
1157
1156
@register_model
1158
1157
def resnetblur50 (pretrained = False , num_classes = 1000 , in_chans = 3 , ** kwargs ):
1159
1158
"""Constructs a ResNet-50 model with blur anti-aliasing
1160
1159
"""
1161
1160
default_cfg = default_cfgs ['resnetblur50' ]
1162
- model = ResNet (Bottleneck , [3 , 4 , 6 , 3 ], num_classes = num_classes , in_chans = in_chans , blur = 'max_strided' , ** kwargs )
1161
+ model = ResNet (
1162
+ Bottleneck , [3 , 4 , 6 , 3 ], num_classes = num_classes , in_chans = in_chans , aa_layer = BlurPool2d , ** kwargs )
1163
1163
model .default_cfg = default_cfg
1164
1164
if pretrained :
1165
1165
load_pretrained (model , default_cfg , num_classes , in_chans )
0 commit comments