-
Notifications
You must be signed in to change notification settings - Fork 32
/
ffc_resnet.py
278 lines (219 loc) · 10.4 KB
/
ffc_resnet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import torch.nn as nn
from .ffc import *
__all__ = ['FFCResNet', 'ffc_resnet18', 'ffc_resnet34',
'ffc_resnet26', 'ffc_resnet50', 'ffc_resnet101',
'ffc_resnet152', 'ffc_resnet200', 'ffc_resnext50_32x4d',
'ffc_resnext101_32x8d']
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
base_width=64, dilation=1, ratio_gin=0.5, ratio_gout=0.5, lfu=True, use_se=False, norm_layer=None):
super(BasicBlock, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
if groups != 1 or base_width != 64:
raise ValueError(
"BasicBlock only supports groups=1 and base_width=64")
if dilation > 1:
raise NotImplementedError(
"Dilation > 1 not supported in BasicBlock")
width = int(planes * (base_width / 64.)) * groups
# Both self.conv2 and self.downsample layers downsample the input when
# stride != 1
self.conv1 = FFC_BN_ACT(inplanes, width, kernel_size=3, padding=1, stride=stride,
ratio_gin=ratio_gin, ratio_gout=ratio_gout, norm_layer=norm_layer, activation_layer=nn.ReLU, enable_lfu=lfu)
self.conv2 = FFC_BN_ACT(width, planes * self.expansion, kernel_size=3, padding=1,
ratio_gin=ratio_gout, ratio_gout=ratio_gout, norm_layer=norm_layer, enable_lfu=lfu)
self.se_block = FFCSE_block(
planes * self.expansion, ratio_gout) if use_se else nn.Identity()
self.relu_l = nn.Identity() if ratio_gout == 1 else nn.ReLU(inplace=True)
self.relu_g = nn.Identity() if ratio_gout == 0 else nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
x = x if type(x) is tuple else (x, 0)
id_l, id_g = x if self.downsample is None else self.downsample(x)
x = self.conv1(x)
x_l, x_g = self.conv2(x)
x_l, x_g = self.se_block(x)
x_l = self.relu_l(x_l + id_l)
x_g = self.relu_g(x_g + id_g)
return x_l, x_g
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
base_width=64, dilation=1, ratio_gin=0.5, ratio_gout=0.5, lfu=True, use_se=False):
super(Bottleneck, self).__init__()
width = int(planes * (base_width / 64.)) * groups
# Both self.conv2 and self.downsample layers downsample the input when
# stride != 1
self.conv1 = FFC_BN_ACT(inplanes, width, kernel_size=1,
ratio_gin=ratio_gin, ratio_gout=ratio_gout,
activation_layer=nn.ReLU, enable_lfu=lfu)
self.conv2 = FFC_BN_ACT(width, width, kernel_size=3,
ratio_gin=ratio_gout, ratio_gout=ratio_gout,
stride=stride, padding=1, groups=groups,
activation_layer=nn.ReLU, enable_lfu=lfu)
self.conv3 = FFC_BN_ACT(width, planes * self.expansion, kernel_size=1,
ratio_gin=ratio_gout, ratio_gout=ratio_gout, enable_lfu=lfu)
self.se_block = FFCSE_block(
planes * self.expansion, ratio_gout) if use_se else nn.Identity()
self.relu_l = nn.Identity() if ratio_gout == 1 else nn.ReLU(inplace=True)
self.relu_g = nn.Identity() if ratio_gout == 0 else nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
x = x if type(x) is tuple else (x, 0)
id_l, id_g = x if self.downsample is None else self.downsample(x)
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x_l, x_g = self.se_block(x)
x_l = self.relu_l(x_l + id_l)
x_g = self.relu_g(x_g + id_g)
return x_l, x_g
class FFCResNet(nn.Module):
def __init__(self, block, layers, num_classes=1000, zero_init_residual=False,
groups=1, width_per_group=64, norm_layer=None, ratio=0.5, lfu=True, use_se=False):
super(FFCResNet, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self._norm_layer = norm_layer
inplanes = 64
# TODO add ratio-inplanes-groups assertion
self.inplanes = inplanes
self.dilation = 1
self.groups = groups
self.base_width = width_per_group
self.lfu = lfu
self.use_se = use_se
self.conv1 = nn.Conv2d(3, inplanes, kernel_size=7,
stride=2, padding=3, bias=False)
self.bn1 = norm_layer(inplanes)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(
block, inplanes * 1, layers[0], stride=1, ratio_gin=0, ratio_gout=ratio)
self.layer2 = self._make_layer(
block, inplanes * 2, layers[1], stride=2, ratio_gin=ratio, ratio_gout=ratio)
self.layer3 = self._make_layer(
block, inplanes * 4, layers[2], stride=2, ratio_gin=ratio, ratio_gout=ratio)
self.layer4 = self._make_layer(
block, inplanes * 8, layers[3], stride=2, ratio_gin=ratio, ratio_gout=0)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(inplanes * 8 * block.expansion, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(
m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
# Zero-initialize the last BN in each residual branch,
# so that the residual branch starts with zeros, and each residual block behaves like an identity.
# This improves the model by 0.2~0.3% according to
# https://arxiv.org/abs/1706.02677
if zero_init_residual:
for m in self.modules():
if isinstance(m, Bottleneck):
nn.init.constant_(m.bn3.weight, 0)
elif isinstance(m, BasicBlock):
nn.init.constant_(m.bn2.weight, 0)
def _make_layer(self, block, planes, blocks, stride=1, ratio_gin=0.5, ratio_gout=0.5):
norm_layer = self._norm_layer
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion or ratio_gin == 0:
downsample = FFC_BN_ACT(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride,
ratio_gin=ratio_gin, ratio_gout=ratio_gout, enable_lfu=self.lfu)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample, self.groups, self.base_width,
self.dilation, ratio_gin, ratio_gout, lfu=self.lfu, use_se=self.use_se))
self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes, groups=self.groups, base_width=self.base_width, dilation=self.dilation,
ratio_gin=ratio_gout, ratio_gout=ratio_gout, lfu=self.lfu, use_se=self.use_se))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x[0])
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
def ffc_resnet18(pretrained=False, **kwargs):
"""Constructs a FFT ResNet-18 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = FFCResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
return model
def ffc_resnet34(pretrained=False, **kwargs):
"""Constructs a FFT ResNet-34 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = FFCResNet(BasicBlock, [3, 4, 6, 3], **kwargs)
return model
def ffc_resnet26(pretrained=False, **kwargs):
"""Constructs a FFT ResNet-26 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = FFCResNet(Bottleneck, [2, 2, 2, 2], **kwargs)
return model
def ffc_resnet50(pretrained=False, **kwargs):
"""Constructs a FFT ResNet-50 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = FFCResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
return model
def ffc_resnet101(pretrained=False, **kwargs):
"""Constructs a FFT ResNet-101 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = FFCResNet(Bottleneck, [3, 4, 23, 3], **kwargs)
return model
def ffc_resnet152(pretrained=False, **kwargs):
"""Constructs a FFT ResNet-152 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = FFCResNet(Bottleneck, [3, 8, 36, 3], **kwargs)
return model
def ffc_resnet200(pretrained=False, **kwargs):
"""Constructs a FFT ResNet-200 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = FFCResNet(Bottleneck, [3, 24, 36, 3], **kwargs)
return model
def ffc_resnext50_32x4d(pretrained=False, **kwargs):
r"""ResNeXt-50 32x4d model from
`"Aggregated Residual Transformation for Deep Neural Networks" <https://arxiv.org/pdf/1611.05431.pdf>`_
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
progress (bool): If True, displays a progress bar of the download to stderr
"""
kwargs['groups'] = 32
kwargs['width_per_group'] = 4
model = FFCResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
return model
def ffc_resnext101_32x8d(pretrained=False, **kwargs):
r"""ResNeXt-101 32x8d model from
`"Aggregated Residual Transformation for Deep Neural Networks" <https://arxiv.org/pdf/1611.05431.pdf>`_
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
progress (bool): If True, displays a progress bar of the download to stderr
"""
kwargs['groups'] = 32
kwargs['width_per_group'] = 8
model = FFCResNet(Bottleneck, [3, 4, 32, 3], **kwargs)
return model