| 107 | |
| 108 | |
| 109 | class HighResolutionModule(nn.Module): |
| 110 | def __init__(self, num_branches, blocks, num_blocks, num_inchannels, |
| 111 | num_channels, fuse_method, multi_scale_output=True): |
| 112 | super(HighResolutionModule, self).__init__() |
| 113 | self._check_branches( |
| 114 | num_branches, blocks, num_blocks, num_inchannels, num_channels) |
| 115 | |
| 116 | self.num_inchannels = num_inchannels |
| 117 | self.fuse_method = fuse_method |
| 118 | self.num_branches = num_branches |
| 119 | |
| 120 | self.multi_scale_output = multi_scale_output |
| 121 | |
| 122 | self.branches = self._make_branches( |
| 123 | num_branches, blocks, num_blocks, num_channels) |
| 124 | self.fuse_layers = self._make_fuse_layers() |
| 125 | self.relu = nn.ReLU(inplace=relu_inplace) |
| 126 | |
| 127 | def _check_branches(self, num_branches, blocks, num_blocks, |
| 128 | num_inchannels, num_channels): |
| 129 | if num_branches != len(num_blocks): |
| 130 | error_msg = 'NUM_BRANCHES({}) <> NUM_BLOCKS({})'.format( |
| 131 | num_branches, len(num_blocks)) |
| 132 | logx.msg(error_msg) |
| 133 | raise ValueError(error_msg) |
| 134 | |
| 135 | if num_branches != len(num_channels): |
| 136 | error_msg = 'NUM_BRANCHES({}) <> NUM_CHANNELS({})'.format( |
| 137 | num_branches, len(num_channels)) |
| 138 | logx.msg(error_msg) |
| 139 | raise ValueError(error_msg) |
| 140 | |
| 141 | if num_branches != len(num_inchannels): |
| 142 | error_msg = 'NUM_BRANCHES({}) <> NUM_INCHANNELS({})'.format( |
| 143 | num_branches, len(num_inchannels)) |
| 144 | logx.msg(error_msg) |
| 145 | raise ValueError(error_msg) |
| 146 | |
| 147 | def _make_one_branch(self, branch_index, block, num_blocks, num_channels, |
| 148 | stride=1): |
| 149 | downsample = None |
| 150 | if stride != 1 or \ |
| 151 | self.num_inchannels[branch_index] != (num_channels[branch_index] * |
| 152 | block.expansion): |
| 153 | downsample = nn.Sequential( |
| 154 | nn.Conv2d(self.num_inchannels[branch_index], |
| 155 | num_channels[branch_index] * block.expansion, |
| 156 | kernel_size=1, stride=stride, bias=False), |
| 157 | Norm2d(num_channels[branch_index] * block.expansion, |
| 158 | momentum=BN_MOMENTUM), |
| 159 | ) |
| 160 | |
| 161 | layers = [] |
| 162 | layers.append(block(self.num_inchannels[branch_index], |
| 163 | num_channels[branch_index], stride, downsample)) |
| 164 | self.num_inchannels[branch_index] = \ |
| 165 | num_channels[branch_index] * block.expansion |
| 166 | for i in range(1, num_blocks[branch_index]): |