Exemple #1
0
    def _make_deconv_layer(self, num_layers, num_filters, num_kernels):
        """Make deconv layers."""
        if num_layers != len(num_filters):
            error_msg = f'num_layers({num_layers}) ' \
                        f'!= length of num_filters({len(num_filters)})'
            raise ValueError(error_msg)
        if num_layers != len(num_kernels):
            error_msg = f'num_layers({num_layers}) ' \
                        f'!= length of num_kernels({len(num_kernels)})'
            raise ValueError(error_msg)

        layers = []
        for i in range(num_layers):
            kernel, padding, output_padding = \
                self._get_deconv_cfg(num_kernels[i])

            planes = num_filters[i]
            layers.append(
                build_upsample_layer(dict(type='deconv'),
                                     in_channels=self.in_channels,
                                     out_channels=planes,
                                     kernel_size=kernel,
                                     stride=2,
                                     padding=padding,
                                     output_padding=output_padding,
                                     bias=False))
            layers.append(nn.BatchNorm2d(planes))
            layers.append(nn.ReLU(inplace=True))
            self.in_channels = planes

        return nn.Sequential(*layers)
Exemple #2
0
    def __init__(self,
                 in_filters,
                 out_filters,
                 conv_cfg,
                 norm_cfg,
                 upsample_cfg,
                 up_output_padding=0):
        super(UpBlock, self).__init__()
        # self.drop_out = drop_out
        self.trans_conv = conv3x3(in_filters, out_filters, conv_cfg)
        self.trans_bn = build_norm_layer(norm_cfg, out_filters)[1]
        self.trans_act = nn.ReLU(inplace=True)

        self.conv0 = conv1x3(out_filters, out_filters, conv_cfg)
        self.bn0 = build_norm_layer(norm_cfg, out_filters)[1]
        self.act0 = nn.ReLU(inplace=True)

        self.conv1 = conv3x1(out_filters, out_filters, conv_cfg)
        self.bn1 = build_norm_layer(norm_cfg, out_filters)[1]
        self.act1 = nn.ReLU(inplace=True)

        self.conv2 = conv3x3(out_filters, out_filters, conv_cfg)
        self.bn2 = build_norm_layer(norm_cfg, out_filters)[1]
        self.act2 = nn.ReLU(inplace=True)

        self.up_conv = build_upsample_layer(upsample_cfg,
                                            in_channels=out_filters,
                                            out_channels=out_filters,
                                            kernel_size=2,
                                            stride=2,
                                            padding=0,
                                            output_padding=up_output_padding)
    def _make_deconv_layers(self, in_channels, deconv_layer_output_channels,
                            num_deconv_layers, num_deconv_filters,
                            num_deconv_kernels, num_basic_blocks, cat_output):

        deconv_layers = []
        for i in range(num_deconv_layers):
            if cat_output[i]:
                in_channels += deconv_layer_output_channels[i]

            planes = num_deconv_filters[i]
            deconv_kernel, padding, output_padding = \
                self._get_deconv_cfg(num_deconv_kernels[i])

            layers = []
            layers.append(
                nn.Sequential(
                    build_upsample_layer(
                        dict(type='deconv'),
                        in_channels=in_channels,
                        out_channels=planes,
                        kernel_size=deconv_kernel,
                        stride=2,
                        padding=padding,
                        output_padding=output_padding,
                        bias=False), nn.BatchNorm2d(planes, momentum=0.1),
                    nn.ReLU(inplace=True)))
            for _ in range(num_basic_blocks):
                layers.append(nn.Sequential(BasicBlock(planes, planes), ))
            deconv_layers.append(nn.Sequential(*layers))
            in_channels = planes

        return nn.ModuleList(deconv_layers)
