Safemotion Lib
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Protected Member Functions | List of all members
fastreid.modeling.backbones.resnest.ResNest Class Reference
Inheritance diagram for fastreid.modeling.backbones.resnest.ResNest:

Public Member Functions

 __init__ (self, last_stride, bn_norm, with_ibn, with_nl, block, layers, non_layers, radix=1, groups=1, bottleneck_width=64, dilated=False, dilation=1, deep_stem=False, stem_width=64, avg_down=False, rectified_conv=False, rectify_avg=False, avd=False, avd_first=False, final_drop=0.0, dropblock_prob=0, last_gamma=False)
 
 forward (self, x)
 

Public Attributes

 cardinality
 
 bottleneck_width
 
 inplanes
 
 avg_down
 
 last_gamma
 
 radix
 
 avd
 
 avd_first
 
 rectified_conv
 
 rectify_avg
 
 conv1
 
 bn1
 
 relu
 
 maxpool
 
 layer1
 
 layer2
 
 layer3
 
 layer4
 
 NL_1_idx
 
 NL_1
 
 NL_2
 
 NL_2_idx
 
 NL_3
 
 NL_3_idx
 
 NL_4
 
 NL_4_idx
 

Protected Member Functions

 _make_layer (self, block, planes, blocks, stride=1, bn_norm="BN", with_ibn=False, dilation=1, dropblock_prob=0.0, is_first=True)
 
 _build_nonlocal (self, layers, non_layers, bn_norm)
 

Detailed Description

ResNet Variants ResNest
Parameters
----------
block : Block
    Class for the residual block. Options are BasicBlockV1, BottleneckV1.
layers : list of int
    Numbers of layers in each block
classes : int, default 1000
    Number of classification classes.
dilated : bool, default False
    Applying dilation strategy to pretrained ResNet yielding a stride-8 model,
    typically used in Semantic Segmentation.
norm_layer : object
    Normalization layer used in backbone network (default: :class:`mxnet.gluon.nn.BatchNorm`;
    for Synchronized Cross-GPU BachNormalization).
Reference:
    - He, Kaiming, et al. "Deep residual learning for image recognition." Proceedings of the IEEE conference on computer vision and pattern recognition. 2016.
    - Yu, Fisher, and Vladlen Koltun. "Multi-scale context aggregation by dilated convolutions."

Definition at line 142 of file resnest.py.

Constructor & Destructor Documentation

◆ __init__()

fastreid.modeling.backbones.resnest.ResNest.__init__ ( self,
last_stride,
bn_norm,
with_ibn,
with_nl,
block,
layers,
non_layers,
radix = 1,
groups = 1,
bottleneck_width = 64,
dilated = False,
dilation = 1,
deep_stem = False,
stem_width = 64,
avg_down = False,
rectified_conv = False,
rectify_avg = False,
avd = False,
avd_first = False,
final_drop = 0.0,
dropblock_prob = 0,
last_gamma = False )

Definition at line 164 of file resnest.py.

172 last_gamma=False):
173 self.cardinality = groups
174 self.bottleneck_width = bottleneck_width
175 # ResNet-D params
176 self.inplanes = stem_width * 2 if deep_stem else 64
177 self.avg_down = avg_down
178 self.last_gamma = last_gamma
179 # ResNeSt params
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 # fmt: off
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 # fmt: on
239

Member Function Documentation

◆ _build_nonlocal()

fastreid.modeling.backbones.resnest.ResNest._build_nonlocal ( self,
layers,
non_layers,
bn_norm )
protected

Definition at line 295 of file resnest.py.

295 def _build_nonlocal(self, layers, non_layers, bn_norm):
296 self.NL_1 = nn.ModuleList(
297 [Non_local(256, bn_norm) for _ in range(non_layers[0])])
298 self.NL_1_idx = sorted([layers[0] - (i + 1) for i in range(non_layers[0])])
299 self.NL_2 = nn.ModuleList(
300 [Non_local(512, bn_norm) for _ in range(non_layers[1])])
301 self.NL_2_idx = sorted([layers[1] - (i + 1) for i in range(non_layers[1])])
302 self.NL_3 = nn.ModuleList(
303 [Non_local(1024, bn_norm) for _ in range(non_layers[2])])
304 self.NL_3_idx = sorted([layers[2] - (i + 1) for i in range(non_layers[2])])
305 self.NL_4 = nn.ModuleList(
306 [Non_local(2048, bn_norm) for _ in range(non_layers[3])])
307 self.NL_4_idx = sorted([layers[3] - (i + 1) for i in range(non_layers[3])])
308

