Exemplo n.º 1
0
    def __init__(self, in_channels, out_channels, stride=1):
        super(ResBlockDiscriminator_SN, self).__init__()

        self.conv1 = nn.Conv2d(in_channels, out_channels, 3, 1, padding=1)
        self.conv2 = nn.Conv2d(out_channels, out_channels, 3, 1, padding=1)
        nn.init.xavier_uniform_(self.conv1.weight.data, 1.)
        nn.init.xavier_uniform_(self.conv2.weight.data, 1.)

        if stride == 1:
            self.model = nn.Sequential(nn.ReLU(), SpectralNorm(self.conv1),
                                       nn.ReLU(), SpectralNorm(self.conv2))
        else:
            self.model = nn.Sequential(
                nn.ReLU(), SpectralNorm(self.conv1), nn.ReLU(),
                SpectralNorm(self.conv2),
                nn.AvgPool2d(2, stride=stride, padding=0))

        self.bypass = nn.Sequential()
        if stride != 1:

            self.bypass_conv = nn.Conv2d(in_channels,
                                         out_channels,
                                         1,
                                         1,
                                         padding=0)
            nn.init.xavier_uniform_(self.bypass_conv.weight.data, np.sqrt(2))

            self.bypass = nn.Sequential(
                SpectralNorm(self.bypass_conv),
                nn.AvgPool2d(2, stride=stride, padding=0))
Exemplo n.º 2
0
    def __init__(self, ndf, img_shape, n_gpu):
        super(Discriminator_MLP_SN, self).__init__()
        self.gpu = n_gpu
        self.img_shape = img_shape

        self.model = nn.Sequential(
            SpectralNorm(nn.Linear(int(np.prod(img_shape)), ndf * 2**4)),
            nn.LeakyReLU(0.2, inplace=True),
            SpectralNorm(nn.Linear(ndf * 2**4, ndf * 2**3)),
            nn.LeakyReLU(0.2, inplace=True),
            SpectralNorm(nn.Linear(ndf * 2**3, 1)),  # return a scalar score
        )
Exemplo n.º 3
0
    def __init__(self, in_channels, out_channels, stride=1):
        super(FirstResBlockDiscriminator_SN, self).__init__()

        self.conv1 = nn.Conv2d(in_channels, out_channels, 3, 1, padding=1)
        self.conv2 = nn.Conv2d(out_channels, out_channels, 3, 1, padding=1)
        self.bypass_conv = nn.Conv2d(in_channels,
                                     out_channels,
                                     1,
                                     1,
                                     padding=0)
        nn.init.xavier_uniform_(self.conv1.weight.data, 1.)
        nn.init.xavier_uniform_(self.conv2.weight.data, 1.)
        nn.init.xavier_uniform_(self.bypass_conv.weight.data, np.sqrt(2))

        # we don't want to apply ReLU activation to raw image before convolution transformation.
        self.model = nn.Sequential(SpectralNorm(self.conv1), nn.ReLU(),
                                   SpectralNorm(self.conv2), nn.AvgPool2d(2))
        self.bypass = nn.Sequential(
            nn.AvgPool2d(2),
            SpectralNorm(self.bypass_conv),
        )
Exemplo n.º 4
0
        def discriminator_block(in_filters, out_filters, kernel_size, stride,
                                padding):
            # w_out = floor( (w_in - kernel_size + 2 * padding) / stride + 1 )
            # kernel_size=4, stride=2, padding=1 -> shrink to half of the size
            # kernel_size=3, stride=1, padding=1 -> stay at the same size
            # kernel_size=img_size, stride=1, padding=0 -> reduce to scalar 1, used for the last layer of discriminator

            block = [
                SpectralNorm(
                    nn.Conv2d(in_filters,
                              out_filters,
                              kernel_size=kernel_size,
                              stride=stride,
                              padding=padding,
                              bias=False)),
                nn.LeakyReLU(0.2, inplace=True)
            ]
            return block
Exemplo n.º 5
0
    def __init__(self, nc, ndf, n_gpu):
        super(Discriminator_Res_SN, self).__init__()
        self.n_gpu = n_gpu
        self.out_channels = ndf * 2**2

        self.model = nn.Sequential(
            FirstResBlockDiscriminator_SN(nc, self.out_channels, stride=2),
            # state size. out_channels x 16 x 16
            ResBlockDiscriminator_SN(self.out_channels,
                                     self.out_channels,
                                     stride=2),
            # state size. out_channels x 8 x 8
            ResBlockDiscriminator_SN(self.out_channels, self.out_channels),
            # state size. out_channels x 8 x 8
            ResBlockDiscriminator_SN(self.out_channels, self.out_channels),
            # state size. out_channels x 8 x 8
            nn.ReLU(),
            nn.AvgPool2d(8),
            # state size. out_channels x 1 x 1
        )
        self.fc = nn.Linear(self.out_channels, 1)
        nn.init.xavier_uniform_(self.fc.weight.data, 1.)
        self.fc = SpectralNorm(self.fc)
Exemplo n.º 6
0
    def __init__(self, nc, ndf, n_gpu):
        super(Discriminator_SN, self).__init__()
        self.n_gpu = n_gpu

        def discriminator_block(in_filters, out_filters, kernel_size, stride,
                                padding):
            # w_out = floor( (w_in - kernel_size + 2 * padding) / stride + 1 )
            # kernel_size=4, stride=2, padding=1 -> shrink to half of the size
            # kernel_size=3, stride=1, padding=1 -> stay at the same size
            # kernel_size=img_size, stride=1, padding=0 -> reduce to scalar 1, used for the last layer of discriminator

            block = [
                SpectralNorm(
                    nn.Conv2d(in_filters,
                              out_filters,
                              kernel_size=kernel_size,
                              stride=stride,
                              padding=padding,
                              bias=False)),
                nn.LeakyReLU(0.2, inplace=True)
            ]
            return block

        # shrink 2**3=8 times, floor(img_size/8) = 23/8 = 2
        # shrink 2**4=16 times, floor(img_size/16) = 32/16 = 2
        self.model = nn.Sequential(
            # input state size. nc x img_size x img_size
            *discriminator_block(nc, ndf, kernel_size=4, stride=2, padding=1),
            # state size. ndf x (img_size/2) x (img_size/2)
            *discriminator_block(ndf,
                                 ndf * 2,
                                 kernel_size=4,
                                 stride=2,
                                 padding=1),
            # state size. (ndf*2) x (img_size/2**2) x (img_size/2**2)
            *discriminator_block(ndf * 2,
                                 ndf * 4,
                                 kernel_size=4,
                                 stride=2,
                                 padding=1),
            # state size. (ndf*4) x (img_size/2**3) x (img_size/2**3)
            *discriminator_block(ndf * 4,
                                 ndf * 8,
                                 kernel_size=4,
                                 stride=2,
                                 padding=1),
            # state size. (ndf*8) x (img_size/2**4) x (img_size/2**4)
        )
        # state size. (ndf*4) x 2 x 2

        # the last layer of discriminator should return a scalar value, but the input features depends on the previous conv layers,
        # use print to decide the dimensions, it varies from input to input
        self.last_layer = nn.Sequential(
            SpectralNorm(nn.Linear((ndf * 8) * 2 * 2, 1)),
            # state size. 1 x 1 x 1

            # originally, DCGAN output the value in (0, 1)
            # but if we choose loss function during training as BCEWithLogitsLoss, we do not need to define sigmoid() here
            # To better compare with other GANs, we do not use sigmoid() in D
            # nn.Sigmoid()
        )