172 last_gamma=False):
173 self.cardinality = groups
174 self.bottleneck_width = bottleneck_width
175
176 self.inplanes = stem_width * 2 if deep_stem else 64
177 self.avg_down = avg_down
178 self.last_gamma = last_gamma
179
180 self.radix = radix
181 self.avd = avd
182 self.avd_first = avd_first
183
184 super().__init__()
185 self.rectified_conv = rectified_conv
186 self.rectify_avg = rectify_avg
187 if rectified_conv:
188 from rfconv import RFConv2d
189 conv_layer = RFConv2d
190 else:
191 conv_layer = nn.Conv2d
192 conv_kwargs = {'average_mode': rectify_avg} if rectified_conv else {}
193 if deep_stem:
194 self.conv1 = nn.Sequential(
195 conv_layer(3, stem_width, kernel_size=3, stride=2, padding=1, bias=False, **conv_kwargs),
196 get_norm(bn_norm, stem_width),
197 nn.ReLU(inplace=True),
198 conv_layer(stem_width, stem_width, kernel_size=3, stride=1, padding=1, bias=False, **conv_kwargs),
199 get_norm(bn_norm, stem_width),
200 nn.ReLU(inplace=True),
201 conv_layer(stem_width, stem_width * 2, kernel_size=3, stride=1, padding=1, bias=False, **conv_kwargs),
202 )
203 else:
204 self.conv1 = conv_layer(3, 64, kernel_size=7, stride=2, padding=3,
205 bias=False, **conv_kwargs)
206 self.bn1 = get_norm(bn_norm, self.inplanes)
207 self.relu = nn.ReLU(inplace=True)
208 self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
209 self.layer1 = self._make_layer(block, 64, layers[0], 1, bn_norm, with_ibn=with_ibn, is_first=False)
210 self.layer2 = self._make_layer(block, 128, layers[1], 2, bn_norm, with_ibn=with_ibn)
211 if dilated or dilation == 4:
212 self.layer3 = self._make_layer(block, 256, layers[2], 1, bn_norm, with_ibn=with_ibn,
213 dilation=2, dropblock_prob=dropblock_prob)
214 self.layer4 = self._make_layer(block, 512, layers[3], 1, bn_norm, with_ibn=with_ibn,
215 dilation=4, dropblock_prob=dropblock_prob)
216 elif dilation == 2:
217 self.layer3 = self._make_layer(block, 256, layers[2], 2, bn_norm, with_ibn=with_ibn,
218 dilation=1, dropblock_prob=dropblock_prob)
219 self.layer4 = self._make_layer(block, 512, layers[3], 1, bn_norm, with_ibn=with_ibn,
220 dilation=2, dropblock_prob=dropblock_prob)
221 else:
222 self.layer3 = self._make_layer(block, 256, layers[2], 2, bn_norm, with_ibn=with_ibn,
223 dropblock_prob=dropblock_prob)
224 self.layer4 = self._make_layer(block, 512, layers[3], last_stride, bn_norm, with_ibn=with_ibn,
225 dropblock_prob=dropblock_prob)
226
227 for m in self.modules():
228 if isinstance(m, nn.Conv2d):
229 n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
230 m.weight.data.normal_(0, math.sqrt(2. / n))
231 elif isinstance(m, nn.BatchNorm2d):
232 m.weight.data.fill_(1)
233 m.bias.data.zero_()
234
235
236 if with_nl: self._build_nonlocal(layers, non_layers, bn_norm)
237 else: self.NL_1_idx = self.NL_2_idx = self.NL_3_idx = self.NL_4_idx = []
238
239