Exemple #4
0
def test_build_upsample_layer():
    layer1 = nn.ConvTranspose2d(
        in_channels=3,
        out_channels=10,
        kernel_size=3,
        stride=2,
        padding=1,
        output_padding=1,
        bias=False)

    layer2 = build_upsample_layer(
        dict(type='deconv'),
        in_channels=3,
        out_channels=10,
        kernel_size=3,
        stride=2,
        padding=1,
        output_padding=1,
        bias=False)
    layer2.load_state_dict(layer1.state_dict())

    input_shape = (1, 3, 32, 32)
    inputs = _demo_inputs(input_shape)
    out1 = layer1(inputs)
    out2 = layer2(inputs)
    assert torch.equal(out1, out2)
    def __init__(self,
                 in_channels=[128, 128, 256],
                 out_channels=[256, 256, 256],
                 upsample_strides=[1, 2, 4],
                 norm_cfg=dict(type='BN', eps=1e-3, momentum=0.01),
                 upsample_cfg=dict(type='deconv', bias=False)):
        # if for GroupNorm,
        # cfg is dict(type='GN', num_groups=num_groups, eps=1e-3, affine=True)
        super(SECONDFPN, self).__init__()
        assert len(out_channels) == len(upsample_strides) == len(in_channels)
        self.in_channels = in_channels
        self.out_channels = out_channels

        deblocks = []
        for i, out_channel in enumerate(out_channels):
            upsample_layer = build_upsample_layer(
                upsample_cfg,
                in_channels=in_channels[i],
                out_channels=out_channel,
                kernel_size=upsample_strides[i],
                stride=upsample_strides[i])
            deblock = nn.Sequential(upsample_layer,
                                    build_norm_layer(norm_cfg, out_channel)[1],
                                    nn.ReLU(inplace=True))
            deblocks.append(deblock)
        self.deblocks = nn.ModuleList(deblocks)
    def __init__(self,
                 in_channels=[128, 128, 256],
                 out_channels=[256, 256, 256],
                 upsample_strides=[1, 2, 4],
                 norm_cfg=dict(type='BN', eps=1e-3, momentum=0.01),
                 upsample_cfg=dict(type='deconv', bias=False),
                 conv_cfg=dict(type='Conv2d', bias=False),
                 use_conv_for_no_stride=False):
        # if for GroupNorm,
        # cfg is dict(type='GN', num_groups=num_groups, eps=1e-3, affine=True)
        super(SECONDFPNMASK, self).__init__()
        assert len(out_channels) == len(upsample_strides) == len(in_channels)
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.fp16_enabled = False

        deblocks = []
        channel_blocks = []
        for i, out_channel in enumerate(out_channels):
            stride = upsample_strides[i]
            if stride > 1 or (stride == 1 and not use_conv_for_no_stride):
                upsample_layer = build_upsample_layer(
                    upsample_cfg,
                    in_channels=in_channels[i],
                    out_channels=out_channel,
                    kernel_size=upsample_strides[i],
                    stride=upsample_strides[i])

            else:
                stride = np.round(1 / stride).astype(np.int64)
                upsample_layer = build_conv_layer(
                    conv_cfg,
                    in_channels=in_channels[i],
                    out_channels=out_channel,
                    kernel_size=stride,
                    stride=stride)

            deblock = nn.Sequential(upsample_layer,
                                    build_norm_layer(norm_cfg, out_channel)[1],
                                    nn.ReLU(inplace=True))
            deblocks.append(deblock)

            conv_1 = build_conv_layer(conv_cfg, in_channels=out_channel, out_channels=out_channel,
                                      kernel_size=1, stride=1)
           
        self.deblocks = nn.ModuleList(deblocks)
        self.binary_cls = nn.Sequential(
            build_conv_layer(conv_cfg, sum(out_channels), out_channels[0], 3, padding=1),
            build_norm_layer(norm_cfg, out_channels[0])[1],
            nn.ReLU(inplace=True),
            build_conv_layer(conv_cfg, out_channels[0], out_channels[0], 1, 1),
            build_norm_layer(norm_cfg, out_channels[0])[1],
            nn.ReLU(inplace=True),
            build_conv_layer(conv_cfg, out_channels[0], 1, 1),
            nn.Sigmoid()
        )
