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