-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
193 lines (160 loc) · 7.82 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
import numpy as np
import tensorflow as tf
import dynamic_fixed_point as dfxp
from random import randrange
def batch_generator(X, y, shuffle=True, batch_size=64):
n = X.shape[0]
n_batch = (n-1)//batch_size + 1
shuffle_idx = np.arange(n)
if shuffle:
shuffle_idx = np.random.permutation(shuffle_idx)
X = X[shuffle_idx]
y = y[shuffle_idx]
for batch in range(n_batch):
start = batch * batch_size
end = (batch+1) * batch_size
yield X[start:end], y[start:end], batch
def preprocess_image(image, label):
image = tf.image.random_flip_left_right(image)
image = tf.image.pad_to_bounding_box(image, 4, 4, 40, 40)
image = tf.random_crop(image, [32, 32, 3])
return image, label
class Trainer:
def __init__(self, model, dataset, logger, logdir,
lr=1e-2, lr_decay_factor=0.5, lr_decay_epoch=50,
momentum=0.95, n_epoch=5, batch_size=128):
self.model = model
self.model.backward()
self.n_epoch = n_epoch
self.batch_size = batch_size
(self.X_train, self.y_train), (self.X_test, self.y_test) = dataset
self.train_iterator, self.test_iterator = self.get_dataset_iterators(dataset)
self.logger = logger
self.logger.info('Model info:')
self.logger.info('\n' + model.info())
self.logger.info('Trainer info:')
self.logger.info('lr %f decay %f in %f epoch' % (
lr, lr_decay_factor, lr_decay_epoch))
self.logger.info('momentum %f' % momentum)
self.logger.info('training epoch %d' % n_epoch)
self.logger.info('batch_size %d' % batch_size)
self.logger.info('logdir %s' % logdir)
self.global_step = tf.train.get_or_create_global_step()
self.lr = lr
self.momentum = momentum
self.lr_decay_epoch = lr_decay_epoch
self.lr_decay_factor = lr_decay_factor
self.update_range_op = tf.get_collection('update_range')
# add summary
self.summary = tf.summary.merge_all()
# reduce memory usage
config = tf.ConfigProto()
config.gpu_options.allow_growth = True #pylint: disable=E1101
self.sess = tf.Session(config=config)
self.train_writer = tf.summary.FileWriter(logdir+'/train', self.sess.graph)
self.test_writer = tf.summary.FileWriter(logdir+'/test', self.sess.graph)
def init_model(self):
self.logger.info('Initializing model')
self.sess.run(tf.global_variables_initializer())
def get_train_op(self):
# This reset the optimizer variables after lr/momentum changes
optimizer = tf.train.MomentumOptimizer(learning_rate=self.lr, momentum=self.momentum)
train_op = optimizer.apply_gradients(self.model.grads_and_vars(), self.global_step)
self.sess.run(tf.variables_initializer(optimizer.variables()))
return train_op
def get_dataset_iterators(self, dataset):
(X_train, y_train), (X_test, y_test) = dataset
with tf.device('/cpu:0'):
self.X_train_placeholder = tf.placeholder(tf.float32, X_train.shape)
self.X_test_placeholder = tf.placeholder(tf.float32, X_test.shape)
self.y_train_placeholder = tf.placeholder(tf.int32, y_train.shape)
self.y_test_placeholder = tf.placeholder(tf.int32, y_test.shape)
train_dataset = tf.data.Dataset.from_tensor_slices((self.X_train_placeholder, self.y_train_placeholder))
train_dataset = (train_dataset.shuffle(buffer_size=X_train.shape[0])
.map(preprocess_image, num_parallel_calls=4)
.batch(self.batch_size)
.prefetch(1)
)
train_iterator = train_dataset.make_initializable_iterator()
test_dataset = tf.data.Dataset.from_tensor_slices(
(self.X_test_placeholder, self.y_test_placeholder)).batch(1000)
test_iterator = test_dataset.make_initializable_iterator()
return train_iterator, test_iterator
def train(self):
self.logger.info('Start of training')
next_train_op = self.train_iterator.get_next()
next_test_op = self.test_iterator.get_next()
train_iter_init_op = self.train_iterator.initializer
test_iter_init_op = self.test_iterator.initializer
for epoch in range(self.n_epoch):
if epoch == 0:
self.logger.info('New training optimizer with lr=%f' % self.lr)
train_op = self.get_train_op()
elif epoch == 80:
self.lr *= self.lr_decay_factor
self.logger.info('New training optimizer with lr=%f' % self.lr)
train_op = self.get_train_op()
elif epoch == 120:
self.lr *= self.lr_decay_factor
self.logger.info('New training optimizer with lr=%f' % self.lr)
train_op = self.get_train_op()
elif epoch == 140:
self.lr *= self.lr_decay_factor
self.logger.info('New training optimizer with lr=%f' % self.lr)
train_op = self.get_train_op()
# if epoch % self.lr_decay_epoch == 0:
# self.logger.info('New training optimizer with lr=%f' % self.lr)
# train_op = self.get_train_op()
# self.lr *= self.lr_decay_factor
self.sess.run([self.model.set_training, train_iter_init_op], feed_dict={
self.X_train_placeholder: self.X_train,
self.y_train_placeholder: self.y_train,
})
b = 0
while True:
try:
X, y = self.sess.run(next_train_op)
b += 1
if b % 100 == 0:
_, _, loss, acc, summary, step = self.sess.run([train_op, self.update_range_op,
self.model.loss, self.model.accuracy, self.summary, self.global_step,
# dfxp.pre_dense_op,
],
feed_dict={self.model.input_X: X, self.model.input_y: y})
self.train_writer.add_summary(summary, step)
self.logger.info('Batch %d loss %f acc %f' % (b, loss, acc))
else:
self.sess.run([train_op, self.update_range_op,
# dfxp.pre_dense_op,
# dfxp.print_op,
], feed_dict={self.model.input_X: X, self.model.input_y: y})
except tf.errors.OutOfRangeError:
break
# TODO BatchNorm bug
# self.sess.run(self.model.set_testing)
self.sess.run(test_iter_init_op, feed_dict={
self.X_test_placeholder: self.X_test,
self.y_test_placeholder: self.y_test,
})
test_acc = 0
test_loss = 0
batch_cnt = 0
while True:
try:
X, y = self.sess.run(next_test_op)
batch_cnt += 1
acc, loss, summary, step = self.sess.run([self.model.accuracy, self.model.loss,
self.summary, self.global_step],
feed_dict={self.model.input_X: X, self.model.input_y: y})
test_acc += acc
test_loss += loss
self.test_writer.add_summary(summary, step+batch_cnt*50) # some trick
except tf.errors.OutOfRangeError:
break
test_acc /= batch_cnt
test_loss /= batch_cnt
self.logger.info('Epoch %d test accuracy %f' % (epoch+1, test_acc))
def save_model(self, exp_path):
self.logger.info('Saving model')
saver = tf.train.Saver()
saver.save(self.sess, exp_path+'/model.ckpt')