Exemple #7
0
    def __init__(self,
                 conv_block,
                 in_channels,
                 skip_channels,
                 out_channels,
                 num_convs=2,
                 stride=1,
                 dilation=1,
                 with_cp=False,
                 conv_cfg=None,
                 norm_cfg=dict(type='BN'),
                 act_cfg=dict(type='ReLU'),
                 upsample_cfg=dict(type='InterpConv'),
                 dcn=None,
                 plugins=None):
        super(UpConvBlock, self).__init__()
        assert dcn is None, 'Not implemented yet.'
        assert plugins is None, 'Not implemented yet.'

        self.conv_block = conv_block(in_channels=2 * skip_channels,
                                     out_channels=out_channels,
                                     num_convs=num_convs,
                                     stride=stride,
                                     dilation=dilation,
                                     with_cp=with_cp,
                                     conv_cfg=conv_cfg,
                                     norm_cfg=norm_cfg,
                                     act_cfg=act_cfg,
                                     dcn=None,
                                     plugins=None)
        if upsample_cfg is not None:
            self.upsample = build_upsample_layer(cfg=upsample_cfg,
                                                 in_channels=in_channels,
                                                 out_channels=skip_channels,
                                                 with_cp=with_cp,
                                                 norm_cfg=norm_cfg,
                                                 act_cfg=act_cfg)
        else:
            self.upsample = ConvModule(in_channels,
                                       skip_channels,
                                       kernel_size=1,
                                       stride=1,
                                       padding=0,
                                       conv_cfg=conv_cfg,
                                       norm_cfg=norm_cfg,
                                       act_cfg=act_cfg)
    def __init__(self,
                 semantic_in_channel=256,
                 semantic_out_channel=256,
                 instance_in_channel=256,
                 instance_out_channel=256,
                 dilations=[1, 3, 5],
                 out_size=14,
                 num_classes=80,
                 semantic_out_stride=4,
                 mask_use_sigmoid=False,
                 upsample_cfg=dict(type='bilinear', scale_factor=2)):
        super(SFMStage, self).__init__()

        self.semantic_out_stride = semantic_out_stride
        self.mask_use_sigmoid = mask_use_sigmoid
        self.num_classes = num_classes

        # for extracting instance-wise semantic feats
        self.semantic_transform_in = nn.Conv2d(semantic_in_channel,
                                               semantic_out_channel, 1)
        self.semantic_roi_extractor = build_roi_extractor(
            dict(type='SingleRoIExtractor',
                 roi_layer=dict(type='RoIAlign',
                                output_size=out_size,
                                sampling_ratio=0),
                 out_channels=semantic_out_channel,
                 featmap_strides=[
                     semantic_out_stride,
                 ]))
        self.semantic_transform_out = nn.Conv2d(semantic_out_channel,
                                                semantic_out_channel, 1)

        self.instance_logits = nn.Conv2d(instance_in_channel, num_classes, 1)

        fuse_in_channel = instance_in_channel + semantic_out_channel + 2
        self.fuse_conv = nn.ModuleList([
            nn.Conv2d(fuse_in_channel, instance_in_channel, 1),
            MultiBranchFusion(instance_in_channel, dilations=dilations)
        ])

        self.fuse_transform_out = nn.Conv2d(instance_in_channel,
                                            instance_out_channel - 2, 1)
        self.upsample = build_upsample_layer(upsample_cfg.copy())
        self.relu = nn.ReLU(inplace=True)

        self._init_weights()
Exemple #9
0
    def __init__(self,
                 in_channels=[128, 128, 256],
                 out_channels=[256, 256, 256],
                 upsample_strides=[1, 2, 4],
                 norm_cfg=dict(type='BN', eps=1e-3, momentum=0.01),
                 upsample_cfg=dict(type='deconv', bias=False),
                 conv_cfg=dict(type='Conv2d', bias=False),
                 use_conv_for_no_stride=False,
                 init_cfg=None):
        # if for GroupNorm,
        # cfg is dict(type='GN', num_groups=num_groups, eps=1e-3, affine=True)
        super(SECONDFPN, self).__init__(init_cfg=init_cfg)
        assert len(out_channels) == len(upsample_strides) == len(in_channels)
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.fp16_enabled = False

        deblocks = []
        for i, out_channel in enumerate(out_channels):
            stride = upsample_strides[i]
            if stride > 1 or (stride == 1 and not use_conv_for_no_stride):
                upsample_layer = build_upsample_layer(
                    upsample_cfg,
                    in_channels=in_channels[i],
                    out_channels=out_channel,
                    kernel_size=upsample_strides[i],
                    stride=upsample_strides[i])
            else:
                stride = np.round(1 / stride).astype(np.int64)
                upsample_layer = build_conv_layer(conv_cfg,
                                                  in_channels=in_channels[i],
                                                  out_channels=out_channel,
                                                  kernel_size=stride,
                                                  stride=stride)

            deblock = nn.Sequential(upsample_layer,
                                    build_norm_layer(norm_cfg, out_channel)[1],
                                    nn.ReLU(inplace=True))
            deblocks.append(deblock)
        self.deblocks = nn.ModuleList(deblocks)

        if init_cfg is None:
            self.init_cfg = [
                dict(type='Kaiming', layer='ConvTranspose2d'),
                dict(type='Constant', layer='NaiveSyncBatchNorm2d', val=1.0)
            ]
