def __init__(self, in_channels, out_channels, activation=nn.ReLU(inplace=False), upsample=functools.partial(F.interpolate, scale_factor=2)): super(GBlock, self).__init__() self.in_channels, self.out_channels = in_channels, out_channels self.activation = activation self.upsample = upsample # Conv layers self.conv1 = SpectralNorm( nn.Conv2d(self.in_channels, self.out_channels, kernel_size=3, padding=1)) self.conv2 = SpectralNorm( nn.Conv2d(self.out_channels, self.out_channels, kernel_size=3, padding=1)) self.learnable_sc = in_channels != out_channels or upsample if self.learnable_sc: self.conv_sc = SpectralNorm( nn.Conv2d(in_channels, out_channels, kernel_size=1, padding=0)) # upsample layers self.upsample = upsample
def __init__(self, model_dim=64, num_classes=10, if_SN=True): super(DiscriminatorDCGAN, self).__init__() self.model_dim = model_dim self.num_classes = num_classes if if_SN: self.conv1 = SpectralNorm( nn.Conv2d(1, model_dim, 5, stride=2, padding=2)) self.conv2 = SpectralNorm( nn.Conv2d(model_dim, model_dim * 2, 5, stride=2, padding=2)) self.conv3 = SpectralNorm( nn.Conv2d(model_dim * 2, model_dim * 4, 5, stride=2, padding=2)) self.linear = SpectralNorm(nn.Linear(4 * 4 * 4 * model_dim, 1)) self.linear_y = SpectralNorm( nn.Embedding(num_classes, 4 * 4 * 4 * model_dim)) else: self.conv1 = nn.Conv2d(1, model_dim, 5, stride=2, padding=2) self.conv2 = nn.Conv2d(model_dim, model_dim * 2, 5, stride=2, padding=2) self.conv3 = nn.Conv2d(model_dim * 2, model_dim * 4, 5, stride=2, padding=2) self.linear = nn.Linear(4 * 4 * 4 * model_dim, 1) self.linear_y = nn.Embedding(num_classes, 4 * 4 * 4 * model_dim) self.relu = nn.ReLU()
def __init__(self, z_dim=10, model_dim=64, num_classes=10, outact=nn.Sigmoid()): super(GeneratorResNet, self).__init__() self.model_dim = model_dim self.z_dim = z_dim self.num_classes = num_classes fc = SpectralNorm(nn.Linear(z_dim + num_classes, 4 * 4 * 4 * model_dim)) block1 = GBlock(model_dim * 4, model_dim * 4) block2 = GBlock(model_dim * 4, model_dim * 4) block3 = GBlock(model_dim * 4, model_dim * 4) output = SpectralNorm( nn.Conv2d(model_dim * 4, IMG_C, kernel_size=3, padding=0)) self.block1 = block1 self.block2 = block2 self.block3 = block3 self.fc = fc self.output = output self.relu = nn.ReLU() self.outact = outact
def __init__(self, model_dim=64, num_classes=10, if_SN=True): super(DiscriminatorDCGAN_cifar_TS, self).__init__() self.model_dim = model_dim self.num_classes = num_classes if if_SN: self.conv1 = SpectralNorm(nn.Conv2d(1, model_dim, 4, 2, 1, bias=False)) self.conv2 = SpectralNorm(nn.Conv2d(model_dim, model_dim * 2, 4, 2, 1, bias=False)) self.conv3 = SpectralNorm(nn.Conv2d(model_dim * 2, model_dim * 4, 4, 2, 1, bias=False)) self.conv4 = SpectralNorm(nn.Conv2d(model_dim * 4, 1, 4, 1, 0, bias=False)) self.BN_1 = nn.BatchNorm2d(model_dim * 2) self.BN_2 = nn.BatchNorm2d(model_dim * 4) self.linear = SpectralNorm(nn.Linear(4 * 4 * 4 * model_dim, 1)) self.linear_y = SpectralNorm(nn.Embedding(num_classes, 4 * 4 * 4 * model_dim)) else: self.conv1 = nn.Conv2d(1, model_dim, 5, stride=2, padding=2) self.conv2 = nn.Conv2d(model_dim, model_dim * 2, 5, stride=2, padding=2) self.conv3 = nn.Conv2d(model_dim * 2, model_dim * 4, 5, stride=2, padding=2) self.linear = nn.Linear(4 * 4 * 4 * model_dim, 1) self.linear_y = nn.Embedding(num_classes, 4 * 4 * 4 * model_dim) self.LeakyReLU = nn.LeakyReLU(0.2, inplace=True) self.Sigmoid = nn.Sigmoid() ''' reference by https://github.com/Ksuryateja/DCGAN-CIFAR10-pytorch/blob/master/gan_cifar.py
def __init__(self, ngpu): super(Discriminator_celeba, self).__init__() self.ngpu = ngpu self.conv1 = SpectralNorm(nn.Conv2d(nc, ndf, 4, 2, 1, bias=False)) self.main = nn.Sequential( # input is (nc) x 64 x 64 nn.LeakyReLU(0.2, inplace=True), # state size. (ndf) x 32 x 32 SpectralNorm(nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False)), nn.BatchNorm2d(ndf * 2), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf*2) x 16 x 16 SpectralNorm(nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False)), nn.BatchNorm2d(ndf * 4), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf*4) x 8 x 8 SpectralNorm(nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False)), nn.BatchNorm2d(ndf * 8), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf*8) x 4 x 4 SpectralNorm(nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False)), nn.Sigmoid() )