Beispiel #1
0
 def __init__(self,
              *args,
              upsample=dict(type='nearest', scale_factor=2),
              **kwargs):
     super().__init__(*args, **kwargs)
     self.with_upsample = upsample is not None
     if self.with_upsample:
         if upsample.get('type') == 'fused_nn':
             assert isinstance(self.conv, nn.ConvTranspose2d)
             self.conv.register_forward_pre_hook(
                 EqualizedLRConvUpModule.fused_nn_hook)
         else:
             self.upsample_layer = build_upsample_layer(upsample)
Beispiel #2
0
def test_upsample_layer():
    with pytest.raises(TypeError):
        # cfg must be a dict
        cfg = 'bilinear'
        build_upsample_layer(cfg)

    with pytest.raises(KeyError):
        # `type` must be in cfg
        cfg = dict()
        build_upsample_layer(cfg)

    with pytest.raises(KeyError):
        # unsupported activation type
        cfg = dict(type='FancyUpsample')
        build_upsample_layer(cfg)

    for type_name in ['nearest', 'bilinear']:
        cfg['type'] = type_name
        layer = build_upsample_layer(cfg)
        assert isinstance(layer, nn.Upsample)
        assert layer.mode == type_name

    cfg = dict(type='deconv',
               in_channels=3,
               out_channels=3,
               kernel_size=3,
               stride=2)
    layer = build_upsample_layer(cfg)
    assert isinstance(layer, nn.ConvTranspose2d)

    cfg = dict(type='pixel_shuffle',
               in_channels=3,
               out_channels=3,
               scale_factor=2,
               upsample_kernel=3)
    layer = build_upsample_layer(cfg)

    assert isinstance(layer, PixelShufflePack)
    assert layer.scale_factor == 2
    assert layer.upsample_kernel == 3
Beispiel #3
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 dim_after_concat,
                 act_cfg=dict(type='ReLU'),
                 upsample_cfg=dict(type='nearest', scale_factor=2),
                 sn_eps=1e-6,
                 with_spectral_norm=True,
                 input_is_label=False,
                 auto_sync_bn=True):
        super().__init__()
        self.activation = build_activation_layer(act_cfg)
        self.upsample_cfg = deepcopy(upsample_cfg)
        self.with_upsample = upsample_cfg is not None
        if self.with_upsample:
            self.upsample_layer = build_upsample_layer(self.upsample_cfg)
        self.learnable_sc = in_channels != out_channels or self.with_upsample
        if self.learnable_sc:
            self.shortcut = SNConvModule(
                in_channels=in_channels,
                out_channels=out_channels,
                kernel_size=1,
                stride=1,
                padding=0,
                act_cfg=None,
                with_spectral_norm=with_spectral_norm,
                spectral_norm_cfg=dict(eps=sn_eps))
        # Here in_channels of BigGANGenResBlock equal to num_features of
        # BigGANConditionBN
        self.bn1 = BigGANConditionBN(
            in_channels,
            dim_after_concat,
            sn_eps=sn_eps,
            input_is_label=input_is_label,
            with_spectral_norm=with_spectral_norm,
            auto_sync_bn=auto_sync_bn)
        # Here out_channels of BigGANGenResBlock equal to num_features of
        # BigGANConditionBN
        self.bn2 = BigGANConditionBN(
            out_channels,
            dim_after_concat,
            sn_eps=sn_eps,
            input_is_label=input_is_label,
            with_spectral_norm=with_spectral_norm,
            auto_sync_bn=auto_sync_bn)

        self.conv1 = SNConvModule(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=3,
            stride=1,
            padding=1,
            act_cfg=None,
            with_spectral_norm=with_spectral_norm,
            spectral_norm_cfg=dict(eps=sn_eps))

        self.conv2 = SNConvModule(
            in_channels=out_channels,
            out_channels=out_channels,
            kernel_size=3,
            stride=1,
            padding=1,
            act_cfg=None,
            with_spectral_norm=with_spectral_norm,
            spectral_norm_cfg=dict(eps=sn_eps))
