-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtrainer.py
252 lines (198 loc) · 8.72 KB
/
trainer.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from six.moves import xrange
from util import log
from pprint import pprint
from model import Model
from input_ops import create_input_ops
import os
import time
import h5py
import tensorflow as tf
import tensorflow.contrib.slim as slim
import numpy as np
class Trainer(object):
def __init__(self,
config,
dataset_train,
dataset_test):
self.config = config
hyper_parameter_str = config.dataset+'_lr_'+str(config.learning_rate)
self.train_dir = './train_dir/%s-%s-%s' % (
config.prefix,
hyper_parameter_str,
time.strftime("%Y%m%d-%H%M%S")
)
if not os.path.exists(self.train_dir):
os.makedirs(self.train_dir)
log.infov("Train Dir: %s", self.train_dir)
# --- input ops ---
self.batch_size = config.batch_size
_, self.batch_train = create_input_ops(dataset_train, self.batch_size,
is_training=True)
_, self.batch_test = create_input_ops(dataset_test, self.batch_size,
is_training=False)
# --- create model ---
self.model = Model(config)
# --- optimizer ---
self.global_step = tf.contrib.framework.get_or_create_global_step(graph=None)
self.learning_rate = config.learning_rate
if config.lr_weight_decay:
self.learning_rate = tf.train.exponential_decay(
config.learning_rate,
global_step=self.global_step,
decay_steps=10000,
decay_rate=0.5,
staircase=True,
name='decaying_learning_rate'
)
self.check_op = tf.no_op()
# --- checkpoint and monitoring ---
log.warn("********* var ********** ")
slim.model_analyzer.analyze_vars(tf.trainable_variables(), print_info=True)
self.g_optimizer = tf.contrib.layers.optimize_loss(
loss=self.model.loss,
global_step=self.global_step,
learning_rate=self.learning_rate,
optimizer=tf.train.AdamOptimizer,
clip_gradients=20.0,
name='g_optimizer_loss',
)
self.summary_op = tf.summary.merge_all()
self.saver = tf.train.Saver(max_to_keep=1000)
self.summary_writer = tf.summary.FileWriter(self.train_dir)
self.checkpoint_secs = 600 # 10 min
self.supervisor = tf.train.Supervisor(
logdir=self.train_dir,
is_chief=True,
saver=None,
summary_op=None,
summary_writer=self.summary_writer,
save_summaries_secs=300,
save_model_secs=self.checkpoint_secs,
global_step=self.global_step,
)
session_config = tf.ConfigProto(
allow_soft_placement=True,
gpu_options=tf.GPUOptions(allow_growth=True),
device_count={'GPU': 1},
)
self.session = self.supervisor.prepare_or_wait_for_session(config=session_config)
self.ckpt_path = config.checkpoint
if self.ckpt_path is not None:
log.info("Checkpoint path: %s", self.ckpt_path)
self.saver.restore(self.session, self.ckpt_path)
log.info("Loaded the pretrain parameters from the provided checkpoint path")
def train(self, dataset):
log.infov("Training Starts!")
pprint(self.batch_train)
max_steps = 100000
output_save_step = 1000
for s in xrange(max_steps):
step, summary, x, loss, loss_g_update, loss_z_update, step_time = \
self.run_single_step(self.batch_train, dataset, step=s, is_train=True)
if s % 10 == 0:
self.log_step_message(step, loss, loss_g_update, loss_z_update, step_time)
self.summary_writer.add_summary(summary, global_step=step)
if s % output_save_step == 0:
log.infov("Saved checkpoint at %d", s)
save_path = self.saver.save(self.session,
os.path.join(self.train_dir, 'model'),
global_step=step)
if self.config.dump_result:
f = h5py.File(os.path.join(self.train_dir, 'dump_result_'+str(s)+'.hdf5'), 'w')
f['image'] = x
f.close()
def run_single_step(self, batch, dataset, step=None, is_train=True):
_start_time = time.time()
batch_chunk = self.session.run(batch)
# Optmize the generator {{{
# ========
fetch = [self.global_step, self.summary_op, self.model.loss,
self.model.x_recon, self.check_op, self.g_optimizer]
fetch_values = self.session.run(
fetch, feed_dict=self.model.get_feed_dict(batch_chunk, step=step)
)
[step, summary, loss, x] = fetch_values[:4]
# }}}
# Optimize the latent vectors {{{
fetch = [self.model.z, self.model.z_grad, self.model.loss]
fetch_values = self.session.run(
fetch, feed_dict=self.model.get_feed_dict(batch_chunk, step=step)
)
[z, z_grad, loss_g_update] = fetch_values
z_update = z - self.config.alpha * z_grad[0]
norm = np.sqrt(np.sum(z_update ** 2, axis=1))
z_update_norm = z_update / norm[:, np.newaxis]
loss_z_update = self.session.run(
self.model.loss, feed_dict={self.model.x: batch_chunk['image'], self.model.z: z_update_norm}
)
for i in range(len(batch_chunk['id'])):
dataset.set_data(batch_chunk['id'][i], z_update_norm[i, :])
# }}}
_end_time = time.time()
return step, summary, x, loss, loss_g_update, loss_z_update, (_end_time - _start_time)
def run_test(self, batch, is_train=False, repeat_times=8):
batch_chunk = self.session.run(batch)
loss = self.session.run(
self.model.loss, feed_dict=self.model.get_feed_dict(batch_chunk, is_training=False)
)
return loss
def log_step_message(self, step, loss, loss_g_update,
loss_z_update, step_time, is_train=True):
if step_time == 0:
step_time = 0.001
log_fn = (is_train and log.info or log.infov)
log_fn((" [{split_mode:5s} step {step:4d}] " +
"Loss: {loss:.5f} " +
"G update: {loss_g_update:.5f} " +
"Z update: {loss_z_update:.5f} " +
"({sec_per_batch:.3f} sec/batch, {instance_per_sec:.3f} instances/sec) "
).format(split_mode=(is_train and 'train' or 'val'),
step=step,
loss=loss,
loss_z_update=loss_z_update,
loss_g_update=loss_g_update,
sec_per_batch=step_time,
instance_per_sec=self.batch_size / step_time
)
)
def check_data_path(path):
if os.path.isfile(os.path.join(path, 'data.hy')) \
and os.path.isfile(os.path.join(path, 'id.txt')):
return True
else:
return False
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', type=int, default=16)
parser.add_argument('--prefix', type=str, default='default')
parser.add_argument('--checkpoint', type=str, default=None)
parser.add_argument('--dataset', type=str, default='MNIST', choices=['MNIST', 'SVHN', 'CIFAR10'])
parser.add_argument('--learning_rate', type=float, default=1e-4)
parser.add_argument('--alpha', type=float, default=1.0)
parser.add_argument('--lr_weight_decay', action='store_true', default=False)
parser.add_argument('--dump_result', action='store_true', default=False)
config = parser.parse_args()
if config.dataset == 'MNIST':
import datasets.mnist as dataset
elif config.dataset == 'SVHN':
import datasets.svhn as dataset
elif config.dataset == 'CIFAR10':
import datasets.cifar10 as dataset
else:
raise ValueError(config.dataset)
config.conv_info = dataset.get_conv_info()
config.deconv_info = dataset.get_deconv_info()
dataset_train, dataset_test = dataset.create_default_splits()
m, l = dataset_train.get_data(dataset_train.ids[0])
config.data_info = np.concatenate([np.asarray(m.shape), np.asarray(l.shape)])
trainer = Trainer(config,
dataset_train, dataset_test)
log.warning("dataset: %s, learning_rate: %f",
config.dataset, config.learning_rate)
trainer.train(dataset_train)
if __name__ == '__main__':
main()