forked from isikdogan/deep_learning_tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
160 lines (128 loc) · 6.53 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
""" Coding Session 8: using a python iterator as a data generator and training a denoising autoencoder
"""
import tensorflow as tf
import numpy as np
import os, glob
import argparse
from nets.model import model
from datagenerator import DataGenerator #########################
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'
class TFModelTrainer:
def __init__(self, checkpoint_path):
self.checkpoint_path = checkpoint_path
# set training parameters
self.learning_rate = 0.01
self.num_iter = 100000
self.save_iter = 1000
self.val_iter = 1000
self.log_iter = 100
self.batch_size = 16
# set up data layer
self.image_size = (224, 224)
self.data_generator = DataGenerator(self.image_size)
def preprocess_image(self, image):
# normalize image to [-1, +1]
image = tf.cast(image, tf.float32)
image = image / 127.5
image = image - 1
return image
def _preprocess_images(self, image_orig, image_noisy):
image_orig = self.preprocess_image(image_orig)
image_noisy = self.preprocess_image(image_noisy)
return image_orig, image_noisy
def _data_layer(self, num_threads=8, prefetch_buffer=100):
with tf.variable_scope('data'):
data_shape = self.data_generator.get_tensor_shape() #########################
dataset = tf.data.Dataset.from_generator(lambda: self.data_generator,
(tf.float32, tf.float32),
(tf.TensorShape(data_shape),
tf.TensorShape(data_shape)))
dataset = dataset.map(self._preprocess_images, num_parallel_calls=num_threads)
dataset = dataset.batch(self.batch_size)
dataset = dataset.prefetch(prefetch_buffer)
iterator = dataset.make_initializable_iterator()
return iterator
def _loss_functions(self, preds, ground_truth):
with tf.name_scope('loss'):
tf.losses.mean_squared_error(ground_truth, preds) #########################
total_loss = tf.losses.get_total_loss()
return total_loss
def _optimizer(self, loss, global_step):
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, epsilon=0.1)
optimizer = optimizer.minimize(loss, global_step=global_step)
return optimizer
def train(self):
# iteration number
global_step = tf.Variable(1, dtype=tf.int32, trainable=False, name='iter_number')
# training graph
iterator = self._data_layer()
image_orig, image_noisy = iterator.get_next()
training = tf.placeholder(tf.bool, name='is_training')
logits = model(image_noisy, training=training)
loss = self._loss_functions(logits, image_orig)
optimizer = self._optimizer(loss, global_step)
# summary placeholders
streaming_loss_p = tf.placeholder(tf.float32)
validation_loss_p = tf.placeholder(tf.float32)
summ_op_train = tf.summary.scalar('streaming_loss', streaming_loss_p)
summ_op_test = tf.summary.scalar('validation_loss', validation_loss_p)
# don't allocate entire gpu memory
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
sess.run(tf.global_variables_initializer())
sess.run(iterator.initializer)
writer = tf.summary.FileWriter(self.checkpoint_path, sess.graph)
saver = tf.train.Saver(max_to_keep=None) # keep all checkpoints
ckpt = tf.train.get_checkpoint_state(self.checkpoint_path)
# resume training if a checkpoint exists
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
print('Loaded parameters from {}'.format(ckpt.model_checkpoint_path))
initial_step = global_step.eval()
# train the model
streaming_loss = 0
for i in range(initial_step, self.num_iter + 1):
_, loss_batch = sess.run([optimizer, loss], feed_dict={training: True})
if not np.isfinite(loss_batch):
print('loss diverged, stopping')
exit()
# log summary
streaming_loss += loss_batch
if i % self.log_iter == self.log_iter - 1:
streaming_loss /= self.log_iter
print(i + 1, streaming_loss)
summary_train = sess.run(summ_op_train, feed_dict={streaming_loss_p: streaming_loss})
writer.add_summary(summary_train, global_step=i)
streaming_loss = 0
# save model
if i % self.save_iter == self.save_iter - 1:
saver.save(sess, os.path.join(self.checkpoint_path, 'checkpoint'), global_step=global_step)
print("Model saved!")
# run validation
if i % self.val_iter == self.val_iter - 1:
print("Running validation.")
self.data_generator.set_mode(is_training=False)
sess.run(iterator.initializer)
validation_loss = 0
for j in range(self.data_generator.num_val // self.batch_size):
loss_batch = sess.run(loss, feed_dict={training: False})
validation_loss += loss_batch
validation_loss /= j
print("Validation loss: {}".format(validation_loss))
summary_test = sess.run(summ_op_test, feed_dict={validation_loss_p: validation_loss})
writer.add_summary(summary_test, global_step=i)
self.data_generator.set_mode(is_training=True)
sess.run(iterator.initializer)
writer.close()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--checkpoint_path', type=str, default='./checkpoints/',
help="Path to the dir where the checkpoints are saved")
args = parser.parse_args()
trainer = TFModelTrainer(args.checkpoint_path)
trainer.train()
if __name__ == '__main__':
main()