-
Notifications
You must be signed in to change notification settings - Fork 5
/
base_train.py
49 lines (39 loc) · 1.84 KB
/
base_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
import tensorflow as tf
class BaseTrain:
def __init__(self, sess, model, data, config):
self.model = model
self.config = config
self.sess = sess
self.data = data
self.init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
self.sess.run(self.init)
self.summary_placeholders = {}
self.summary_ops = {}
self.summary_writer = tf.summary.FileWriter(self.config.summary_dir, self.sess.graph)
if self.config.load:
sess.model.load(self.sess)
def summarize(self, step, scope='train', summaries_dict=None, summaries_merged=None):
"""
Add the summaries to tensorboard
:param step:
:param summaries_dict:
:param summaries_merged:
:return:
"""
with tf.variable_scope(scope):
if summaries_dict is not None:
summary_list = []
for tag, value in summaries_dict.items():
if tag not in self.summary_ops:
self.summary_placeholders[tag] = tf.placeholder('float32', value.shape, name=tag)
if len(value.shape) <= 1:
self.summary_ops[tag] = tf.summary.scalar(tag, self.summary_placeholders[tag])
else:
self.summary_ops[tag] = tf.summary.image(tag, self.summary_placeholders[tag])
summary_list.append(self.sess.run(self.summary_ops[tag], {self.summary_placeholders[tag]: value}))
for summary in summary_list:
self.summary_writer.add_summary(summary, step)
self.summary_writer.flush()
if summaries_merged is not None:
self.summary_writer.add_summary(summaries_merged, step)
self.summary_writer.flush()