| 11 | from torchvision import transforms |
| 12 | |
| 13 | class Generator(torch.nn.Module): |
| 14 | def __init__(self,channels_noise,channels_img,features_g): |
| 15 | super(Generator, self).__init__() |
| 16 | self.gen = torch.nn.Sequential( |
| 17 | #imgsize: 4 x 4 |
| 18 | self._block(in_channels = channels_noise,out_channels = features_g * 16,kernel_size = (4,4), |
| 19 | stride=(1,1),padding=0), |
| 20 | # imgsize: 8 x 8 |
| 21 | self._block(in_channels=features_g * 16, out_channels=features_g * 8, kernel_size=(4, 4), stride=(2,2), |
| 22 | padding=1), |
| 23 | # imgsize: 16 x 16 |
| 24 | self._block(in_channels=features_g * 8, out_channels=features_g * 4, kernel_size=(4, 4), stride=(2,2), |
| 25 | padding=1), |
| 26 | # imgsize: 32 x 32 |
| 27 | self._block(in_channels=features_g * 4, out_channels=features_g * 2, kernel_size=(4, 4), stride=(2,2), |
| 28 | padding=1), |
| 29 | # imgsize: N x 3 x 64 x 64 |
| 30 | torch.nn.ConvTranspose2d( |
| 31 | in_channels=features_g * 2, out_channels=channels_img, kernel_size=(4,4), stride=(2,2), |
| 32 | padding=(1,1) |
| 33 | ), |
| 34 | torch.nn.Tanh() |
| 35 | ) |
| 36 | |
| 37 | def _block(self,in_channels,out_channels,kernel_size,stride,padding): |
| 38 | self.conv = torch.nn.Sequential( |
| 39 | torch.nn.ConvTranspose2d( |
| 40 | in_channels=in_channels,out_channels=out_channels,kernel_size=kernel_size,stride=stride,padding=padding,bias=False |
| 41 | ), |
| 42 | torch.nn.BatchNorm2d(num_features=out_channels), |
| 43 | torch.nn.ReLU() |
| 44 | ) |
| 45 | return self.conv |
| 46 | |
| 47 | def forward(self,input): |
| 48 | x = self.gen(input) |
| 49 | return x |
| 50 | |
| 51 | if __name__ == '__main__': |
| 52 | noise_dim = 100 |