예제 #1
0
    def __init__(self, cin, cout, stride=1, conv_cfg=None, norm_cfg=None):
        super(conv_duc, self).__init__()
        self.branch0 = ConvModule(cin,
                                  cout,
                                  kernel_size=3,
                                  stride=1,
                                  padding=1,
                                  conv_cfg=conv_cfg,
                                  norm_cfg=norm_cfg)

        self.pool = nn.AvgPool2d(kernel_size=stride, stride=stride)
        down_ch = cout * stride * stride
        self.branch1_1 = ConvModule(cin,
                                    cin,
                                    kernel_size=3,
                                    stride=1,
                                    padding=1,
                                    conv_cfg=conv_cfg,
                                    norm_cfg=norm_cfg)
        self.branch1_2 = ConvModule(cin,
                                    down_ch,
                                    kernel_size=1,
                                    stride=1,
                                    padding=0,
                                    conv_cfg=conv_cfg,
                                    norm_cfg=norm_cfg)
        self.up = nn.PixelShuffle(upscale_factor=stride)
        self.branch1 = nn.Sequential(
            self.pool,
            self.branch1_1,
            self.branch1_2,
            self.up,
        )
예제 #2
0
    def _init_branch_layers(self, planes, idx=0):
        wh_layers, hm_layers = [], []
        inp = planes
        if self.bn_before_head:
            wh_layers.append(nn.BatchNorm2d(inp))
            hm_layers.append(nn.BatchNorm2d(inp))
        for i in range(self.wh_head_conv_num[idx]):
            wh_layers.append(
                ConvModule(inp,
                           self.wh_head_channels[idx],
                           3,
                           padding=1,
                           conv_cfg=self.conv_cfg))
            inp = self.wh_head_channels[idx]
        if self.head_conv_cfg:
            wh_layers.append(
                build_conv_layer(self.head_conv_cfg,
                                 self.wh_head_channels[idx],
                                 4,
                                 kernel_size=3,
                                 padding=1))
        else:
            wh_layers.append(
                nn.Conv2d(self.wh_head_channels[idx], 4, 3, padding=1))

        inp = planes
        for i in range(self.hm_head_conv_num[idx]):
            hm_layers.append(
                ConvModule(inp,
                           self.hm_head_channels[idx],
                           3,
                           padding=1,
                           conv_cfg=self.conv_cfg))
            inp = self.hm_head_channels[idx]
        if self.head_conv_cfg:
            hm_layers.append(
                build_conv_layer(self.head_conv_cfg,
                                 self.hm_head_channels[idx],
                                 self.num_fg,
                                 kernel_size=3,
                                 padding=1))
        else:
            hm_layers.append(
                nn.Conv2d(self.hm_head_channels[idx],
                          self.num_fg,
                          3,
                          padding=1))

        wh_layers = nn.Sequential(*wh_layers)
        hm_layers = nn.Sequential(*hm_layers)
        return wh_layers, hm_layers
예제 #3
0
    def __init__(self,
                 in_channels,
                 num_levels,
                 refine_level=2,
                 refine_type=None,
                 conv_cfg=None,
                 norm_cfg=None):
        super(BFP, self).__init__()
        assert refine_type in [None, 'conv', 'non_local']

        self.in_channels = in_channels
        self.num_levels = num_levels
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg

        self.refine_level = refine_level
        self.refine_type = refine_type
        assert 0 <= self.refine_level < self.num_levels

        if self.refine_type == 'conv':
            self.refine = ConvModule(self.in_channels,
                                     self.in_channels,
                                     3,
                                     padding=1,
                                     conv_cfg=self.conv_cfg,
                                     norm_cfg=self.norm_cfg)
        elif self.refine_type == 'non_local':
            self.refine = NonLocal2D(self.in_channels,
                                     reduction=1,
                                     use_scale=False,
                                     conv_cfg=self.conv_cfg,
                                     norm_cfg=self.norm_cfg)
예제 #4
0
 def __init__(self, cin, cout, dilation=1, conv_cfg=None, norm_cfg=None):
     super(Pang_unit_stride, self).__init__()
     self.branch0 = ConvModule(cin,
                               cout,
                               kernel_size=3,
                               stride=2,
                               padding=dilation,
                               dilation=dilation,
                               conv_cfg=conv_cfg,
                               norm_cfg=norm_cfg)
     self.branch1 = ConvModule(cin,
                               cout,
                               kernel_size=1,
                               stride=1,
                               padding=0,
                               conv_cfg=conv_cfg,
                               norm_cfg=norm_cfg)
예제 #5
0
    def _build_head(self, out_channel, conv_num=1, head_conv_plane=None):
        head_convs = []
        for i in range(conv_num):
            inp = self._planes[-1] if i == 0 else head_conv_plane
            head_convs.append(ConvModule(inp, head_conv_plane, 3, padding=1))

        inp = self._planes[-1] if conv_num <= 0 else head_conv_plane
        head_convs.append(nn.Conv2d(inp, out_channel, 1))
        return nn.Sequential(*head_convs)
