nlayers.py 3.1 KB
Newer Older
L
lijianshe02 已提交
1
import paddle
L
LielinJiang 已提交
2 3
import functools
import numpy as np
L
fix nan  
LielinJiang 已提交
4
import paddle.nn as nn
L
lijianshe02 已提交
5 6 7
import paddle.nn.functional as F

from ...modules.nn import Conv2d, Spectralnorm
L
LielinJiang 已提交
8 9 10 11 12 13
from ...modules.norm import build_norm_layer

from .builder import DISCRIMINATORS


@DISCRIMINATORS.register()
L
lijianshe02 已提交
14
class NLayerDiscriminator(paddle.nn.Layer):
L
LielinJiang 已提交
15 16 17 18
    """Defines a PatchGAN discriminator"""
    def __init__(self, input_nc, ndf=64, n_layers=3, norm_type='instance'):
        """Construct a PatchGAN discriminator

L
lijianshe02 已提交
19
        Parameters:
L
LielinJiang 已提交
20 21 22
            input_nc (int)  -- the number of channels in input images
            ndf (int)       -- the number of filters in the last conv layer
            n_layers (int)  -- the number of conv layers in the discriminator
L
lijianshe02 已提交
23
            norm_layer      -- normalization layer
L
LielinJiang 已提交
24 25 26
        """
        super(NLayerDiscriminator, self).__init__()
        norm_layer = build_norm_layer(norm_type)
L
lijianshe02 已提交
27 28 29 30
        if type(
                norm_layer
        ) == functools.partial:  # no need to use bias as BatchNorm2d has affine parameters
            use_bias = norm_layer.func == nn.InstanceNorm2d
L
LielinJiang 已提交
31
        else:
L
lijianshe02 已提交
32
            use_bias = norm_layer == nn.InstanceNorm2d
L
fix nan  
LielinJiang 已提交
33

L
LielinJiang 已提交
34 35
        kw = 4
        padw = 1
L
lijianshe02 已提交
36
        #sequence = [Conv2d(input_nc, ndf, kernel_size=kw, stride=2, padding=padw), nn.LeakyReLU(0.01)]
L
fix nan  
LielinJiang 已提交
37
        sequence = [
L
lijianshe02 已提交
38 39 40
            Spectralnorm(
                Conv2d(input_nc, ndf, kernel_size=kw, stride=2, padding=padw)),
            nn.LeakyReLU(0.01)
L
fix nan  
LielinJiang 已提交
41
        ]
L
LielinJiang 已提交
42 43
        nf_mult = 1
        nf_mult_prev = 1
L
lijianshe02 已提交
44
        for n in range(1, n_layers):  # gradually increase the number of filters
L
LielinJiang 已提交
45
            nf_mult_prev = nf_mult
L
fix nan  
LielinJiang 已提交
46
            nf_mult = min(2**n, 8)
L
LielinJiang 已提交
47
            sequence += [
L
lijianshe02 已提交
48 49 50 51 52 53 54 55 56
                #Conv2d(ndf * nf_mult_prev, ndf * nf_mult, kernel_size=kw, stride=2, padding=padw, bias_attr=use_bias),
                Spectralnorm(
                    Conv2d(ndf * nf_mult_prev,
                           ndf * nf_mult,
                           kernel_size=kw,
                           stride=2,
                           padding=padw)),
                #norm_layer(ndf * nf_mult),
                nn.LeakyReLU(0.01)
L
LielinJiang 已提交
57 58 59
            ]

        nf_mult_prev = nf_mult
L
fix nan  
LielinJiang 已提交
60
        nf_mult = min(2**n_layers, 8)
L
LielinJiang 已提交
61
        sequence += [
L
lijianshe02 已提交
62 63 64 65 66 67 68 69 70
            #Conv2d(ndf * nf_mult_prev, ndf * nf_mult, kernel_size=kw, stride=1, padding=padw, bias_attr=use_bias),
            Spectralnorm(
                Conv2d(ndf * nf_mult_prev,
                       ndf * nf_mult,
                       kernel_size=kw,
                       stride=1,
                       padding=padw)),
            #norm_layer(ndf * nf_mult),
            nn.LeakyReLU(0.01)
L
LielinJiang 已提交
71 72
        ]

L
lijianshe02 已提交
73
        #sequence += [Conv2d(ndf * nf_mult, 1, kernel_size=kw, stride=1, padding=padw)]  # output 1 channel prediction map
L
fix nan  
LielinJiang 已提交
74
        sequence += [
L
lijianshe02 已提交
75 76 77 78 79 80 81 82
            Spectralnorm(
                Conv2d(ndf * nf_mult,
                       1,
                       kernel_size=kw,
                       stride=1,
                       padding=padw,
                       bias_attr=False))
        ]  # output 1 channel prediction map
L
LielinJiang 已提交
83 84 85 86
        self.model = nn.Sequential(*sequence)

    def forward(self, input):
        """Standard forward."""
L
fix nan  
LielinJiang 已提交
87
        return self.model(input)