Beispiel #1
0
    def __init__(self,
                 in_channels=64,
                 out_channels=64,
                 bias=True,
                 normalization=False):
        super(DisBlock, self).__init__()
        self.lrelu = nn.LeakyReLU(negative_slope=0.1, inplace=True)
        self.conv1 = nn.Conv2d(in_channels=in_channels,
                               out_channels=out_channels,
                               kernel_size=3,
                               padding=1,
                               bias=bias)
        self.conv2 = nn.Conv2d(in_channels=out_channels,
                               out_channels=out_channels,
                               kernel_size=4,
                               stride=2,
                               padding=1,
                               bias=bias)
        self.bn1 = nn.BatchNorm2d(out_channels, affine=True)
        self.bn2 = nn.BatchNorm2d(out_channels, affine=True)

        initialize_weights([self.conv1, self.conv2], 0.1)

        if normalization:
            self.conv1 = SpectralNorm(self.conv1)
            self.conv2 = SpectralNorm(self.conv2)
Beispiel #2
0
    def __init__(self, in_channels, num_features, gen_blocks, dis_blocks,
                 growth_rate, bn_size):
        super(SNDiscriminator, self).__init__()
        self.crop_size = 4 * pow(2, dis_blocks)

        # image to features
        self.image_to_features = DisBlock(in_channels=in_channels,
                                          out_channels=num_features,
                                          bias=True,
                                          normalization=True)

        # features
        blocks = []
        for i in range(0, dis_blocks - 1):
            blocks.append(
                DisBlock(in_channels=num_features * min(pow(2, i), 8),
                         out_channels=num_features * min(pow(2, i + 1), 8),
                         bias=False,
                         normalization=True))
        self.features = nn.Sequential(*blocks)

        # classifier
        self.classifier = nn.Sequential(
            SpectralNorm(
                nn.Linear(
                    num_features * min(pow(2, dis_blocks - 1), 8) * 4 * 4,
                    100)), nn.LeakyReLU(negative_slope=0.1),
            SpectralNorm(nn.Linear(100, 1)))
Beispiel #3
0
    def __init__(self, in_channels, num_features, gen_blocks, dis_blocks):
        super(SNDiscriminator, self).__init__()

        # image to features
        self.image_to_features = DisBlock(in_channels=in_channels,
                                          out_channels=num_features,
                                          bias=True,
                                          normalization=True)

        # features
        blocks = []
        for i in range(0, dis_blocks - 1):
            blocks.append(
                DisBlock(in_channels=num_features * min(pow(2, i), 8),
                         out_channels=num_features * min(pow(2, i + 1), 8),
                         bias=False,
                         normalization=True))
        self.features = nn.Sequential(*blocks)

        # classifier
        self.classifier = SpectralNorm(
            nn.Conv2d(in_channels=num_features *
                      min(pow(2, dis_blocks - 1), 8),
                      out_channels=1,
                      kernel_size=4,
                      padding=0))
    def __init__(self,
                 input_nc=3,
                 ndf=64,
                 img_f=1024,
                 layers=6,
                 norm='none',
                 activation='LeakyReLU',
                 use_spect=True,
                 use_coord=False):
        super(ResDiscriminator, self).__init__()

        self.layers = layers

        norm_layer = get_norm_layer(norm_type=norm)
        nonlinearity = get_nonlinearity_layer(activation_type=activation)
        self.nonlinearity = nonlinearity

        # encoder part
        self.block0 = ResBlockEncoder(input_nc, ndf, ndf, norm_layer,
                                      nonlinearity, use_spect, use_coord)

        mult = 1
        for i in range(layers - 1):
            mult_prev = mult
            mult = min(2**(i + 1), img_f // ndf)
            block = ResBlockEncoder(ndf * mult_prev, ndf * mult,
                                    ndf * mult_prev, norm_layer, nonlinearity,
                                    use_spect, use_coord)
            setattr(self, 'encoder' + str(i), block)
        self.conv = SpectralNorm(nn.Conv2d(ndf * mult, 1, 1))
    def __init__(self,
                 input_nc=3,
                 input_length=6,
                 ndf=64,
                 img_f=1024,
                 layers=6,
                 norm='none',
                 activation='LeakyReLU',
                 use_spect=True,
                 use_coord=False):
        super(TemporalDiscriminator, self).__init__()

        self.layers = layers
        norm_layer = get_norm_layer(norm_type=norm)
        nonlinearity = get_nonlinearity_layer(activation_type=activation)
        self.nonlinearity = nonlinearity

        # self.pool = nn.AvgPool3d(kernel_size=(1,2,2), stride=(1,2,2))

        # encoder part
        self.block0 = ResBlock3DEncoder(input_nc, 1 * ndf, 1 * ndf, norm_layer,
                                        nonlinearity, use_spect, use_coord)
        self.block1 = ResBlock3DEncoder(1 * ndf, 2 * ndf, 1 * ndf, norm_layer,
                                        nonlinearity, use_spect, use_coord)

        feature_len = input_length - 4
        mult = 2 * feature_len
        for i in range(layers - 2):
            mult_prev = mult
            mult = min(2**(i + 2), img_f // ndf)
            block = ResBlockEncoder(ndf * mult_prev, ndf * mult,
                                    ndf * mult_prev, norm_layer, nonlinearity,
                                    use_spect, use_coord)
            setattr(self, 'encoder' + str(i), block)
        self.conv = SpectralNorm(nn.Conv2d(ndf * mult, 1, 1))
Beispiel #6
0
 def __init__(
     self,
     name="weight",
     n_power_iterations=1,
     dim=1,
     eps=1e-12,
     mode="",
     strict=True,
 ):
     super().__init__(name, n_power_iterations, dim, eps)
     assert validate_mode(mode)
     self.alpha, self.mode = parse_mode(mode)
     self.strict = strict
     self.dense = SpectralNorm(name, n_power_iterations, dim, eps)
     self.conv = ConvSpectralNorm(name, n_power_iterations, None, eps)
Beispiel #7
0
def spectral_norm(module, use_spect=True):
    """use spectral normal layer to stable the training process"""
    if use_spect:
        return SpectralNorm(module)
    else:
        return module