예제 #6
0
    def __init__(self,
                 in_channels=(512, 2048),
                 planes=256,
                 ffm_out_channels=(256, 512),
                 num_levels=8,
                 norm_cfg=dict(type='BN')):
        super(MLFPN, self).__init__()
        assert len(in_channels) == len(ffm_out_channels) == 2
        self.planes = planes
        self.num_levels = num_levels

        # construct base features
        self.reduce_s8 = ConvModule(in_channels[0],
                                    ffm_out_channels[0],
                                    3,
                                    padding=1,
                                    norm_cfg=norm_cfg)
        self.reduce_s16 = ConvModule(in_channels[1],
                                     ffm_out_channels[1],
                                     1,
                                     norm_cfg=norm_cfg)

        # share same params
        self.leach = nn.ModuleList([
            ConvModule(
                sum(ffm_out_channels), self.planes // 2, 1, norm_cfg=norm_cfg)
        ] * self.num_levels)

        for i in range(self.num_levels):
            side_channel = 512 if i == 0 else self.planes
            setattr(self, 'unet{}'.format(i + 1),
                    TUM(first_block=(i == 0),
                        input_planes=self.planes // 2,
                        is_smooth=self.smooth,
                        scales=self.num_scales,
                        side_channel=side_channel,
                        norm_cfg=norm_cfg))  # side channel isn't fixed.

        # construct SFAM module
        if self.sfam:
            self.sfam_module = SEBlock(self.planes,
                                       self.num_levels,
                                       self.num_scales,
                                       compress_ratio=16)
예제 #7
0
    def _init_branch_layers(self, planes, idx=-1):
        wh_layers, hm_layers = [], []
        inp = planes
        for outp in self.wh_head_channels[idx]:
            wh_layers.append(
                ConvModule(inp, outp, 3, padding=1, conv_cfg=self.conv_cfg))
            inp = outp
        wh_layers.append(nn.Conv2d(inp, 4, 3, padding=1))

        inp = planes
        for outp in self.hm_head_channels[idx]:
            hm_layers.append(
                ConvModule(inp, outp, 3, padding=1, conv_cfg=self.conv_cfg))
            inp = outp
        hm_layers.append(nn.Conv2d(inp, self.num_fg, 3, padding=1))

        wh_layers = nn.Sequential(*wh_layers)
        hm_layers = nn.Sequential(*hm_layers)
        return wh_layers, hm_layers
예제 #8
0
 def __init__(self, channels=256, with_conv=True, norm_cfg=None):
     super(MergingCell, self).__init__()
     self.with_conv = with_conv
     if self.with_conv:
         self.conv_out = ConvModule(channels,
                                    channels,
                                    3,
                                    padding=1,
                                    norm_cfg=norm_cfg,
                                    order=('act', 'conv', 'norm'))
예제 #9
0
    def __init__(self,
                 inplanes=(64, 128, 256, 512),
                 planes=(256, 128, 64),
                 outplane=64,
                 shortcut_kernel=3,
                 conv_cfg=None,
                 norm_cfg=None,
                 norm_eval=False):
        super(CTFPN, self).__init__()
        self.norm_eval = norm_eval

        # repeat deconv 3 times, 32x to 4x.
        self.deconv_layers = nn.ModuleList()
        self.shortcut_layers = nn.ModuleList()
        self.output_layers = nn.ModuleList()
        self.deconv_layers.extend(
            [self._make_deconv_layer(inplanes[-1], 1, [planes[0]], [4], norm_cfg=norm_cfg),
             self._make_deconv_layer(planes[0], 1, [planes[1]], [4], norm_cfg=norm_cfg),
             self._make_deconv_layer(planes[1], 1, [planes[2]], [4], norm_cfg=norm_cfg)])

        padding = (shortcut_kernel - 1) // 2
        self.shortcut_layers.extend(
            [ConvModule(inplanes[2], planes[0], shortcut_kernel,
                        padding=padding, conv_cfg=conv_cfg, activation=None),
             ConvModule(inplanes[1], planes[1], shortcut_kernel,
                        padding=padding, conv_cfg=conv_cfg, activation=None),
             ConvModule(inplanes[0], planes[2], shortcut_kernel,
                        padding=padding, conv_cfg=conv_cfg, activation=None)])

        self.output_layers.extend(
            [ConvModule(planes[0], outplane, 1, conv_cfg=conv_cfg, activation=None),
             ConvModule(planes[1], outplane, 1, conv_cfg=conv_cfg, activation=None),
             ConvModule(planes[2], outplane, 1, conv_cfg=conv_cfg, activation=None)])
예제 #10
0
    def __init__(self,
                 feature_channels=256,
                 num_convs=3,
                 num_levels=5,
                 conv_out_channels=256,
                 conv_cfg=None,
                 norm_cfg=None):
        super(NSemanticPyramidNeck, self).__init__()
        self.num_convs = num_convs
        self.num_levels = num_levels
        self.conv_out_channels = conv_out_channels
        self.feature_channels = feature_channels
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.fp16_enabled = False

        # self.lateral_convs = nn.ModuleList()
        self.combine_convs = nn.ModuleList()
        # self.proto_convs = nn.ModuleList()
        for i in range(num_levels):
            self.combine_convs.append(
                nn.Sequential(
                    ConvModule(self.feature_channels,
                               self.conv_out_channels,
                               3,
                               padding=1,
                               norm_cfg=norm_cfg,
                               conv_cfg=conv_cfg),
                    ConvModule(self.conv_out_channels,
                               self.conv_out_channels,
                               3,
                               padding=1,
                               norm_cfg=norm_cfg,
                               conv_cfg=conv_cfg),
                    ConvModule(self.conv_out_channels,
                               self.conv_out_channels,
                               3,
                               padding=1,
                               norm_cfg=norm_cfg,
                               conv_cfg=conv_cfg)))
예제 #11
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 num_outs=5,
                 pooling_type='AVG',
                 conv_cfg=None,
                 norm_cfg=None,
                 with_cp=False,
                 stride=1):
        super(HRFPN, self).__init__()
        assert isinstance(in_channels, list)
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.num_ins = len(in_channels)
        self.num_outs = num_outs
        self.with_cp = with_cp
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg

        self.reduction_conv = ConvModule(sum(in_channels),
                                         out_channels,
                                         kernel_size=1,
                                         conv_cfg=self.conv_cfg,
                                         activation=None)

        self.fpn_convs = nn.ModuleList()
        for i in range(self.num_outs):
            self.fpn_convs.append(
                ConvModule(out_channels,
                           out_channels,
                           kernel_size=3,
                           padding=1,
                           stride=stride,
                           conv_cfg=self.conv_cfg,
                           activation=None))

        if pooling_type == 'MAX':
            self.pooling = F.max_pool2d
        else:
            self.pooling = F.avg_pool2d
예제 #12
0
 def _make_conv_layer(self, num_output):
     conv_layers = []
     for i in range(self.stacked_convs):
         in_chn = self.inplane if i == 0 else self.head_conv
         conv_layers.append(
             ConvModule(in_chn,
                        self.head_conv,
                        3,
                        stride=1,
                        padding=1,
                        conv_cfg=self.conv_cfg,
                        norm_cfg=self.norm_cfg))
     conv_layers.append(nn.Conv2d(self.head_conv, num_output, 1))
     return nn.Sequential(*conv_layers)
예제 #13
0
    def _make_conv_layer(self, out_channel, conv_num=1, head_conv_plane=None):
        head_convs = []
        if not self.new_wh_generate:
            head_conv_plane = self.head_conv if not head_conv_plane else head_conv_plane
            for i in range(conv_num):
                inp = self.planes[-1] if i == 0 else head_conv_plane
                head_convs.append(
                    ConvModule(inp, head_conv_plane, 3, padding=1))
        else:
            for i in range(conv_num):
                inp = self.planes[-1] if i == 0 else self.head_conv
                head_convs.append(ConvModule(inp, self.head_conv, 3,
                                             padding=1))

            if head_conv_plane:
                head_convs.append(
                    ConvModule(self.head_conv, head_conv_plane, 3, padding=1))
            else:
                head_conv_plane = self.head_conv

        inp = self.planes[-1] if conv_num <= 0 else head_conv_plane
        head_convs.append(nn.Conv2d(inp, out_channel, 1))
        return nn.Sequential(*head_convs)
예제 #14
0
    def _make_conv_layer(self, out_channel, conv_num, use_exp_conv=False):
        head_convs = []
        for i in range(conv_num):
            inp = self.planes[-1] if i == 0 else self.head_conv
            head_convs.append(ConvModule(inp, self.head_conv, 3, padding=1))

        inp = self.planes[-1] if conv_num <= 0 else self.head_conv
        head_convs.append(nn.Conv2d(inp, out_channel, 1))
        if use_exp_conv:
            head_convs.append(
                build_conv_layer(dict(type='ExpConv'),
                                 out_channel,
                                 out_channel,
                                 neg_x=True))
        return nn.Sequential(*head_convs)
예제 #15
0
    def _add_conv_fc_branch(self,
                            num_branch_convs,
                            num_branch_fcs,
                            in_channels,
                            is_shared=False):
        """Add shared or separable branch

        convs -> avg pool (optional) -> fcs
        """
        last_layer_dim = in_channels
        # add branch specific conv layers
        branch_convs = nn.ModuleList()
        if num_branch_convs > 0:
            for i in range(num_branch_convs):
                conv_in_channels = (last_layer_dim
                                    if i == 0 else self.conv_out_channels)
                branch_convs.append(
                    ConvModule(conv_in_channels,
                               self.conv_out_channels,
                               3,
                               padding=1,
                               conv_cfg=self.conv_cfg,
                               norm_cfg=self.norm_cfg))
            last_layer_dim = self.conv_out_channels
        # add branch specific fc layers
        branch_fcs = nn.ModuleList()
        if num_branch_fcs > 0:
            # for shared branch, only consider self.with_avg_pool
            # for separated branches, also consider self.num_shared_fcs
            if (is_shared
                    or self.num_shared_fcs == 0) and not self.with_avg_pool:
                last_layer_dim *= self.roi_feat_area
            for i in range(num_branch_fcs):
                fc_in_channels = (last_layer_dim
                                  if i == 0 else self.fc_out_channels)
                branch_fcs.append(
                    nn.Linear(fc_in_channels, self.fc_out_channels))
            last_layer_dim = self.fc_out_channels
        return branch_convs, branch_fcs, last_layer_dim
