-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMNIST_cDCGAN_pytorch.py
420 lines (331 loc) · 14.1 KB
/
MNIST_cDCGAN_pytorch.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# MNIST image generation using Conditional DCGAN
import torch
from torch.autograd import Variable
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import numpy as np
import matplotlib.pyplot as plt
import os
import imageio
from logger import Logger
from draw_convnet import DrawNet
# Parameters
image_size = 32
label_dim = 10
G_input_dim = 100
G_output_dim = 1
D_input_dim = 1
D_output_dim = 1
num_filters = [512, 256, 128]
learning_rate = 0.0002
betas = (0.5, 0.999)
batch_size = 128
num_epochs = 20
data_dir = '../../Data/MNIST_data/'
save_dir = 'MNIST_cDCGAN_results/'
# MNIST dataset
transform = transforms.Compose([transforms.Scale(image_size),
transforms.ToTensor(),
transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))])
mnist_data = dsets.MNIST(root=data_dir,
train=True,
transform=transform,
download=True)
data_loader = torch.utils.data.DataLoader(dataset=mnist_data,
batch_size=batch_size,
shuffle=True)
# For logger
def to_np(x):
return x.data.cpu().numpy()
def to_var(x):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x)
# De-normalization
def denorm(x):
out = (x + 1) / 2
return out.clamp(0, 1)
# Generator model
class Generator(torch.nn.Module):
def __init__(self, input_dim, label_dim, num_filters, output_dim):
super(Generator, self).__init__()
# Hidden layers
self.hidden_layer1 = torch.nn.Sequential()
self.hidden_layer2 = torch.nn.Sequential()
self.hidden_layer = torch.nn.Sequential()
for i in range(len(num_filters)):
# Deconvolutional layer
if i == 0:
# For input
input_deconv = torch.nn.ConvTranspose2d(input_dim, int(num_filters[i]/2), kernel_size=4, stride=1, padding=0)
self.hidden_layer1.add_module('input_deconv', input_deconv)
# Initializer
torch.nn.init.normal(input_deconv.weight, mean=0.0, std=0.02)
torch.nn.init.constant(input_deconv.bias, 0.0)
# Batch normalization
self.hidden_layer1.add_module('input_bn', torch.nn.BatchNorm2d(int(num_filters[i]/2)))
# Activation
self.hidden_layer1.add_module('input_act', torch.nn.ReLU())
# For label
label_deconv = torch.nn.ConvTranspose2d(label_dim, int(num_filters[i]/2), kernel_size=4, stride=1, padding=0)
self.hidden_layer2.add_module('label_deconv', label_deconv)
# Initializer
torch.nn.init.normal(label_deconv.weight, mean=0.0, std=0.02)
torch.nn.init.constant(label_deconv.bias, 0.0)
# Batch normalization
self.hidden_layer2.add_module('label_bn', torch.nn.BatchNorm2d(int(num_filters[i]/2)))
# Activation
self.hidden_layer2.add_module('label_act', torch.nn.ReLU())
else:
deconv = torch.nn.ConvTranspose2d(num_filters[i-1], num_filters[i], kernel_size=4, stride=2, padding=1)
deconv_name = 'deconv' + str(i + 1)
self.hidden_layer.add_module(deconv_name, deconv)
# Initializer
torch.nn.init.normal(deconv.weight, mean=0.0, std=0.02)
torch.nn.init.constant(deconv.bias, 0.0)
# Batch normalization
bn_name = 'bn' + str(i + 1)
self.hidden_layer.add_module(bn_name, torch.nn.BatchNorm2d(num_filters[i]))
# Activation
act_name = 'act' + str(i + 1)
self.hidden_layer.add_module(act_name, torch.nn.ReLU())
# Output layer
self.output_layer = torch.nn.Sequential()
# Deconvolutional layer
out = torch.nn.ConvTranspose2d(num_filters[i], output_dim, kernel_size=4, stride=2, padding=1)
self.output_layer.add_module('out', out)
# Initializer
torch.nn.init.normal(out.weight, mean=0.0, std=0.02)
torch.nn.init.constant(out.bias, 0.0)
# Activation
self.output_layer.add_module('act', torch.nn.Tanh())
def forward(self, z, c):
h1 = self.hidden_layer1(z)
h2 = self.hidden_layer2(c)
x = torch.cat([h1, h2], 1)
h = self.hidden_layer(x)
out = self.output_layer(h)
return out
# Discriminator model
class Discriminator(torch.nn.Module):
def __init__(self, input_dim, label_dim, num_filters, output_dim):
super(Discriminator, self).__init__()
self.hidden_layer1 = torch.nn.Sequential()
self.hidden_layer2 = torch.nn.Sequential()
self.hidden_layer = torch.nn.Sequential()
for i in range(len(num_filters)):
# Convolutional layer
if i == 0:
# For input
input_conv = torch.nn.Conv2d(input_dim, int(num_filters[i]/2), kernel_size=4, stride=2, padding=1)
self.hidden_layer1.add_module('input_conv', input_conv)
# Initializer
torch.nn.init.normal(input_conv.weight, mean=0.0, std=0.02)
torch.nn.init.constant(input_conv.bias, 0.0)
# Activation
self.hidden_layer1.add_module('input_act', torch.nn.LeakyReLU(0.2))
# For label
label_conv = torch.nn.Conv2d(label_dim, int(num_filters[i]/2), kernel_size=4, stride=2, padding=1)
self.hidden_layer2.add_module('label_conv', label_conv)
# Initializer
torch.nn.init.normal(label_conv.weight, mean=0.0, std=0.02)
torch.nn.init.constant(label_conv.bias, 0.0)
# Activation
self.hidden_layer2.add_module('label_act', torch.nn.LeakyReLU(0.2))
else:
conv = torch.nn.Conv2d(num_filters[i-1], num_filters[i], kernel_size=4, stride=2, padding=1)
conv_name = 'conv' + str(i + 1)
self.hidden_layer.add_module(conv_name, conv)
# Initializer
torch.nn.init.normal(conv.weight, mean=0.0, std=0.02)
torch.nn.init.constant(conv.bias, 0.0)
# Batch normalization
bn_name = 'bn' + str(i + 1)
self.hidden_layer.add_module(bn_name, torch.nn.BatchNorm2d(num_filters[i]))
# Activation
act_name = 'act' + str(i + 1)
self.hidden_layer.add_module(act_name, torch.nn.LeakyReLU(0.2))
# Output layer
self.output_layer = torch.nn.Sequential()
# Convolutional layer
out = torch.nn.Conv2d(num_filters[i], output_dim, kernel_size=4, stride=1, padding=0)
self.output_layer.add_module('out', out)
# Initializer
torch.nn.init.normal(out.weight, mean=0.0, std=0.02)
torch.nn.init.constant(out.bias, 0.0)
# Activation
self.output_layer.add_module('act', torch.nn.Sigmoid())
def forward(self, z, c):
h1 = self.hidden_layer1(z)
h2 = self.hidden_layer2(c)
x = torch.cat([h1, h2], 1)
h = self.hidden_layer(x)
out = self.output_layer(h)
return out
# Plot losses
def plot_loss(d_losses, g_losses, num_epoch, save=False, save_dir='MNIST_cDCGAN_results/', show=False):
fig, ax = plt.subplots()
ax.set_xlim(0, num_epochs)
ax.set_ylim(0, max(np.max(g_losses), np.max(d_losses))*1.1)
plt.xlabel('Epoch {0}'.format(num_epoch + 1))
plt.ylabel('Loss values')
plt.plot(d_losses, label='Discriminator')
plt.plot(g_losses, label='Generator')
plt.legend()
# save figure
if save:
if not os.path.exists(save_dir):
os.mkdir(save_dir)
save_fn = save_dir + 'MNIST_cDCGAN_losses_epoch_{:d}'.format(num_epoch + 1) + '.png'
plt.savefig(save_fn)
if show:
plt.show()
else:
plt.close()
def plot_result(generator, noise, label, num_epoch, save=False, save_dir='MNIST_cDCGAN_results/', show=False, fig_size=(5, 5)):
generator.eval()
noise = Variable(noise.cuda())
label = Variable(label.cuda())
gen_image = generator(noise, label)
gen_image = denorm(gen_image)
generator.train()
n_rows = np.sqrt(noise.size()[0]).astype(np.int32)
n_cols = np.sqrt(noise.size()[0]).astype(np.int32)
fig, axes = plt.subplots(n_rows, n_cols, figsize=fig_size)
for ax, img in zip(axes.flatten(), gen_image):
ax.axis('off')
ax.set_adjustable('box-forced')
ax.imshow(img.cpu().data.view(image_size, image_size).numpy(), cmap='gray', aspect='equal')
plt.subplots_adjust(wspace=0, hspace=0)
title = 'Epoch {0}'.format(num_epoch+1)
fig.text(0.5, 0.04, title, ha='center')
# save figure
if save:
if not os.path.exists(save_dir):
os.mkdir(save_dir)
save_fn = save_dir + 'MNIST_cDCGAN_epoch_{:d}'.format(num_epoch+1) + '.png'
plt.savefig(save_fn)
if show:
plt.show()
else:
plt.close()
# Models
G = Generator(G_input_dim, label_dim, num_filters, G_output_dim)
D = Discriminator(D_input_dim, label_dim, num_filters[::-1], D_output_dim)
G.cuda()
D.cuda()
# Draw net
G_net = DrawNet([1, 1], G_input_dim, num_filters, [image_size, image_size], G_output_dim, 4, 'Deconv2D', save_dir)
D_net = DrawNet([image_size, image_size], D_input_dim, num_filters[::-1], [1, 1], D_output_dim, 4, 'Conv2D', save_dir)
G_net.draw()
D_net.draw()
# Set the logger
D_log_dir = save_dir + 'D_logs'
G_log_dir = save_dir + 'G_logs'
if not os.path.exists(D_log_dir):
os.mkdir(D_log_dir)
D_logger = Logger(D_log_dir)
if not os.path.exists(G_log_dir):
os.mkdir(G_log_dir)
G_logger = Logger(G_log_dir)
# Loss function
criterion = torch.nn.BCELoss()
# Optimizers
G_optimizer = torch.optim.Adam(G.parameters(), lr=learning_rate, betas=betas)
D_optimizer = torch.optim.Adam(D.parameters(), lr=learning_rate/2, betas=betas)
# Training GAN
D_avg_losses = []
G_avg_losses = []
# Fixed noise & label for test
num_test_samples = 10*10
temp_noise = torch.randn(label_dim, G_input_dim)
fixed_noise = temp_noise
fixed_c = torch.zeros(label_dim, 1)
for i in range(9):
fixed_noise = torch.cat([fixed_noise, temp_noise], 0)
temp = torch.ones(label_dim, 1) + i
fixed_c = torch.cat([fixed_c, temp], 0)
fixed_noise = fixed_noise.view(-1, G_input_dim, 1, 1)
fixed_label = torch.zeros(G_input_dim, label_dim)
fixed_label.scatter_(1, fixed_c.type(torch.LongTensor), 1)
fixed_label = fixed_label.view(-1, label_dim, 1, 1)
# label preprocess
onehot = torch.zeros(label_dim, label_dim)
onehot = onehot.scatter_(1, torch.LongTensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).view(label_dim, 1), 1).view(label_dim, label_dim, 1, 1)
fill = torch.zeros([label_dim, label_dim, image_size, image_size])
for i in range(label_dim):
fill[i, i, :, :] = 1
step = 0
for epoch in range(num_epochs):
D_losses = []
G_losses = []
if epoch == 5 or epoch == 10:
G_optimizer.param_groups[0]['lr'] /= 2
D_optimizer.param_groups[0]['lr'] /= 2
# minibatch training
for i, (images, labels) in enumerate(data_loader):
# image data
mini_batch = images.size()[0]
x_ = Variable(images.cuda())
# labels
y_real_ = Variable(torch.ones(mini_batch).cuda())
y_fake_ = Variable(torch.zeros(mini_batch).cuda())
c_fill_ = Variable(fill[labels].cuda())
# Train discriminator with real data
D_real_decision = D(x_, c_fill_).squeeze()
D_real_loss = criterion(D_real_decision, y_real_)
# Train discriminator with fake data
z_ = torch.randn(mini_batch, G_input_dim).view(-1, G_input_dim, 1, 1)
z_ = Variable(z_.cuda())
c_ = (torch.rand(mini_batch, 1) * label_dim).type(torch.LongTensor).squeeze()
c_onehot_ = Variable(onehot[c_].cuda())
gen_image = G(z_, c_onehot_)
c_fill_ = Variable(fill[c_].cuda())
D_fake_decision = D(gen_image, c_fill_).squeeze()
D_fake_loss = criterion(D_fake_decision, y_fake_)
# Back propagation
D_loss = D_real_loss + D_fake_loss
D.zero_grad()
D_loss.backward()
D_optimizer.step()
# Train generator
z_ = torch.randn(mini_batch, G_input_dim).view(-1, G_input_dim, 1, 1)
z_ = Variable(z_.cuda())
c_ = (torch.rand(mini_batch, 1) * label_dim).type(torch.LongTensor).squeeze()
c_onehot_ = Variable(onehot[c_].cuda())
gen_image = G(z_, c_onehot_)
c_fill_ = Variable(fill[c_].cuda())
D_fake_decision = D(gen_image, c_fill_).squeeze()
G_loss = criterion(D_fake_decision, y_real_)
# Back propagation
G.zero_grad()
G_loss.backward()
G_optimizer.step()
# loss values
D_losses.append(D_loss.data[0])
G_losses.append(G_loss.data[0])
print('Epoch [%d/%d], Step [%d/%d], D_loss: %.4f, G_loss: %.4f'
% (epoch+1, num_epochs, i+1, len(data_loader), D_loss.data[0], G_loss.data[0]))
# ============ TensorBoard logging ============#
D_logger.scalar_summary('losses', D_loss.data[0], step + 1)
G_logger.scalar_summary('losses', G_loss.data[0], step + 1)
step += 1
D_avg_loss = torch.mean(torch.FloatTensor(D_losses))
G_avg_loss = torch.mean(torch.FloatTensor(G_losses))
# avg loss values for plot
D_avg_losses.append(D_avg_loss)
G_avg_losses.append(G_avg_loss)
plot_loss(D_avg_losses, G_avg_losses, epoch, save=True)
# Show result for fixed noise
plot_result(G, fixed_noise, fixed_label, epoch, save=True)
# Make gif
loss_plots = []
gen_image_plots = []
for epoch in range(num_epochs):
# plot for generating gif
save_fn1 = save_dir + 'MNIST_cDCGAN_losses_epoch_{:d}'.format(epoch + 1) + '.png'
loss_plots.append(imageio.imread(save_fn1))
save_fn2 = save_dir + 'MNIST_cDCGAN_epoch_{:d}'.format(epoch + 1) + '.png'
gen_image_plots.append(imageio.imread(save_fn2))
imageio.mimsave(save_dir + 'MNIST_cDCGAN_losses_epochs_{:d}'.format(num_epochs) + '.gif', loss_plots, fps=5)
imageio.mimsave(save_dir + 'MNIST_cDCGAN_epochs_{:d}'.format(num_epochs) + '.gif', gen_image_plots, fps=5)