-
Notifications
You must be signed in to change notification settings - Fork 8
/
models.py
209 lines (193 loc) · 7.96 KB
/
models.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import torch
import torch.nn as nn
import torch.nn.parallel
def weights(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
# DCGAN model, fully convolutional architecture
class NetG1(nn.Module):
def __init__(self, ngpu, nz, nc , ngf, n_extra_layers_g):
super(NetG1, self).__init__()
self.ngpu = ngpu
#self.nz = nz
#self.nc = nc
#self.ngf = ngf
main = nn.Sequential(
# input is Z, going into a convolution
# state size. nz x 1 x 1
nn.ConvTranspose2d(nz, ngf * 8, 4, 1, 0, bias=False),
nn.BatchNorm2d(ngf * 8),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ngf*8) x 4 x 4
nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 4),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ngf*4) x 8 x 8
nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 2),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ngf*2) x 16 x 16
nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ngf) x 32 x 32
)
# Extra layers
for t in range(n_extra_layers_g):
main.add_module('extra-layers-{0}.{1}.conv'.format(t, ngf),
nn.Conv2d(ngf, ngf, 3, 1, 1, bias=False))
main.add_module('extra-layers-{0}.{1}.batchnorm'.format(t, ngf),
nn.BatchNorm2d(ngf))
main.add_module('extra-layers-{0}.{1}.relu'.format(t, ngf),
nn.LeakyReLU(0.2, inplace=True))
main.add_module('final_layer.deconv',
nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False)) # 5,3,1 for 96x96
main.add_module('final_layer.tanh',
nn.Tanh())
# state size. (nc) x 96 x 96
self.main = main
def forward(self, input):
gpu_ids = None
if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu > 1:
#gpu_ids = range(self.ngpu)
output = nn.parallel.data_parallel(self.main, input, range(self.ngpu)), 0
else:
output = self.main(input)
return output
class NetD1(nn.Module):
def __init__(self, ngpu, nz, nc, ndf, n_extra_layers_d):
super(NetD1, self).__init__()
self.ngpu = ngpu
main = nn.Sequential(
# input is (nc) x 96 x 96
nn.Conv2d(nc, ndf, 4, 2, 1, bias=False), # 5,3,1 for 96x96
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf) x 32 x 32
nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*2) x 16 x 16
nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*4) x 8 x 8
nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*8) x 4 x 4
)
# Extra layers
for t in range(n_extra_layers_d):
main.add_module('extra-layers-{0}.{1}.conv'.format(t, ndf * 8),
nn.Conv2d(ndf * 8, ndf * 8, 3, 1, 1, bias=False))
main.add_module('extra-layers-{0}.{1}.batchnorm'.format(t, ndf * 8),
nn.BatchNorm2d(ndf * 8))
main.add_module('extra-layers-{0}.{1}.relu'.format(t, ndf * 8),
nn.LeakyReLU(0.2, inplace=True))
main.add_module('final_layers.conv', nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False))
main.add_module('final_layers.sigmoid', nn.Sigmoid())
# state size. 1 x 1 x 1
self.main = main
def forward(self, input):
gpu_ids = None
if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu > 1:
gpu_ids = range(self.ngpu)
output = nn.parallel.data_parallel(self.main, input, gpu_ids)
return output.view(-1, 1)
# with z decoder and fc layers
class NetG2(nn.Module):
def __init__(self, ngpu, nz, nc , ngf):
super(NetG2, self).__init__()
self.ngpu = ngpu
self.nz = nz
self.fcs = nn.Sequential(
# input is Z, going into a convolution
# state size. nz x 1 x 1
nn.Linear(nz, 1024),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(1024, 1024),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
)
self.decode_fcs = nn.Sequential(
nn.Linear(1024, 1024),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(1024, nz),
)
self.convs = nn.Sequential(
# 1024x1x1
nn.ConvTranspose2d(1024, ngf * 8, 4, 1, 0, bias=False),
nn.BatchNorm2d(ngf * 8),
nn.ReLU(inplace=True),
# state size. (ngf*8) x 4 x 4
nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 4),
nn.ReLU(inplace=True),
# state size. (ngf*4) x 8 x 8
nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 2),
nn.ReLU(inplace=True),
# state size. (ngf*2) x 16 x 16
nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf),
nn.ReLU(inplace=True),
# state size. (ngf) x 32 x 32
nn.ConvTranspose2d( ngf, nc, 4, 2, 1, bias=False),
nn.Tanh()
# state size. (nc) x 96 x 96
)
def forward(self, input):
input = self.fcs(input.view(-1,self.nz))
gpu_ids = None
if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu > 1:
gpu_ids = range(self.ngpu)
z_prediction = self.decode_fcs(input)
input = input.view(-1,1024,1,1)
output = nn.parallel.data_parallel(self.convs, input, gpu_ids)
return output, z_prediction
class NetD2(nn.Module):
def __init__(self, ngpu, nz, nc , ndf):
super(NetD2, self).__init__()
self.ngpu = ngpu
self.convs = nn.Sequential(
# input is (nc) x 96 x 96
nn.Conv2d(nc, ndf, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf) x 32 x 32
nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*2) x 16 x 16
nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*4) x 8 x 8
nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*8) x 4 x 4
nn.Conv2d(ndf * 8, 1024, 4, 1, 0, bias=False),
nn.LeakyReLU(inplace=True),
nn.Dropout(0.5),
# state size. 1024 x 1 x 1
)
self.fcs = nn.Sequential(
nn.Linear(1024, 1024),
nn.LeakyReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(1024, 1),
nn.Sigmoid()
)
def forward(self, input):
gpu_ids = None
if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu > 1:
gpu_ids = range(self.ngpu)
output = nn.parallel.data_parallel(self.convs, input, gpu_ids)
output = self.fcs(output.view(-1,1024))
return output.view(-1, 1)