예제 #16
0
    def __init__(self,
                 first_block=True,
                 input_planes=128,
                 is_smooth=True,
                 side_channel=512,
                 scales=6,
                 norm_cfg=dict(type='BN')):
        super(TUM, self).__init__()
        self.is_smooth = is_smooth
        self.first_block = first_block
        planes = 2 * input_planes
        in1 = input_planes if first_block else input_planes + side_channel

        self.layers = nn.Sequential()
        self.latlayer = nn.Sequential()
        self.toplayer = nn.Sequential(
            ConvModule(planes, planes, 1, norm_cfg=norm_cfg))

        # from shallow to deep
        self.layers.add_module(
            '{}'.format(len(self.layers)),
            ConvModule(in1, planes, 3, stride=2, padding=1, norm_cfg=norm_cfg))
        for i in range(scales - 2):
            stride, padding = (2, 1) if i != scales - 3 else (1, 0)
            self.layers.add_module(
                '{}'.format(len(self.layers)),
                ConvModule(planes,
                           planes,
                           3,
                           stride=stride,
                           padding=padding,
                           norm_cfg=norm_cfg))

        # from deep to shallow
        for i in range(scales - 2):
            self.latlayer.add_module(
                '{}'.format(len(self.latlayer)),
                ConvModule(planes, planes, 3, norm_cfg=norm_cfg))
        self.latlayer.add_module('{}'.format(len(self.latlayer)),
                                 ConvModule(in1, planes, 3, norm_cfg=norm_cfg))

        if self.is_smooth:
            smooth = []
            for i in range(scales - 1):
                smooth.append(ConvModule(planes, planes, 1, norm_cfg=norm_cfg))
            self.smooth = nn.Sequential(*smooth)
예제 #17
0
    def build_head(self,
                   out_channel,
                   conv_num=1,
                   head_conv_plane=None,
                   use_sym_conv=False):
        head_convs = []
        head_conv_plane = self.head_conv if not head_conv_plane else head_conv_plane
        for i in range(conv_num):
            inp = self.planes[-1] if i == 0 else head_conv_plane
            head_convs.append(
                ConvModule(inp,
                           head_conv_plane,
                           self.head_conv_size,
                           conv_cfg=self.conv_cfg,
                           padding=1))

        inp = self.planes[-1] if conv_num <= 0 else head_conv_plane
        if use_sym_conv:
            assert out_channel == 4
            head_convs.append(nn.Conv2d(inp, out_channel, 3, padding=1))
            # head_convs.append(ConvModule(inp, out_channel, 3, conv_cfg=dict(type='WHSymConv')))
        else:
            if self.hm_last_3x3:
                head_convs.append(nn.Conv2d(inp, out_channel, 3, padding=1))
            elif self.hm_last_3x3_d2:
                head_convs.append(
                    nn.Conv2d(inp, out_channel, 3, padding=2, dilation=2))
            elif self.hm_last_5x5:
                head_convs.append(nn.Conv2d(inp, out_channel, 5, padding=2))
            elif self.hm_last_7x7:
                head_convs.append(nn.Conv2d(inp, out_channel, 7, padding=3))
            elif self.hm_last_se3x3:
                head_convs.append(nn.Conv2d(inp, out_channel, 3, padding=1))
                if not self.no_wh_se or out_channel != 4:
                    head_convs.append(SEBlock(out_channel, compress_ratio=4))
            else:
                head_convs.append(nn.Conv2d(inp, out_channel, 1))
        return nn.Sequential(*head_convs)
예제 #18
0
    def __init__(self, level):
        super(ASFF, self).__init__()
        self.level = level
        self.dim = [256, 128, 64]
        self.inter_dim = self.dim[self.level]
        if level == 0:
            self.stride_level_1 = ConvModule(128,
                                             self.inter_dim,
                                             3,
                                             padding=1,
                                             stride=2,
                                             norm_cfg=dict(type='BN'))
            self.stride_level_2 = ConvModule(64,
                                             self.inter_dim,
                                             3,
                                             padding=1,
                                             stride=2,
                                             norm_cfg=dict(type='BN'))
            self.expand = ConvModule(self.inter_dim,
                                     256,
                                     3,
                                     padding=1,
                                     norm_cfg=dict(type='BN'))
        elif level == 1:
            self.compress_level_0 = ConvModule(256,
                                               self.inter_dim,
                                               1,
                                               norm_cfg=dict(type='BN'))
            self.stride_level_2 = ConvModule(64,
                                             self.inter_dim,
                                             3,
                                             padding=1,
                                             stride=2,
                                             norm_cfg=dict(type='BN'))
            self.expand = ConvModule(self.inter_dim,
                                     128,
                                     3,
                                     padding=1,
                                     norm_cfg=dict(type='BN'))
        elif level == 2:
            self.compress_level_0 = ConvModule(256,
                                               self.inter_dim,
                                               1,
                                               norm_cfg=dict(type='BN'))
            self.compress_level_1 = ConvModule(128,
                                               self.inter_dim,
                                               1,
                                               norm_cfg=dict(type='BN'))
            self.expand = ConvModule(self.inter_dim,
                                     64,
                                     3,
                                     padding=1,
                                     norm_cfg=dict(type='BN'))

        compress_c = 16

        self.weight_level_0 = ConvModule(self.inter_dim,
                                         compress_c,
                                         1,
                                         norm_cfg=dict(type='BN'))
        self.weight_level_1 = ConvModule(self.inter_dim,
                                         compress_c,
                                         1,
                                         norm_cfg=dict(type='BN'))
        self.weight_level_2 = ConvModule(self.inter_dim,
                                         compress_c,
                                         1,
                                         norm_cfg=dict(type='BN'))

        self.weight_levels = nn.Conv2d(compress_c * 3,
                                       3,
                                       kernel_size=1,
                                       stride=1,
                                       padding=0)
예제 #19
0
    def __init__(self,
                 inplanes=(64, 128, 256, 512),
                 planes=(256, 128, 64),
                 down_ratio=4,
                 head_conv=256,
                 wh_conv=64,
                 hm_head_conv_num=2,
                 wh_head_conv_num=2,
                 num_classes=81,
                 shortcut_kernel=3,
                 conv_cfg=None,
                 head_conv_size=3,
                 use_trident=False,
                 use_dla=False,
                 wh_sym=False,
                 upsample_vanilla_conv=False,
                 upsample_multiscale_conv=False,
                 up_conv_cfg=None,
                 norm_cfg=dict(type='BN'),
                 shortcut_cfg=(1, 2, 3),
                 wh_offset_base=16.,
                 wh_area_process='log',
                 wh_agnostic=True,
                 wh_gaussian=True,
                 box_size_range=None,
                 two_stage=False,
                 alpha=0.54,
                 beta=0.54,
                 hm_weight=1.,
                 dcn_mean=False,
                 iou_type='giou',
                 use_simple_nms=True,
                 aug_reg=False,
                 hm_last_3x3=False,
                 hm_last_3x3_d2=False,
                 hm_last_se3x3=False,
                 hm_last_5x5=False,
                 hm_last_7x7=False,
                 no_wh_se=False,
                 wh_weight=5.,
                 max_objs=128):
        super(AnchorHead, self).__init__()
        assert len(planes) in [2, 3, 4]
        shortcut_num = min(len(inplanes) - 1, len(planes))
        assert shortcut_num == len(shortcut_cfg)
        assert wh_area_process in [None, 'norm', 'log', 'sqrt']

        self.planes = planes
        self.head_conv = head_conv
        self.num_classes = num_classes
        self.conv_cfg = conv_cfg
        self.head_conv_size = head_conv_size
        self.use_trident = use_trident
        self.use_dla = use_dla
        self.wh_sym = wh_sym
        self.upsample_vanilla_conv = upsample_vanilla_conv
        self.upsample_multiscale_conv = upsample_multiscale_conv
        self.up_conv_cfg = up_conv_cfg
        self.wh_offset_base = wh_offset_base
        self.wh_area_process = wh_area_process
        self.wh_agnostic = wh_agnostic
        self.wh_gaussian = wh_gaussian
        self.box_size_range = box_size_range
        self.two_stage = two_stage
        self.alpha = alpha
        self.beta = beta
        self.hm_weight = hm_weight
        self.dcn_mean = dcn_mean
        self.iou_loss = eval(iou_type + '_loss')
        self.use_simple_nms = use_simple_nms
        self.aug_reg = aug_reg
        self.hm_last_3x3 = hm_last_3x3
        self.hm_last_3x3_d2 = hm_last_3x3_d2
        self.hm_last_se3x3 = hm_last_se3x3
        self.no_wh_se = no_wh_se
        self.hm_last_5x5 = hm_last_5x5
        self.hm_last_7x7 = hm_last_7x7
        self.wh_weight = wh_weight
        self.max_objs = max_objs
        self.fp16_enabled = False

        self.down_ratio = down_ratio
        self.num_fg = num_classes - 1
        self.wh_planes = 4 if wh_agnostic else 4 * self.num_fg
        self.base_loc = None

        # repeat upsampling n times. 32x to 4x by default.
        self.deconv_layers = nn.ModuleList([
            self.build_upsample(inplanes[-1], planes[0], norm_cfg=norm_cfg),
            self.build_upsample(planes[0], planes[1], norm_cfg=norm_cfg)
        ])
        for i in range(2, len(planes)):
            self.deconv_layers.append(
                self.build_upsample(planes[i - 1],
                                    planes[i],
                                    norm_cfg=norm_cfg,
                                    no_upsample=(down_ratio == 8)))

        padding = (shortcut_kernel - 1) // 2
        self.shortcut_layers = self.build_shortcut(
            inplanes[:-1][::-1][:shortcut_num],
            planes[:shortcut_num],
            shortcut_cfg,
            kernel_size=shortcut_kernel,
            padding=padding)

        # heads
        self.wh = self.build_head(self.wh_planes,
                                  wh_head_conv_num,
                                  head_conv_plane=wh_conv,
                                  use_sym_conv=wh_sym)
        self.hm = self.build_head(self.num_fg, hm_head_conv_num)
        if two_stage:
            assert wh_agnostic
            self.align = RoIAlign(7, spatial_scale=1 / 4., sample_num=2)
            self.wh2 = nn.Sequential(
                ConvModule(self.planes[-1], 32, 5, norm_cfg=norm_cfg),  # 3x3
                ConvModule(32, 32, 3, norm_cfg=norm_cfg),
                ConvModule(32, 32, 1),
                ConvModule(32, 4, 1, activation=None))
