-
Notifications
You must be signed in to change notification settings - Fork 6
/
helper.py
197 lines (161 loc) · 6.8 KB
/
helper.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
import tensorflow as tf
import tensorflow.contrib.slim as slim
import os
import time
def getTimeString():
timeStamp = int(time.time())
timeArray = time.localtime(timeStamp)
timeString = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
return timeString
class Base:
def __init__(self, model, session, args, dataset):
self.model = model
self.session = session
self.args = args
self.dataset = dataset
self.init_base()
def init_base(self):
self.label_names = self.dataset.prepare_words_list
assert len(self.label_names) == self.args.num_classes
self._saver = None
@property
def saver(self):
if self._saver is None:
self._saver = tf.compat.v1.train.Saver(
var_list=self.model.model_variables, max_to_keep=self.args.max_to_keep)
return self._saver
def display_model(self):
list_v = slim.get_variables_to_restore()
sum_params = 0
for v in list_v:
params = 1
for dim in v.shape:
params *= dim
sum_params += params
print(v)
print('total parameter number: {}'.format(sum_params))
def routine_restore_and_initialize(self):
self.session.run(tf.compat.v1.global_variables_initializer())
self.session.run(
tf.compat.v1.local_variables_initializer()) # for metrics
if self.args.checkpoint_path:
self.saver.restore(self.session, self.args.checkpoint_path)
print('restore from {}'.format(self.args.checkpoint_path))
else:
print('from init model')
class Trainer(Base):
def __init__(self, model, session, args, dataset):
super().__init__(model, session, args, dataset)
self.display_model()
self.init_trainer()
self.routine_restore_and_initialize()
def init_trainer(self):
if not os.path.exists(self.args.save_folder):
os.makedirs(self.args.save_folder)
self.global_step_from_checkpoint = self.get_global_step_from_checkpoint(
self.args.checkpoint_path)
self.global_step = tf.Variable(
self.global_step_from_checkpoint, name="global_step", trainable=False)
self.boundaries = self.args.boundaries
self.learning_rate_placeholder = tf.compat.v1.train.piecewise_constant(
self.global_step, self.boundaries, self.args.lr_list
)
self.optimizer = self.build_optimizer(self.args.optimizer,
learning_rate=self.learning_rate_placeholder,
momentum=self.args.momentum)
self.train_op = self.build_train_op(total_loss=self.model.total_loss,
optimizer=self.optimizer,
global_step=self.global_step)
def get_global_step_from_checkpoint(self, checkpoint_path):
"""It is assumed that `checkpoint_path` is path to checkpoint file, not path to directory
with checkpoint files.
In case checkpoint path is not defined, 0 is returned."""
if checkpoint_path is None or checkpoint_path == "":
return 0
else:
if "-" in checkpoint_path:
return int(checkpoint_path.split("-")[-1])
else:
return 0
def build_optimizer(self, optimizer, learning_rate, momentum=None):
kwargs = {
"learning_rate": learning_rate
}
if optimizer == "gd":
opt = tf.train.GradientDescentOptimizer(**kwargs)
print("Use GradientDescentOptimizer")
elif optimizer == "adam":
opt = tf.compat.v1.train.AdamOptimizer(**kwargs)
print("Use AdamOptimizer")
elif optimizer == "mom":
if momentum:
kwargs["momentum"] = momentum
opt = tf.train.MomentumOptimizer(**kwargs)
print("Use MomentumOptimizer")
elif optimizer == "rmsprop":
opt = tf.train.RMSPropOptimizer(**kwargs)
print("Use RMSPropOptimizer")
else:
print("Unknown optimizer: {}".format(optimizer))
raise NotImplementedError
return opt
def build_train_op(self, total_loss, optimizer, global_step):
variables_to_train = tf.compat.v1.trainable_variables()
if variables_to_train:
train_op = slim.learning.create_train_op(
total_loss,
optimizer,
global_step=global_step,
variables_to_train=variables_to_train,
)
else:
print("Empty variables_to_train")
train_op = tf.no_op()
return train_op
def train(self):
print("Training started from {} step(s)".format(
self.global_step_from_checkpoint))
for global_step in range(self.global_step_from_checkpoint, self.args.steps):
# Session.Run!
fetch_vals = self.session.run(
{
'train_op': self.train_op,
'acc': self.model.acc,
'total_loss': self.model.total_loss,
'model_loss': self.model.model_loss,
}
)
# Logging
if (global_step+1) % self.args.step_logging == 0:
print("{} [{:5d}/{}], Acc: {:.4f}, total loss: {:.6f}, model loss: {:.6f}".format(
getTimeString(), global_step + 1,
self.args.steps, fetch_vals['acc'] * 100,
fetch_vals['total_loss'], fetch_vals['model_loss']
))
# Save
if (global_step+1) % self.args.step_save_checkpoint == 0:
self.saver.save(self.session, os.path.join(self.args.save_folder, self.args.arch),
global_step=global_step+1)
print("Training finished!")
class Evaluator(Base):
def __init__(self, model, session, args, dataset):
super().__init__(model, session, args, dataset)
self.display_model()
self.routine_restore_and_initialize()
def evaluate(self):
print("Evaluating started")
total_acc = 0
for global_step in range((self.dataset.num_samples-1) // self.args.batch_size + 1):
# Session.Run!
fetch_vals = self.session.run(
{
'logits': self.model.logits,
'acc': self.model.acc,
}
)
total_acc += fetch_vals['acc'] * len(fetch_vals['logits'])
total_acc /= self.dataset.num_samples
# Logging
print("checkpoint: {}, dataset: {}, Acc: {:.4f}\n".format(
self.args.checkpoint_path, self.args.dataset_name, 100 * total_acc))
print("Evaluating finished!")