Exemplo n.º 1
0
 def __init__(self,
              input_nc,
              ndf=64,
              n_blocks=3,
              norm_layer=nn.BatchNorm2d,
              use_sigmoid=False):
     super(Discriminator_p, self).__init__()
     sequence = []
     sequence += nn.Sequential(*[
         spectral_norm(nn.Conv2d(input_nc, ndf, 4, 2, 1, bias=False)),
         nn.LeakyReLU(0.2, inplace=True)
     ])
     n_mult = 1
     for _ in range(n_blocks):
         sequence += nn.Sequential(*[
             spectral_norm(
                 nn.Conv2d(
                     ndf * n_mult, ndf * n_mult * 2, 4, 2, 1, bias=False)),
             norm_layer(ndf * n_mult * 2),
             nn.LeakyReLU(0.2, inplace=True)
         ])
         n_mult *= 2
         # sequence += [ResBlock(ndf * n_mult)]
     sequence += [nn.Conv2d(ndf * n_mult, 1, 4, 1, 1, bias=False)]
     if use_sigmoid:
         sequence += [nn.Sigmoid()]
     self.model = nn.ModuleList(sequence)
Exemplo n.º 2
0
Arquivo: model.py Projeto: mingyr/san
    def _build(self, inputs):
        outputs = tf.matmul(inputs, spectral_norm(self._d_w1))
        outputs = outputs + self._d_b1
        outputs = self._act(outputs)

        outputs = tf.matmul(outputs, spectral_norm(self._d_w2))
        outputs = outputs + self._d_b2
        outputs = self._act(outputs)

        outputs = tf.matmul(outputs, spectral_norm(self._d_w3))
        outputs = outputs + self._d_b3

        return outputs
Exemplo n.º 3
0
    def __init__(self, input_nc, n_layer=3, norm_layer=nn.BatchNorm2d, ngf=64):
        super(pix2pixStyle, self).__init__()
        self.input_nc = input_nc
        self.ngf = ngf

        encoder = [
            nn.ReflectionPad2d(3),
            spectral_norm(
                nn.Conv2d(input_nc, ngf, kernel_size=7, stride=1, padding=0)),
            nn.ReLU(True),
            spectral_norm(nn.Conv2d(ngf, ngf, 3, 2, 1, bias=False)),
            norm_layer(ngf),
            nn.ReLU(True),
        ]
        n_mult = 1
        for _ in range(n_layer):
            ngf_in = min(ngf * n_mult, ngf * 4)
            ngf_out = min(ngf * (n_mult * 2), ngf * 4)
            encoder += [
                spectral_norm(nn.Conv2d(ngf_in, ngf_out, 3, 2, 1, bias=False)),
                norm_layer(ngf_out),
                nn.ReLU(True)
            ]
            n_mult = n_mult * 2
        self.encoder = nn.Sequential(*encoder)

        style_encoder = [
            spectral_norm(nn.Conv2d(21, ngf, 3, 1, 1, bias=False)),
            norm_layer(ngf),
            nn.ReLU(True)
        ]
        n_mult = 1
        for _ in range(n_layer):
            ngf_in = min(ngf * n_mult, ngf * 4)
            ngf_out = min(ngf * (n_mult * 2), ngf * 4)
            style_encoder += [
                Dense(ngf_in, ngf_in),
                spectral_norm(nn.Conv2d(ngf_in, ngf_out, 3, 2, 1, bias=False)),
                norm_layer(ngf_out),
                nn.ReLU(True)
            ]
            n_mult = n_mult * 2
        self.style_encoder = nn.Sequential(*style_encoder)

        resnet = []
        n_blocks = 6
        for _ in range(n_blocks):
            resnet += [StyleBlock(4 * ngf)]
        self.resnet = nn.ModuleList(resnet)

        decoder = [
            nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True),
            spectral_norm(nn.Conv2d(ngf * 4, ngf * 4, 3, 1, 1, bias=False)),
            norm_layer(ngf * 4),
            nn.ReLU(True)
        ]

        decoder += [
            nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True),
            spectral_norm(nn.Conv2d(ngf * 4, ngf * 4, 3, 1, 1, bias=False)),
            norm_layer(ngf * 4),
            nn.ReLU(True)
        ]
        n_mult = 4
        for _ in range(n_layer - 1):
            decoder += [
                nn.Upsample(scale_factor=2,
                            mode='bilinear',
                            align_corners=True),
                spectral_norm(
                    nn.Conv2d(ngf * n_mult,
                              ngf * (n_mult // 2),
                              3,
                              1,
                              1,
                              bias=False)),
                norm_layer(ngf * (n_mult // 2)),
                nn.ReLU(True)
            ]
            n_mult = n_mult // 2

        decoder += [
            nn.ReflectionPad2d(3),
            nn.Conv2d(ngf, 3, kernel_size=7, stride=1, padding=0),
            nn.Tanh()
        ]
        self.decoder = nn.Sequential(*decoder)
Exemplo n.º 4
0
    def __init__(self, input_nc, ndf=64, n_layers=5):
        super(Discriminator, self).__init__()
        model = [
            ReflectionPad2D(1),
            spectral_norm(
                Conv2D(input_nc,
                       ndf,
                       filter_size=4,
                       stride=2,
                       padding=0,
                       bias_attr=fluid.ParamAttr(
                           initializer=fluid.initializer.Uniform(
                               low=-1 / math.sqrt(input_nc * 16),
                               high=1 / math.sqrt(input_nc * 16))))),
            LeakyReLU(0.2, False)
        ]

        for i in range(1, n_layers - 2):
            mult = 2**(i - 1)
            model += [
                ReflectionPad2D(1),
                spectral_norm(
                    Conv2D(ndf * mult,
                           ndf * mult * 2,
                           filter_size=4,
                           stride=2,
                           padding=0,
                           bias_attr=fluid.ParamAttr(
                               initializer=fluid.initializer.Uniform(
                                   low=-1 / math.sqrt(ndf * mult * 16),
                                   high=1 / math.sqrt(ndf * mult * 16))))),
                LeakyReLU(0.2, False)
            ]

        mult = 2**(n_layers - 2 - 1)
        model += [
            ReflectionPad2D(1),
            spectral_norm(
                Conv2D(ndf * mult,
                       ndf * mult * 2,
                       filter_size=4,
                       stride=1,
                       padding=0,
                       bias_attr=fluid.ParamAttr(
                           initializer=fluid.initializer.Uniform(
                               low=-1 / math.sqrt(ndf * mult * 16),
                               high=1 / math.sqrt(ndf * mult * 16))))),
            LeakyReLU(0.2, False)
        ]

        # Class Activation Map
        mult = 2**(n_layers - 2)
        self.gap_fc = spectral_norm(Linear(ndf * mult, 1, bias_attr=False))
        self.gmp_fc = spectral_norm(Linear(ndf * mult, 1, bias_attr=False))
        self.conv1x1 = Conv2D(
            ndf * mult * 2,
            ndf * mult,
            filter_size=1,
            stride=1,
            bias_attr=fluid.ParamAttr(initializer=fluid.initializer.Uniform(
                low=-1 / math.sqrt(ndf * mult * 2),
                high=1 / math.sqrt(ndf * mult * 2))))
        self.leaky_relu = LeakyReLU(0.2, False)

        self.pad = ReflectionPad2D(1)
        self.conv = spectral_norm(
            Conv2D(ndf * mult,
                   1,
                   filter_size=4,
                   stride=1,
                   padding=0,
                   bias_attr=False))

        self.model = Sequential(*model)