예제 #20
0
    def __init__(self,
                 depth,
                 num_stages=4,
                 strides=(1, 2, 2, 2),
                 dilations=(1, 1, 1, 1),
                 out_indices=(0, 1, 2, 3),
                 style='pytorch',
                 frozen_stages=-1,
                 conv_cfg=None,
                 norm_cfg=dict(type='BN', requires_grad=True),
                 norm_eval=True,
                 with_att=False,
                 without_ip=False,
                 without_dconv=False,
                 num_branch=4,
                 dcn=None,
                 stage_with_dcn=(False, False, False, False),
                 gcb=None,
                 stage_with_gcb=(False, False, False, False),
                 gen_attention=None,
                 stage_with_gen_attention=((), (), (), ()),
                 with_cp=False,
                 zero_init_residual=True):
        super(IPN_kite, self).__init__()
        if depth not in self.arch_settings:
            raise KeyError('invalid depth {} for resnet'.format(depth))
        self.depth = depth
        self.without_ip = without_ip
        self.with_att = with_att
        self.without_dconv = without_dconv
        self.num_branch = num_branch
        self.num_stages = num_stages
        assert num_stages >= 1 and num_stages <= 4
        self.strides = strides
        self.dilations = dilations
        assert len(strides) == len(dilations) == num_stages
        self.out_indices = out_indices
        assert max(out_indices) < num_stages
        self.style = style
        self.frozen_stages = frozen_stages
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.with_cp = with_cp
        self.norm_eval = norm_eval
        self.dcn = dcn
        self.stage_with_dcn = stage_with_dcn
        if dcn is not None:
            assert len(stage_with_dcn) == num_stages
        self.gen_attention = gen_attention
        self.gcb = gcb
        self.stage_with_gcb = stage_with_gcb
        if gcb is not None:
            assert len(stage_with_gcb) == num_stages
        self.zero_init_residual = zero_init_residual
        self.block, stage_blocks = self.arch_settings[depth]
        self.stage_blocks = stage_blocks[:num_stages]
        self.inplanes = 64
        if with_att:
            #self.attention2048 = MultiHeadAttention(n_head=3,d_model=2048,d_v=2048,d_k=2048)
            self.attention1024 = MultiHeadAttention(n_head=3,
                                                    d_model=1024,
                                                    d_v=64,
                                                    d_k=64)
            self.attention512 = MultiHeadAttention(n_head=3,
                                                   d_model=512,
                                                   d_v=64,
                                                   d_k=64)
            self.attention256 = MultiHeadAttention(n_head=3,
                                                   d_model=256,
                                                   d_v=64,
                                                   d_k=64)
            self.maxpooling = nn.MaxPool2d(kernel_size=6, stride=6, padding=0)

        self._make_stem_layer()
        self.channel_setting = [256, 512, 1024, 2048]
        self.dconvs = nn.ModuleList()
        for i in range(self.num_stages - 1):
            #for j in range(self.num_stages - i - 1):
            dconv = ConvModule(self.channel_setting[i],
                               self.channel_setting[i],
                               3,
                               stride=1,
                               padding=2,
                               dilation=2,
                               conv_cfg=conv_cfg,
                               norm_cfg=norm_cfg,
                               activation=None,
                               inplace=False)
            self.dconvs.append(dconv)
        self.res_layers = []
        for i, num_blocks in enumerate(self.stage_blocks):
            stride = strides[i]
            dilation = dilations[i]
            dcn = self.dcn if self.stage_with_dcn[i] else None
            gcb = self.gcb if self.stage_with_gcb[i] else None
            planes = 64 * 2**i
            res_layer = make_res_layer(
                self.block,
                self.inplanes,
                planes,
                num_blocks,
                stride=stride,
                dilation=dilation,
                style=self.style,
                with_cp=with_cp,
                conv_cfg=conv_cfg,
                norm_cfg=norm_cfg,
                dcn=dcn,
                gcb=gcb,
                gen_attention=gen_attention,
                gen_attention_blocks=stage_with_gen_attention[i])
            self.inplanes = planes * self.block.expansion
            layer_name = 'layer{}'.format(i + 1)
            self.add_module(layer_name, res_layer)
            self.res_layers.append(layer_name)

        self._freeze_stages()

        self.feat_dim = self.block.expansion * 64 * 2**(
            len(self.stage_blocks) - 1)
예제 #21
0
    def __init__(self,
                 depth,
                 num_stages=4,
                 strides=(1, 2, 2, 2),
                 dilations=(1, 1, 1, 1),
                 out_indices=(0, 1, 2, 3),
                 style='pytorch',
                 frozen_stages=-1,
                 out_channels=256,
                 conv_cfg=None,
                 norm_cfg=dict(type='BN', requires_grad=True),
                 norm_eval=True,
                 dcn=None,
                 stage_with_dcn=(False, False, False, False),
                 gcb=None,
                 stage_with_gcb=(False, False, False, False),
                 gen_attention=None,
                 stage_with_gen_attention=((), (), (), ()),
                 with_cp=False,
                 zero_init_residual=True):
        super(ResNet2stream, self).__init__()
        if depth not in self.arch_settings:
            raise KeyError('invalid depth {} for resnet'.format(depth))
        self.depth = depth
        self.num_stages = num_stages
        assert num_stages >= 1 and num_stages <= 4
        self.strides = strides
        self.dilations = dilations
        assert len(strides) == len(dilations) == num_stages
        self.out_indices = out_indices
        assert max(out_indices) < num_stages
        self.style = style
        self.frozen_stages = frozen_stages
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.with_cp = with_cp
        self.norm_eval = norm_eval
        self.dcn = dcn
        self.stage_with_dcn = stage_with_dcn
        if dcn is not None:
            assert len(stage_with_dcn) == num_stages
        self.gen_attention = gen_attention
        self.gcb = gcb
        self.stage_with_gcb = stage_with_gcb
        if gcb is not None:
            assert len(stage_with_gcb) == num_stages
        self.zero_init_residual = zero_init_residual
        self.block, stage_blocks = self.arch_settings[depth]
        self.stage_blocks = stage_blocks[:num_stages]
        self.inplanes = 64

        self._make_stem_layer()

        self.res_layers = []
        for i, num_blocks in enumerate(self.stage_blocks):
            stride = strides[i]
            dilation = dilations[i]
            dcn = self.dcn if self.stage_with_dcn[i] else None
            gcb = self.gcb if self.stage_with_gcb[i] else None
            planes = 64 * 2**i
            res_layer = make_res_layer(
                self.block,
                self.inplanes,
                planes,
                num_blocks,
                stride=stride,
                dilation=dilation,
                style=self.style,
                with_cp=with_cp,
                conv_cfg=conv_cfg,
                norm_cfg=norm_cfg,
                dcn=dcn,
                gcb=gcb,
                gen_attention=gen_attention,
                gen_attention_blocks=stage_with_gen_attention[i])
            self.inplanes = planes * self.block.expansion
            layer_name = 'layer{}'.format(i + 1)
            self.add_module(layer_name, res_layer)
            self.res_layers.append(layer_name)

        self.lateral_convs = nn.ModuleList()
        self.fpn_convs = nn.ModuleList()
        in_channels = self.channel_setting[depth]
        for i in range(0, self.num_stages):
            l_conv = ConvModule(in_channels[i],
                                out_channels,
                                1,
                                conv_cfg=conv_cfg,
                                norm_cfg=norm_cfg,
                                activation=None,
                                inplace=False)
            fpn_conv = ConvModule(out_channels,
                                  out_channels,
                                  3,
                                  padding=1,
                                  conv_cfg=conv_cfg,
                                  norm_cfg=norm_cfg,
                                  activation=None,
                                  inplace=False)

            self.lateral_convs.append(l_conv)
            self.fpn_convs.append(fpn_conv)

        self._freeze_stages()

        self.feat_dim = self.block.expansion * 64 * 2**(
            len(self.stage_blocks) - 1)
