-
Notifications
You must be signed in to change notification settings - Fork 20
/
MNIST_DCGAN_pytorch.py
297 lines (235 loc) · 9.39 KB
/
MNIST_DCGAN_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
# MNIST image generation using 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
# Parameters
image_size = 64
G_input_dim = 100
G_output_dim = 1
D_input_dim = 1
D_output_dim = 1
num_filters = [1024, 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_DCGAN_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)
# 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, num_filters, output_dim):
super(Generator, self).__init__()
# Hidden layers
self.hidden_layer = torch.nn.Sequential()
for i in range(len(num_filters)):
# Deconvolutional layer
if i == 0:
deconv = torch.nn.ConvTranspose2d(input_dim, num_filters[i], kernel_size=4, stride=1, padding=0)
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, x):
h = self.hidden_layer(x)
out = self.output_layer(h)
return out
# Discriminator model
class Discriminator(torch.nn.Module):
def __init__(self, input_dim, num_filters, output_dim):
super(Discriminator, self).__init__()
# Hidden layers
self.hidden_layer = torch.nn.Sequential()
for i in range(len(num_filters)):
# Convolutional layer
if i == 0:
conv = torch.nn.Conv2d(input_dim, num_filters[i], kernel_size=4, stride=2, padding=1)
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
if i != 0:
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, x):
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_DCGAN_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_DCGAN_losses_epoch_{:d}'.format(num_epoch + 1) + '.png'
plt.savefig(save_fn)
if show:
plt.show()
else:
plt.close()
def plot_result(generator, noise, num_epoch, save=False, save_dir='MNIST_DCGAN_results/', show=False, fig_size=(5, 5)):
generator.eval()
noise = Variable(noise.cuda())
gen_image = generator(noise)
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_DCGAN_epoch_{:d}'.format(num_epoch+1) + '.png'
plt.savefig(save_fn)
if show:
plt.show()
else:
plt.close()
# Models
G = Generator(G_input_dim, num_filters, G_output_dim)
D = Discriminator(D_input_dim, num_filters[::-1], D_output_dim)
G.cuda()
D.cuda()
# 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, betas=betas)
# Training GAN
D_avg_losses = []
G_avg_losses = []
# Fixed noise for test
num_test_samples = 5*5
fixed_noise = torch.randn(num_test_samples, G_input_dim).view(-1, G_input_dim, 1, 1)
for epoch in range(num_epochs):
D_losses = []
G_losses = []
# minibatch training
for i, (images, _) 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())
# Train discriminator with real data
D_real_decision = D(x_).squeeze()
# print(D_real_decision, y_real_)
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())
gen_image = G(z_)
D_fake_decision = D(gen_image).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())
gen_image = G(z_)
D_fake_decision = D(gen_image).squeeze()
G_loss = criterion(D_fake_decision, y_real_)
# Back propagation
D.zero_grad()
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]))
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, epoch, save=True, fig_size=(5, 5))
# Make gif
loss_plots = []
gen_image_plots = []
for epoch in range(num_epochs):
# plot for generating gif
save_fn1 = save_dir + 'MNIST_DCGAN_losses_epoch_{:d}'.format(epoch + 1) + '.png'
loss_plots.append(imageio.imread(save_fn1))
save_fn2 = save_dir + 'MNIST_DCGAN_epoch_{:d}'.format(epoch + 1) + '.png'
gen_image_plots.append(imageio.imread(save_fn2))
imageio.mimsave(save_dir + 'MNIST_DCGAN_losses_epochs_{:d}'.format(num_epochs) + '.gif', loss_plots, fps=5)
imageio.mimsave(save_dir + 'MNIST_DCGAN_epochs_{:d}'.format(num_epochs) + '.gif', gen_image_plots, fps=5)