-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
106 lines (96 loc) · 3.54 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import torch.nn as nn
from spectral_norm import SpectralNorm
NUM_OF_CHANNELS = 3
Z_SIZE = 100
GEN_FEATURE_MAP_SIZE = 64
DISC_FEATURE_MAP_SIZE = 64
def weights_init(m):
classname = m.__class__.__name__
if classname.find("Conv") != -1:
nn.init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find("BatchNorm") != -1:
nn.init.normal_(m.weight.data, 1.0, 0.02)
nn.init.constant_(m.bias.data, 0)
class Generator(nn.Module):
def __init__(self, ngpu):
super(Generator, self).__init__()
self.ngpu = ngpu
self.main = nn.Sequential(
# input is Z, going into a convolution
nn.ConvTranspose2d(Z_SIZE, GEN_FEATURE_MAP_SIZE * 8, 4, 1, 0, bias=False),
nn.BatchNorm2d(GEN_FEATURE_MAP_SIZE * 8),
nn.ReLU(True),
# state size. (GEN_FEATURE_MAP_SIZE*8) x 4 x 4
nn.ConvTranspose2d(
GEN_FEATURE_MAP_SIZE * 8, GEN_FEATURE_MAP_SIZE * 4, 4, 2, 1, bias=False
),
nn.BatchNorm2d(GEN_FEATURE_MAP_SIZE * 4),
nn.ReLU(True),
# state size. (GEN_FEATURE_MAP_SIZE*4) x 8 x 8
nn.ConvTranspose2d(
GEN_FEATURE_MAP_SIZE * 4, GEN_FEATURE_MAP_SIZE * 2, 4, 2, 1, bias=False
),
nn.BatchNorm2d(GEN_FEATURE_MAP_SIZE * 2),
nn.ReLU(True),
# state size. (GEN_FEATURE_MAP_SIZE*2) x 16 x 16
nn.ConvTranspose2d(
GEN_FEATURE_MAP_SIZE * 2, GEN_FEATURE_MAP_SIZE, 4, 2, 1, bias=False
),
nn.BatchNorm2d(GEN_FEATURE_MAP_SIZE),
nn.ReLU(True),
# state size. (GEN_FEATURE_MAP_SIZE) x 32 x 32
nn.ConvTranspose2d(
GEN_FEATURE_MAP_SIZE, NUM_OF_CHANNELS, 4, 2, 1, bias=False
),
nn.Tanh()
# state size. (NUM_OF_CHANNELS) x 64 x 64
)
def forward(self, x):
return self.main(x)
class Discriminator(nn.Module):
def __init__(self, ngpu):
super(Discriminator, self).__init__()
self.ngpu = ngpu
self.main = nn.Sequential(
# input is (NUM_OF_CHANNELS) x 64 x 64
SpectralNorm(
nn.Conv2d(NUM_OF_CHANNELS, GEN_FEATURE_MAP_SIZE, 4, 2, 1, bias=False)
),
nn.LeakyReLU(0.2, inplace=True),
# state size. (GEN_FEATURE_MAP_SIZE) x 32 x 32
SpectralNorm(
nn.Conv2d(
GEN_FEATURE_MAP_SIZE, GEN_FEATURE_MAP_SIZE * 2, 4, 2, 1, bias=False
)
),
nn.LeakyReLU(0.2, inplace=True),
# state size. (GEN_FEATURE_MAP_SIZE*2) x 16 x 16
SpectralNorm(
nn.Conv2d(
GEN_FEATURE_MAP_SIZE * 2,
GEN_FEATURE_MAP_SIZE * 4,
4,
2,
1,
bias=False,
)
),
nn.LeakyReLU(0.2, inplace=True),
# state size. (GEN_FEATURE_MAP_SIZE*4) x 8 x 8
SpectralNorm(
nn.Conv2d(
GEN_FEATURE_MAP_SIZE * 4,
GEN_FEATURE_MAP_SIZE * 8,
4,
2,
1,
bias=False,
)
),
nn.LeakyReLU(0.2, inplace=True),
# state size. (GEN_FEATURE_MAP_SIZE*8) x 4 x 4
SpectralNorm(nn.Conv2d(GEN_FEATURE_MAP_SIZE * 8, 1, 4, 1, 0, bias=False)),
nn.Sigmoid(),
)
def forward(self, x):
return self.main(x)