예제 #22
0
    def _init_branch_layers(self, planes, upsample_inplane, shortcut_inplanes):
        upsample_layers = nn.ModuleList([
            UpsamplingLayers(
                upsample_inplane, planes[0], norm_cfg=self.norm_cfg),
            UpsamplingLayers(
                planes[0], planes[1], norm_cfg=self.norm_cfg),
        ])

        shortcut_layers = nn.ModuleList()
        for (inp, outp, layer_num) in zip(shortcut_inplanes,
                                          planes, self.shortcut_cfg):
            assert layer_num > 0, "Shortcut connection must be included."
            shortcut_layers.append(
                ShortcutConnection(inp, outp, [3] * layer_num, self.shortcut_conv_cfg))

        wh_layers, hm_layers = [], []
        inp = planes[-1]
        for i in range(self.wh_head_conv_num):
            wh_layers.append(
                ConvModule(
                    inp,
                    self.wh_head_channels,
                    3,
                    padding=1,
                    conv_cfg=self.conv_cfg))
            inp = self.wh_head_channels
        if self.head_conv_cfg:
            wh_layers.append(
                build_conv_layer(
                    self.head_conv_cfg,
                    self.wh_head_channels,
                    4,
                    kernel_size=3,
                    padding=1
                )
            )
        else:
            wh_layers.append(nn.Conv2d(self.wh_head_channels, 4, 3, padding=1))

        inp = planes[-1]
        for i in range(self.hm_head_conv_num):
            hm_layers.append(
                ConvModule(
                    inp,
                    self.hm_head_channels,
                    3,
                    padding=1,
                    conv_cfg=self.conv_cfg))
            inp = self.hm_head_channels
        if self.head_conv_cfg:
            hm_layers.append(
                build_conv_layer(
                    self.head_conv_cfg,
                    self.hm_head_channels,
                    self.num_fg,
                    kernel_size=3,
                    padding=1
                )
            )
        elif self.depthwise_hm:
            hm_layers.append(DepthwiseHead(self.hm_head_channels, self.num_fg, 3,
                                           conv_relu=self.relu_before_depthwise,
                                           depth_kernel_sizes=self.depth_kernel_sizes,
                                           depth_group=self.depth_group,
                                           use_deform=self.depth_deform))
        else:
            hm_layers.append(nn.Conv2d(self.hm_head_channels, self.num_fg, 3, padding=1))

        wh_layers = nn.Sequential(*wh_layers)
        hm_layers = nn.Sequential(*hm_layers)
        return upsample_layers, shortcut_layers, wh_layers, hm_layers
예제 #23
0
    def __init__(
        self,
        conv_cfg=None,
        norm_cfg=None,
    ):
        super(FatNetSimple, self).__init__()

        self.conv1 = ConvModule(3,
                                64,
                                kernel_size=3,
                                stride=1,
                                padding=1,
                                conv_cfg=conv_cfg,
                                norm_cfg=norm_cfg)
        self.conv2 = ConvModule(64,
                                64,
                                kernel_size=3,
                                stride=1,
                                padding=1,
                                conv_cfg=conv_cfg,
                                norm_cfg=norm_cfg)

        self.conv3 = conv_duc(64,
                              32,
                              stride=2,
                              conv_cfg=conv_cfg,
                              norm_cfg=norm_cfg)
        self.conv4 = conv_duc(32,
                              32,
                              stride=2,
                              conv_cfg=conv_cfg,
                              norm_cfg=norm_cfg)

        self.conv5 = conv_duc(32,
                              16,
                              stride=4,
                              conv_cfg=conv_cfg,
                              norm_cfg=norm_cfg)
        self.conv6 = conv_duc(16,
                              16,
                              stride=4,
                              conv_cfg=conv_cfg,
                              norm_cfg=norm_cfg)
        self.conv7 = conv_duc(16,
                              16,
                              stride=4,
                              conv_cfg=conv_cfg,
                              norm_cfg=norm_cfg)

        self.conv8 = conv_duc(16,
                              16,
                              stride=8,
                              conv_cfg=conv_cfg,
                              norm_cfg=norm_cfg)
        self.conv9 = conv_duc(16,
                              16,
                              stride=8,
                              conv_cfg=conv_cfg,
                              norm_cfg=norm_cfg)
        self.conv10 = conv_duc(16,
                               16,
                               stride=8,
                               conv_cfg=conv_cfg,
                               norm_cfg=norm_cfg)

        self.conv11 = conv_duc(16,
                               16,
                               stride=16,
                               conv_cfg=conv_cfg,
                               norm_cfg=norm_cfg)
        self.conv12 = conv_duc(16,
                               16,
                               stride=16,
                               conv_cfg=conv_cfg,
                               norm_cfg=norm_cfg)
        self.conv13 = conv_duc(16,
                               16,
                               stride=16,
                               conv_cfg=conv_cfg,
                               norm_cfg=norm_cfg)
