gianTheo commited on
Commit
4c2d938
1 Parent(s): 0fa5da9

Create model.py

Browse files
Files changed (1) hide show
  1. model.py +67 -0
model.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+
4
+ class Generator(nn.Module):
5
+ def __init__(self, z_dim=100, img_channels=3):
6
+ super(Generator, self).__init__()
7
+ self.gen = nn.Sequential(
8
+ # input is Z, going into a convolution
9
+ nn.ConvTranspose2d(z_dim, 512, 4, 1, 0, bias=False),
10
+ nn.BatchNorm2d(512),
11
+ nn.ReLU(True),
12
+ # state size. 512 x 4 x 4
13
+ nn.ConvTranspose2d(512, 256, 4, 2, 1, bias=False),
14
+ nn.BatchNorm2d(256),
15
+ nn.ReLU(True),
16
+ # state size. 256 x 8 x 8
17
+ nn.ConvTranspose2d(256, 128, 4, 2, 1, bias=False),
18
+ nn.BatchNorm2d(128),
19
+ nn.ReLU(True),
20
+ # state size. 128 x 16 x 16
21
+ nn.ConvTranspose2d(128, 64, 4, 2, 1, bias=False),
22
+ nn.BatchNorm2d(64),
23
+ nn.ReLU(True),
24
+ # state size. 64 x 32 x 32
25
+ nn.ConvTranspose2d(64, img_channels, 4, 2, 1, bias=False),
26
+ nn.Tanh()
27
+ # state size. img_channels x 64 x 64
28
+ )
29
+
30
+ def forward(self, input):
31
+ return self.gen(input)
32
+
33
+ class Discriminator(nn.Module):
34
+ def __init__(self, img_channels=3):
35
+ super(Discriminator, self).__init__()
36
+ self.disc = nn.Sequential(
37
+ # input is img_channels x 64 x 64
38
+ nn.Conv2d(img_channels, 64, 4, 2, 1, bias=False),
39
+ nn.LeakyReLU(0.2, inplace=True),
40
+ # state size. 64 x 32 x 32
41
+ nn.Conv2d(64, 128, 4, 2, 1, bias=False),
42
+ nn.BatchNorm2d(128),
43
+ nn.LeakyReLU(0.2, inplace=True),
44
+ # state size. 128 x 16 x 16
45
+ nn.Conv2d(128, 256, 4, 2, 1, bias=False),
46
+ nn.BatchNorm2d(256),
47
+ nn.LeakyReLU(0.2, inplace=True),
48
+ # state size. 256 x 8 x 8
49
+ nn.Conv2d(256, 512, 4, 2, 1, bias=False),
50
+ nn.BatchNorm2d(512),
51
+ nn.LeakyReLU(0.2, inplace=True),
52
+ # state size. 512 x 4 x 4
53
+ nn.Conv2d(512, 1, 4, 1, 0, bias=False),
54
+ nn.Sigmoid()
55
+ )
56
+
57
+ def forward(self, input):
58
+ return self.disc(input).view(-1, 1).squeeze(1)
59
+
60
+ batch_size = 32
61
+ latent_vector_size = 100
62
+
63
+ generator = Generator()
64
+ discriminator = Discriminator()
65
+
66
+ generator.load_state_dict(torch.load('netG.pth', map_location=torch.device('cpu') ))
67
+ discriminator.load_state_dict(torch.load('netD.pth', map_location=torch.device('cpu') ))