-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_draw.py
302 lines (187 loc) · 8.58 KB
/
main_draw.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
import tensorflow as tf
import numpy as np
import os
import shutil
import time
import random
import sys
from layers import *
from ops import *
from tensorflow.examples.tutorials.mnist import input_data
from scipy.misc import imsave
from PIL import Image
from options import trainOptions
from tqdm import tqdm
class Draw():
def initialize(self):
opt = trainOptions().parse()[0]
self.batch_size = opt.batch_size
self.img_width = opt.img_width
self.img_height = opt.img_height
self.img_depth = opt.img_depth
self.z_size = opt.z_size
self.img_size = self.img_depth*self.img_height*self.img_width
self.nef = opt.nef
self.max_epoch = opt.max_epoch
self.mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
self.to_test = opt.test
self.steps = opt.steps
self.enc_size = opt.enc_size
self.dec_size = opt.dec_size
self.load_checkpoint = False
self.n_samples = self.mnist.train.num_examples
self.tensorboard_dir = "./output/draw/tensorboard"
self.check_dir = "./output/draw/checkpoints"
self.images_dir = "./output/draw/imgs"
def read(self, input_x, input_x_hat, input_h, name="read"):
with tf.variable_scope(name) as scope:
r_temp = tf.concat((input_x, input_x_hat),1)
return tf.concat((r_temp, input_h),1)
def write(self, input_h, name="write"):
with tf.variable_scope(name) as scope:
return linear1d(input_h, self.dec_size, self.img_size, name="linear")
def encoder(self, input_x, enc_state, name="encoder"):
with tf.variable_scope(name) as scope:
return self.LSTM_enc(input_x, enc_state)
def decoder(self, input_z, dec_state, name="decoder"):
with tf.variable_scope(name) as scope:
return self.LSTM_dec(input_z, dec_state)
def linear(self, input_h, name="linear"):
with tf.variable_scope(name) as scope:
mean = linear1d(input_h, self.enc_size, self.z_size, name="mean")
stddev = linear1d(input_h, self.enc_size, self.z_size, name="stddev")
return mean, tf.exp(stddev)
def sampler(self, mean, stddev, name="sampler"):
with tf.variable_scope(name) as scope:
z = tf.random_normal([self.batch_size, self.z_size], 0 , 1, dtype=tf.float32)
return z*stddev + mean
def generation_loss(self, input_img, output_img, loss_type='log_diff'):
if (loss_type == 'diff'):
return tf.reduce_sum(tf.squared_difference(input_img, output_img),1)
elif (loss_type == 'log_diff'):
epsilon = 1e-8
return -tf.reduce_sum(input_img*tf.log(output_img+epsilon) + (1 - input_img)*tf.log(epsilon + 1 - output_img),1)
def latent_loss(self, mean_z, std_z) :
loss = [0]*self.steps
for i in range(0, self.steps):
loss[i] = 0.5*tf.reduce_sum(tf.square(mean_z[i]) + tf.square(std_z[i]) - tf.log(tf.square(std_z[i])) - 1,1)
return tf.add_n(loss)
def same_sample(self, num_tensor, tensor_size):
elem = tf.random_normal([tensor_size],0,1,dtype=tf.float32)
list_tensor = [elem] * num_tensor
return tf.stack(list_tensor)
def model_setup(self):
with tf.variable_scope("Model") as scope:
self.input_x = tf.placeholder(tf.float32, [self.batch_size, self.img_size])
# For testing
self.LSTM_enc = tf.contrib.rnn.LSTMCell(self.enc_size, state_is_tuple=True)
self.LSTM_dec = tf.contrib.rnn.LSTMCell(self.dec_size, state_is_tuple=True)
#Loop to train the Model
self.gen_x = tf.zeros([self.batch_size, self.img_size])
enc_state = self.LSTM_enc.zero_state(self.batch_size, tf.float32)
dec_state = self.LSTM_dec.zero_state(self.batch_size, tf.float32)
h_dec = tf.zeros([self.batch_size, self.dec_size])
self.mean_z = [0]*self.steps
self.std_z = [0]*self.steps
#T steps for traning and training
for t in range(0, self.steps):
x_hat = self.input_x - tf.nn.sigmoid(self.gen_x)
r = self.read(self.input_x,x_hat,h_dec)
h_enc, enc_state = self.encoder(r, enc_state)
self.mean_z[t], self.std_z[t] = self.linear(h_enc)
z = self.sampler(self.mean_z[t], self.std_z[t])
h_dec, dec_state = self.decoder(z, dec_state)
self.gen_x = self.gen_x + self.write(h_dec)
scope.reuse_variables()
self.gen_x = tf.nn.sigmoid(self.gen_x)
# T steps for generating after training
if(self.to_test):
self.gen_x_gen = tf.zeros([self.batch_size, self.img_size])
self.images_output = [0]*self.steps
dec_state_gen = self.LSTM_dec.zero_state(self.batch_size, tf.float32)
for t in range(0, self.steps):
# if t > 2:
# z_gen = self.same_sample(self.batch_size, self.z_size)
# else :
# z_gen = tf.random_normal([self.batch_size, self.z_size], 0 , 1, dtype=tf.float32)
z_gen = tf.random_normal([self.batch_size, self.z_size], 0 , 1, dtype=tf.float32)
h_dec_gen, dec_state_gen = self.decoder(z_gen, dec_state_gen)
self.gen_x_gen = self.gen_x_gen + self.write(h_dec_gen)
self.images_output[t] = tf.sigmoid(self.gen_x_gen)
scope.reuse_variables()
self.model_vars = tf.trainable_variables()
# for var in self.model_vars: print(var.name, var.get_shape())
def loss_setup(self):
self.images_loss = self.generation_loss(self.input_x, self.gen_x)
self.lat_loss = self.latent_loss(self.mean_z, self.std_z)
self.images_loss_mean = tf.reduce_mean(self.images_loss)
self.lat_loss_mean = tf.reduce_mean(self.lat_loss)
self.draw_loss = self.images_loss_mean + self.lat_loss_mean
optimizer = tf.train.AdamOptimizer(0.001, beta1=0.5)
grads = optimizer.compute_gradients(self.draw_loss)
for i,(g,v) in enumerate(grads):
if g is not None:
grads[i]=(tf.clip_by_norm(g,5),v) # clip gradients
self.loss_optimizer=optimizer.apply_gradients(grads)
# self.loss_optimizer = optimizer.minimize(self.draw_loss)
self.images_loss_summ = tf.summary.scalar("images_loss", self.images_loss_mean)
self.draw_loss_summ = tf.summary.scalar("draw_loss", self.draw_loss)
self.lat_loss_summ = tf.summary.scalar("latent_loss", self.lat_loss_mean)
self.merged_summ = tf.summary.merge_all()
def train(self):
#Setting up the model and graph
self.model_setup()
self.loss_setup()
init = tf.global_variables_initializer()
saver = tf.train.Saver()
if not os.path.exists(self.images_dir+"/train/"):
os.makedirs(self.images_dir+"/train/")
if not os.path.exists(self.check_dir):
os.makedirs(self.check_dir)
# Train
with tf.Session() as sess:
sess.run(init)
writer = tf.summary.FileWriter(self.tensorboard_dir)
test_imgs = self.mnist.train.next_batch(self.batch_size)[0]
test_imgs = test_imgs.reshape((self.batch_size,28*28*1))
if self.load_checkpoint:
chkpt_fname = tf.train.latest_checkpoint(self.check_dir)
saver.restore(sess,chkpt_fname)
for epoch in tqdm(range(0,self.max_epoch),"Epoch "):
for itr in tqdm(range(0,int(self.n_samples/self.batch_size)),"Iteration"):
batch = self.mnist.train.next_batch(self.batch_size)
imgs = batch[0]
labels = batch[1]
imgs = imgs.reshape((self.batch_size,28*28*1))
_, summary_str = sess.run([self.loss_optimizer, self.merged_summ],feed_dict={self.input_x:imgs})
# print('In the iteration '+str(itr)+" of epoch"+str(epoch))
writer.add_summary(summary_str,epoch*int(self.n_samples/self.batch_size) + itr)
# After each epoch things
out_img_test = sess.run(self.gen_x,feed_dict={self.input_x:test_imgs})
out_img_test = np.reshape(out_img_test,[self.batch_size, self.img_width, self.img_height, self.img_depth])
imsave(self.images_dir+"/train/epoch_"+str(epoch)+".jpg", flat_batch(out_img_test,self.batch_size,10,10))
saver.save(sess,os.path.join(self.check_dir,"draw"),global_step=epoch)
writer.add_graph(sess.graph)
def test(self):
if not os.path.exists(self.images_dir+"/test/"):
os.makedirs(self.images_dir+"/test/")
self.model_setup()
saver = tf.train.Saver()
with tf.Session() as sess:
chkpt_fname = tf.train.latest_checkpoint(self.check_dir)
saver.restore(sess,chkpt_fname)
z_sample = np.random.normal(0, 1, [self.batch_size, self.steps, self.z_size])
gen_x_temp = sess.run(self.images_output)
for t in range(self.steps):
gen_x_temp[t] = np.reshape(gen_x_temp[t],[self.batch_size, self.img_width, self.img_height, self.img_depth])
gen_x_temp[t] = flat_batch(gen_x_temp[t],self.batch_size,10,10)
imsave(self.images_dir+"/test/output_draw_" + str(t) + ".jpg", gen_x_temp[t])
def main():
model = Draw()
model.initialize()
if(model.to_test):
model.test()
else:
model.train()
if __name__ == "__main__":
main()