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