Пример #1
0
    def __init__(self, channel, kernel, stride, padding):
        super(resnet_block, self).__init__()
        self.channel = channel
        self.kernel = kernel
        self.strdie = stride
        self.padding = padding
        self.conv1 = nn.Conv2d(channel, channel, kernel, stride, padding)
        self.conv1_norm = nn.InstanceNorm2d(channel)
        self.conv2 = nn.Conv2d(channel, channel, kernel, stride, padding)
        self.conv2_norm = nn.InstanceNorm2d(channel)

        utils.initialize_weights(self)
Пример #2
0
    def __init__(self, in_nc, out_nc, nf=32, use_sigmoid=False):
        super(discriminator, self).__init__()
        self.input_nc = in_nc
        self.output_nc = out_nc
        self.nf = nf
        layers = [
            nn.Conv2d(in_nc, nf, 3, 1, 1),
            nn.LeakyReLU(0.2, True),
            nn.Conv2d(nf, nf * 2, 3, 2, 1),
            nn.LeakyReLU(0.2, True),
            nn.Conv2d(nf * 2, nf * 4, 3, 1, 1),
            nn.InstanceNorm2d(nf * 4),
            nn.LeakyReLU(0.2, True),
            nn.Conv2d(nf * 4, nf * 4, 3, 2, 1),
            nn.LeakyReLU(0.2, True),
            nn.Conv2d(nf * 4, nf * 8, 3, 1, 1),
            nn.InstanceNorm2d(nf * 8),
            nn.LeakyReLU(0.2, True),
            nn.Conv2d(nf * 8, nf * 8, 3, 2, 1),
            nn.InstanceNorm2d(nf * 8),
            nn.LeakyReLU(0.2, True),
            nn.Conv2d(nf * 8, out_nc, 3, 1, 1)
        ]
        if use_sigmoid:
            layers.append(nn.Sigmoid())

        self.convs = nn.Sequential(*layers)
        #         self.convs = nn.Sequential(
        #             nn.Conv2d(in_nc, nf, 3, 1, 1),
        #             nn.LeakyReLU(0.2, True),
        #             nn.Conv2d(nf, nf * 2, 3, 2, 1),
        #             nn.LeakyReLU(0.2, True),
        #             nn.Conv2d(nf * 2, nf * 4, 3, 1, 1),
        #             nn.InstanceNorm2d(nf * 4),
        #             nn.LeakyReLU(0.2, True),
        #             nn.Conv2d(nf * 4, nf * 4, 3, 2, 1),
        #             nn.LeakyReLU(0.2, True),
        #             nn.Conv2d(nf * 4, nf * 8, 3, 1, 1),
        #             nn.InstanceNorm2d(nf * 8),
        #             nn.LeakyReLU(0.2, True),
        #             nn.Conv2d(nf * 8, nf * 8, 3, 2, 1),
        #             nn.InstanceNorm2d(nf * 8),
        #             nn.LeakyReLU(0.2, True),
        #             nn.Conv2d(nf * 8, out_nc, 3, 1, 1)
        # #             nn.Sigmoid(),
        #         )

        utils.initialize_weights(self)
Пример #3
0
    def __init__(self, in_nc, out_nc, nf=32, nb=6):
        super(generator, self).__init__()
        self.input_nc = in_nc
        self.output_nc = out_nc
        self.nf = nf
        self.nb = nb
        self.down_convs = nn.Sequential(
            #nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
            nn.Conv2d(in_nc, nf, 7, 1, 3),
            nn.InstanceNorm2d(nf),
            nn.ReLU(True),
            nn.Conv2d(nf, nf * 2, 3, 2, 1),
            nn.Conv2d(nf * 2, nf * 2, 3, 1, 1),
            nn.InstanceNorm2d(nf * 2),
            nn.ReLU(True),
            nn.Conv2d(nf * 2, nf * 4, 3, 2, 1),
            nn.Conv2d(nf * 4, nf * 4, 3, 1, 1),
            nn.InstanceNorm2d(nf * 4),
            nn.ReLU(True),
        )

        self.resnet_blocks = []
        for i in range(nb):
            self.resnet_blocks.append(resnet_block(nf * 4, 3, 1, 1))

        self.resnet_blocks = nn.Sequential(*self.resnet_blocks)

        self.up_convs = nn.Sequential(
            nn.ConvTranspose2d(nf * 4, nf * 2, 4, 2, 1),
            nn.Conv2d(nf * 2, nf * 2, 3, 1, 1),
            nn.InstanceNorm2d(nf * 2),
            nn.ReLU(True),
            nn.ConvTranspose2d(nf * 2, nf, 4, 2, 1),
            nn.Conv2d(nf, nf, 3, 1, 1),
            nn.InstanceNorm2d(nf),
            nn.ReLU(True),
            nn.Conv2d(nf, out_nc, 7, 1, 3),
            nn.Tanh(),
        )

        utils.initialize_weights(self)
