-
Notifications
You must be signed in to change notification settings - Fork 11
/
run_main.py
351 lines (256 loc) · 11.7 KB
/
run_main.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
import torch
import numpy as np
import mnist_data
import os
import vae
import plot_utils
import glob
import argparse
IMAGE_SIZE_MNIST = 28
"""parsing and configuration"""
def parse_args():
desc = "Pytorch implementation of 'Variational AutoEncoder (VAE)'"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('--results_path', type=str, default='results',
help='File path of output images')
parser.add_argument('--add_noise', type=bool, default=False,
help='Boolean for adding salt & pepper noise to input image')
parser.add_argument('--dim_z', type=int, default='20', help='Dimension of latent vector', required=True)
parser.add_argument('--n_hidden', type=int, default=500, help='Number of hidden units in MLP')
parser.add_argument('--learn_rate', type=float, default=1e-3, help='Learning rate for Adam optimizer')
parser.add_argument('--num_epochs', type=int, default=20, help='The number of epochs to run')
parser.add_argument('--batch_size', type=int, default=128, help='Batch size')
parser.add_argument('--PRR', type=bool, default=True,
help='Boolean for plot-reproduce-result')
parser.add_argument('--PRR_n_img_x', type=int, default=10,
help='Number of images along x-axis')
parser.add_argument('--PRR_n_img_y', type=int, default=10,
help='Number of images along y-axis')
parser.add_argument('--PRR_resize_factor', type=float, default=1.0,
help='Resize factor for each displayed image')
parser.add_argument('--PMLR', type=bool, default=False,
help='Boolean for plot-manifold-learning-result')
parser.add_argument('--PMLR_n_img_x', type=int, default=20,
help='Number of images along x-axis')
parser.add_argument('--PMLR_n_img_y', type=int, default=20,
help='Number of images along y-axis')
parser.add_argument('--PMLR_resize_factor', type=float, default=1.0,
help='Resize factor for each displayed image')
parser.add_argument('--PMLR_z_range', type=float, default=2.0,
help='Range for unifomly distributed latent vector')
parser.add_argument('--PMLR_n_samples', type=int, default=5000,
help='Number of samples in order to get distribution of labeled data')
return check_args(parser.parse_args())
def check_args(args):
# --results_path
try:
os.mkdir(args.results_path)
except(FileExistsError):
pass
# delete all existing files
files = glob.glob(args.results_path + '/*')
for f in files:
os.remove(f)
# --add_noise
try:
assert args.add_noise == True or args.add_noise == False
except:
print('add_noise must be boolean type')
return None
# --dim-z
try:
assert args.dim_z > 0
except:
print('dim_z must be positive integer')
return None
# --n_hidden
try:
assert args.n_hidden >= 1
except:
print('number of hidden units must be larger than one')
# --learn_rate
try:
assert args.learn_rate > 0
except:
print('learning rate must be positive')
# --num_epochs
try:
assert args.num_epochs >= 1
except:
print('number of epochs must be larger than or equal to one')
# --batch_size
try:
assert args.batch_size >= 1
except:
print('batch size must be larger than or equal to one')
# --PRR
try:
assert args.PRR == True or args.PRR == False
except:
print('PRR must be boolean type')
return None
if args.PRR == True:
# --PRR_n_img_x, --PRR_n_img_y
try:
assert args.PRR_n_img_x >= 1 and args.PRR_n_img_y >= 1
except:
print('PRR : number of images along each axis must be larger than or equal to one')
# --PRR_resize_factor
try:
assert args.PRR_resize_factor > 0
except:
print('PRR : resize factor for each displayed image must be positive')
# --PMLR
try:
assert args.PMLR == True or args.PMLR == False
except:
print('PMLR must be boolean type')
return None
if args.PMLR == True:
try:
assert args.dim_z == 2
except:
print('PMLR : dim_z must be two')
# --PMLR_n_img_x, --PMLR_n_img_y
try:
assert args.PMLR_n_img_x >= 1 and args.PMLR_n_img_y >= 1
except:
print('PMLR : number of images along each axis must be larger than or equal to one')
# --PMLR_resize_factor
try:
assert args.PMLR_resize_factor > 0
except:
print('PMLR : resize factor for each displayed image must be positive')
# --PMLR_z_range
try:
assert args.PMLR_z_range > 0
except:
print('PMLR : range for unifomly distributed latent vector must be positive')
# --PMLR_n_samples
try:
assert args.PMLR_n_samples > 100
except:
print('PMLR : Number of samples in order to get distribution of labeled data must be large enough')
return args
def main(args):
# torch.manual_seed(222)
# torch.cuda.manual_seed_all(222)
# np.random.seed(222)
device = torch.device('cuda')
RESULTS_DIR = args.results_path
ADD_NOISE = args.add_noise
n_hidden = args.n_hidden
dim_img = IMAGE_SIZE_MNIST ** 2 # number of pixels for a MNIST image
dim_z = args.dim_z
# train
n_epochs = args.num_epochs
batch_size = args.batch_size
learn_rate = args.learn_rate
# Plot
PRR = args.PRR # Plot Reproduce Result
PRR_n_img_x = args.PRR_n_img_x # number of images along x-axis in a canvas
PRR_n_img_y = args.PRR_n_img_y # number of images along y-axis in a canvas
PRR_resize_factor = args.PRR_resize_factor # resize factor for each image in a canvas
PMLR = args.PMLR # Plot Manifold Learning Result
PMLR_n_img_x = args.PMLR_n_img_x # number of images along x-axis in a canvas
PMLR_n_img_y = args.PMLR_n_img_y # number of images along y-axis in a canvas
PMLR_resize_factor = args.PMLR_resize_factor # resize factor for each image in a canvas
PMLR_z_range = args.PMLR_z_range # range for random latent vector
PMLR_n_samples = args.PMLR_n_samples # number of labeled samples to plot a map from input data space to the latent space
""" prepare MNIST data """
train_total_data, train_size, _, _, test_data, test_labels = mnist_data.prepare_MNIST_data()
n_samples = train_size
""" create network """
keep_prob = 0.99
encoder = vae.Encoder(dim_img, n_hidden, dim_z, keep_prob).to(device)
decoder = vae.Decoder(dim_z, n_hidden, dim_img, keep_prob).to(device)
# + operator will return but .extend is inplace no return.
optimizer = torch.optim.Adam(list(encoder.parameters()) + list(decoder.parameters()), lr=learn_rate)
# vae.init_weights(encoder, decoder)
""" training """
# Plot for reproduce performance
if PRR:
PRR = plot_utils.Plot_Reproduce_Performance(RESULTS_DIR, PRR_n_img_x, PRR_n_img_y, IMAGE_SIZE_MNIST,
IMAGE_SIZE_MNIST, PRR_resize_factor)
x_PRR = test_data[0:PRR.n_tot_imgs, :]
x_PRR_img = x_PRR.reshape(PRR.n_tot_imgs, IMAGE_SIZE_MNIST, IMAGE_SIZE_MNIST)
PRR.save_images(x_PRR_img, name='input.jpg')
print('saved:', 'input.jpg')
if ADD_NOISE:
x_PRR = x_PRR * np.random.randint(2, size=x_PRR.shape)
x_PRR += np.random.randint(2, size=x_PRR.shape)
x_PRR_img = x_PRR.reshape(PRR.n_tot_imgs, IMAGE_SIZE_MNIST, IMAGE_SIZE_MNIST)
PRR.save_images(x_PRR_img, name='input_noise.jpg')
print('saved:', 'input_noise.jpg')
x_PRR = torch.from_numpy(x_PRR).float().to(device)
# Plot for manifold learning result
if PMLR and dim_z == 2:
PMLR = plot_utils.Plot_Manifold_Learning_Result(RESULTS_DIR, PMLR_n_img_x, PMLR_n_img_y, IMAGE_SIZE_MNIST,
IMAGE_SIZE_MNIST, PMLR_resize_factor, PMLR_z_range)
x_PMLR = test_data[0:PMLR_n_samples, :]
id_PMLR = test_labels[0:PMLR_n_samples, :]
if ADD_NOISE:
x_PMLR = x_PMLR * np.random.randint(2, size=x_PMLR.shape)
x_PMLR += np.random.randint(2, size=x_PMLR.shape)
z_ = torch.from_numpy(PMLR.z).float().to(device)
x_PMLR = torch.from_numpy(x_PMLR).float().to(device)
# train
total_batch = int(n_samples / batch_size)
min_tot_loss = np.inf
for epoch in range(n_epochs):
# Random shuffling
np.random.shuffle(train_total_data)
train_data_ = train_total_data[:, :-mnist_data.NUM_LABELS]
# Loop over all batches
encoder.train()
decoder.train()
for i in range(total_batch):
# Compute the offset of the current minibatch in the data.
offset = (i * batch_size) % (n_samples)
batch_xs_input = train_data_[offset:(offset + batch_size), :]
batch_xs_target = batch_xs_input
# add salt & pepper noise
if ADD_NOISE:
batch_xs_input = batch_xs_input * np.random.randint(2, size=batch_xs_input.shape)
batch_xs_input += np.random.randint(2, size=batch_xs_input.shape)
batch_xs_input, batch_xs_target = torch.from_numpy(batch_xs_input).float().to(device), \
torch.from_numpy(batch_xs_target).float().to(device)
assert not torch.isnan(batch_xs_input).any()
assert not torch.isnan(batch_xs_target).any()
y, z, tot_loss, loss_likelihood, loss_divergence = \
vae.get_loss(encoder, decoder, batch_xs_input, batch_xs_target)
optimizer.zero_grad()
tot_loss.backward()
optimizer.step()
# print cost every epoch
print("epoch %d: L_tot %03.2f L_likelihood %03.2f L_divergence %03.2f" % (
epoch, tot_loss.item(), loss_likelihood.item(), loss_divergence.item()))
encoder.eval()
decoder.eval()
# if minimum loss is updated or final epoch, plot results
if min_tot_loss > tot_loss.item() or epoch + 1 == n_epochs:
min_tot_loss = tot_loss.item()
# Plot for reproduce performance
if PRR:
y_PRR = vae.get_ae(encoder, decoder, x_PRR)
y_PRR_img = y_PRR.reshape(PRR.n_tot_imgs, IMAGE_SIZE_MNIST, IMAGE_SIZE_MNIST)
PRR.save_images(y_PRR_img.detach().cpu().numpy(), name="/PRR_epoch_%02d" % (epoch) + ".jpg")
print('saved:', "/PRR_epoch_%02d" % (epoch) + ".jpg")
# Plot for manifold learning result
if PMLR and dim_z == 2:
y_PMLR = decoder(z_)
y_PMLR_img = y_PMLR.reshape(PMLR.n_tot_imgs, IMAGE_SIZE_MNIST, IMAGE_SIZE_MNIST)
PMLR.save_images(y_PMLR_img.detach().cpu().numpy(), name="/PMLR_epoch_%02d" % (epoch) + ".jpg")
print('saved:', "/PMLR_epoch_%02d" % (epoch) + ".jpg")
# plot distribution of labeled images
z_PMLR = vae.get_z(encoder, x_PMLR)
PMLR.save_scattered_image(z_PMLR.detach().cpu().numpy(), id_PMLR,
name="/PMLR_map_epoch_%02d" % (epoch) + ".jpg")
print('saved:', "/PMLR_map_epoch_%02d" % (epoch) + ".jpg")
if __name__ == '__main__':
# parse arguments
args = parse_args()
if args is None:
exit()
# main
main(args)