-
Notifications
You must be signed in to change notification settings - Fork 5
/
train.py
executable file
·147 lines (113 loc) · 5.4 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
141
142
143
144
import scipy.misc as misc
import time
import tensorflow as tf
from architecture import netD, netG
#from resnet import *
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=False,help='Batch size',type=int,default=128)
parser.add_argument('--NORM', required=False,help='Use layer normalization in D',type=int,default=0)
parser.add_argument('--SELU', required=False,help='Use SELU',type=int,default=0)
parser.add_argument('--SCALE', required=False,help='Scale of gradient penalty',type=int,default=10)
parser.add_argument('--MAX_STEPS', required=False,help='How long to train',type=int,default=100000)
a = parser.parse_args()
DATASET = a.DATASET
DATA_DIR = a.DATA_DIR
BATCH_SIZE = a.BATCH_SIZE
SCALE = a.SCALE
NORM = bool(a.NORM)
SELU = bool(a.SELU)
MAX_STEPS = a.MAX_STEPS
CHECKPOINT_DIR = 'checkpoints/DATASET_'+DATASET+'/SCALE_'+str(SCALE)+'/NORM_'+str(NORM)+'/SELU_'+str(SELU)+'/'
IMAGES_DIR = CHECKPOINT_DIR+'images/'
try: os.makedirs(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, SELU, NORM)
errD_fake = netD(gen_images, BATCH_SIZE, SELU, NORM, reuse=True)
# cost functions
errD = tf.reduce_mean(errD_real) - tf.reduce_mean(errD_fake)
errG = tf.reduce_mean(errD_fake)
# gradient penalty
epsilon = tf.random_uniform([], 0.0, 1.0)
x_hat = real_images*epsilon + (1-epsilon)*gen_images
d_hat = netD(x_hat, BATCH_SIZE, SELU, NORM, reuse=True)
gradients = tf.gradients(d_hat, x_hat)[0]
slopes = tf.sqrt(tf.reduce_sum(tf.square(gradients), reduction_indices=[1]))
gradient_penalty = 10*tf.reduce_mean((slopes-1.0)**2)
errD += gradient_penalty
# tensorboard summaries
tf.summary.scalar('d_loss', errD)
tf.summary.scalar('g_loss', errG)
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]
# optimize G
G_train_op = tf.train.AdamOptimizer(learning_rate=1e-4,beta1=0.0,beta2=0.9).minimize(errG, var_list=g_vars, global_step=global_step)
# optimize D
D_train_op = tf.train.AdamOptimizer(learning_rate=1e-4,beta1=0.0,beta2=0.9).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)
n_critic = 5
while step < MAX_STEPS:
start = time.time()
# 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})
# 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'