Пример #4
0
    def __init__(self,
                 in_nc,
                 out_nc,
                 nf=16,
                 nb=2,
                 n_downsampling=5,
                 addtional_conv=False):
        super(cyclegan_generator1, self).__init__()
        self.input_nc = in_nc
        self.output_nc = out_nc
        self.nf = nf
        self.nb = nb
        self.mult = 1
        model_conv = [
            nn.Conv2d(in_nc, nf, 7, 1, 3),
            nn.InstanceNorm2d(nf),
            nn.ReLU(True)
        ]
        for i in range(n_downsampling):
            if addtional_conv:
                model_conv += [
                    nn.Conv2d(nf * self.mult, nf * self.mult * 2, 3, 2, 1),
                    nn.Conv2d(nf * self.mult * 2, nf * self.mult * 2, 3, 1, 1),
                    nn.InstanceNorm2d(nf * self.mult * 2),
                    nn.ReLU(True)
                ]
            else:
                model_conv += [
                    nn.Conv2d(nf * self.mult, nf * self.mult * 2, 3, 2, 1),
                    nn.InstanceNorm2d(nf * self.mult * 2),
                    nn.ReLU(True)
                ]
            self.mult *= 2
        self.down_convs = nn.Sequential(*model_conv)

        self.resnet_blocks = []
        for i in range(nb):
            self.resnet_blocks.append(resnet_block(nf * self.mult, 3, 1, 1))
        self.resnet_blocks = nn.Sequential(*self.resnet_blocks)

        model_deconv = []
        for i in range(n_downsampling):
            if addtional_conv:
                model_deconv += [
                    nn.ConvTranspose2d(nf * self.mult, nf * self.mult // 2, 4,
                                       2, 1),
                    nn.Conv2d(nf * self.mult // 2, nf * self.mult // 2, 3, 1,
                              1),
                    nn.InstanceNorm2d(nf * self.mult // 2),
                    nn.ReLU(True)
                ]
            else:
                model_deconv += [
                    nn.ConvTranspose2d(nf * self.mult, nf * self.mult // 2, 4,
                                       2, 1),
                    nn.InstanceNorm2d(nf * self.mult // 2),
                    nn.ReLU(True)
                ]
            self.mult = self.mult // 2
        model_deconv += [nn.Conv2d(nf, out_nc, 7, 1, 3), nn.Tanh()]

        self.up_convs = nn.Sequential(*model_deconv)

        utils.initialize_weights(self)
Пример #5
0
 def __init__(self, in_nc, out_nc, nf=16, fcn=1024):
     super(xgan_generator, self).__init__()
     #input 128*128*3
     self.conv_s2t = nn.Sequential(
         nn.Conv2d(in_nc, nf, 3, 2, 1),
         nn.InstanceNorm2d(nf),
         nn.ReLU(True),  #64*64*nf
         nn.Conv2d(nf, 2 * nf, 3, 2, 1),
         nn.InstanceNorm2d(2 * nf),
         nn.ReLU(True),  #32*32*2nf
         nn.Conv2d(2 * nf, 4 * nf, 3, 2, 1),
         nn.InstanceNorm2d(4 * nf),
         nn.ReLU(True)  #16*16*4nf
     )
     self.conv_t2s = nn.Sequential(
         nn.Conv2d(in_nc, nf, 3, 2, 1),
         nn.InstanceNorm2d(nf),
         nn.ReLU(True),  #64*64*nf
         nn.Conv2d(nf, 2 * nf, 3, 2, 1),
         nn.InstanceNorm2d(2 * nf),
         nn.ReLU(True),  #32*32*2nf
         nn.Conv2d(2 * nf, 4 * nf, 3, 2, 1),
         nn.InstanceNorm2d(4 * nf),
         nn.ReLU(True)  #16*16*4nf
     )
     self.conv_sharing = nn.Sequential(
         nn.Conv2d(4 * nf, 8 * nf, 3, 2, 1),
         nn.InstanceNorm2d(8 * nf),
         nn.ReLU(True),  #8*8*8nf
         nn.Conv2d(8 * nf, 16 * nf, 3, 2, 1),
         nn.InstanceNorm2d(16 * nf),
         nn.ReLU(True)  #4*4*16nf               
     )
     self.fc_sharing = nn.Sequential(
         nn.Linear(4 * 4 * 16 * nf, 1 * 1 * fcn),
         nn.Linear(1 * 1 * fcn, 1 * 1 * fcn))
     self.deconv_sharing = nn.Sequential(
         nn.Upsample(scale_factor=4),
         nn.Conv2d(fcn, 32 * nf, 3, 1, 1),
         nn.InstanceNorm2d(32 * nf),
         nn.ReLU(True),  #4*4*32nf
         nn.ConvTranspose2d(32 * nf, 16 * nf, 4, 2, 1),
         nn.InstanceNorm2d(16 * nf),
         nn.ReLU(True)  #8*8*16nf                
     )
     self.deconv_s2t = nn.Sequential(
         nn.ConvTranspose2d(16 * nf, 8 * nf, 4, 2, 1),
         nn.InstanceNorm2d(8 * nf),
         nn.ReLU(True),  #16*16*8nf
         nn.ConvTranspose2d(8 * nf, 4 * nf, 4, 2, 1),
         nn.InstanceNorm2d(4 * nf),
         nn.ReLU(True),  #32*32*4nf   
         nn.ConvTranspose2d(4 * nf, 2 * nf, 4, 2, 1),
         nn.InstanceNorm2d(2 * nf),
         nn.ReLU(True),  #64*64*2nf   
         nn.ConvTranspose2d(2 * nf, nf, 4, 2, 1),  #128*128*nf
         nn.Conv2d(nf, out_nc, 3, 1, 1),  #128*128*out_nc
         nn.Tanh())
     self.deconv_t2s = nn.Sequential(
         nn.ConvTranspose2d(16 * nf, 8 * nf, 4, 2, 1),
         nn.InstanceNorm2d(8 * nf),
         nn.ReLU(True),  #16*16*8nf
         nn.ConvTranspose2d(8 * nf, 4 * nf, 4, 2, 1),
         nn.InstanceNorm2d(4 * nf),
         nn.ReLU(True),  #32*32*4nf   
         nn.ConvTranspose2d(4 * nf, 2 * nf, 4, 2, 1),
         nn.InstanceNorm2d(2 * nf),
         nn.ReLU(True),  #64*64*2nf   
         nn.ConvTranspose2d(2 * nf, nf, 4, 2, 1),  #128*128*nf
         nn.Conv2d(nf, out_nc, 3, 1, 1),  #128*128*out_nc
         nn.Tanh())
     utils.initialize_weights(self)
Пример #6
0
    def __init__(self,
                 in_nc,
                 out_nc,
                 nf=16,
                 nb=4,
                 coupled_layer=1,
                 decoupled_layer=4):
        super(xgan_generator2, self).__init__()
        self.mult = 1

        model_conv_s2t = [
            nn.Conv2d(in_nc, nf, 7, 1, 3),
            nn.InstanceNorm2d(nf),
            nn.ReLU(True)
        ]
        model_conv_t2s = [
            nn.Conv2d(in_nc, nf, 7, 1, 3),
            nn.InstanceNorm2d(nf),
            nn.ReLU(True)
        ]

        for i in range(decoupled_layer):
            model_conv_s2t += [
                nn.Conv2d(nf * self.mult, nf * self.mult * 2, 3, 2, 1),
                nn.InstanceNorm2d(nf * self.mult * 2),
                nn.ReLU(True)
            ]
            model_conv_t2s += [
                nn.Conv2d(nf * self.mult, nf * self.mult * 2, 3, 2, 1),
                nn.InstanceNorm2d(nf * self.mult * 2),
                nn.ReLU(True)
            ]
            self.mult *= 2

        self.conv_s2t = nn.Sequential(*model_conv_s2t)
        self.conv_t2s = nn.Sequential(*model_conv_t2s)

        model_conv_sharing = []
        for i in range(coupled_layer):
            model_conv_sharing += [
                nn.Conv2d(nf * self.mult, nf * self.mult * 2, 3, 2, 1),
                nn.InstanceNorm2d(nf * self.mult * 2),
                nn.ReLU(True)
            ]
            self.mult *= 2
        for i in range(nb // 2):
            model_conv_sharing += [resnet_block(nf * self.mult, 3, 1, 1)]
        self.conv_sharing = nn.Sequential(*model_conv_sharing)

        model_deconv_sharing = []
        for i in range(nb // 2):
            model_deconv_sharing += [resnet_block(nf * self.mult, 3, 1, 1)]
        for i in range(coupled_layer):
            model_deconv_sharing += [
                nn.ConvTranspose2d(nf * self.mult, nf * self.mult // 2, 4, 2,
                                   1),
                nn.InstanceNorm2d(nf * self.mult // 2),
                nn.ReLU(True)
            ]
            self.mult = self.mult // 2
        self.deconv_sharing = nn.Sequential(*model_deconv_sharing)

        model_deconv_s2t = []
        model_deconv_t2s = []

        for i in range(decoupled_layer):
            model_deconv_s2t += [
                nn.ConvTranspose2d(nf * self.mult, nf * self.mult // 2, 4, 2,
                                   1),
                nn.InstanceNorm2d(nf * self.mult // 2),
                nn.ReLU(True)
            ]
            model_deconv_t2s += [
                nn.ConvTranspose2d(nf * self.mult, nf * self.mult // 2, 4, 2,
                                   1),
                nn.InstanceNorm2d(nf * self.mult // 2),
                nn.ReLU(True)
            ]
            self.mult = self.mult // 2
        model_deconv_s2t += [nn.Conv2d(nf, out_nc, 7, 1, 3), nn.Tanh()]
        model_deconv_t2s += [nn.Conv2d(nf, out_nc, 7, 1, 3), nn.Tanh()]
        self.deconv_s2t = nn.Sequential(*model_deconv_s2t)
        self.deconv_t2s = nn.Sequential(*model_deconv_t2s)

        utils.initialize_weights(self)
Пример #7
0
    def __init__(self,
                 in_nc,
                 out_nc,
                 nf=8,
                 input_size=128,
                 coupled_layer=2,
                 decoupled_layer=4,
                 fc=1024):
        super(xgan_generator3, self).__init__()

        self.mult = 1
        model_conv_s2t = [
            nn.Conv2d(in_nc, nf, 7, 1, 3),
            nn.InstanceNorm2d(nf),
            nn.ReLU(True)
        ]
        model_conv_t2s = [
            nn.Conv2d(in_nc, nf, 7, 1, 3),
            nn.InstanceNorm2d(nf),
            nn.ReLU(True)
        ]
        for i in range(decoupled_layer):
            model_conv_s2t += [
                nn.Conv2d(nf * self.mult, nf * self.mult * 2, 3, 2, 1),
                nn.InstanceNorm2d(nf * self.mult * 2),
                nn.ReLU(True)
            ]
            model_conv_t2s += [
                nn.Conv2d(nf * self.mult, nf * self.mult * 2, 3, 2, 1),
                nn.InstanceNorm2d(nf * self.mult * 2),
                nn.ReLU(True)
            ]
            self.mult *= 2
        self.conv_s2t = nn.Sequential(*model_conv_s2t)
        self.conv_t2s = nn.Sequential(*model_conv_t2s)

        model_conv_sharing = []
        for i in range(coupled_layer - 1):
            model_conv_sharing += [
                nn.Conv2d(nf * self.mult, nf * self.mult * 2, 3, 2, 1),
                nn.InstanceNorm2d(nf * self.mult * 2),
                nn.ReLU(True)
            ]
            self.mult *= 2

        model_conv_sharing += [
            nn.Conv2d(nf * self.mult, nf * self.mult * 2, 3, 2, 1)
        ]
        self.mult *= 2
        self.conv_sharing = nn.Sequential(*model_conv_sharing)

        #         self.size_aft_conv = input_size // self.mult
        #         self.nf_aft_conv = nf * self.mult
        #         self.flattened_dim = self.size_aft_conv * self.size_aft_conv * self.nf_aft_conv

        #         self.fc_1 = nn.Sequential(
        #             nn.Linear(self.flattened_dim, fc),
        #             nn.Linear(fc, fc)
        #         )
        #         self.fc_2 = nn.Sequential(
        #             nn.Linear(fc, fc),
        #             nn.Linear(fc, self.flattened_dim)
        #         )

        model_deconv_sharing = []
        for i in range(coupled_layer):
            model_deconv_sharing += [
                nn.ConvTranspose2d(nf * self.mult, nf * self.mult // 2, 4, 2,
                                   1),
                nn.InstanceNorm2d(nf * self.mult // 2),
                nn.ReLU(True)
            ]
            self.mult = self.mult // 2
        self.deconv_sharing = nn.Sequential(*model_deconv_sharing)

        model_deconv_s2t = []
        model_deconv_t2s = []
        for i in range(decoupled_layer):
            model_deconv_s2t += [
                nn.ConvTranspose2d(nf * self.mult, nf * self.mult // 2, 4, 2,
                                   1),
                nn.InstanceNorm2d(nf * self.mult // 2),
                nn.ReLU(True)
            ]
            model_deconv_t2s += [
                nn.ConvTranspose2d(nf * self.mult, nf * self.mult // 2, 4, 2,
                                   1),
                nn.InstanceNorm2d(nf * self.mult // 2),
                nn.ReLU(True)
            ]
            self.mult = self.mult // 2
        model_deconv_s2t += [nn.Conv2d(nf, out_nc, 7, 1, 3), nn.Tanh()]
        model_deconv_t2s += [nn.Conv2d(nf, out_nc, 7, 1, 3), nn.Tanh()]
        self.deconv_s2t = nn.Sequential(*model_deconv_s2t)
        self.deconv_t2s = nn.Sequential(*model_deconv_t2s)

        utils.initialize_weights(self)