예제 #24
0
    def __init__(self,
                 depth,
                 num_stages=4,
                 strides=(1, 2, 2, 2),
                 dilations=(1, 1, 1, 1),
                 out_indices=(0, 1, 2, 3),
                 style='pytorch',
                 frozen_stages=-1,
                 conv_cfg=None,
                 norm_cfg=dict(type='BN', requires_grad=True),
                 norm_eval=True,
                 dcn=None,
                 stage_with_dcn=(False, False, False, False),
                 gcb=None,
                 stage_with_gcb=(False, False, False, False),
                 gen_attention=None,
                 stage_with_gen_attention=((), (), (), ()),
                 with_cp=False,
                 zero_init_residual=True):
        super(IPN_fusing, self).__init__()
        if depth not in self.arch_settings:
            raise KeyError('invalid depth {} for resnet'.format(depth))
        self.depth = depth
        self.num_stages = num_stages
        assert num_stages >= 1 and num_stages <= 4
        self.strides = strides
        self.dilations = dilations
        assert len(strides) == len(dilations) == num_stages
        self.out_indices = out_indices
        assert max(out_indices) < num_stages
        self.style = style
        self.frozen_stages = frozen_stages
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.with_cp = with_cp
        self.norm_eval = norm_eval
        self.dcn = dcn
        self.stage_with_dcn = stage_with_dcn
        if dcn is not None:
            assert len(stage_with_dcn) == num_stages
        self.gen_attention = gen_attention
        self.gcb = gcb
        self.stage_with_gcb = stage_with_gcb
        if gcb is not None:
            assert len(stage_with_gcb) == num_stages
        self.zero_init_residual = zero_init_residual
        self.block, stage_blocks = self.arch_settings[depth]
        self.stage_blocks = stage_blocks[:num_stages]
        self.inplanes = 64

        self._make_stem_layer()

        self.channel_setting = [256,512,1024,2048]
        # make col layer
        self.collayers = [[] for _ in range(self.num_stages)]
        for ii in range(self.num_stages-1):
            length = ii +1
            for jj in range(length):
                collayer = ConvModule(
                            self.channel_setting[jj],
                            self.channel_setting[jj],
                            3,
                            padding=1,
                            conv_cfg=conv_cfg,
                            norm_cfg=norm_cfg,
                            activation=None,
                            inplace=False)
                layer_name = 'col_layer{}_{}'.format(ii,jj)
                self.add_module(layer_name, collayer)
                self.collayers[ii].append(layer_name)
        
        # make row layer
        self.rowlayers = [[] for _ in range(self.num_stages)]
        for ii in range(1,self.num_stages):
            length = ii + 1
            for jj in range(1,length):
                rowlayer = ConvModule(
                            self.channel_setting[jj-1],
                            self.channel_setting[jj],
                            3,
                            stride=2,
                            padding=1,
                            conv_cfg=conv_cfg,
                            norm_cfg=norm_cfg,
                            activation=None,
                            inplace=False)
                layer_name = 'row_layer{}_{}'.format(ii, jj)
                self.add_module(layer_name, rowlayer)
                self.rowlayers[ii].append(layer_name)

        self.res_layers = []
        for i, num_blocks in enumerate(self.stage_blocks):
            stride = strides[i]
            dilation = dilations[i]
            dcn = self.dcn if self.stage_with_dcn[i] else None
            gcb = self.gcb if self.stage_with_gcb[i] else None
            planes = 64 * 2**i
            res_layer = make_res_layer(
                self.block,
                self.inplanes,
                planes,
                num_blocks,
                stride=stride,
                dilation=dilation,
                style=self.style,
                with_cp=with_cp,
                conv_cfg=conv_cfg,
                norm_cfg=norm_cfg,
                dcn=dcn,
                gcb=gcb,
                gen_attention=gen_attention,
                gen_attention_blocks=stage_with_gen_attention[i])
            self.inplanes = planes * self.block.expansion
            layer_name = 'layer{}'.format(i + 1)
            self.add_module(layer_name, res_layer)
            self.res_layers.append(layer_name)

        self._freeze_stages()

        self.feat_dim = self.block.expansion * 64 * 2**(
            len(self.stage_blocks) - 1)
예제 #25
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 num_outs,
                 start_level=0,
                 end_level=-1,
                 add_extra_convs=False,
                 extra_convs_on_inputs=True,
                 relu_before_extra_convs=False,
                 no_norm_on_lateral=False,
                 conv_cfg=None,
                 norm_cfg=None,
                 activation=None):
        super(FPN, self).__init__()
        assert isinstance(in_channels, list)
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.num_ins = len(in_channels)
        self.num_outs = num_outs
        self.activation = activation
        self.relu_before_extra_convs = relu_before_extra_convs
        self.no_norm_on_lateral = no_norm_on_lateral
        self.fp16_enabled = False

        if end_level == -1:
            self.backbone_end_level = self.num_ins
            assert num_outs >= self.num_ins - start_level
        else:
            # if end_level < inputs, no extra level is allowed
            self.backbone_end_level = end_level
            assert end_level <= len(in_channels)
            assert num_outs == end_level - start_level
        self.start_level = start_level
        self.end_level = end_level
        self.add_extra_convs = add_extra_convs
        self.extra_convs_on_inputs = extra_convs_on_inputs

        self.lateral_convs = nn.ModuleList()
        self.fpn_convs = nn.ModuleList()

        for i in range(self.start_level, self.backbone_end_level):
            l_conv = ConvModule(
                in_channels[i],
                out_channels,
                1,
                conv_cfg=conv_cfg,
                norm_cfg=norm_cfg if not self.no_norm_on_lateral else None,
                activation=self.activation,
                inplace=False)
            fpn_conv = ConvModule(out_channels,
                                  out_channels,
                                  3,
                                  padding=1,
                                  conv_cfg=conv_cfg,
                                  norm_cfg=norm_cfg,
                                  activation=self.activation,
                                  inplace=False)

            self.lateral_convs.append(l_conv)
            self.fpn_convs.append(fpn_conv)

        # add extra conv layers (e.g., RetinaNet)
        extra_levels = num_outs - self.backbone_end_level + self.start_level
        if add_extra_convs and extra_levels >= 1:
            for i in range(extra_levels):
                if i == 0 and self.extra_convs_on_inputs:
                    in_channels = self.in_channels[self.backbone_end_level - 1]
                else:
                    in_channels = out_channels
                extra_fpn_conv = ConvModule(in_channels,
                                            out_channels,
                                            3,
                                            stride=2,
                                            padding=1,
                                            conv_cfg=conv_cfg,
                                            norm_cfg=norm_cfg,
                                            activation=self.activation,
                                            inplace=False)
                self.fpn_convs.append(extra_fpn_conv)
    def __init__(self,
                 num_convs=4,
                 feature_channels=256,
                 mask_channels=80,
                 combine_level=2,
                 num_levels=5,
                 conv_out_channels=256,
                 groups=True,
                 conv_cfg=None,
                 norm_cfg=None):
        super(SemanticProcessNeck, self).__init__()
        self.num_convs = num_convs
        self.mask_channels = mask_channels
        self.combine_level = combine_level
        self.num_levels = num_levels
        self.conv_out_channels = conv_out_channels
        self.feature_channels = feature_channels
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.fp16_enabled = False
        self.groups = groups

        if self.groups:
            self.lateral_conv = ConvModule(self.mask_channels,
                                           self.mask_channels,
                                           3,
                                           stride=2,
                                           padding=1,
                                           groups=self.mask_channels,
                                           conv_cfg=self.conv_cfg,
                                           norm_cfg=self.norm_cfg)
        else:
            self.lateral_conv = ConvModule(self.mask_channels,
                                           self.mask_channels,
                                           3,
                                           stride=2,
                                           padding=1,
                                           conv_cfg=self.conv_cfg,
                                           norm_cfg=self.norm_cfg)

        # self.convs = nn.ModuleList()
        # for i in range(self.num_convs):
        #     # in_channels = self.in_channels if i == 0 else conv_out_channels
        #     if self.groups:
        #         self.convs.append(
        #             ConvModule(
        #                 conv_out_channels,
        #                 conv_out_channels,
        #                 3,
        #                 padding=1,
        #                 groups=in_channels,
        #                 conv_cfg=self.conv_cfg,
        #                 norm_cfg=self.norm_cfg))
        #     else:
        #         self.convs.append(
        #             ConvModule(
        #                 conv_out_channels,
        #                 conv_out_channels,
        #                 3,
        #                 padding=1,
        #                 conv_cfg=self.conv_cfg,
        #                 norm_cfg=self.norm_cfg))

        # self.conv_embedding = ConvModule(
        #     conv_out_channels,
        #     conv_out_channels,
        #     1,
        #     conv_cfg=self.conv_cfg,
        #     norm_cfg=self.norm_cfg)
        # self.conv_logits = nn.Conv2d(conv_out_channels, self.num_classes, 1)
        self.combine_conv = ConvModule(self.mask_channels +
                                       self.feature_channels,
                                       self.conv_out_channels,
                                       3,
                                       padding=1,
                                       norm_cfg=norm_cfg,
                                       conv_cfg=conv_cfg)