Exemple #10
0
    def __init__(self,
                 hidden_dim,
                 input_dim,
                 simi_dim=None,
                 conv_cfg=dict(type='Conv2d'),
                 norm_cfg=dict(type='BN1d', eps=1e-3, momentum=0.01),
                 upsample_cfg=dict(type='deconv', bias=False),
                 upsample_stride=2,
                 need_offset=True):
        super(UpdateBlock, self).__init__()

        self.upsample_stride = upsample_stride
        self.need_offset = need_offset

        self.encoder = MotionEncoder(simi_dim,
                                     conv_cfg=conv_cfg,
                                     norm_cfg=norm_cfg)
        self.gru = ConvGRU(hidden_dim=hidden_dim,
                           input_dim=128 + hidden_dim,
                           conv_cfg=conv_cfg)

        stride = upsample_stride
        if stride > 1:
            upsample_layer = build_upsample_layer(upsample_cfg,
                                                  in_channels=hidden_dim,
                                                  out_channels=hidden_dim,
                                                  kernel_size=upsample_stride,
                                                  stride=upsample_stride)
        else:
            stride = np.round(1 / stride).astype(np.int64)
            upsample_layer = build_conv_layer(conv_cfg,
                                              in_channels=hidden_dim,
                                              out_channels=hidden_dim,
                                              kernel_size=stride,
                                              stride=stride)

        self.upsample = nn.Sequential(upsample_layer, nn.ReLU(inplace=True))

        if self.need_offset:
            self.offset_head = OffsetHead(hidden_dim, conv_cfg=conv_cfg)
    def __init__(self,
                 in_channels,
                 out_channels,
                 num_outs,
                 start_level=0,
                 end_level=-1,
                 norm_cfg=None,
                 act_cfg=None,
                 order=('conv', 'norm', 'act'),
                 upsample_cfg=dict(type='carafe',
                                   up_kernel=5,
                                   up_group=1,
                                   encoder_kernel=3,
                                   encoder_dilation=1)):
        super(FPN_CARAFE, 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.norm_cfg = norm_cfg
        self.act_cfg = act_cfg
        self.with_bias = norm_cfg is None
        self.upsample_cfg = upsample_cfg.copy()
        self.upsample = self.upsample_cfg.get('type')
        self.relu = nn.ReLU(inplace=False)

        self.order = order
        assert order in [('conv', 'norm', 'act'), ('act', 'conv', 'norm')]

        assert self.upsample in [
            'nearest', 'bilinear', 'deconv', 'pixel_shuffle', 'carafe', None
        ]
        if self.upsample in ['deconv', 'pixel_shuffle']:
            assert hasattr(
                self.upsample_cfg,
                'upsample_kernel') and self.upsample_cfg.upsample_kernel > 0
            self.upsample_kernel = self.upsample_cfg.pop('upsample_kernel')

        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.lateral_convs = nn.ModuleList()
        self.fpn_convs = nn.ModuleList()
        self.upsample_modules = 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,
                                bias=self.with_bias,
                                act_cfg=act_cfg,
                                inplace=False,
                                order=self.order)
            fpn_conv = ConvModule(out_channels,
                                  out_channels,
                                  3,
                                  padding=1,
                                  norm_cfg=self.norm_cfg,
                                  bias=self.with_bias,
                                  act_cfg=act_cfg,
                                  inplace=False,
                                  order=self.order)
            if i != self.backbone_end_level - 1:
                upsample_cfg_ = self.upsample_cfg.copy()
                if self.upsample == 'deconv':
                    upsample_cfg_.update(
                        in_channels=out_channels,
                        out_channels=out_channels,
                        kernel_size=self.upsample_kernel,
                        stride=2,
                        padding=(self.upsample_kernel - 1) // 2,
                        output_padding=(self.upsample_kernel - 1) // 2)
                elif self.upsample == 'pixel_shuffle':
                    upsample_cfg_.update(in_channels=out_channels,
                                         out_channels=out_channels,
                                         scale_factor=2,
                                         upsample_kernel=self.upsample_kernel)
                elif self.upsample == 'carafe':
                    upsample_cfg_.update(channels=out_channels, scale_factor=2)
                else:
                    # suppress warnings
                    align_corners = (None
                                     if self.upsample == 'nearest' else False)
                    upsample_cfg_.update(scale_factor=2,
                                         mode=self.upsample,
                                         align_corners=align_corners)
                upsample_module = build_upsample_layer(upsample_cfg_)
                self.upsample_modules.append(upsample_module)
            self.lateral_convs.append(l_conv)
            self.fpn_convs.append(fpn_conv)

        # add extra conv layers (e.g., RetinaNet)
        extra_out_levels = (num_outs - self.backbone_end_level +
                            self.start_level)
        if extra_out_levels >= 1:
            for i in range(extra_out_levels):
                in_channels = (self.in_channels[self.backbone_end_level -
                                                1] if i == 0 else out_channels)
                extra_l_conv = ConvModule(in_channels,
                                          out_channels,
                                          3,
                                          stride=2,
                                          padding=1,
                                          norm_cfg=norm_cfg,
                                          bias=self.with_bias,
                                          act_cfg=act_cfg,
                                          inplace=False,
                                          order=self.order)
                if self.upsample == 'deconv':
                    upsampler_cfg_ = dict(
                        in_channels=out_channels,
                        out_channels=out_channels,
                        kernel_size=self.upsample_kernel,
                        stride=2,
                        padding=(self.upsample_kernel - 1) // 2,
                        output_padding=(self.upsample_kernel - 1) // 2)
                elif self.upsample == 'pixel_shuffle':
                    upsampler_cfg_ = dict(in_channels=out_channels,
                                          out_channels=out_channels,
                                          scale_factor=2,
                                          upsample_kernel=self.upsample_kernel)
                elif self.upsample == 'carafe':
                    upsampler_cfg_ = dict(channels=out_channels,
                                          scale_factor=2,
                                          **self.upsample_cfg)
                else:
                    # suppress warnings
                    align_corners = (None
                                     if self.upsample == 'nearest' else False)
                    upsampler_cfg_ = dict(scale_factor=2,
                                          mode=self.upsample,
                                          align_corners=align_corners)
                upsampler_cfg_['type'] = self.upsample
                upsample_module = build_upsample_layer(upsampler_cfg_)
                extra_fpn_conv = ConvModule(out_channels,
                                            out_channels,
                                            3,
                                            padding=1,
                                            norm_cfg=self.norm_cfg,
                                            bias=self.with_bias,
                                            act_cfg=act_cfg,
                                            inplace=False,
                                            order=self.order)
                self.upsample_modules.append(upsample_module)
                self.fpn_convs.append(extra_fpn_conv)
                self.lateral_convs.append(extra_l_conv)
Exemple #12
0
    def __init__(self,
                 num_convs=8,
                 roi_feat_size=28,
                 in_channels=256,
                 conv_kernel_size=3,
                 conv_out_channels=256,
                 num_keypoints=17,
                 heatmap_size=56,
                 upsample_cfg=dict(type='deconv', scale_factor=2),
                 up_scale=2,
                 conv_cfg=None,
                 norm_cfg=None,
                 loss_keypoint=dict(type='CrossEntropyLoss',
                                    use_mask=True,
                                    loss_weight=1.0)):
        super(FCNKeypointHead, self).__init__()
        self.upsample_cfg = upsample_cfg.copy()
        if self.upsample_cfg['type'] not in [
                None, 'deconv', 'nearest', 'bilinear', 'carafe'
        ]:
            raise ValueError(
                'Invalid upsample method {}, accepted methods '
                'are "deconv", "nearest", "bilinear", "carafe"'.format(
                    self.upsample_cfg['type']))
        self.num_convs = num_convs
        # WARN: roi_feat_size is reserved and not used
        self.roi_feat_size = _pair(roi_feat_size)
        self.in_channels = in_channels
        self.conv_kernel_size = conv_kernel_size
        self.conv_out_channels = conv_out_channels
        self.upsample_method = self.upsample_cfg.get('type')
        self.scale_factor = self.upsample_cfg.pop('scale_factor')
        self.num_keypoints = num_keypoints
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.fp16_enabled = False
        #self.loss_keypoint = build_loss(loss_keypoint)

        self.convs = nn.ModuleList()
        for i in range(self.num_convs):
            in_channels = (self.in_channels
                           if i == 0 else self.conv_out_channels)
            padding = (self.conv_kernel_size - 1) // 2
            self.convs.append(
                ConvModule(in_channels,
                           self.conv_out_channels,
                           self.conv_kernel_size,
                           padding=padding,
                           conv_cfg=conv_cfg,
                           norm_cfg=norm_cfg))

        upsample_in_channels = (self.conv_out_channels
                                if self.num_convs > 0 else in_channels)
        upsample_cfg_ = self.upsample_cfg.copy()
        upsample_cfg_.update(in_channels=upsample_in_channels,
                             out_channels=num_keypoints,
                             kernel_size=self.scale_factor * 2,
                             stride=self.scale_factor,
                             padding=1)

        #self.conv_logits = nn.Conv2d(self.conv_out_channels, num_keypoints, 1)
        self.upsample = build_upsample_layer(upsample_cfg_)
        #self.relu = nn.ReLU(inplace=True)
        self.up_scale = up_scale
Exemple #13
0
    def __init__(self,
                 num_convs=4,
                 roi_feat_size=14,
                 in_channels=256,
                 conv_kernel_size=3,
                 conv_out_channels=256,
                 num_classes=80,
                 class_agnostic=False,
                 upsample_cfg=dict(type='deconv', scale_factor=2),
                 conv_cfg=None,
                 norm_cfg=None,
                 predictor_cfg=dict(type='Conv'),
                 loss_mask=dict(type='CrossEntropyLoss',
                                use_mask=True,
                                loss_weight=1.0),
                 init_cfg=None):
        assert init_cfg is None, 'To prevent abnormal initialization ' \
                                 'behavior, init_cfg is not allowed to be set'
        super(FCNMaskHead, self).__init__(init_cfg)
        self.upsample_cfg = upsample_cfg.copy()
        if self.upsample_cfg['type'] not in [
                None, 'deconv', 'nearest', 'bilinear', 'carafe'
        ]:
            raise ValueError(
                f'Invalid upsample method {self.upsample_cfg["type"]}, '
                'accepted methods are "deconv", "nearest", "bilinear", '
                '"carafe"')
        self.num_convs = num_convs
        # WARN: roi_feat_size is reserved and not used
        self.roi_feat_size = _pair(roi_feat_size)
        self.in_channels = in_channels
        self.conv_kernel_size = conv_kernel_size
        self.conv_out_channels = conv_out_channels
        self.upsample_method = self.upsample_cfg.get('type')
        self.scale_factor = self.upsample_cfg.pop('scale_factor', None)
        self.num_classes = num_classes
        self.class_agnostic = class_agnostic
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.predictor_cfg = predictor_cfg
        self.fp16_enabled = False
        self.loss_mask = build_loss(loss_mask)

        self.convs = ModuleList()
        for i in range(self.num_convs):
            in_channels = (self.in_channels
                           if i == 0 else self.conv_out_channels)
            padding = (self.conv_kernel_size - 1) // 2
            self.convs.append(
                ConvModule(in_channels,
                           self.conv_out_channels,
                           self.conv_kernel_size,
                           padding=padding,
                           conv_cfg=conv_cfg,
                           norm_cfg=norm_cfg))
        upsample_in_channels = (self.conv_out_channels
                                if self.num_convs > 0 else in_channels)
        upsample_cfg_ = self.upsample_cfg.copy()
        if self.upsample_method is None:
            self.upsample = None
        elif self.upsample_method == 'deconv':
            upsample_cfg_.update(in_channels=upsample_in_channels,
                                 out_channels=self.conv_out_channels,
                                 kernel_size=self.scale_factor,
                                 stride=self.scale_factor)
            self.upsample = build_upsample_layer(upsample_cfg_)
        elif self.upsample_method == 'carafe':
            upsample_cfg_.update(channels=upsample_in_channels,
                                 scale_factor=self.scale_factor)
            self.upsample = build_upsample_layer(upsample_cfg_)
        else:
            # suppress warnings
            align_corners = (None
                             if self.upsample_method == 'nearest' else False)
            upsample_cfg_.update(scale_factor=self.scale_factor,
                                 mode=self.upsample_method,
                                 align_corners=align_corners)
            self.upsample = build_upsample_layer(upsample_cfg_)

        out_channels = 1 if self.class_agnostic else self.num_classes
        logits_in_channel = (self.conv_out_channels if self.upsample_method
                             == 'deconv' else upsample_in_channels)
        self.conv_logits = build_conv_layer(self.predictor_cfg,
                                            logits_in_channel, out_channels, 1)
        self.relu = nn.ReLU(inplace=True)
        self.debug_imgs = None
Exemple #14
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 hidden_channels=None,
                 num_classes=0,
                 use_cbn=True,
                 use_norm_affine=False,
                 act_cfg=dict(type='ReLU'),
                 norm_cfg=dict(type='BN'),
                 upsample_cfg=dict(type='nearest', scale_factor=2),
                 upsample=True,
                 auto_sync_bn=True,
                 conv_cfg=None,
                 with_spectral_norm=False,
                 with_embedding_spectral_norm=None,
                 sn_style='torch',
                 norm_eps=1e-4,
                 sn_eps=1e-12,
                 init_cfg=dict(type='BigGAN')):

        super().__init__()
        self.learnable_sc = in_channels != out_channels or upsample
        self.with_upsample = upsample
        self.init_type = init_cfg.get('type', None)

        self.activate = build_activation_layer(act_cfg)
        hidden_channels = out_channels if hidden_channels is None \
            else hidden_channels

        if self.with_upsample:
            self.upsample = build_upsample_layer(upsample_cfg)

        self.conv_cfg = deepcopy(self._default_conv_cfg)
        if conv_cfg is not None:
            self.conv_cfg.update(conv_cfg)

        # set `norm_spectral_norm` as `with_spectral_norm` if not defined
        with_embedding_spectral_norm = with_embedding_spectral_norm \
            if with_embedding_spectral_norm is not None else with_spectral_norm

        sn_cfg = dict(eps=sn_eps, sn_style=sn_style)
        self.conv_1 = SNConvModule(
            in_channels,
            hidden_channels,
            with_spectral_norm=with_spectral_norm,
            spectral_norm_cfg=sn_cfg,
            **self.conv_cfg)
        self.conv_2 = SNConvModule(
            hidden_channels,
            out_channels,
            with_spectral_norm=with_spectral_norm,
            spectral_norm_cfg=sn_cfg,
            **self.conv_cfg)

        self.norm_1 = SNConditionNorm(in_channels, num_classes, use_cbn,
                                      norm_cfg, use_norm_affine, auto_sync_bn,
                                      with_embedding_spectral_norm, sn_style,
                                      norm_eps, sn_eps, init_cfg)
        self.norm_2 = SNConditionNorm(hidden_channels, num_classes, use_cbn,
                                      norm_cfg, use_norm_affine, auto_sync_bn,
                                      with_embedding_spectral_norm, sn_style,
                                      norm_eps, sn_eps, init_cfg)

        if self.learnable_sc:
            # use hyperparameters-fixed shortcut here
            self.shortcut = SNConvModule(
                in_channels,
                out_channels,
                kernel_size=1,
                stride=1,
                padding=0,
                act_cfg=None,
                with_spectral_norm=with_spectral_norm,
                spectral_norm_cfg=sn_cfg)
        self.init_weights()