◆ _make_layer()

fastreid.modeling.backbones.resnest.ResNest._make_layer ( self,
block,
planes,
blocks,
stride = 1,
bn_norm = "BN",
with_ibn = False,
dilation = 1,
dropblock_prob = 0.0,
is_first = True )
protected

Definition at line 240 of file resnest.py.

241 dilation=1, dropblock_prob=0.0, is_first=True):
242 downsample = None
243 if stride != 1 or self.inplanes != planes * block.expansion:
244 down_layers = []
245 if self.avg_down:
246 if dilation == 1:
247 down_layers.append(nn.AvgPool2d(kernel_size=stride, stride=stride,
248 ceil_mode=True, count_include_pad=False))
249 else:
250 down_layers.append(nn.AvgPool2d(kernel_size=1, stride=1,
251 ceil_mode=True, count_include_pad=False))
252 down_layers.append(nn.Conv2d(self.inplanes, planes * block.expansion,
253 kernel_size=1, stride=1, bias=False))
254 else:
255 down_layers.append(nn.Conv2d(self.inplanes, planes * block.expansion,
256 kernel_size=1, stride=stride, bias=False))
257 down_layers.append(get_norm(bn_norm, planes * block.expansion))
258 downsample = nn.Sequential(*down_layers)
259
260 layers = []
261 if dilation == 1 or dilation == 2:
262 layers.append(block(self.inplanes, planes, bn_norm, with_ibn, stride, downsample=downsample,
263 radix=self.radix, cardinality=self.cardinality,
264 bottleneck_width=self.bottleneck_width,
265 avd=self.avd, avd_first=self.avd_first,
266 dilation=1, is_first=is_first, rectified_conv=self.rectified_conv,
267 rectify_avg=self.rectify_avg,
268 dropblock_prob=dropblock_prob,
269 last_gamma=self.last_gamma))
270 elif dilation == 4:
271 layers.append(block(self.inplanes, planes, bn_norm, with_ibn, stride, downsample=downsample,
272 radix=self.radix, cardinality=self.cardinality,
273 bottleneck_width=self.bottleneck_width,
274 avd=self.avd, avd_first=self.avd_first,
275 dilation=2, is_first=is_first, rectified_conv=self.rectified_conv,
276 rectify_avg=self.rectify_avg,
277 dropblock_prob=dropblock_prob,
278 last_gamma=self.last_gamma))
279 else:
280 raise RuntimeError("=> unknown dilation size: {}".format(dilation))
281
282 self.inplanes = planes * block.expansion
283 for i in range(1, blocks):
284 layers.append(block(self.inplanes, planes, bn_norm, with_ibn,
285 radix=self.radix, cardinality=self.cardinality,
286 bottleneck_width=self.bottleneck_width,
287 avd=self.avd, avd_first=self.avd_first,
288 dilation=dilation, rectified_conv=self.rectified_conv,
289 rectify_avg=self.rectify_avg,
290 dropblock_prob=dropblock_prob,
291 last_gamma=self.last_gamma))
292
293 return nn.Sequential(*layers)
294

◆ forward()

fastreid.modeling.backbones.resnest.ResNest.forward ( self,
x )

Definition at line 309 of file resnest.py.

309 def forward(self, x):
310 x = self.conv1(x)
311 x = self.bn1(x)
312 x = self.relu(x)
313 x = self.maxpool(x)
314
315 NL1_counter = 0
316 if len(self.NL_1_idx) == 0:
317 self.NL_1_idx = [-1]
318 for i in range(len(self.layer1)):
319 x = self.layer1[i](x)
320 if i == self.NL_1_idx[NL1_counter]:
321 _, C, H, W = x.shape
322 x = self.NL_1[NL1_counter](x)
323 NL1_counter += 1
324 # Layer 2
325 NL2_counter = 0
326 if len(self.NL_2_idx) == 0:
327 self.NL_2_idx = [-1]
328 for i in range(len(self.layer2)):
329 x = self.layer2[i](x)
330 if i == self.NL_2_idx[NL2_counter]:
331 _, C, H, W = x.shape
332 x = self.NL_2[NL2_counter](x)
333 NL2_counter += 1
334 # Layer 3
335 NL3_counter = 0
336 if len(self.NL_3_idx) == 0:
337 self.NL_3_idx = [-1]
338 for i in range(len(self.layer3)):
339 x = self.layer3[i](x)
340 if i == self.NL_3_idx[NL3_counter]:
341 _, C, H, W = x.shape
342 x = self.NL_3[NL3_counter](x)
343 NL3_counter += 1
344 # Layer 4
345 NL4_counter = 0
346 if len(self.NL_4_idx) == 0:
347 self.NL_4_idx = [-1]
348 for i in range(len(self.layer4)):
349 x = self.layer4[i](x)
350 if i == self.NL_4_idx[NL4_counter]:
351 _, C, H, W = x.shape
352 x = self.NL_4[NL4_counter](x)
353 NL4_counter += 1
354
355 return x
356
357
358@BACKBONE_REGISTRY.register()

