| from torch import nn |
|
|
| class Generator(nn.Module): |
| def __init__(self, config): |
| super(Generator, self).__init__() |
| |
| self.latent_dim = config["latent_dim"] |
| self.ngf = config["ngf"] |
| self.nc = config["nc"] |
|
|
| |
| self.main = nn.Sequential( |
|
|
| |
| nn.ConvTranspose2d(self.latent_dim, self.ngf * 8, 4, 1, 0, bias=False), |
| nn.BatchNorm2d(self.ngf * 8), |
| nn.ReLU(True), |
|
|
| |
| nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False), |
| nn.BatchNorm2d(self.ngf * 4), |
| nn.ReLU(True), |
|
|
| |
| nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False), |
| nn.BatchNorm2d(self.ngf * 2), |
| nn.ReLU(True), |
|
|
| |
| nn.ConvTranspose2d(self.ngf * 2, self.ngf, 4, 2, 1, bias=False), |
| nn.BatchNorm2d(self.ngf), |
| nn.ReLU(True), |
|
|
| |
| nn.ConvTranspose2d(self.ngf, self.nc, 4, 2, 1, bias=False), |
| nn.Tanh() |
| |
| ) |
| |
| def forward(self, input): |
| return self.main(input) |
|
|
| class Discriminator(nn.Module): |
| def __init__(self, config): |
| super(Discriminator, self).__init__() |
|
|
| self.ndf = config["ndf"] |
| self.nc = config["nc"] |
|
|
| |
| self.main = nn.Sequential( |
|
|
| |
| nn.Conv2d(self.nc, self.ndf, 4, 2, 1, bias=False), |
| nn.LeakyReLU(0.2, inplace=True), |
|
|
| |
| nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False), |
| nn.BatchNorm2d(self.ndf * 2), |
| nn.LeakyReLU(0.2, inplace=True), |
|
|
| |
| nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False), |
| nn.BatchNorm2d(self.ndf * 4), |
| nn.LeakyReLU(0.2, inplace=True), |
|
|
| |
| nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False), |
| nn.BatchNorm2d(self.ndf * 8), |
| nn.LeakyReLU(0.2, inplace=True), |
| |
| |
| nn.Conv2d(self.ndf * 8, 1, 4, 1, 0, bias=False), |
| nn.Sigmoid() |
| ) |
| |
| def forward(self, input): |
| return self.main(input).view(-1, 1).squeeze(1) |