nlayers.py 4.9 KB
Newer Older
L
lijianshe02 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

L
lijianshe02 已提交
15
import paddle
L
LielinJiang 已提交
16 17
import functools
import numpy as np
L
fix nan  
LielinJiang 已提交
18
import paddle.nn as nn
L
lijianshe02 已提交
19 20 21
import paddle.nn.functional as F

from ...modules.nn import Conv2d, Spectralnorm
L
LielinJiang 已提交
22 23 24 25 26 27
from ...modules.norm import build_norm_layer

from .builder import DISCRIMINATORS


@DISCRIMINATORS.register()
28
class NLayerDiscriminator(nn.Layer):
L
LielinJiang 已提交
29 30 31 32
    """Defines a PatchGAN discriminator"""
    def __init__(self, input_nc, ndf=64, n_layers=3, norm_type='instance'):
        """Construct a PatchGAN discriminator

L
lijianshe02 已提交
33
        Parameters:
L
LielinJiang 已提交
34 35 36
            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
37
            norm_type (str)      -- normalization layer type
L
LielinJiang 已提交
38 39 40
        """
        super(NLayerDiscriminator, self).__init__()
        norm_layer = build_norm_layer(norm_type)
L
lijianshe02 已提交
41 42 43 44
        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 已提交
45
        else:
L
lijianshe02 已提交
46
            use_bias = norm_layer == nn.InstanceNorm2d
L
fix nan  
LielinJiang 已提交
47

L
LielinJiang 已提交
48 49
        kw = 4
        padw = 1
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

        if norm_type == 'spectral':
            sequence = [
                Spectralnorm(
                    Conv2d(input_nc,
                           ndf,
                           kernel_size=kw,
                           stride=2,
                           padding=padw)),
                nn.LeakyReLU(0.01)
            ]
        else:
            sequence = [
                Conv2d(input_nc,
                       ndf,
                       kernel_size=kw,
                       stride=2,
                       padding=padw,
                       bias_attr=use_bias),
                nn.LeakyReLU(0.2)
            ]
L
LielinJiang 已提交
71 72
        nf_mult = 1
        nf_mult_prev = 1
L
lijianshe02 已提交
73
        for n in range(1, n_layers):  # gradually increase the number of filters
L
LielinJiang 已提交
74
            nf_mult_prev = nf_mult
L
fix nan  
LielinJiang 已提交
75
            nf_mult = min(2**n, 8)
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
            if norm_type == 'spectral':
                sequence += [
                    Spectralnorm(
                        Conv2d(ndf * nf_mult_prev,
                               ndf * nf_mult,
                               kernel_size=kw,
                               stride=2,
                               padding=padw)),
                    nn.LeakyReLU(0.01)
                ]
            else:
                sequence += [
                    Conv2d(ndf * nf_mult_prev,
                           ndf * nf_mult,
                           kernel_size=kw,
                           stride=2,
                           padding=padw,
                           bias_attr=use_bias),
                    norm_layer(ndf * nf_mult),
                    nn.LeakyReLU(0.2)
                ]

        nf_mult_prev = nf_mult
        nf_mult = min(2**n_layers, 8)
        if norm_type == 'spectral':
L
LielinJiang 已提交
101
            sequence += [
L
lijianshe02 已提交
102 103 104 105
                Spectralnorm(
                    Conv2d(ndf * nf_mult_prev,
                           ndf * nf_mult,
                           kernel_size=kw,
106
                           stride=1,
L
lijianshe02 已提交
107 108
                           padding=padw)),
                nn.LeakyReLU(0.01)
L
LielinJiang 已提交
109
            ]
110 111
        else:
            sequence += [
L
lijianshe02 已提交
112 113 114 115
                Conv2d(ndf * nf_mult_prev,
                       ndf * nf_mult,
                       kernel_size=kw,
                       stride=1,
116 117 118 119 120
                       padding=padw,
                       bias_attr=use_bias),
                norm_layer(ndf * nf_mult),
                nn.LeakyReLU(0.2)
            ]
L
LielinJiang 已提交
121

122 123 124 125 126 127 128 129 130 131 132 133
        if norm_type == 'spectral':
            sequence += [
                Spectralnorm(
                    Conv2d(ndf * nf_mult,
                           1,
                           kernel_size=kw,
                           stride=1,
                           padding=padw,
                           bias_attr=False))
            ]  # output 1 channel prediction map
        else:
            sequence += [
L
lijianshe02 已提交
134 135 136 137 138
                Conv2d(ndf * nf_mult,
                       1,
                       kernel_size=kw,
                       stride=1,
                       padding=padw,
139 140 141
                       bias_attr=False)
            ]  # output 1 channel prediction map

L
LielinJiang 已提交
142 143 144 145
        self.model = nn.Sequential(*sequence)

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