|
| 1 | +import torch.nn as nn |
| 2 | +from mmdet.cv_core.cnn import ConvModule |
| 3 | + |
| 4 | +from ..builder import BACKBONES |
| 5 | +from ..utils import ResLayer |
| 6 | +from .resnet import BasicBlock |
| 7 | + |
| 8 | + |
| 9 | +class HourglassModule(nn.Module): |
| 10 | + """Hourglass Module for HourglassNet backbone. |
| 11 | +
|
| 12 | + Generate module recursively and use BasicBlock as the base unit. |
| 13 | +
|
| 14 | + Args: |
| 15 | + depth (int): Depth of current HourglassModule. |
| 16 | + stage_channels (list[int]): Feature channels of sub-modules in current |
| 17 | + and follow-up HourglassModule. |
| 18 | + stage_blocks (list[int]): Number of sub-modules stacked in current and |
| 19 | + follow-up HourglassModule. |
| 20 | + norm_cfg (dict): Dictionary to construct and config norm layer. |
| 21 | + """ |
| 22 | + |
| 23 | + def __init__(self, |
| 24 | + depth, |
| 25 | + stage_channels, |
| 26 | + stage_blocks, |
| 27 | + norm_cfg=dict(type='BN', requires_grad=True)): |
| 28 | + super(HourglassModule, self).__init__() |
| 29 | + |
| 30 | + self.depth = depth |
| 31 | + |
| 32 | + cur_block = stage_blocks[0] |
| 33 | + next_block = stage_blocks[1] |
| 34 | + |
| 35 | + cur_channel = stage_channels[0] |
| 36 | + next_channel = stage_channels[1] |
| 37 | + |
| 38 | + self.up1 = ResLayer( |
| 39 | + BasicBlock, cur_channel, cur_channel, cur_block, norm_cfg=norm_cfg) |
| 40 | + |
| 41 | + self.low1 = ResLayer( |
| 42 | + BasicBlock, |
| 43 | + cur_channel, |
| 44 | + next_channel, |
| 45 | + cur_block, |
| 46 | + stride=2, |
| 47 | + norm_cfg=norm_cfg) |
| 48 | + |
| 49 | + if self.depth > 1: |
| 50 | + self.low2 = HourglassModule(depth - 1, stage_channels[1:], |
| 51 | + stage_blocks[1:]) |
| 52 | + else: |
| 53 | + self.low2 = ResLayer( |
| 54 | + BasicBlock, |
| 55 | + next_channel, |
| 56 | + next_channel, |
| 57 | + next_block, |
| 58 | + norm_cfg=norm_cfg) |
| 59 | + |
| 60 | + self.low3 = ResLayer( |
| 61 | + BasicBlock, |
| 62 | + next_channel, |
| 63 | + cur_channel, |
| 64 | + cur_block, |
| 65 | + norm_cfg=norm_cfg, |
| 66 | + downsample_first=False) |
| 67 | + |
| 68 | + self.up2 = nn.Upsample(scale_factor=2) |
| 69 | + |
| 70 | + def forward(self, x): |
| 71 | + """Forward function.""" |
| 72 | + up1 = self.up1(x) |
| 73 | + low1 = self.low1(x) |
| 74 | + low2 = self.low2(low1) |
| 75 | + low3 = self.low3(low2) |
| 76 | + up2 = self.up2(low3) |
| 77 | + return up1 + up2 |
| 78 | + |
| 79 | + |
| 80 | +@BACKBONES.register_module() |
| 81 | +class HourglassNet(nn.Module): |
| 82 | + """HourglassNet backbone. |
| 83 | +
|
| 84 | + Stacked Hourglass Networks for Human Pose Estimation. |
| 85 | + More details can be found in the `paper |
| 86 | + <https://arxiv.org/abs/1603.06937>`_ . |
| 87 | +
|
| 88 | + Args: |
| 89 | + downsample_times (int): Downsample times in a HourglassModule. |
| 90 | + num_stacks (int): Number of HourglassModule modules stacked, |
| 91 | + 1 for Hourglass-52, 2 for Hourglass-104. |
| 92 | + stage_channels (list[int]): Feature channel of each sub-module in a |
| 93 | + HourglassModule. |
| 94 | + stage_blocks (list[int]): Number of sub-modules stacked in a |
| 95 | + HourglassModule. |
| 96 | + feat_channel (int): Feature channel of conv after a HourglassModule. |
| 97 | + norm_cfg (dict): Dictionary to construct and config norm layer. |
| 98 | +
|
| 99 | + Example: |
| 100 | + >>> from mmdet.models import HourglassNet |
| 101 | + >>> import torch |
| 102 | + >>> self = HourglassNet() |
| 103 | + >>> self.eval() |
| 104 | + >>> inputs = torch.rand(1, 3, 511, 511) |
| 105 | + >>> level_outputs = self.forward(inputs) |
| 106 | + >>> for level_output in level_outputs: |
| 107 | + ... print(tuple(level_output.shape)) |
| 108 | + (1, 256, 128, 128) |
| 109 | + (1, 256, 128, 128) |
| 110 | + """ |
| 111 | + |
| 112 | + def __init__(self, |
| 113 | + downsample_times=5, |
| 114 | + num_stacks=2, |
| 115 | + stage_channels=(256, 256, 384, 384, 384, 512), |
| 116 | + stage_blocks=(2, 2, 2, 2, 2, 4), |
| 117 | + feat_channel=256, |
| 118 | + norm_cfg=dict(type='BN', requires_grad=True)): |
| 119 | + super(HourglassNet, self).__init__() |
| 120 | + |
| 121 | + self.num_stacks = num_stacks |
| 122 | + assert self.num_stacks >= 1 |
| 123 | + assert len(stage_channels) == len(stage_blocks) |
| 124 | + assert len(stage_channels) > downsample_times |
| 125 | + |
| 126 | + cur_channel = stage_channels[0] |
| 127 | + |
| 128 | + self.stem = nn.Sequential( |
| 129 | + ConvModule(3, 128, 7, padding=3, stride=2, norm_cfg=norm_cfg), |
| 130 | + ResLayer(BasicBlock, 128, 256, 1, stride=2, norm_cfg=norm_cfg)) |
| 131 | + |
| 132 | + self.hourglass_modules = nn.ModuleList([ |
| 133 | + HourglassModule(downsample_times, stage_channels, stage_blocks) |
| 134 | + for _ in range(num_stacks) |
| 135 | + ]) |
| 136 | + |
| 137 | + self.inters = ResLayer( |
| 138 | + BasicBlock, |
| 139 | + cur_channel, |
| 140 | + cur_channel, |
| 141 | + num_stacks - 1, |
| 142 | + norm_cfg=norm_cfg) |
| 143 | + |
| 144 | + self.conv1x1s = nn.ModuleList([ |
| 145 | + ConvModule( |
| 146 | + cur_channel, cur_channel, 1, norm_cfg=norm_cfg, act_cfg=None) |
| 147 | + for _ in range(num_stacks - 1) |
| 148 | + ]) |
| 149 | + |
| 150 | + self.out_convs = nn.ModuleList([ |
| 151 | + ConvModule( |
| 152 | + cur_channel, feat_channel, 3, padding=1, norm_cfg=norm_cfg) |
| 153 | + for _ in range(num_stacks) |
| 154 | + ]) |
| 155 | + |
| 156 | + self.remap_convs = nn.ModuleList([ |
| 157 | + ConvModule( |
| 158 | + feat_channel, cur_channel, 1, norm_cfg=norm_cfg, act_cfg=None) |
| 159 | + for _ in range(num_stacks - 1) |
| 160 | + ]) |
| 161 | + |
| 162 | + self.relu = nn.ReLU(inplace=True) |
| 163 | + |
| 164 | + def init_weights(self, pretrained=None): |
| 165 | + """Init module weights. |
| 166 | +
|
| 167 | + We do nothing in this function because all modules we used |
| 168 | + (ConvModule, BasicBlock and etc.) have default initialization, and |
| 169 | + currently we don't provide pretrained model of HourglassNet. |
| 170 | +
|
| 171 | + Detector's __init__() will call backbone's init_weights() with |
| 172 | + pretrained as input, so we keep this function. |
| 173 | + """ |
| 174 | + # Training Centripetal Model needs to reset parameters for Conv2d |
| 175 | + for m in self.modules(): |
| 176 | + if isinstance(m, nn.Conv2d): |
| 177 | + m.reset_parameters() |
| 178 | + |
| 179 | + def forward(self, x): |
| 180 | + """Forward function.""" |
| 181 | + inter_feat = self.stem(x) |
| 182 | + out_feats = [] |
| 183 | + |
| 184 | + for ind in range(self.num_stacks): |
| 185 | + single_hourglass = self.hourglass_modules[ind] |
| 186 | + out_conv = self.out_convs[ind] |
| 187 | + |
| 188 | + hourglass_feat = single_hourglass(inter_feat) |
| 189 | + out_feat = out_conv(hourglass_feat) |
| 190 | + out_feats.append(out_feat) |
| 191 | + |
| 192 | + if ind < self.num_stacks - 1: |
| 193 | + inter_feat = self.conv1x1s[ind]( |
| 194 | + inter_feat) + self.remap_convs[ind]( |
| 195 | + out_feat) |
| 196 | + inter_feat = self.inters[ind](self.relu(inter_feat)) |
| 197 | + |
| 198 | + return out_feats |
0 commit comments