-
Notifications
You must be signed in to change notification settings - Fork 0
/
pca_gan_test.py
283 lines (226 loc) · 9.48 KB
/
pca_gan_test.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
import os, time, itertools, imageio, pickle
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
# For some reason this doesn't work with sklearn?
# from tensorflow.examples.tutorials.mnist import input_data
from sklearn.datasets import fetch_mldata
import math, pdb
from sklearn.decomposition import PCA
# def minibatch_discriminator(input_layer, width, name='minibatch_discrim'):
# batch_size = input_layer.shape[0]
# num_features = input_layer.shape[1]
# W = self.variable('W', [num_features, width],
# init=tf.contrib.layers.xavier_initializer())
# b = self.variable('b', [width], init=tf.constant_initializer(0.0))
# activation = tf.matmul(input_layer, W)
# activation = tf.reshape(activation, [batch_size, width])
# pdb.set_trace()
# tmp1 = tf.expand_dims(activation, 3)
# tmp2 = tf.transpose(activation, perm=[1,2,0])
# tmp2 = tf.expand_dims(tmp2, 0)
# abs_diff = tf.reduce_sum(tf.abs(tmp1 - tmp2), reduction_indices=[2])
# f = tf.reduce_sum(tf.exp(-abs_diff), reduction_indices=[2])
# f = f + b
# return f
def pca_reconstruct(pca, dat0, n_components):
# Make sure we don't ask for more components than we have
# assert n_components < 51
# Halfway through just start training on the real dataset
if n_components > 200:
return dat0
# Grab the principle components (forward pass through encoder)
X_train_pca = pca.transform(dat0)
# Only use the first n principle components
X_train_pca[:, n_components:] = 0
# Project back to original space (pass components through decoder)
X_projected = pca.inverse_transform(X_train_pca)
X_projected = (X_projected-np.min(X_projected))/(np.max(X_projected)-np.min(X_projected))
# train_set = mnist.train.images
X_projected = (X_projected - 0.5) / 0.5 # normalization; range: -1 ~ 1
return X_projected
# G(z)
def generator(x):
# initializers
w_init = tf.truncated_normal_initializer(mean=0, stddev=0.02)
b_init = tf.constant_initializer(0.)
# 1st hidden layer
w0 = tf.get_variable('G_w0', [x.get_shape()[1], 256], initializer=w_init)
b0 = tf.get_variable('G_b0', [256], initializer=b_init)
h0 = tf.nn.relu(tf.matmul(x, w0) + b0)
# 2nd hidden layer
w1 = tf.get_variable('G_w1', [h0.get_shape()[1], 512], initializer=w_init)
b1 = tf.get_variable('G_b1', [512], initializer=b_init)
h1 = tf.nn.relu(tf.matmul(h0, w1) + b1)
# 3rd hidden layer
w2 = tf.get_variable('G_w2', [h1.get_shape()[1], 1024], initializer=w_init)
b2 = tf.get_variable('G_b2', [1024], initializer=b_init)
h2 = tf.nn.relu(tf.matmul(h1, w2) + b2)
# output hidden layer
w3 = tf.get_variable('G_w3', [h2.get_shape()[1], 784], initializer=w_init)
b3 = tf.get_variable('G_b3', [784], initializer=b_init)
o = tf.nn.tanh(tf.matmul(h2, w3) + b3)
return o
# D(x)
def discriminator(x, drop_out):
# initializers
w_init = tf.truncated_normal_initializer(mean=0, stddev=0.02)
b_init = tf.constant_initializer(0.)
# 1st hidden layer
w0 = tf.get_variable('D_w0', [x.get_shape()[1], 1024], initializer=w_init)
b0 = tf.get_variable('D_b0', [1024], initializer=b_init)
h0 = tf.nn.relu(tf.matmul(x, w0) + b0)
h0 = tf.nn.dropout(h0, drop_out)
# 2nd hidden layer
w1 = tf.get_variable('D_w1', [h0.get_shape()[1], 512], initializer=w_init)
b1 = tf.get_variable('D_b1', [512], initializer=b_init)
h1 = tf.nn.relu(tf.matmul(h0, w1) + b1)
h1 = tf.nn.dropout(h1, drop_out)
# 3rd hidden layer
w2 = tf.get_variable('D_w2', [h1.get_shape()[1], 256], initializer=w_init)
b2 = tf.get_variable('D_b2', [256], initializer=b_init)
h2 = tf.nn.relu(tf.matmul(h1, w2) + b2)
h2 = tf.nn.dropout(h2, drop_out)
# output layer
w3 = tf.get_variable('D_w3', [h2.get_shape()[1], 1], initializer=w_init)
b3 = tf.get_variable('D_b3', [1], initializer=b_init)
o = tf.sigmoid(tf.matmul(h2, w3) + b3)
return o
fixed_z_ = np.random.normal(0, 1, (25, 100))
def show_result(num_epoch, show = False, save = False, path = 'result.png', isFix=False):
z_ = np.random.normal(0, 1, (25, 100))
if isFix:
test_images = sess.run(G_z, {z: fixed_z_, drop_out: 0.0})
else:
test_images = sess.run(G_z, {z: z_, drop_out: 0.0})
size_figure_grid = 5
fig, ax = plt.subplots(size_figure_grid, size_figure_grid, figsize=(5, 5))
for i, j in itertools.product(range(size_figure_grid), range(size_figure_grid)):
ax[i, j].get_xaxis().set_visible(False)
ax[i, j].get_yaxis().set_visible(False)
for k in range(5*5):
i = k // 5
j = k % 5
ax[i, j].cla()
ax[i, j].imshow(np.reshape(test_images[k], (28, 28)), cmap='gray')
label = 'Epoch {0}'.format(num_epoch)
fig.text(0.5, 0.04, label, ha='center')
plt.savefig(path)
if show:
plt.show()
else:
plt.close()
def show_train_hist(hist, show = False, save = False, path = '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()
# training parameters
batch_size = 100
lr = 0.0002
train_epoch = 100
# load MNIST
# mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
mnist = fetch_mldata('MNIST original')
train_set = mnist.data[:].astype(np.float32)
# Mix 'er up
np.random.shuffle(train_set)
# Normalize 0,1
train_set = (train_set-np.min(train_set))/(np.max(train_set)-np.min(train_set))
# train_set = mnist.train.images
train_set = (train_set - 0.5) / 0.5 # normalization; range: -1 ~ 1
# train_set = (train_set - np.mean(train_set)) / np.std(train_set)
# Fit PCA on MNIST
pca = PCA(n_components=300)
pca.fit(train_set)
# networks : generator
with tf.variable_scope('G'):
z = tf.placeholder(tf.float32, shape=(None, 100))
G_z = generator(z)
# networks : discriminator
with tf.variable_scope('D') as scope:
drop_out = tf.placeholder(dtype=tf.float32, name='drop_out')
x = tf.placeholder(tf.float32, shape=(None, 784))
D_real = discriminator(x, drop_out)
scope.reuse_variables()
D_fake = discriminator(G_z, drop_out)
# loss for each network
eps = 1e-2
D_loss = tf.reduce_mean(-tf.log(D_real + eps) - tf.log(1 - D_fake + eps))
G_loss = tf.reduce_mean(-tf.log(D_fake + eps))
# trainable variables for each network
t_vars = tf.trainable_variables()
D_vars = [var for var in t_vars if 'D_' in var.name]
G_vars = [var for var in t_vars if 'G_' in var.name]
# optimizer for each network
D_optim = tf.train.AdamOptimizer(lr).minimize(D_loss, var_list=D_vars)
G_optim = tf.train.AdamOptimizer(lr).minimize(G_loss, var_list=G_vars)
# open session and initialize all variables
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
# results save folder
if not os.path.isdir('MNIST_GAN_results'):
os.mkdir('MNIST_GAN_results')
if not os.path.isdir('MNIST_GAN_results/Random_results'):
os.mkdir('MNIST_GAN_results/Random_results')
if not os.path.isdir('MNIST_GAN_results/Fixed_results'):
os.mkdir('MNIST_GAN_results/Fixed_results')
train_hist = {}
train_hist['D_losses'] = []
train_hist['G_losses'] = []
train_hist['per_epoch_ptimes'] = []
train_hist['total_ptime'] = []
# training-loop
np.random.seed(int(time.time()))
start_time = time.time()
for epoch in range(train_epoch):
G_losses = []
D_losses = []
epoch_start_time = time.time()
for iter in range(train_set.shape[0] // batch_size):
# update discriminator
x_ = train_set[iter*batch_size:(iter+1)*batch_size]
x_ = pca_reconstruct(pca, x_, int(epoch)*3)
z_ = np.random.normal(0, 1, (batch_size, 100))
loss_d_, _ = sess.run([D_loss, D_optim], {x: x_, z: z_, drop_out: 0.4})
D_losses.append(loss_d_)
# update generator
z_ = np.random.normal(0, 1, (batch_size, 100))
loss_g_, _ = sess.run([G_loss, G_optim], {z: z_, drop_out: 0.4})
G_losses.append(loss_g_)
epoch_end_time = time.time()
per_epoch_ptime = epoch_end_time - epoch_start_time
print('[%d/%d] - ptime: %.2f loss_d: %.3f, loss_g: %.3f' % ((epoch + 1), train_epoch, per_epoch_ptime, np.mean(D_losses), np.mean(G_losses)))
p = 'MNIST_GAN_results/Random_results/MNIST_GAN_' + str(epoch + 1) + '.png'
fixed_p = 'MNIST_GAN_results/Fixed_results/MNIST_GAN_' + str(epoch + 1) + '.png'
show_result((epoch + 1), save=True, path=p, isFix=False)
show_result((epoch + 1), save=True, path=fixed_p, isFix=True)
train_hist['D_losses'].append(np.mean(D_losses))
train_hist['G_losses'].append(np.mean(G_losses))
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']), train_epoch, total_ptime))
print("Training finish!... save training results")
with open('MNIST_GAN_results/train_hist.pkl', 'wb') as f:
pickle.dump(train_hist, f)
show_train_hist(train_hist, save=True, path='MNIST_GAN_results/MNIST_GAN_train_hist.png')
images = []
for e in range(train_epoch):
img_name = 'MNIST_GAN_results/Fixed_results/MNIST_GAN_' + str(e + 1) + '.png'
images.append(imageio.imread(img_name))
imageio.mimsave('MNIST_GAN_results/generation_animation.gif', images, fps=5)
sess.close()