-
Notifications
You must be signed in to change notification settings - Fork 0
/
GANs.py
349 lines (274 loc) · 10.9 KB
/
GANs.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
# -*- coding: utf-8 -*-
"""Copy of Computer_Vision CW2.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1-jfdJQaSXpbC92kMzAFtcJ_xRyJJO3sP
"""
import os
import pickle
import time
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn as nn
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from tqdm import tqdm
class Generator(nn.Module):
"""Image generator
Takes a noise vector as input and syntheses a single channel image accordingly
"""
def __init__(self, input_dims, output_dims):
"""Init function
Declare the network structure as indicated in CW2 Guidance
Arguments:
input_dims {int} -- Dimension of input noise vector
output_dims {int} -- Dimension of the output vector (flatten image)
"""
super(Generator, self).__init__()
### TODO: Change the architecture and value as CW2 Guidance required
self.fc1 = nn.Sequential(nn.Linear(input_dims, 256), nn.LeakyReLU(0.2))
self.fc2 = nn.Sequential(nn.Linear(256,512), nn.LeakyReLU(0.2))
self.fc3 = nn.Sequential(nn.Linear(512,1024),nn.LeakyReLU(0.2))
# output hidden layer
self.fc4 = nn.Sequential(nn.Linear(1024, output_dims), nn.Tanh())
def forward(self, x):
"""Forward function
Arguments:
x {Tensor} -- a batch of noise vectors in shape (<batch_size>x<input_dims>)
Returns:
Tensor -- a batch of flatten image in shape (<batch_size>x<output_dims>)
"""
### TODO: modify to be consistent with the network structure
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
x = self.fc4(x)
return x
class Discriminator(nn.Module):
"""Image discriminator
Takes a image as input and predict if it is real from the dataset or fake synthesised by the generator
"""
def __init__(self, input_dims, output_dims=1):
"""Init function
Declare the discriminator network structure as indicated in CW2 Guidance
Arguments:
input_dims {int} -- Dimension of the flatten input images
Keyword Arguments:
output_dims {int} -- Predicted probability (default: {1})
"""
super(Discriminator, self).__init__()
### TODO: Change the architecture and value as CW2 Guidance required
self.fc0 = nn.Sequential(
nn.Linear(input_dims, 784),
nn.LeakyReLU(0.2),
nn.Dropout(0.3)
)
self.fc1 = nn.Sequential(
nn.Linear(784, 1024),
nn.LeakyReLU(0.2),
nn.Dropout(0.3)
)
self.fc2 = nn.Sequential(
nn.Linear(1024, 512),
nn.LeakyReLU(0.2),
nn.Dropout(0.3)
)
self.fc3 = nn.Sequential(
nn.Linear(512, 256),
nn.LeakyReLU(0.2),
nn.Dropout(0.3)
)
self.fc4 = nn.Sequential(
nn.Linear(256, 1),
nn.Sigmoid()
)
def forward(self, x):
"""Forward function
Arguments:
x {Tensor} -- a batch of 2D image in shape (<batch_size>xHxW)
Returns:
Tensor -- predicted probabilities (<batch_size>)
"""
### TODO: modify to be consistent with the network structure
x = self.fc0(x)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
x = self.fc4(x)
return x
def show_result(G_net, z_, num_epoch, show=False, save=False, path='result.png'):
"""Result visualisation
Show and save the generated figures in the grid fashion
Arguments:
G_net {[nn.Module]} -- The generator instant
z_ {[Tensor]} -- Input noise vectors
num_epoch {[int]} -- Indicate how many epoch has the generator been trained
Keyword Arguments:
show {bool} -- If to display the images (default: {False})
save {bool} -- If to store the images (default: {False})
path {str} -- path to store the images (default: {'result.png'})
"""
plt.figure(figsize=(5, 5))
for i in range(25):
if device == 'cuda':
img = G_net(z_[i].to(device)).cuda().detach().numpy().reshape(28,28)
else:
img = G_net(z_[i].to(device)).cpu().detach().numpy().reshape(28,28)
plt.subplot(5, 5, i+1)
plt.imshow(img, cmap='gray')
plt.xticks([])
plt.yticks([])
plt.tight_layout()
plt.xlabel('EPOCH '+str(num_epoch))
if save:
plt.savefig(path)
if show:
plt.show()
else:
plt.close()
### TODO: complete the rest of part
# hint: use plt.subplots to construct grid
# hint: use plt.imshow and plt.savefig to display and store the images
def show_train_hist(hist, show=False, save=False, path='Train_hist.png'):
"""Loss tracker
Plot the losses of generator and discriminator independently to see the trend
Arguments:
hist {[dict]} -- Tracking variables
Keyword Arguments:
show {bool} -- If to display the figure (default: {False})
save {bool} -- If to store the figure (default: {False})
path {str} -- path to store the figure (default: {'Train_hist.png'})
"""
x = range(len(hist['D_losses']))
y1 = hist['D_losses']
y2 = hist['G_losses']
plt.plot(x, y1, label='D_loss')
plt.plot(x, y2, label='G_loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend(loc=4)
plt.grid(True)
plt.tight_layout()
if save:
plt.savefig(path)
if show:
plt.show()
else:
plt.close()
def create_noise(num, dim):
"""Noise constructor
returns a tensor filled with random numbers from a standard normal distribution
Arguments:
num {int} -- Number of vectors
dim {int} -- Dimension of vectors
Returns:
[Tensor] -- the generated noise vector batch
"""
return torch.randn(num, dim)
if __name__ == '__main__':
# initialise the device for training, if gpu is available, device = 'cuda', else: device = 'cpu'
from six.moves import urllib
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
data_dir = './MNIST_data/'
save_dir = './MNIST_GAN_results/'
image_save_dir = './MNIST_GAN_results/results'
# create folder if not exist
if not os.path.exists(save_dir):
os.mkdir(save_dir)
if not os.path.exists(image_save_dir):
os.mkdir(image_save_dir)
# training parameters
batch_size = 100
learning_rate = 0.0002
epochs = 100
# parameters for Models
image_size = 28
G_input_dim = 100
G_output_dim = image_size * image_size
D_input_dim = image_size * image_size
D_output_dim = 1
# construct the dataset and data loader
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=(0.5,), std=(0.5,))])
train_data = datasets.MNIST(root=data_dir, train=True, transform=transform, download=True)
train_loader = DataLoader(dataset=train_data, batch_size=batch_size, shuffle=True)
# declare the generator and discriminator networks
G_net = Generator(G_input_dim, G_output_dim).to(device)
D_net = Discriminator(D_input_dim, D_output_dim).to(device)
# Binary Cross Entropy Loss function
criterion = nn.BCELoss().to(device)
# Initialise the Optimizers
G_optimizer = torch.optim.Adam(G_net.parameters(), lr=learning_rate)
D_optimizer = torch.optim.Adam(D_net.parameters(), lr=learning_rate)
# tracking variables
train_hist = {}
train_hist['D_losses'] = []
train_hist['G_losses'] = []
train_hist['per_epoch_ptimes'] = []
train_hist['total_ptime'] = []
start_time = time.time()
# training loop
for epoch in range(epochs):
G_net.train()
D_net.train()
Loss_G = []
Loss_D = []
epoch_start_time = time.time()
for (image, _) in tqdm(train_loader):
image = image.to(device)
b_size = len(image)
# creat real and fake labels
real_label = torch.ones(b_size, 1).to(device)
fake_label = torch.zeros(b_size, 1).to(device)
# generate fake images
data_fake = G_net(create_noise(b_size, G_input_dim).to(device))
data_real = image.view(b_size, D_input_dim)
# --------train the discriminator network----------
# compute the loss for real and fake images
output_real = D_net(data_real)
output_fake = D_net(data_fake)
loss_real = criterion(output_real, real_label)
loss_fake = criterion(output_fake, fake_label)
loss_d = loss_real + loss_fake
# back propagation
D_optimizer.zero_grad()
loss_d.backward()
D_optimizer.step()
# -------- train the generator network-----------
data_fake = G_net(create_noise(b_size, G_input_dim).to(device))
# compute the loss for generator network
output_fake = D_net(data_fake)
loss_g = criterion(output_fake, real_label)
## back propagation
G_optimizer.zero_grad()
loss_g.backward()
G_optimizer.step()
## store the loss of each iter
Loss_D.append(loss_d.item())
Loss_G.append(loss_g.item())
epoch_loss_g = np.mean(Loss_G) # mean generator loss for the epoch
epoch_loss_d = np.mean(Loss_D) # mean discriminator loss for the epoch
epoch_end_time = time.time()
per_epoch_ptime = epoch_end_time - epoch_start_time
print("Epoch %d of %d with %.2f s" % (epoch + 1, epochs, per_epoch_ptime))
print("Generator loss: %.8f, Discriminator loss: %.8f" % (epoch_loss_g, epoch_loss_d))
path = image_save_dir + '/MNIST_GAN_' + str(epoch + 1) + '.png'
show_result(G_net, create_noise(25, 100).to(device), (epoch + 1), save=True, path=path)
# record the loss for every epoch
train_hist['G_losses'].append(epoch_loss_g)
train_hist['D_losses'].append(epoch_loss_d)
train_hist['per_epoch_ptimes'].append(per_epoch_ptime)
end_time = time.time()
total_ptime = end_time - start_time
train_hist['total_ptime'].append(total_ptime)
print('Avg per epoch ptime: %.2f, total %d epochs ptime: %.2f' % (
np.mean(train_hist['per_epoch_ptimes']), epochs, total_ptime))
print("Training finish!... save training results")
with open(save_dir + '/train_hist.pkl', 'wb') as f:
pickle.dump(train_hist, f)
show_train_hist(train_hist, save=True, path=save_dir + '/MNIST_GAN_train_hist.png')