| 30 | return model |
| 31 | |
| 32 | class HighResolutionModule(nn.Module): |
| 33 | def __init__(self, num_branches, blocks, num_blocks, num_inchannels, |
| 34 | num_channels, fuse_method, multi_scale_output=True): |
| 35 | super(HighResolutionModule, self).__init__() |
| 36 | self._check_branches( |
| 37 | num_branches, blocks, num_blocks, num_inchannels, num_channels) |
| 38 | |
| 39 | self.num_inchannels = num_inchannels |
| 40 | self.fuse_method = fuse_method |
| 41 | self.num_branches = num_branches |
| 42 | |
| 43 | self.multi_scale_output = multi_scale_output |
| 44 | |
| 45 | self.branches = self._make_branches( |
| 46 | num_branches, blocks, num_blocks, num_channels) |
| 47 | self.fuse_layers = self._make_fuse_layers() |
| 48 | self.relu = nn.ReLU(True) |
| 49 | |
| 50 | def _check_branches(self, num_branches, blocks, num_blocks, |
| 51 | num_inchannels, num_channels): |
| 52 | if num_branches != len(num_blocks): |
| 53 | error_msg = 'NUM_BRANCHES({}) <> NUM_BLOCKS({})'.format( |
| 54 | num_branches, len(num_blocks)) |
| 55 | raise ValueError(error_msg) |
| 56 | |
| 57 | if num_branches != len(num_channels): |
| 58 | error_msg = 'NUM_BRANCHES({}) <> NUM_CHANNELS({})'.format( |
| 59 | num_branches, len(num_channels)) |
| 60 | raise ValueError(error_msg) |
| 61 | |
| 62 | if num_branches != len(num_inchannels): |
| 63 | error_msg = 'NUM_BRANCHES({}) <> NUM_INCHANNELS({})'.format( |
| 64 | num_branches, len(num_inchannels)) |
| 65 | raise ValueError(error_msg) |
| 66 | |
| 67 | def _make_one_branch(self, branch_index, block, num_blocks, num_channels, |
| 68 | stride=1): |
| 69 | downsample = None |
| 70 | if stride != 1 or \ |
| 71 | self.num_inchannels[branch_index] != num_channels[branch_index] * block.expansion: |
| 72 | downsample = nn.Sequential( |
| 73 | nn.Conv2d( |
| 74 | self.num_inchannels[branch_index], |
| 75 | num_channels[branch_index] * block.expansion, |
| 76 | kernel_size=1, stride=stride, bias=False |
| 77 | ), |
| 78 | nn.BatchNorm2d( |
| 79 | num_channels[branch_index] * block.expansion, |
| 80 | momentum=BN_MOMENTUM |
| 81 | ), |
| 82 | ) |
| 83 | |
| 84 | layers = [] |
| 85 | layers.append( |
| 86 | block( |
| 87 | self.num_inchannels[branch_index], |
| 88 | num_channels[branch_index], |
| 89 | stride, |