forked from cameronfabbri/Wasserstein-GAN-Tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·143 lines (110 loc) · 4.9 KB
/
train.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
import scipy.misc as misc
import time
import tensorflow as tf
from architecture import netD, netG
import numpy as np
import random
import ntpath
import sys
import cv2
import os
from skimage import color
import argparse
import data_ops
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--DATASET', required=True,help='The DATASET to use')
parser.add_argument('--DATA_DIR', required=True,help='Directory where data is')
parser.add_argument('--BATCH_SIZE', required=True,help='Batch size',type=int)
a = parser.parse_args()
DATASET = a.DATASET
DATA_DIR = a.DATA_DIR
BATCH_SIZE = a.BATCH_SIZE
CHECKPOINT_DIR = 'checkpoints/'+DATASET+'/'
IMAGES_DIR = CHECKPOINT_DIR+'images/'
try: os.mkdir('checkpoints/')
except: pass
try: os.mkdir(CHECKPOINT_DIR)
except: pass
try: os.mkdir(IMAGES_DIR)
except: pass
# placeholders for data going into the network
global_step = tf.Variable(0, name='global_step', trainable=False)
z = tf.placeholder(tf.float32, shape=(BATCH_SIZE, 100), name='z')
train_images_list = data_ops.loadData(DATA_DIR, DATASET)
filename_queue = tf.train.string_input_producer(train_images_list)
real_images = data_ops.read_input_queue(filename_queue, BATCH_SIZE)
# generated images
gen_images = netG(z, BATCH_SIZE)
# get the output from D on the real and fake data
errD_real = netD(real_images, BATCH_SIZE)
errD_fake = netD(gen_images, BATCH_SIZE, reuse=True)
# cost functions
errD = tf.reduce_mean(errD_real - errD_fake)
errG = tf.reduce_mean(errD_fake)
# tensorboard summaries
tf.summary.scalar('d_loss', errD)
tf.summary.scalar('g_loss', errG)
#tf.summary.image('real_images', real_images, max_outputs=BATCH_SIZE)
#tf.summary.image('generated_images', gen_images, max_outputs=BATCH_SIZE)
merged_summary_op = tf.summary.merge_all()
# get all trainable variables, and split by network G and network D
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]
# clip weights in D
clip_values = [-0.005, 0.005]
clip_discriminator_var_op = [var.assign(tf.clip_by_value(var, clip_values[0], clip_values[1])) for
var in d_vars]
# optimize G
G_train_op = tf.train.RMSPropOptimizer(learning_rate=0.00005).minimize(errG, var_list=g_vars, global_step=global_step)
# optimize D
D_train_op = tf.train.RMSPropOptimizer(learning_rate=0.00005).minimize(errD, var_list=d_vars)
saver = tf.train.Saver(max_to_keep=1)
init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
sess = tf.Session()
sess.run(init)
summary_writer = tf.summary.FileWriter(CHECKPOINT_DIR+'/'+'logs/', graph=tf.get_default_graph())
tf.add_to_collection('G_train_op', G_train_op)
tf.add_to_collection('D_train_op', D_train_op)
# restore previous model if there is one
ckpt = tf.train.get_checkpoint_state(CHECKPOINT_DIR)
if ckpt and ckpt.model_checkpoint_path:
print "Restoring previous model..."
try:
saver.restore(sess, ckpt.model_checkpoint_path)
print "Model restored"
except:
print "Could not restore model"
pass
########################################### training portion
step = sess.run(global_step)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess, coord=coord)
while True:
start = time.time()
# get the discriminator properly trained at the start
if step < 25 or step % 500 == 0:
n_critic = 100
else: n_critic = 5
# train the discriminator for 5 or 25 runs
for critic_itr in range(n_critic):
batch_z = np.random.normal(-1.0, 1.0, size=[BATCH_SIZE, 100]).astype(np.float32)
sess.run(D_train_op, feed_dict={z:batch_z})
sess.run(clip_discriminator_var_op)
# now train the generator once! use normal distribution, not uniform!!
batch_z = np.random.normal(-1.0, 1.0, size=[BATCH_SIZE, 100]).astype(np.float32)
sess.run(G_train_op, feed_dict={z:batch_z})
# now get all losses and summary *without* performing a training step - for tensorboard
D_loss, G_loss, summary = sess.run([errD, errG, merged_summary_op], feed_dict={z:batch_z})
summary_writer.add_summary(summary, step)
print 'step:',step,'D loss:',D_loss,'G_loss:',G_loss,'time:',time.time()-start
step += 1
if step%500 == 0:
print 'Saving model...'
saver.save(sess, CHECKPOINT_DIR+'checkpoint-'+str(step))
saver.export_meta_graph(CHECKPOINT_DIR+'checkpoint-'+str(step)+'.meta')
batch_z = np.random.normal(-1.0, 1.0, size=[BATCH_SIZE, 100]).astype(np.float32)
gen_imgs = sess.run([gen_images], feed_dict={z:batch_z})
data_ops.saveImage(gen_imgs[0], step, IMAGES_DIR)
print 'Done saving'