|
| 1 | +import torch.nn as nn |
| 2 | +from mmcv.cnn import ConvModule, normal_init, xavier_init |
| 3 | + |
| 4 | +from mmdet.models.backbones.resnet import Bottleneck |
| 5 | +from mmdet.models.builder import HEADS |
| 6 | +from .obbox_head import OBBoxHead |
| 7 | + |
| 8 | + |
| 9 | +class BasicResBlock(nn.Module): |
| 10 | + """Basic residual block. |
| 11 | +
|
| 12 | + This block is a little different from the block in the ResNet backbone. |
| 13 | + The kernel size of conv1 is 1 in this block while 3 in ResNet BasicBlock. |
| 14 | +
|
| 15 | + Args: |
| 16 | + in_channels (int): Channels of the input feature map. |
| 17 | + out_channels (int): Channels of the output feature map. |
| 18 | + conv_cfg (dict): The config dict for convolution layers. |
| 19 | + norm_cfg (dict): The config dict for normalization layers. |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self, |
| 23 | + in_channels, |
| 24 | + out_channels, |
| 25 | + conv_cfg=None, |
| 26 | + norm_cfg=dict(type='BN')): |
| 27 | + super(BasicResBlock, self).__init__() |
| 28 | + |
| 29 | + # main path |
| 30 | + self.conv1 = ConvModule( |
| 31 | + in_channels, |
| 32 | + in_channels, |
| 33 | + kernel_size=3, |
| 34 | + padding=1, |
| 35 | + bias=False, |
| 36 | + conv_cfg=conv_cfg, |
| 37 | + norm_cfg=norm_cfg) |
| 38 | + self.conv2 = ConvModule( |
| 39 | + in_channels, |
| 40 | + out_channels, |
| 41 | + kernel_size=1, |
| 42 | + bias=False, |
| 43 | + conv_cfg=conv_cfg, |
| 44 | + norm_cfg=norm_cfg, |
| 45 | + act_cfg=None) |
| 46 | + |
| 47 | + # identity path |
| 48 | + self.conv_identity = ConvModule( |
| 49 | + in_channels, |
| 50 | + out_channels, |
| 51 | + kernel_size=1, |
| 52 | + conv_cfg=conv_cfg, |
| 53 | + norm_cfg=norm_cfg, |
| 54 | + act_cfg=None) |
| 55 | + |
| 56 | + self.relu = nn.ReLU(inplace=True) |
| 57 | + |
| 58 | + def forward(self, x): |
| 59 | + identity = x |
| 60 | + |
| 61 | + x = self.conv1(x) |
| 62 | + x = self.conv2(x) |
| 63 | + |
| 64 | + identity = self.conv_identity(identity) |
| 65 | + out = x + identity |
| 66 | + |
| 67 | + out = self.relu(out) |
| 68 | + return out |
| 69 | + |
| 70 | + |
| 71 | +@HEADS.register_module() |
| 72 | +class OBBDoubleConvFCBBoxHead(OBBoxHead): |
| 73 | + r"""Bbox head used in Double-Head R-CNN |
| 74 | +
|
| 75 | + .. code-block:: none |
| 76 | +
|
| 77 | + /-> cls |
| 78 | + /-> shared convs -> |
| 79 | + \-> reg |
| 80 | + roi features |
| 81 | + /-> cls |
| 82 | + \-> shared fc -> |
| 83 | + \-> reg |
| 84 | + """ # noqa: W605 |
| 85 | + |
| 86 | + def __init__(self, |
| 87 | + num_convs=0, |
| 88 | + num_fcs=0, |
| 89 | + conv_out_channels=1024, |
| 90 | + fc_out_channels=1024, |
| 91 | + conv_cfg=None, |
| 92 | + norm_cfg=dict(type='BN'), |
| 93 | + **kwargs): |
| 94 | + kwargs.setdefault('with_avg_pool', True) |
| 95 | + super(OBBDoubleConvFCBBoxHead, self).__init__(**kwargs) |
| 96 | + assert self.with_avg_pool |
| 97 | + assert num_convs > 0 |
| 98 | + assert num_fcs > 0 |
| 99 | + self.num_convs = num_convs |
| 100 | + self.num_fcs = num_fcs |
| 101 | + self.conv_out_channels = conv_out_channels |
| 102 | + self.fc_out_channels = fc_out_channels |
| 103 | + self.conv_cfg = conv_cfg |
| 104 | + self.norm_cfg = norm_cfg |
| 105 | + |
| 106 | + # increase the channel of input features |
| 107 | + self.res_block = BasicResBlock(self.in_channels, |
| 108 | + self.conv_out_channels) |
| 109 | + |
| 110 | + # add conv heads |
| 111 | + self.conv_branch = self._add_conv_branch() |
| 112 | + # add fc heads |
| 113 | + self.fc_branch = self._add_fc_branch() |
| 114 | + |
| 115 | + out_dim_reg = self.reg_dim if self.reg_class_agnostic else \ |
| 116 | + self.reg_dim * self.num_classes |
| 117 | + self.fc_reg = nn.Linear(self.conv_out_channels, out_dim_reg) |
| 118 | + |
| 119 | + self.fc_cls = nn.Linear(self.fc_out_channels, self.num_classes + 1) |
| 120 | + self.relu = nn.ReLU(inplace=True) |
| 121 | + |
| 122 | + def _add_conv_branch(self): |
| 123 | + """Add the fc branch which consists of a sequential of conv layers""" |
| 124 | + branch_convs = nn.ModuleList() |
| 125 | + for i in range(self.num_convs): |
| 126 | + branch_convs.append( |
| 127 | + Bottleneck( |
| 128 | + inplanes=self.conv_out_channels, |
| 129 | + planes=self.conv_out_channels // 4, |
| 130 | + conv_cfg=self.conv_cfg, |
| 131 | + norm_cfg=self.norm_cfg)) |
| 132 | + return branch_convs |
| 133 | + |
| 134 | + def _add_fc_branch(self): |
| 135 | + """Add the fc branch which consists of a sequential of fc layers""" |
| 136 | + branch_fcs = nn.ModuleList() |
| 137 | + for i in range(self.num_fcs): |
| 138 | + fc_in_channels = ( |
| 139 | + self.in_channels * |
| 140 | + self.roi_feat_area if i == 0 else self.fc_out_channels) |
| 141 | + branch_fcs.append(nn.Linear(fc_in_channels, self.fc_out_channels)) |
| 142 | + return branch_fcs |
| 143 | + |
| 144 | + def init_weights(self): |
| 145 | + # conv layers are already initialized by ConvModule |
| 146 | + normal_init(self.fc_cls, std=0.01) |
| 147 | + normal_init(self.fc_reg, std=0.001) |
| 148 | + |
| 149 | + for m in self.fc_branch.modules(): |
| 150 | + if isinstance(m, nn.Linear): |
| 151 | + xavier_init(m, distribution='uniform') |
| 152 | + |
| 153 | + def forward(self, x_cls, x_reg): |
| 154 | + # conv head |
| 155 | + x_conv = self.res_block(x_reg) |
| 156 | + |
| 157 | + for conv in self.conv_branch: |
| 158 | + x_conv = conv(x_conv) |
| 159 | + |
| 160 | + if self.with_avg_pool: |
| 161 | + x_conv = self.avg_pool(x_conv) |
| 162 | + |
| 163 | + x_conv = x_conv.view(x_conv.size(0), -1) |
| 164 | + bbox_pred = self.fc_reg(x_conv) |
| 165 | + |
| 166 | + # fc head |
| 167 | + x_fc = x_cls.view(x_cls.size(0), -1) |
| 168 | + for fc in self.fc_branch: |
| 169 | + x_fc = self.relu(fc(x_fc)) |
| 170 | + |
| 171 | + cls_score = self.fc_cls(x_fc) |
| 172 | + |
| 173 | + return cls_score, bbox_pred |
0 commit comments