ResLayer to build ResNet style backbone. Args: block (nn.Module): block used to build ResLayer. inplanes (int): inplanes of block. planes (int): planes of block. num_blocks (int): number of blocks. stride (int): stride of the first block. Default: 1
| 4 | |
| 5 | |
| 6 | class ResLayer(Sequential): |
| 7 | """ResLayer to build ResNet style backbone. |
| 8 | |
| 9 | Args: |
| 10 | block (nn.Module): block used to build ResLayer. |
| 11 | inplanes (int): inplanes of block. |
| 12 | planes (int): planes of block. |
| 13 | num_blocks (int): number of blocks. |
| 14 | stride (int): stride of the first block. Default: 1 |
| 15 | avg_down (bool): Use AvgPool instead of stride conv when |
| 16 | downsampling in the bottleneck. Default: False |
| 17 | conv_cfg (dict): dictionary to construct and config conv layer. |
| 18 | Default: None |
| 19 | norm_cfg (dict): dictionary to construct and config norm layer. |
| 20 | Default: dict(type='BN') |
| 21 | downsample_first (bool): Downsample at the first block or last block. |
| 22 | False for Hourglass, True for ResNet. Default: True |
| 23 | """ |
| 24 | def __init__(self, |
| 25 | block, |
| 26 | inplanes, |
| 27 | planes, |
| 28 | num_blocks, |
| 29 | stride=1, |
| 30 | avg_down=False, |
| 31 | conv_cfg=None, |
| 32 | norm_cfg=dict(type='BN'), |
| 33 | downsample_first=True, |
| 34 | **kwargs): |
| 35 | self.block = block |
| 36 | |
| 37 | downsample = None |
| 38 | if stride != 1 or inplanes != planes * block.expansion: |
| 39 | downsample = [] |
| 40 | conv_stride = stride |
| 41 | if avg_down: |
| 42 | conv_stride = 1 |
| 43 | downsample.append( |
| 44 | nn.AvgPool2d(kernel_size=stride, |
| 45 | stride=stride, |
| 46 | ceil_mode=True, |
| 47 | count_include_pad=False)) |
| 48 | downsample.extend([ |
| 49 | build_conv_layer(conv_cfg, |
| 50 | inplanes, |
| 51 | planes * block.expansion, |
| 52 | kernel_size=1, |
| 53 | stride=conv_stride, |
| 54 | bias=False), |
| 55 | build_norm_layer(norm_cfg, planes * block.expansion)[1] |
| 56 | ]) |
| 57 | downsample = nn.Sequential(*downsample) |
| 58 | |
| 59 | layers = [] |
| 60 | if downsample_first: |
| 61 | layers.append( |
| 62 | block(inplanes=inplanes, |
| 63 | planes=planes, |