예제 #27
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 num_outs,
                 stack_times,
                 start_level=0,
                 end_level=-1,
                 add_extra_convs=False,
                 norm_cfg=None):
        super(NASFPN, self).__init__()
        assert isinstance(in_channels, list)
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.num_ins = len(in_channels)  # num of input feature levels
        self.num_outs = num_outs  # num of output feature levels
        self.stack_times = stack_times
        self.norm_cfg = norm_cfg

        if end_level == -1:
            self.backbone_end_level = self.num_ins
            assert num_outs >= self.num_ins - start_level
        else:
            # if end_level < inputs, no extra level is allowed
            self.backbone_end_level = end_level
            assert end_level <= len(in_channels)
            assert num_outs == end_level - start_level
        self.start_level = start_level
        self.end_level = end_level
        self.add_extra_convs = add_extra_convs

        # add lateral connections
        self.lateral_convs = nn.ModuleList()
        for i in range(self.start_level, self.backbone_end_level):
            l_conv = ConvModule(in_channels[i],
                                out_channels,
                                1,
                                norm_cfg=norm_cfg,
                                activation=None)
            self.lateral_convs.append(l_conv)

        # add extra downsample layers (stride-2 pooling or conv)
        extra_levels = num_outs - self.backbone_end_level + self.start_level
        self.extra_downsamples = nn.ModuleList()
        for i in range(extra_levels):
            extra_conv = ConvModule(out_channels,
                                    out_channels,
                                    1,
                                    norm_cfg=norm_cfg,
                                    activation=None)
            self.extra_downsamples.append(
                nn.Sequential(extra_conv, nn.MaxPool2d(2, 2)))

        # add NAS FPN connections
        self.fpn_stages = nn.ModuleList()
        for _ in range(self.stack_times):
            stage = nn.ModuleDict()
            # gp(p6, p4) -> p4_1
            stage['gp_64_4'] = GPCell(out_channels, norm_cfg=norm_cfg)
            # sum(p4_1, p4) -> p4_2
            stage['sum_44_4'] = SumCell(out_channels, norm_cfg=norm_cfg)
            # sum(p4_2, p3) -> p3_out
            stage['sum_43_3'] = SumCell(out_channels, norm_cfg=norm_cfg)
            # sum(p3_out, p4_2) -> p4_out
            stage['sum_34_4'] = SumCell(out_channels, norm_cfg=norm_cfg)
            # sum(p5, gp(p4_out, p3_out)) -> p5_out
            stage['gp_43_5'] = GPCell(with_conv=False)
            stage['sum_55_5'] = SumCell(out_channels, norm_cfg=norm_cfg)
            # sum(p7, gp(p5_out, p4_2)) -> p7_out
            stage['gp_54_7'] = GPCell(with_conv=False)
            stage['sum_77_7'] = SumCell(out_channels, norm_cfg=norm_cfg)
            # gp(p7_out, p5_out) -> p6_out
            stage['gp_75_6'] = GPCell(out_channels, norm_cfg=norm_cfg)
            self.fpn_stages.append(stage)
예제 #28
0
    def __init__(self,
                 inplanes=(64, 128, 256, 512),
                 planes=(256, 128, 64),
                 head_conv=256,
                 hm_head_conv_num=1,
                 wh_head_conv_num=1,
                 deconv_with_bias=False,
                 num_classes=81,
                 use_reg_offset=True,
                 use_smooth_l1=False,
                 use_exp_wh=False,
                 use_giou=False,
                 use_shortcut=False,
                 use_upsample_conv=False,
                 use_trident=False,
                 use_dla=False,
                 shortcut_cfg=(1, 1, 1),
                 shortcut_attention=(False, False, False),
                 shortcut_kernel=3,
                 use_rep_points=False,
                 rep_points_kernel=3,
                 conv_cfg=None,
                 norm_cfg=dict(type='BN'),
                 neg_shortcut=False,
                 hm_init_value=-2.19,
                 use_exp_hm=False,
                 use_truncate_gaussia=False,
                 use_tight_gauusia=False,
                 gt_plus_dot5=False,
                 shortcut_in_shortcut=False,
                 giou_weight=1.,
                 wh_weight=0.1,
                 off_weight=1.,
                 hm_weight=1.):
        super(AnchorHead, self).__init__()
        assert len(planes) in [2, 3, 4] and \
               len(planes) == len(shortcut_cfg) == len(shortcut_attention)
        self.down_ratio = 32 // 2**len(planes)

        self.planes = planes
        self.head_conv = head_conv
        self.deconv_with_bias = deconv_with_bias
        self.num_classes = num_classes
        self.num_fg = num_classes - 1

        self.use_reg_offset = use_reg_offset
        self.use_shortcut = use_shortcut
        self.use_rep_points = use_rep_points
        assert rep_points_kernel in [3, 5]
        self.rep_points_kernel = rep_points_kernel
        self.use_exp_wh = use_exp_wh
        self.use_exp_hm = use_exp_hm
        self.use_upsample_conv = use_upsample_conv
        self.use_trident = use_trident
        self.use_dla = use_dla
        self.conv_cfg = conv_cfg
        self.neg_shortcut = neg_shortcut

        self.hm_init_value = hm_init_value
        self.wh_weight = wh_weight
        self.off_weight = off_weight
        self.hm_weight = hm_weight

        # repeat deconv n times. 32x to 4x by default.
        self.deconv_layers = nn.ModuleList([
            self._make_deconv_layer(inplanes[-1],
                                    1, [planes[0]], [4],
                                    norm_cfg=norm_cfg),
            self._make_deconv_layer(planes[0],
                                    1, [planes[1]], [4],
                                    norm_cfg=norm_cfg)
        ])
        for i in range(2, len(planes)):
            self.deconv_layers.append(
                self._make_deconv_layer(planes[i - 1],
                                        1, [planes[i]], [4],
                                        norm_cfg=norm_cfg))

        padding = (shortcut_kernel - 1) // 2
        self.shortcut_layers = self._make_shortcut(
            inplanes[:-1][::-1][:len(planes)],
            planes,
            shortcut_cfg,
            shortcut_attention,
            kernel_size=shortcut_kernel,
            padding=padding,
            shortcut_in_shortcut=shortcut_in_shortcut)

        # heads
        if use_rep_points:
            assert hm_head_conv_num == wh_head_conv_num
            share_head_conv = [
                ConvModule(self.planes[-1], head_conv, 3, padding=1)
            ]
            for i in range(1, wh_head_conv_num):
                share_head_conv.append(
                    ConvModule(head_conv, head_conv, 3, padding=1))
            self.share_head_conv = nn.Sequential(*share_head_conv)
            padding = 1 if rep_points_kernel == 3 else 2
            kernel_spatial = rep_points_kernel**2
            self.wh = nn.Conv2d(head_conv,
                                kernel_spatial * 3,
                                rep_points_kernel,
                                padding=padding)
            self.hm = ModulatedDeformConv(head_conv,
                                          self.num_fg,
                                          rep_points_kernel,
                                          padding=padding)
        else:
            self.wh = self._make_conv_layer(2, wh_head_conv_num)
            self.hm = self._make_conv_layer(self.num_fg,
                                            hm_head_conv_num,
                                            use_exp_conv=use_exp_hm)
        if use_reg_offset:
            self.reg = self._make_conv_layer(2, wh_head_conv_num)

        self._target_generator = CTTargetGenerator(self.num_fg,
                                                   use_reg_offset,
                                                   use_giou,
                                                   use_truncate_gaussia,
                                                   use_tight_gauusia,
                                                   gt_plus_dot5,
                                                   down_ratio=self.down_ratio)
        self._loss = CTLoss(use_reg_offset, use_smooth_l1, use_giou,
                            giou_weight, wh_weight, off_weight, hm_weight)
