-
Notifications
You must be signed in to change notification settings - Fork 1
/
gan.py
349 lines (254 loc) · 11.1 KB
/
gan.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
from time import time as time
import numpy as np
from tqdm import tqdm
import torch
from torch.autograd import Variable
import torch.utils.data
from torch import nn as nn
import torchvision.utils as vutils
from torchvision.utils import make_grid
from tensorboardX import SummaryWriter
class Options:
def __init__(self):
self.cuda = False
self.batch_size = 256
self.nz = 2
self.num_iter = 50
self.num_disc_iters = 10
self.wgangp_lambda = 0.1
self.visualize_nth = 10
self.n_classes = 4
self.n_classes1 = 41
self.n_classes2 = 35
self.conditional = False
self.shuffle_labels = False
self.checkpoints = []
self.path = ''
self.two_labels = False
self.test_labels = False
TENSORBOARD = True
DATASET = 'MNIST' # 'MNIST', 'gaussians'
class GAN_base():
def __init__(self, netG, netD, optimizerD, optimizerG, opt):
self.netD, self.netG = netD, netG
self.optimizerD, self.optimizerG = optimizerD, optimizerG
self.opt = opt
if self.opt is not None and self.opt.cuda:
if self.netD is not None:
self.netD.cuda()
if self.netG is not None:
self.netG.cuda()
def compute_disc_score(self, data_a, data_b):
raise NotImplementedError
errD = None
return errG
def compute_gen_score(self, data):
raise NotImplementedError
errD = None
return errD
def train_D_one_step(self, iterator_a, iterator_b):
self.netD.zero_grad()
for p in self.netD.parameters():
p.requires_grad = True # to avoid computation
# get data and scores
data_a = next(iterator_a)
data_b = next(iterator_b)
errD = self.compute_disc_score(data_a, data_b)
errD = errD.mean()
errD.backward()
self.optimizerD.step()
return errD.data[0], data_a, data_b
def train_G_one_step(self, iterator_fake, fake_images=None):
self.netG.zero_grad()
for p in self.netD.parameters():
p.requires_grad = False # to avoid computation
if fake_images is None:
fake_images = next(iterator_fake)
errG = self.compute_gen_score(fake_images)
try:
errG.backward()
self.optimizerG.step()
except:
pass
return errG.data[0], fake_images
def train_one_step(self, iterator_data, iterator_fake, num_disc_iters=1, i_iter=None):
fake_images, errD, errG = None, None, None
# Update D network
for i in range(num_disc_iters):
errD, real_data, fake_images = self.train_D_one_step(iterator_data, iterator_fake)
# Update G network
errG, fake_images = self.train_G_one_step(iterator_fake, fake_images)
return errD, errG
def train(self, data_iter, opt=None, logger=None, callback=None):
if opt is not None:
self.opt = opt
if TENSORBOARD:
writer = SummaryWriter(opt.path)
netD, netG = self.netD, self.netG
netD.train()
netG.train()
# move everything on a GPU
if self.opt.cuda:
netD.cuda()
netG.cuda()
# iterators
iterator_data = data_iter
iterator_fake = self.fake_data_generator(opt.batch_size, opt.nz, iterator_data)
gen_score_history = []
disc_score_history = []
# main loop
t_start = time()
time_history = []
for i_iter in tqdm(range(opt.num_iter)):
if (i_iter + 1) in self.opt.checkpoints:
self.save(i_iter + 1)
errD, errG = self.train_one_step(iterator_data, iterator_fake,
num_disc_iters=opt.num_disc_iters, i_iter=i_iter)
if TENSORBOARD:
writer.add_scalar('disc_loss', errD, i_iter)
writer.add_scalar('gen_loss', errG, i_iter)
if logger is not None:
logger.add('disc_loss', errD, i_iter)
logger.add('gen_loss', errG, i_iter)
if callback is not None:
callback(self, i_iter)
gen_score_history.append(errG)
disc_score_history.append(errD)
time_history.append(time() - t_start)
np.save(self.opt.path + 'loss.pkl', np.asarray([self.opt.visualize_nth] + gen_score_history + disc_score_history + time_history))
if TENSORBOARD:
writer.close()
self.save('final')
def save(self, tag):
if self.netG is not None:
torch.save(self.netG.state_dict(), self.opt.path + 'gen_{}.pth'.format(tag))
if self.netD is not None:
torch.save(self.netD.state_dict(), self.opt.path + 'disc_{}.pth'.format(tag))
def join_xy(self, batch):
th = torch.cuda if self.opt.cuda else torch
x, y = batch
if len(x.size()) == 2:
y_onehot = th.FloatTensor(x.size()[0], self.opt.n_classes)
y_onehot.zero_()
y_onehot.scatter_(1, y.data.view(-1,1), 1)
return torch.cat((x, torch.autograd.Variable(y_onehot)), 1)
if len(x.size()) == 4:
y_onehot = th.FloatTensor(x.size()[0], self.opt.n_classes)
y_onehot.zero_()
y_onehot.scatter_(1, y.data.view(-1,1), 1)
y_onehot = y_onehot.view(x.size()[0], self.opt.n_classes, 1, 1)
return torch.cat((x, torch.autograd.Variable(y_onehot.expand(x.size()[0], self.opt.n_classes, x.size()[2], x.size()[3]))), 1)
def gen_labels(self, batch_size, n_classes=None):
if n_classes is None:
n_classes = self.opt.n_classes
th = torch.cuda if self.opt.cuda else torch
if self.opt.cuda:
return torch.autograd.Variable(torch.LongTensor(batch_size).random_(0, n_classes).cuda())
else:
return torch.autograd.Variable(torch.LongTensor(batch_size).random_(0, n_classes))
def gen_latent_noise(self, batch_size, nz):
th = torch.cuda if self.opt.cuda else torch
shape = [batch_size] + list(nz)
if self.opt.cuda:
return torch.zeros(shape).normal_(0, 1).cuda()
else:
return torch.zeros(shape).normal_(0, 1)
def gen_fake_data(self, batch_size, nz, noise=None, label=None, drop_labels=False):
if noise is None:
noise = Variable(self.gen_latent_noise(batch_size, nz))
if self.opt.two_labels:
y1 = self.gen_labels(batch_size, self.opt.n_classes1)
y2 = self.gen_labels(batch_size, self.opt.n_classes2)
return self.netG(noise, y1, y2), y1, y2
if self.opt.conditional:
if label is None:
y = self.gen_labels(batch_size)
else:
y = torch.autograd.Variable(torch.LongTensor(batch_size).zero_() + label)
if self.opt.cuda:
y = y.cuda()
noise = self.join_xy((noise, y))
if drop_labels:
return self.netG(noise)
else:
return self.netG(noise), y
return self.netG(noise)
def fake_data_generator(self, batch_size, nz, iterator_data, selected=None, drop_labels=False):
if self.opt.shuffle_labels:
i = 0
while True:
i = 1 - i
if i:
yield self.gen_fake_data(batch_size, nz)
else:
x, y = next(iterator_data)
shift = torch.from_numpy(np.random.randint(1, self.opt.n_classes, size=y.size()))
if self.opt.cuda:
shift = shift.cuda()
y = torch.autograd.Variable(torch.remainder(y.data + shift, self.opt.n_classes))
a, b = self.gen_fake_data(batch_size, nz)
yield (x,y)
else:
while True:
yield self.gen_fake_data(batch_size, nz, label=selected, drop_labels=drop_labels)
class GAN(GAN_base):
def __init__(self, netG, netD, optimizerD, optimizerG, opt):
GAN_base.__init__(self, netG, netD, optimizerD, optimizerG, opt)
# criterion for training
self.criterion = torch.nn.BCEWithLogitsLoss(size_average=True)
self.real_label = 1
self.fake_label = 0
self.generator_label = 1 # fake labels are real for generator cost
def compute_disc_score(self, data_a, data_b):
th = torch.cuda if self.opt.cuda else torch
if type(data_a) == list or type(data_a) == tuple:
data_a = (data_a[0].detach(),) + tuple(a for a in data_a[1:])
data_b = (data_b[0].detach(),) + tuple(b for b in data_b[1:])
# data_b = [data_b[0].detach(), data_b[1], data_b[2]]
# if type(data_a) == list:
# data_a = [data_a[0].detach(), data_a[1], data_a[2]]
# data_b = [data_b[0].detach(), data_b[1], data_b[2]]
# elif type(data_a) == tuple:
# data_a = data_a[0].detach(), data_a[1]
# data_b = data_b[0].detach(), data_b[1]
else:
data_a = data_a.detach()
data_b = data_b.detach()
if self.opt.conditionalD:
data_a = self.join_xy(data_a)
data_b = self.join_xy(data_b)
if type(data_a) == list or type(data_a) == tuple:
scores_a = self.netD(*data_a)
scores_b = self.netD(*data_b)
else:
scores_a = self.netD(data_a)
scores_b = self.netD(data_b)
if type(scores_a) is tuple:
labels_a = Variable(th.FloatTensor(scores_a[0].size(0)).fill_(self.real_label))
errD_a = self.criterion(scores_a[0], labels_a) + self.criterion(scores_a[1], labels_a)
else:
labels_a = Variable(th.FloatTensor(scores_a.size(0)).fill_(self.real_label))
errD_a = self.criterion(scores_a, labels_a)
if type(scores_b) is tuple:
labels_b = Variable(th.FloatTensor(scores_b[0].size(0)).fill_(self.fake_label))
errD_b = self.criterion(scores_b[0], labels_b) + self.criterion(scores_b[1], labels_b)
else:
labels_b = Variable(th.FloatTensor(scores_b.size(0)).fill_(self.fake_label))
errD_b = self.criterion(scores_b, labels_b)
errD = errD_a + errD_b
return errD
def compute_gen_score(self, data):
th = torch.cuda if self.opt.cuda else torch
if self.opt.conditionalD:
data = self.join_xy(data)
if type(data) == list or type(data) == tuple:
scores = self.netD(*data)
else:
scores = self.netD(data)
if type(scores) is tuple:
labels = Variable(th.FloatTensor(scores[0].size()).fill_(self.generator_label))
errG = self.criterion(scores[0], labels) + self.criterion(scores[1], labels)
else:
labels = Variable(th.FloatTensor(scores.size()).fill_(self.generator_label))
errG = self.criterion(scores, labels)
return errG