forked from hehaodele/ProbGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
379 lines (316 loc) · 12.8 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import torch
import torch.nn as nn
# 32 x 32 ==============================================================================================================
class multi_generator_32(nn.Module):
"""
Generative Network for images with the size of 32 x 32
Parameters:
ngf: number of feature channels
num_gens: number of generators
"""
def __init__(self, z_size=100, out_size=3, ngf=128, num_gens=2):
super(multi_generator_32, self).__init__()
self.z_size = z_size
self.ngf = ngf
self.out_size = out_size
self.main = nn.Sequential(
# state size: (ngf * 4) x 4 x 4
nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ngf * 2),
nn.ReLU(inplace=True),
# state size: (ngf * 2) x 8 x 8
nn.ConvTranspose2d(self.ngf * 2, self.ngf, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ngf),
nn.ReLU(inplace=True),
# state size: ngf x 16 x 16
nn.ConvTranspose2d(self.ngf, self.out_size, 4, 2, 1, bias=False),
nn.Tanh()
# state size: out_size x 32 x 32
)
self.gs = []
for i in range(num_gens):
g = nn.Sequential(
# input size is z_size
nn.ConvTranspose2d(self.z_size, self.ngf * 4, 4, 1, 0, bias=False),
nn.BatchNorm2d(self.ngf * 4),
nn.ReLU(inplace=True),
)
setattr(self, 'G_{}'.format(i), g)
self.gs.append(g)
for m in self.modules():
if isinstance(m, nn.ConvTranspose2d):
m.weight.data.normal_(0.0, 0.02)
if m.bias is not None:
m.bias.data.zero_()
def forward(self, x):
sp_size = (len(x) - 1) // len(self.gs) + 1
y = []
for _x, _g in zip(torch.split(x, sp_size, dim=0), self.gs):
y.append(_g(_x))
y = torch.cat(y, dim=0)
output = self.main(y)
return output
# multiple discriminators 32x32
class multi_discriminator_32(nn.Module):
"""
Discriminative Network
Parameters:
ndf: number of feature channels
num_dics: number of discriminators
"""
def __init__(self, in_size=3, ndf=128, num_discs=2, unbound_output=False):
super(multi_discriminator_32, self).__init__()
self.in_size = in_size
self.ndf = ndf
self.ds = nn.Conv2d(self.ndf * 4, num_discs, 4, 1, 0, bias=False)
self.n_ds = num_discs
self.main = nn.Sequential(
# input size is in_size x 32 x 32
nn.Conv2d(self.in_size, self.ndf, 5, 2, 2, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# state size: ndf x 16 x 16
nn.Conv2d(self.ndf, self.ndf * 2, 5, 2, 2, bias=False),
nn.BatchNorm2d(self.ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
# state size: (ndf * 2) x 8 x 8
nn.Conv2d(self.ndf * 2, self.ndf * 4, 5, 2, 2, bias=False),
nn.BatchNorm2d(self.ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# state size: (ndf * 4) x 4 x 4
self.ds,
)
if not unbound_output:
self.main = nn.Sequential(
self.main,
nn.Sigmoid(),
)
for m in self.modules():
if isinstance(m, nn.Conv2d):
m.weight.data.normal_(0.0, 0.02)
if m.bias is not None:
m.bias.data.zero_()
def forward(self, input):
output = self.main(input)
return output
class multi_discriminator_with_history(nn.Module):
"""
Discriminative Network revised for ProbGAN
Keep historial samples of discriminators
Parameters:
ndf: number of feature channels
num_dics: number of discriminators
"""
def __init__(self, in_size=3, ndf=128, num_discs=2, unbound_output=False):
super(multi_discriminator_with_history, self).__init__()
self.unbound_output = unbound_output
self.in_size = in_size
self.ndf = ndf
self.ds = nn.Conv2d(self.ndf * 4, num_discs, 4, 1, 0, bias=False)
self.n_ds = num_discs
with torch.no_grad():
self.ds_hist_avg = nn.Conv2d(self.ndf * 4, num_discs, 4, 1, 0, bias=False)
self.len_hist = 1.0
self.backbone = nn.Sequential(
# input size is in_size x 32 x 32
nn.Conv2d(self.in_size, self.ndf, 5, 2, 2, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# state size: ndf x 16 x 16
nn.Conv2d(self.ndf, self.ndf * 2, 5, 2, 2, bias=False),
nn.BatchNorm2d(self.ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
# state size: (ndf * 2) x 8 x 8
nn.Conv2d(self.ndf * 2, self.ndf * 4, 5, 2, 2, bias=False),
nn.BatchNorm2d(self.ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# state size: (ndf * 4) x 4 x 4
)
self.main = nn.Sequential(self.backbone, self.ds, )
self.main_hist = nn.Sequential(self.backbone, self.ds_hist_avg, )
if not unbound_output:
self.main = nn.Sequential(self.main, nn.Sigmoid(), )
self.main_hist = nn.Sequential(self.main_hist, nn.Sigmoid(), )
for m in self.modules():
if isinstance(m, nn.Conv2d):
m.weight.data.normal_(0.0, 0.02)
if m.bias is not None:
m.bias.data.zero_()
self.eps = 1e-3
def forward(self, input):
output = self.main(input)
if not self.unbound_output:
output = output * (1 - 2 * self.eps) + self.eps
return output
def forward_by_hist(self, input):
output = self.main_hist(input)
if not self.unbound_output:
output = output * (1 - 2 * self.eps) + self.eps
return output
def update_hist(self):
self.len_hist += 1
alpha = 1.0 / self.len_hist
# alpha = 1e-3
self.ds_hist_avg.weight.data = self.ds_hist_avg.weight.data * (1 - alpha) + self.ds.weight.data * alpha
# 48 x 48 ==============================================================================================================
class multi_generator_48(nn.Module):
"""
Generative Network
"""
def __init__(self, z_size=100, out_size=3, ngf=128, num_gens=2):
super(multi_generator_48, self).__init__()
self.z_size = z_size
self.ngf = ngf
self.out_size = out_size
self.main = nn.Sequential(
# state size: (ngf * 8) x 3 x 3
nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ngf * 4),
nn.ReLU(inplace=True),
# state size: (ngf * 4) x 6 x 6
nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ngf * 2),
nn.ReLU(inplace=True),
# state size: (ngf * 2) x 12 x 12
nn.ConvTranspose2d(self.ngf * 2, self.ngf, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ngf),
nn.ReLU(inplace=True),
# state size: ngf x 24 x 24
nn.ConvTranspose2d(self.ngf, self.out_size, 4, 2, 1, bias=False),
nn.Tanh()
# state size: out_size x 48 x 48
)
self.gs = []
for i in range(num_gens):
g = nn.Sequential(
# input size is z_size
nn.ConvTranspose2d(self.z_size, self.ngf * 8, 3, 1, 0, bias=False),
nn.BatchNorm2d(self.ngf * 8),
nn.ReLU(inplace=True),
)
setattr(self, 'G_{}'.format(i), g)
self.gs.append(g)
for m in self.modules():
if isinstance(m, nn.ConvTranspose2d):
m.weight.data.normal_(0.0, 0.02)
if m.bias is not None:
m.bias.data.zero_()
def forward(self, x):
sp_size = (len(x) - 1) // len(self.gs) + 1
y = []
for _x, _g in zip(torch.split(x, sp_size, dim=0), self.gs):
y.append(_g(_x))
y = torch.cat(y, dim=0)
output = self.main(y)
return output
class multi_discriminator_48(nn.Module):
"""
Discriminative Network
"""
def __init__(self, in_size=3, ndf=128, num_discs=2, unbound_output=False):
super(multi_discriminator_48, self).__init__()
self.in_size = in_size
self.ndf = ndf
self.ds = nn.Conv2d(self.ndf * 8, num_discs, 3, 1, 0, bias=False)
self.n_ds = num_discs
self.main = nn.Sequential(
# input size is in_size x 48 x 48
nn.Conv2d(self.in_size, self.ndf, 5, 2, 2, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# state size: ndf x 24 x 24
nn.Conv2d(self.ndf, self.ndf * 2, 5, 2, 2, bias=False),
nn.BatchNorm2d(self.ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
# state size: (ndf * 2) x 12 x 12
nn.Conv2d(self.ndf * 2, self.ndf * 4, 5, 2, 2, bias=False),
nn.BatchNorm2d(self.ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# state size: (ndf * 4) x 6 x 6
nn.Conv2d(self.ndf * 4, self.ndf * 8, 5, 2, 2, bias=False),
nn.BatchNorm2d(self.ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
# state size: (ndf * 8) x 3 x 3
self.ds,
)
if not unbound_output:
self.main = nn.Sequential(
self.main,
nn.Sigmoid(),
)
for m in self.modules():
if isinstance(m, nn.Conv2d):
m.weight.data.normal_(0.0, 0.02)
if m.bias is not None:
m.bias.data.zero_()
def forward(self, input):
output = self.main(input)
return output
class multi_discriminator_48_with_history(nn.Module):
"""
Discriminative Network
"""
def __init__(self, in_size=3, ndf=128, num_discs=2, unbound_output=False):
super(multi_discriminator_48_with_history, self).__init__()
self.unbound_output = unbound_output
self.in_size = in_size
self.ndf = ndf
self.ds = nn.Conv2d(self.ndf * 8, num_discs, 3, 1, 0, bias=False)
self.n_ds = num_discs
with torch.no_grad():
self.ds_hist_avg = nn.Conv2d(self.ndf * 8, num_discs, 3, 1, 0, bias=False)
self.len_hist = 1.0
self.backbone = nn.Sequential(
# input size is in_size x 48 x 48
nn.Conv2d(self.in_size, self.ndf, 5, 2, 2, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# state size: ndf x 24 x 24
nn.Conv2d(self.ndf, self.ndf * 2, 5, 2, 2, bias=False),
nn.BatchNorm2d(self.ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
# state size: (ndf * 2) x 12 x 12
nn.Conv2d(self.ndf * 2, self.ndf * 4, 5, 2, 2, bias=False),
nn.BatchNorm2d(self.ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# state size: (ndf * 4) x 6 x 6
nn.Conv2d(self.ndf * 4, self.ndf * 8, 5, 2, 2, bias=False),
nn.BatchNorm2d(self.ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
# state size: (ndf * 8) x 3 x 3
)
self.main = nn.Sequential(self.backbone, self.ds, )
self.main_hist = nn.Sequential(self.backbone, self.ds_hist_avg, )
if not unbound_output:
self.main = nn.Sequential(self.main, nn.Sigmoid(), )
self.main_hist = nn.Sequential(self.main_hist, nn.Sigmoid(), )
for m in self.modules():
if isinstance(m, nn.Conv2d):
m.weight.data.normal_(0.0, 0.02)
if m.bias is not None:
m.bias.data.zero_()
self.eps = 1e-3
def forward(self, input):
output = self.main(input)
if not self.unbound_output:
output = output * (1 - 2 * self.eps) + self.eps
return output
def forward_by_hist(self, input):
output = self.main_hist(input)
if not self.unbound_output:
output = output * (1 - 2 * self.eps) + self.eps
return output
def update_hist(self):
self.len_hist += 1
alpha = 1.0 / self.len_hist
# alpha = 1e-3
self.ds_hist_avg.weight.data = self.ds_hist_avg.weight.data * (1 - alpha) + self.ds.weight.data * alpha
# ======================================================================================================================
if __name__ == '__main__':
G = multi_generator_48()
z = torch.zeros(10, 100, 1, 1).to(torch.float)
x = G(z)
print(x.shape)
D = multi_discriminator_48()
y = D(x)
print(y.shape)
D2 = multi_discriminator_48_with_history()
y2 = D2(x)
y2_ = D2.forward_by_hist(x)
print(y2.shape, y2_.shape)