Exemple #1
0
    def __init__(self, config):
        super(Generator, self).__init__()
        self.auxclas = config.auxclas
        self.coupled = config.coupled
        c_len = 0
        if config.auxclas:
            c_len = config.categories

        mult = 2**config.blocks
        layers = (Vector2FeatureMaps(config.z_len + c_len, config.g_dim * mult,
                                     config.g_first_layer),
                  Norm2d(config.g_dim * mult, config.g_norm), nn.ReLU())

        for it in range(config.blocks - 1, -1, -1):
            mult = 2**it
            layers += (nn.ConvTranspose2d(config.g_dim * mult * 2,
                                          config.g_dim * mult,
                                          4,
                                          stride=2,
                                          padding=1),
                       Norm2d(config.g_dim * mult, config.g_norm), nn.ReLU())

        self.main = nn.Sequential(*layers)

        self.last_a = self.get_last_layers(config)
        if config.coupled:
            self.last_b = self.get_last_layers(config)

        weight_init(self, config.weight_init)
Exemple #2
0
    def __init__(self, config):
        super(Discriminator, self).__init__()
        self.auxclas = config.auxclas

        clen = 0
        if self.auxclas:
            self.numcats = len(config.categories)

        self.conv0_a = nn.Conv2d(1, config.d_dim*2, kernel_size=5, stride=1, padding=0)
        self.conv0_b = nn.Conv2d(1, config.d_dim*2, kernel_size=5, stride=1, padding=0)
        #24x24
        self.pool0 = nn.MaxPool2d(kernel_size=2)
        #12x12
        self.conv1 = nn.Conv2d(config.d_dim*2, config.d_dim*5, kernel_size=5, stride=1, padding=0)
        #8x8
        self.pool1 = nn.MaxPool2d(kernel_size=2)
        #4x4
        self.conv2 = nn.Conv2d(config.d_dim*5, config.d_dim*50, kernel_size=4, stride=1, padding=0)
        #1x1
        self.prelu2 = nn.PReLU()
        self.conv3 = FeatureMaps2Vector(config.d_dim*50, 1, config.d_last_layer, kernel_size=1)
        if self.auxclas:
            self.conv3c = ()
            for it in range(self.numcats):
                self.conv3c += (FeatureMaps2Vector(config.d_dim*50, config.categories[it], config.d_last_layer, kernel_size=1),)

            self.init_dummy = nn.Sequential(*self.conv3c) #to apply weight initialization
        weight_init(self, config.weight_init)
Exemple #3
0
    def __init__(self, config):
        super(Generator, self).__init__()
        self.conditional = config.auxclas or config.conditional
        self.coupled = config.coupled
        c_len = 0
        if self.conditional:
            c_len = sum(config.categories)

        mult = 2**config.blocks
        first_fm_size = 4
        layers = (
            nn.Linear(config.z_len+c_len, config.g_dim*mult*first_fm_size*first_fm_size, bias=False),
            Norm1D(config.g_dim*mult*first_fm_size*first_fm_size, config.g_norm),
            Activation(config.g_act),
            Reshape(-1, config.g_dim*mult, first_fm_size, first_fm_size)
            )

        for it in range(config.blocks-1, -1, -1):
            mult = 2**it
            layers += (
                nn.ConvTranspose2d(config.g_dim*mult*2, config.g_dim*mult, 5, 2,
                    padding=2, output_padding=1, bias=False),
                Norm2d(config.g_dim*mult, config.g_norm),
                Activation(config.g_act)
                )

        self.main = nn.Sequential(*layers)

        self.last_a = self.get_last_layers(config.g_dim, config.imgch)
        if config.coupled:
            self.last_b = self.get_last_layers(config.g_dim, config.imgch)

        weight_init(self, config.weight_init)
Exemple #4
0
    def __init__(self, config):
        super(Discriminator, self).__init__()
        self.auxclas = config.auxclas
        self.wasserstein = config.algorithm != 'wgan_gp'
        clen = 0
        if self.auxclas:
            self.numcats = len(config.categories)

        self.conv0_a = SpectralNorm(
            nn.Conv2d(1, config.d_dim * 2, kernel_size=5, stride=1, padding=0))
        self.conv0_b = SpectralNorm(
            nn.Conv2d(1, config.d_dim * 2, kernel_size=5, stride=1, padding=0))
        #24x24
        self.pool0 = nn.MaxPool2d(kernel_size=2)
        #12x12
        self.conv1 = SpectralNorm(
            nn.Conv2d(config.d_dim * 2,
                      config.d_dim * 5,
                      kernel_size=5,
                      stride=1,
                      padding=0))
        #8x8
        self.pool1 = nn.MaxPool2d(kernel_size=2)
        #4x4
        self.conv2 = SpectralNorm(
            nn.Conv2d(config.d_dim * 5,
                      config.d_dim * 50,
                      kernel_size=4,
                      stride=1,
                      padding=0))
        #1x1
        self.prelu2 = nn.PReLU()
        self.conv3 = nn.Sequential(
            SpectralNorm(
                nn.Conv2d(config.d_dim * 50, 1, 1, stride=1, padding=0)),
            Reshape(-1, 1))
        if self.wasserstein:
            self.sigm = nn.Sigmoid()
        if self.auxclas:
            self.conv3c = ()
            for it in range(self.numcats):
                self.conv3c += (nn.Sequential(
                    SpectralNorm(
                        nn.Conv2d(config.d_dim * 50,
                                  config.categories[it],
                                  1,
                                  stride=1,
                                  padding=0)),
                    Reshape(-1, config.categories[it])), )

            self.init_dummy = nn.Sequential(
                *self.conv3c)  #to apply weight initialization
        weight_init(self, config.weight_init)