예제 #29
0
    def __init__(self,
                 depth,
                 num_stages=4,
                 strides=(1, 2, 2, 2,2 ,2,1),
                 dilations=(1, 1, 1, 1,1,1,2),
                 out_indices=(0, 1, 2, 3,4,5,6),
                 style='pytorch',
                 frozen_stages=-1,
                 conv_cfg=None,
                 norm_cfg=dict(type='BN', requires_grad=True),
                 norm_eval=True,
                 multiple=False,
                 dcn=None,
                 num_branch=4,
                 stage_with_dcn=(False, False, False, False,False, False, False),
                 gcb=None,
                 stage_with_gcb=(False, False, False, False,False, False, False),
                 gen_attention=None,
                 stage_with_gen_attention=((), (), (), (),(), (), ()),
                 with_cp=False,
                 zero_init_residual=True):
        super(shareResNet_ju_conv, self).__init__()
        if depth not in self.arch_settings:
            raise KeyError('invalid depth {} for resnet'.format(depth))
        self.depth = depth
        self.num_branch = num_branch
        self.num_stages = num_stages
        self.multiple = multiple
        assert num_stages >= 1 and num_stages <= 7
        self.strides = strides
        self.dilations = dilations
        assert len(strides) == len(dilations) == num_stages
        self.out_indices = out_indices
        assert max(out_indices) < num_stages
        self.style = style
        self.frozen_stages = frozen_stages
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.with_cp = with_cp
        self.norm_eval = norm_eval
        self.dcn = dcn
        self.stage_with_dcn = stage_with_dcn
        if dcn is not None:
            assert len(stage_with_dcn) == num_stages
        self.gen_attention = gen_attention
        self.gcb = gcb
        self.stage_with_gcb = stage_with_gcb
        if gcb is not None:
            assert len(stage_with_gcb) == num_stages
        self.zero_init_residual = zero_init_residual
        self.block, stage_blocks = self.arch_settings[depth]
        self.stage_blocks = stage_blocks[:num_stages]
        self.inplanes = 64

        self._make_stem_layer()

        self.res_layers = []
        for i, num_blocks in enumerate(self.stage_blocks):
            stride = strides[i]
            dilation = dilations[i]
            dcn = self.dcn if self.stage_with_dcn[i] else None
            gcb = self.gcb if self.stage_with_gcb[i] else None
            planes = 64 * 2**i
            if i>=4:
                planes = 64 *2**3
            res_layer = make_res_layer(
                self.block,
                self.inplanes,
                planes,
                num_blocks,
                stride=stride,
                dilation=dilation,
                style=self.style,
                with_cp=with_cp,
                conv_cfg=conv_cfg,
                norm_cfg=norm_cfg,
                dcn=dcn,
                gcb=gcb,
                gen_attention=gen_attention,
                gen_attention_blocks=stage_with_gen_attention[i])
            self.inplanes = planes * self.block.expansion
            layer_name = 'layer{}'.format(i + 1)
            self.add_module(layer_name, res_layer)
            self.res_layers.append(layer_name)

        self._freeze_stages()

        self.feat_dim = self.block.expansion * 64 * 2**(
            len(self.stage_blocks) - 1)
        self.maxpool2048_256 = nn.MaxPool3d(kernel_size=(8,1,1),stride=(8,1,1),padding=(0,0,0))
        self.maxpool1024_256 = nn.MaxPool3d(kernel_size=(4,1,1),stride=(4,1,1),padding=(0,0,0))
        self.maxpool512_256 = nn.MaxPool3d(kernel_size=(2,1,1),stride=(2,1,1),padding=(0,0,0))
        self.conv512_256 = ConvModule(
            512, 256,
            1,
            conv_cfg=conv_cfg,
            norm_cfg=norm_cfg,
            inplace=False)
        self.conv1024_256 = ConvModule(
            1024, 256,
            1,
            conv_cfg=conv_cfg,
            norm_cfg=norm_cfg,
            inplace=False)
        self.conv2048_256 = ConvModule(
            2048, 256,
            1,
            conv_cfg=conv_cfg,
            norm_cfg=norm_cfg,
            inplace=False)
예제 #30
0
    def __init__(self,
                 feature_channels=256,
                 mask_channels=80,
                 num_convs=3,
                 ds_method='interpolate',
                 num_levels=5,
                 proto_out=8,
                 conv_out_channels=256,
                 groups=True,
                 conv_cfg=None,
                 norm_cfg=None):
        super(SemanticPyramidNeck, self).__init__()
        self.mask_channels = mask_channels
        self.num_convs = num_convs
        self.ds_method = ds_method
        self.num_levels = num_levels
        self.conv_out_channels = conv_out_channels
        self.feature_channels = feature_channels
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.proto_out = proto_out
        self.fp16_enabled = False
        self.groups = groups
        if self.ds_method == 'conv':
            self.ds_convs = nn.ModuleList()
            for i in range(self.num_levels - 1):
                self.ds_convs.append(
                    ConvModule(self.mask_channels,
                               self.mask_channels,
                               3,
                               stride=2,
                               padding=1,
                               norm_cfg=norm_cfg,
                               conv_cfg=conv_cfg))

        # self.lateral_convs = nn.ModuleList()
        self.combine_convs = nn.ModuleList()
        # self.proto_convs = nn.ModuleList()
        for i in range(num_levels):
            # convs = nn.ModuleList()
            # feats_stride = 1 if i==0 else 2
            # if self.groups:
            #     self.lateral_convs.append(ConvModule(self.mask_channels,self.mask_channels, 3, stride=feats_stride, padding=1,
            #                                    groups = self.mask_channels,
            #                                    conv_cfg=self.conv_cfg,
            #                                    norm_cfg=self.norm_cfg))
            # else:
            #     self.lateral_convs.append(ConvModule(
            #         self.mask_channels, self.mask_channels, 3, stride=feats_stride, padding=1,
            #         conv_cfg=self.conv_cfg,
            #         norm_cfg=self.norm_cfg
            #     ))

            # self.proto_convs.append(ConvModule(self.mask_channels, self.proto_out, 1, conv_cfg=conv_cfg, norm_cfg=norm_cfg))
            # self.convs = nn.ModuleList()
            # for i in range(self.num_convs):
            #     # in_channels = self.in_channels if i == 0 else conv_out_channels
            #     if self.groups:
            #         self.convs.append(
            #             ConvModule(
            #                 conv_out_channels,
            #                 conv_out_channels,
            #                 3,
            #                 padding=1,
            #                 groups=in_channels,
            #                 conv_cfg=self.conv_cfg,
            #                 norm_cfg=self.norm_cfg))
            #     else:
            #         self.convs.append(
            #             ConvModule(
            #                 conv_out_channels,
            #                 conv_out_channels,
            #                 3,
            #                 padding=1,
            #                 conv_cfg=self.conv_cfg,
            #                 norm_cfg=self.norm_cfg))

            # self.conv_embedding = ConvModule(
            #     conv_out_channels,
            #     conv_out_channels,
            #     1,
            #     conv_cfg=self.conv_cfg,
            #     norm_cfg=self.norm_cfg)
            # self.conv_logits = nn.Conv2d(conv_out_channels, self.num_classes, 1)
            #     for j in range(self.num_convs):
            #         in_channel = self.mask_channels + self.feature_channels if j==0 else self.feature_channels
            #         convs.append(ConvModule(in_channel, self.conv_out_channels, 3, padding=1,
            #                                 norm_cfg=norm_cfg, conv_cfg=conv_cfg))
            #     self.combine_convs.append(convs)
            if self.num_convs == 3:
                self.combine_convs.append(
                    nn.Sequential(
                        ConvModule(self.mask_channels + self.feature_channels,
                                   self.conv_out_channels,
                                   3,
                                   padding=1,
                                   norm_cfg=norm_cfg,
                                   conv_cfg=conv_cfg),
                        ConvModule(self.conv_out_channels,
                                   self.conv_out_channels,
                                   3,
                                   padding=1,
                                   norm_cfg=norm_cfg,
                                   conv_cfg=conv_cfg),
                        ConvModule(self.conv_out_channels,
                                   self.conv_out_channels,
                                   3,
                                   padding=1,
                                   norm_cfg=norm_cfg,
                                   conv_cfg=conv_cfg)))
            elif self.num_convs == 2:
                self.combine_convs.append(
                    nn.Sequential(
                        ConvModule(self.mask_channels + self.feature_channels,
                                   self.conv_out_channels,
                                   3,
                                   padding=1,
                                   norm_cfg=norm_cfg,
                                   conv_cfg=conv_cfg),
                        ConvModule(self.conv_out_channels,
                                   self.conv_out_channels,
                                   3,
                                   padding=1,
                                   norm_cfg=norm_cfg,
                                   conv_cfg=conv_cfg)))
            elif self.num_convs == 1:
                self.combine_convs.append(
                    nn.Sequential(
                        ConvModule(self.mask_channels + self.feature_channels,
                                   self.conv_out_channels,
                                   3,
                                   padding=1,
                                   norm_cfg=norm_cfg,
                                   conv_cfg=conv_cfg)))