Beispiel #4
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 dim_after_concat,
                 act_cfg=dict(type='ReLU'),
                 upsample_cfg=dict(type='nearest', scale_factor=2),
                 sn_eps=1e-6,
                 sn_style='ajbrock',
                 bn_eps=1e-5,
                 with_spectral_norm=True,
                 input_is_label=False,
                 auto_sync_bn=True,
                 channel_ratio=4):
        super().__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.hidden_channels = self.in_channels // channel_ratio
        self.activation = build_activation_layer(act_cfg)
        self.upsample_cfg = deepcopy(upsample_cfg)
        self.with_upsample = upsample_cfg is not None
        if self.with_upsample:
            self.upsample_layer = build_upsample_layer(self.upsample_cfg)
        # Here in_channels of BigGANGenResBlock equal to num_features of
        # BigGANConditionBN
        self.bn1 = BigGANConditionBN(in_channels,
                                     dim_after_concat,
                                     sn_eps=sn_eps,
                                     sn_style=sn_style,
                                     bn_eps=bn_eps,
                                     input_is_label=input_is_label,
                                     with_spectral_norm=with_spectral_norm,
                                     auto_sync_bn=auto_sync_bn)
        # Here out_channels of BigGANGenResBlock equal to num_features of
        # BigGANConditionBN
        self.bn2 = BigGANConditionBN(self.hidden_channels,
                                     dim_after_concat,
                                     sn_eps=sn_eps,
                                     sn_style=sn_style,
                                     bn_eps=bn_eps,
                                     input_is_label=input_is_label,
                                     with_spectral_norm=with_spectral_norm,
                                     auto_sync_bn=auto_sync_bn)

        self.bn3 = BigGANConditionBN(self.hidden_channels,
                                     dim_after_concat,
                                     sn_eps=sn_eps,
                                     sn_style=sn_style,
                                     bn_eps=bn_eps,
                                     input_is_label=input_is_label,
                                     with_spectral_norm=with_spectral_norm,
                                     auto_sync_bn=auto_sync_bn)

        self.bn4 = BigGANConditionBN(self.hidden_channels,
                                     dim_after_concat,
                                     sn_eps=sn_eps,
                                     sn_style=sn_style,
                                     bn_eps=bn_eps,
                                     input_is_label=input_is_label,
                                     with_spectral_norm=with_spectral_norm,
                                     auto_sync_bn=auto_sync_bn)

        self.conv1 = SNConvModule(in_channels=in_channels,
                                  out_channels=self.hidden_channels,
                                  kernel_size=1,
                                  stride=1,
                                  padding=0,
                                  act_cfg=None,
                                  with_spectral_norm=with_spectral_norm,
                                  spectral_norm_cfg=dict(eps=sn_eps,
                                                         sn_style=sn_style))

        self.conv2 = SNConvModule(in_channels=self.hidden_channels,
                                  out_channels=self.hidden_channels,
                                  kernel_size=3,
                                  stride=1,
                                  padding=1,
                                  act_cfg=None,
                                  with_spectral_norm=with_spectral_norm,
                                  spectral_norm_cfg=dict(eps=sn_eps,
                                                         sn_style=sn_style))

        self.conv3 = SNConvModule(in_channels=self.hidden_channels,
                                  out_channels=self.hidden_channels,
                                  kernel_size=3,
                                  stride=1,
                                  padding=1,
                                  act_cfg=None,
                                  with_spectral_norm=with_spectral_norm,
                                  spectral_norm_cfg=dict(eps=sn_eps,
                                                         sn_style=sn_style))

        self.conv4 = SNConvModule(in_channels=self.hidden_channels,
                                  out_channels=out_channels,
                                  kernel_size=1,
                                  stride=1,
                                  padding=0,
                                  act_cfg=None,
                                  with_spectral_norm=with_spectral_norm,
                                  spectral_norm_cfg=dict(eps=sn_eps,
                                                         sn_style=sn_style))