|
| 1 | +""" |
| 2 | +The DCGAN model. |
| 3 | +
|
| 4 | +Reference: |
| 5 | +https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html |
| 6 | +""" |
| 7 | + |
| 8 | +from torch import nn |
| 9 | + |
| 10 | +nz = 100 |
| 11 | +nc = 3 |
| 12 | +ngf = 64 |
| 13 | +ndf = 64 |
| 14 | + |
| 15 | + |
| 16 | +class Generator(nn.Module): |
| 17 | + """ Generator network of DCGAN """ |
| 18 | + |
| 19 | + def __init__(self): |
| 20 | + super().__init__() |
| 21 | + |
| 22 | + self.main = nn.Sequential( |
| 23 | + # input is Z, going into a convolution |
| 24 | + nn.ConvTranspose2d(nz, ngf * 8, 4, 1, 0, bias=False), |
| 25 | + nn.BatchNorm2d(ngf * 8), |
| 26 | + nn.ReLU(True), |
| 27 | + # state size. (ngf*8) x 4 x 4 |
| 28 | + nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False), |
| 29 | + nn.BatchNorm2d(ngf * 4), |
| 30 | + nn.ReLU(True), |
| 31 | + # state size. (ngf*4) x 8 x 8 |
| 32 | + nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False), |
| 33 | + nn.BatchNorm2d(ngf * 2), |
| 34 | + nn.ReLU(True), |
| 35 | + # state size. (ngf*2) x 16 x 16 |
| 36 | + nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False), |
| 37 | + nn.BatchNorm2d(ngf), |
| 38 | + nn.ReLU(True), |
| 39 | + # state size. (ngf) x 32 x 32 |
| 40 | + nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False), |
| 41 | + nn.Tanh() |
| 42 | + # state size. (nc) x 64 x 64 |
| 43 | + ) |
| 44 | + |
| 45 | + def forward(self, input_data): |
| 46 | + """ Forward pass. """ |
| 47 | + return self.main(input_data) |
| 48 | + |
| 49 | + |
| 50 | +class Discriminator(nn.Module): |
| 51 | + """ Discriminator network of DCGAN. """ |
| 52 | + |
| 53 | + def __init__(self): |
| 54 | + super().__init__() |
| 55 | + |
| 56 | + self.main = nn.Sequential( |
| 57 | + # input is (nc) x 64 x 64 |
| 58 | + nn.Conv2d(nc, ndf, 4, 2, 1, bias=False), |
| 59 | + nn.LeakyReLU(0.2, inplace=True), |
| 60 | + # state size. (ndf) x 32 x 32 |
| 61 | + nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False), |
| 62 | + nn.BatchNorm2d(ndf * 2), |
| 63 | + nn.LeakyReLU(0.2, inplace=True), |
| 64 | + # state size. (ndf*2) x 16 x 16 |
| 65 | + nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False), |
| 66 | + nn.BatchNorm2d(ndf * 4), |
| 67 | + nn.LeakyReLU(0.2, inplace=True), |
| 68 | + # state size. (ndf*4) x 8 x 8 |
| 69 | + nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False), |
| 70 | + nn.BatchNorm2d(ndf * 8), |
| 71 | + nn.LeakyReLU(0.2, inplace=True), |
| 72 | + # state size. (ndf*8) x 4 x 4 |
| 73 | + nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False), |
| 74 | + nn.Sigmoid()) |
| 75 | + |
| 76 | + def forward(self, input_data): |
| 77 | + return self.main(input_data) |
| 78 | + |
| 79 | + |
| 80 | +class Model: |
| 81 | + """ A wrapper class to hold the Generator and Discriminator models of DCGAN. """ |
| 82 | + |
| 83 | + def __init__(self) -> None: |
| 84 | + self.generator = Generator() |
| 85 | + self.discriminator = Discriminator() |
| 86 | + self.loss_criterion = nn.BCELoss() |
| 87 | + |
| 88 | + self.nz = nz |
| 89 | + self.nc = nc |
| 90 | + self.ngf = ngf |
| 91 | + self.ndf = ndf |
| 92 | + |
| 93 | + def weights_init(self, model): |
| 94 | + classname = model.__class__.__name__ |
| 95 | + if classname.find('Conv') != -1: |
| 96 | + nn.init.normal_(model.weight.data, 0.0, 0.02) |
| 97 | + elif classname.find('BatchNorm') != -1: |
| 98 | + nn.init.normal_(model.weight.data, 1.0, 0.02) |
| 99 | + nn.init.constant_(model.bias.data, 0) |
| 100 | + |
| 101 | + def cpu(self): |
| 102 | + self.generator.cpu() |
| 103 | + self.discriminator.cpu() |
| 104 | + |
| 105 | + def to(self, device): |
| 106 | + self.generator.to(device) |
| 107 | + self.discriminator.to(device) |
| 108 | + |
| 109 | + def train(self): |
| 110 | + self.generator.train() |
| 111 | + self.discriminator.train() |
| 112 | + |
| 113 | + def eval(self): |
| 114 | + self.generator.eval() |
| 115 | + self.discriminator.eval() |
| 116 | + |
| 117 | + @staticmethod |
| 118 | + def get_model(*args): |
| 119 | + """ Obtaining an instance of this model. """ |
| 120 | + return Model() |
0 commit comments