-
Notifications
You must be signed in to change notification settings - Fork 2
/
callbacks.py
318 lines (269 loc) · 11.8 KB
/
callbacks.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import sys
import time
import os
import warnings
import numpy as np
import matplotlib
matplotlib.use('Agg')
from matplotlib import pylab as plt
from torch.optim.optimizer import Optimizer
import logging
###############################################################################
# TRAINING CALLBACKS
###############################################################################
class PlotLearning(object):
def __init__(self, save_path, num_classes):
self.accuracy = []
self.accuracy5 = []
self.val_accuracy = []
self.val_accuracy5 = []
self.losses = []
self.val_losses = []
self.learning_rates = []
self.save_path_loss = os.path.join(save_path, 'loss_plot.png')
self.save_path_accu = os.path.join(save_path, 'accu_plot.png')
self.save_path_accu5 = os.path.join(save_path, 'accu5_plot.png')
self.save_path_lr = os.path.join(save_path, 'lr_plot.png')
self.init_loss = -np.log(1.0 / num_classes)
def plot(self, logs):
self.accuracy.append(logs.get('acc'))
self.val_accuracy.append(logs.get('val_acc'))
self.accuracy5.append(logs.get('acc5'))
self.val_accuracy5.append(logs.get('val_acc5'))
self.losses.append(logs.get('loss'))
self.val_losses.append(logs.get('val_loss'))
self.learning_rates.append(logs.get('learning_rate'))
# Accuracy@1
best_val_acc = max(self.val_accuracy)
best_train_acc = max(self.accuracy)
best_val_epoch = self.val_accuracy.index(best_val_acc)
best_train_epoch = self.accuracy.index(best_train_acc)
plt.figure(1)
plt.gca().cla()
plt.ylim(0, 1)
plt.plot(self.accuracy, label='train')
plt.plot(self.val_accuracy, label='valid')
plt.title("best_val@{0:}-{1:.2f}, best_train@{2:}-{3:.2f}".format(
best_val_epoch, best_val_acc, best_train_epoch, best_train_acc))
plt.legend()
plt.savefig(self.save_path_accu)
np.save(self.save_path_accu.replace('_plot.png', ''), self.accuracy)
best_val_loss = min(self.val_losses)
best_train_loss = min(self.losses)
best_val_epoch = self.val_losses.index(best_val_loss)
best_train_epoch = self.losses.index(best_train_loss)
plt.figure(2)
plt.gca().cla()
plt.ylim(0, self.init_loss)
plt.plot(self.losses, label='train')
plt.plot(self.val_losses, label='valid')
plt.title("best_val@{0:}-{1:.2f}, best_train@{2:}-{3:.2f}".format(
best_val_epoch, best_val_loss, best_train_epoch, best_train_loss))
plt.legend()
plt.savefig(self.save_path_loss)
np.save(self.save_path_loss.replace('_plot.png', ''), self.losses)
min_learning_rate = min(self.learning_rates)
max_learning_rate = max(self.learning_rates)
plt.figure(2)
plt.gca().cla()
plt.ylim(0, max_learning_rate)
plt.plot(self.learning_rates)
plt.title("max_learning_rate-{0:.6f}, min_learning_rate-{1:.6f}".format(max_learning_rate, min_learning_rate))
plt.savefig(self.save_path_lr)
# Accuracy@5
best_val_acc5 = max(self.val_accuracy5)
best_train_acc5 = max(self.accuracy5)
best_val_epoch5 = self.val_accuracy5.index(best_val_acc5)
best_train_epoch5 = self.accuracy5.index(best_train_acc5)
plt.figure(3)
plt.gca().cla()
plt.ylim(0, 1)
plt.plot(self.accuracy5, label='train')
plt.plot(self.val_accuracy5, label='valid')
plt.title("best_val@{0:}-{1:.2f}, best_train@{2:}-{3:.2f}".format(
best_val_epoch5, best_val_acc5, best_train_epoch5, best_train_acc5))
plt.legend()
plt.savefig(self.save_path_accu5)
np.save(self.save_path_accu5.replace('_plot5.png', ''), self.accuracy5)
# Taken from keras.keras.utils.generic_utils
class Progbar(object):
"""Displays a progress bar.
# Arguments
target: Total number of steps expected.
interval: Minimum visual progress update interval (in seconds).
"""
def __init__(self, target, width=30, verbose=1, interval=0.05):
self.width = width
self.target = target
self.sum_values = {}
self.unique_values = []
self.start = time.time()
self.last_update = 0
self.interval = interval
self.total_width = 0
self.seen_so_far = 0
self.verbose = verbose
def update(self, current, values=None, force=False):
"""Updates the progress bar.
# Arguments
current: Index of current step.
values: List of tuples (name, value_for_last_step).
The progress bar will display averages for these values.
force: Whether to force visual progress update.
"""
values = values or []
for k, v in values:
if k not in self.sum_values:
self.sum_values[k] = [v * (current - self.seen_so_far),
current - self.seen_so_far]
self.unique_values.append(k)
else:
self.sum_values[k][0] += v * (current - self.seen_so_far)
self.sum_values[k][1] += (current - self.seen_so_far)
self.seen_so_far = current
now = time.time()
if self.verbose == 1:
if not force and (now - self.last_update) < self.interval:
return
prev_total_width = self.total_width
sys.stdout.write('\b' * prev_total_width)
sys.stdout.write('\r')
numdigits = int(np.floor(np.log10(self.target))) + 1
barstr = '%%%dd/%%%dd [' % (numdigits, numdigits)
bar = barstr % (current, self.target)
prog = float(current) / self.target
prog_width = int(self.width * prog)
if prog_width > 0:
bar += ('=' * (prog_width - 1))
if current < self.target:
bar += '>'
else:
bar += '='
bar += ('.' * (self.width - prog_width))
bar += ']'
sys.stdout.write(bar)
self.total_width = len(bar)
if current:
time_per_unit = (now - self.start) / current
else:
time_per_unit = 0
eta = time_per_unit * (self.target - current)
info = ''
if current < self.target:
info += ' - ETA: %ds' % eta
else:
info += ' - %ds' % (now - self.start)
for k in self.unique_values:
info += ' - %s:' % k
if isinstance(self.sum_values[k], list):
avg = self.sum_values[k][0] / max(1, self.sum_values[k][1])
if abs(avg) > 1e-3:
info += ' %.4f' % avg
else:
info += ' %.4e' % avg
else:
info += ' %s' % self.sum_values[k]
self.total_width += len(info)
if prev_total_width > self.total_width:
info += ((prev_total_width - self.total_width) * ' ')
sys.stdout.write(info)
sys.stdout.flush()
if current >= self.target:
sys.stdout.write('\n')
if self.verbose == 2:
if current >= self.target:
info = '%ds' % (now - self.start)
for k in self.unique_values:
info += ' - %s:' % k
avg = self.sum_values[k][0] / max(1, self.sum_values[k][1])
if avg > 1e-3:
info += ' %.4f' % avg
else:
info += ' %.4e' % avg
sys.stdout.write(info + "\n")
self.last_update = now
def add(self, n, values=None):
self.update(self.seen_so_far + n, values)
# Taken from PyTorch's examples.imagenet.main
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
import os
from tensorboardX import SummaryWriter
import numpy as np
class Logger:
def __init__(self, log_dir, n_logged_samples=10, summary_writer=SummaryWriter):
self._log_dir = log_dir
print('########################')
print('logging outputs to ', log_dir)
print('########################')
self._n_logged_samples = n_logged_samples
self._summ_writer = summary_writer(log_dir, flush_secs=1, max_queue=1)
log = logging.getLogger(log_dir)
if not log.handlers:
log.setLevel(logging.DEBUG)
if not os.path.exists(log_dir):
os.mkdir(log_dir)
fh = logging.FileHandler(os.path.join(log_dir, 'log.txt'))
fh.setLevel(logging.INFO)
formatter = logging.Formatter(fmt='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S')
fh.setFormatter(formatter)
log.addHandler(fh)
self.log = log
def log_scalar(self, scalar, name, step_):
self._summ_writer.add_scalar('{}'.format(name), scalar, step_)
def log_scalars(self, scalar_dict, group_name, step, phase):
"""Will log all scalars in the same plot."""
self._summ_writer.add_scalars('{}_{}'.format(group_name, phase), scalar_dict, step)
def log_image(self, image, name, step):
assert (len(image.shape) == 3) # [C, H, W]
self._summ_writer.add_image('{}'.format(name), image, step)
def log_video(self, video_frames, name, step, fps=10):
assert len(video_frames.shape) == 5, "Need [N, T, C, H, W] input tensor for video logging!"
self._summ_writer.add_video('{}'.format(name), video_frames, step, fps=fps)
def log_paths_as_videos(self, paths, step, max_videos_to_save=2, fps=10, video_title='video'):
# reshape the rollouts
videos = [np.transpose(p['image_obs'], [0, 3, 1, 2]) for p in paths]
# max rollout length
max_videos_to_save = np.min([max_videos_to_save, len(videos)])
max_length = videos[0].shape[0]
for i in range(max_videos_to_save):
if videos[i].shape[0] > max_length:
max_length = videos[i].shape[0]
# pad rollouts to all be same length
for i in range(max_videos_to_save):
if videos[i].shape[0] < max_length:
padding = np.tile([videos[i][-1]], (max_length - videos[i].shape[0], 1, 1, 1))
videos[i] = np.concatenate([videos[i], padding], 0)
# log videos to tensorboard event file
videos = np.stack(videos[:max_videos_to_save], 0)
self.log_video(videos, video_title, step, fps=fps)
def log_figures(self, figure, name, step, phase):
"""figure: matplotlib.pyplot figure handle"""
assert figure.shape[0] > 0, "Figure logging requires input shape [batch x figures]!"
self._summ_writer.add_figure('{}_{}'.format(name, phase), figure, step)
def log_figure(self, figure, name, step, phase):
"""figure: matplotlib.pyplot figure handle"""
self._summ_writer.add_figure('{}_{}'.format(name, phase), figure, step)
def log_graph(self, array, name, step, phase):
"""figure: matplotlib.pyplot figure handle"""
im = plot_graph(array)
self._summ_writer.add_image('{}_{}'.format(name, phase), im, step)
def dump_scalars(self, log_path=None):
log_path = os.path.join(self._log_dir, "scalar_data.json") if log_path is None else log_path
self._summ_writer.export_scalars_to_json(log_path)
def flush(self):
self._summ_writer.flush()
def log_info(self, info):
self.log.info("{}".format(info))