Exemple #5
0
    def __init__(self, config):
        super(Generator, self).__init__()
        self.auxclas = config.auxclas
        self.coupled = config.coupled
        c_len = 0
        if config.auxclas:
            c_len = sum(config.categories)

        self.dconv0 = Vector2FeatureMaps(config.z_len + c_len,
                                         config.g_dim * 8,
                                         mode=config.g_first_layer)
        self.bn0 = nn.BatchNorm2d(config.g_dim * 8, affine=False)
        self.prelu0 = nn.PReLU()
        self.dconv1 = nn.ConvTranspose2d(config.g_dim * 8,
                                         config.g_dim * 4,
                                         kernel_size=3,
                                         stride=2,
                                         padding=1)
        self.bn1 = nn.BatchNorm2d(config.g_dim * 4, affine=False)
        self.prelu1 = nn.PReLU()
        self.dconv2 = nn.ConvTranspose2d(config.g_dim * 4,
                                         config.g_dim * 2,
                                         kernel_size=3,
                                         stride=2,
                                         padding=1)
        self.bn2 = nn.BatchNorm2d(config.g_dim * 2, affine=False)
        self.prelu2 = nn.PReLU()
        self.dconv3 = nn.ConvTranspose2d(config.g_dim * 2,
                                         config.g_dim,
                                         kernel_size=3,
                                         stride=2,
                                         padding=1)
        self.bn3 = nn.BatchNorm2d(config.g_dim, affine=False)
        self.prelu3 = nn.PReLU()
        self.dconv4_a = nn.ConvTranspose2d(config.g_dim,
                                           1,
                                           kernel_size=6,
                                           stride=1,
                                           padding=1)
        if self.coupled:
            self.dconv4_b = nn.ConvTranspose2d(config.g_dim,
                                               1,
                                               kernel_size=6,
                                               stride=1,
                                               padding=1)
        self.sig4 = nn.Tanh()

        weight_init(self, config.weight_init)
Exemple #6
0
    def __init__(self, config):
        super(Discriminator, self).__init__()
        self.auxclas = config.auxclas
        clen = 0
        if self.auxclas:
            clen = sum(config.categories)
            self.numcats = len(config.categories)

        self.first_a = self.first_layers(config)

        if config.coupled:
            self.first_b = self.first_layers(config)

        layers = ()
        for it in range(config.blocks):
            mult = 2**it
            layers += (SpectralNorm(
                nn.Conv2d(config.d_dim * mult,
                          config.d_dim * mult * 2,
                          5,
                          stride=2,
                          padding=2,
                          bias=False)),
                       Norm2d(config.d_dim * mult * 2,
                              config.d_norm), Activation(config.d_act))

        self.main = nn.Sequential(*layers)

        mult = 2**config.blocks
        prd_src_layers = (
            SpectralNorm(nn.Conv2d(config.d_dim * mult, 1, 4)),
            Reshape(-1, 1),
        )
        if config.algorithm != 'wgan_gp':
            prd_src_layers += (nn.Sigmoid(), )
        self.predict_src = nn.Sequential(*prd_src_layers)

        if self.auxclas:
            self.predict_class = []
            for it in range(self.numcats):
                self.predict_class += nn.Sequential(
                    SpectralNorm(
                        nn.Conv2d(config.d_dim * mult, config.categories[it],
                                  4)), Reshape(-1, config.categories[it]))
            self.init_dummy = nn.Sequential(
                *self.predict_class)  #to apply weight initialization

        weight_init(self, config.weight_init)
Exemple #7
0
    def __init__(self, config):
        super(Discriminator, self).__init__() 
        self.auxclas = config.auxclas

        self.first_a = nn.Sequential(
            nn.Conv2d(config.imgch, config.d_dim, 4, stride=2, padding=1),
            Norm2d(config.d_dim, config.d_norm),
            nn.LeakyReLU(0.2)
        )
        
        if config.coupled:
            self.first_b = nn.Sequential(
                nn.Conv2d(config.imgch, config.d_dim, 4, stride=2, padding=1),
                Norm2d(config.d_dim, config.d_norm),
                nn.LeakyReLU(0.2)
            )
        
        layers = ()
        for it in range(config.blocks):
            mult = 2**it
            layers += (
                nn.Conv2d(config.d_dim*mult,  config.d_dim*mult*2, 4, stride=2, padding=1),
                Norm2d(config.d_dim*mult*2, config.d_norm),
                nn.LeakyReLU(0.2)
                )

        self.main = nn.Sequential(*layers)

        mult = 2**config.blocks
        self.predict_src = nn.Sequential(
            FeatureMaps2Vector(config.d_dim*mult, 1, config.d_last_layer),
            nn.Sigmoid()
        )

        if self.auxclas:
            self.predict_class = nn.Sequential(
                FeatureMaps2Vector(config.d_dim*mult, config.categories, config.d_last_layer),
                )

        weight_init(self, config.weight_init)