Member Data Documentation

◆ avd

fastreid.modeling.backbones.resnest.ResNest.avd

Definition at line 181 of file resnest.py.

◆ avd_first

fastreid.modeling.backbones.resnest.ResNest.avd_first

Definition at line 182 of file resnest.py.

◆ avg_down

fastreid.modeling.backbones.resnest.ResNest.avg_down

Definition at line 177 of file resnest.py.

◆ bn1

fastreid.modeling.backbones.resnest.ResNest.bn1

Definition at line 206 of file resnest.py.

◆ bottleneck_width

fastreid.modeling.backbones.resnest.ResNest.bottleneck_width

Definition at line 174 of file resnest.py.

◆ cardinality

fastreid.modeling.backbones.resnest.ResNest.cardinality

Definition at line 173 of file resnest.py.

◆ conv1

fastreid.modeling.backbones.resnest.ResNest.conv1

Definition at line 194 of file resnest.py.

◆ inplanes

fastreid.modeling.backbones.resnest.ResNest.inplanes

Definition at line 176 of file resnest.py.

◆ last_gamma

fastreid.modeling.backbones.resnest.ResNest.last_gamma

Definition at line 178 of file resnest.py.

◆ layer1

fastreid.modeling.backbones.resnest.ResNest.layer1

Definition at line 209 of file resnest.py.

◆ layer2

fastreid.modeling.backbones.resnest.ResNest.layer2

Definition at line 210 of file resnest.py.

◆ layer3

fastreid.modeling.backbones.resnest.ResNest.layer3

Definition at line 212 of file resnest.py.

◆ layer4

fastreid.modeling.backbones.resnest.ResNest.layer4

Definition at line 214 of file resnest.py.

◆ maxpool

fastreid.modeling.backbones.resnest.ResNest.maxpool

Definition at line 208 of file resnest.py.

◆ NL_1

fastreid.modeling.backbones.resnest.ResNest.NL_1

Definition at line 296 of file resnest.py.

◆ NL_1_idx

fastreid.modeling.backbones.resnest.ResNest.NL_1_idx

Definition at line 237 of file resnest.py.

◆ NL_2

fastreid.modeling.backbones.resnest.ResNest.NL_2

Definition at line 299 of file resnest.py.

◆ NL_2_idx

fastreid.modeling.backbones.resnest.ResNest.NL_2_idx

Definition at line 301 of file resnest.py.

◆ NL_3

fastreid.modeling.backbones.resnest.ResNest.NL_3

Definition at line 302 of file resnest.py.

◆ NL_3_idx

fastreid.modeling.backbones.resnest.ResNest.NL_3_idx

Definition at line 304 of file resnest.py.

◆ NL_4

fastreid.modeling.backbones.resnest.ResNest.NL_4

Definition at line 305 of file resnest.py.

◆ NL_4_idx

fastreid.modeling.backbones.resnest.ResNest.NL_4_idx

Definition at line 307 of file resnest.py.

◆ radix

fastreid.modeling.backbones.resnest.ResNest.radix

Definition at line 180 of file resnest.py.

◆ rectified_conv

fastreid.modeling.backbones.resnest.ResNest.rectified_conv

Definition at line 185 of file resnest.py.

◆ rectify_avg

fastreid.modeling.backbones.resnest.ResNest.rectify_avg

Definition at line 186 of file resnest.py.

◆ relu

fastreid.modeling.backbones.resnest.ResNest.relu

Definition at line 207 of file resnest.py.


The documentation for this class was generated from the following file: