-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
159 lines (131 loc) · 5.34 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
import datetime
import os
import os.path as osp
import shutil
from utils import label_accuracy_score, visualize_segmentation, get_tile_image
import numpy as np
import pytz
import scipy.misc
import tqdm
import tensorflow as tf
class Trainer:
def __init__(self, model, optimizer, train_dataset, val_dataset,
train_size, val_size, out, epochs, n_classes, val_epoch=None):
self.model = model
self.optim = optimizer
self.train_dataset = train_dataset
self.val_dataset = val_dataset
self.train_size = train_size
self.val_size = val_size
self.timestamp_start = \
datetime.datetime.now(pytz.timezone('UTC'))
self.val_epoch = val_epoch
self.out = out
if not osp.exists(self.out):
os.makedirs(self.out)
self.log_headers = [
'epoch',
'train/loss',
'train/acc',
'train/acc_cls',
'train/mean_iu',
'train/fwavacc',
'valid/loss',
'valid/acc',
'valid/acc_cls',
'valid/mean_iu',
'valid/fwavacc',
'elapsed_time',
]
if not osp.exists(osp.join(self.out, 'log.csv')):
with open(osp.join(self.out, 'log.csv'), 'w') as f:
f.write(','.join(self.log_headers) + '\n')
self.n_classes = n_classes
self.epoch = 0
self.epochs = epochs
self.best_mean_iu = 0
# @tf.function
def train_epoch(self):
epoch_loss_avg = tf.keras.metrics.Mean()
epoch_accuracy = tf.keras.metrics.CategoricalAccuracy()
cross_entropy = tf.losses.CategoricalCrossentropy()
metrics = []
for data, target in tqdm.tqdm(
self.train_dataset, total=self.train_size,
desc=f'Train epoch={self.epoch}', ncols=80, leave=False):
with tf.GradientTape() as tape:
score = self.model(data, training=True)
loss = cross_entropy(target, score)
gradients = tape.gradient(loss, self.model.trainable_variables)
self.optim.apply_gradients(zip(gradients, self.model.trainable_variables))
# epoch_loss_avg(target, score)
# epoch_accuracy(target, score)
target = target.numpy()
score = score.numpy()
acc, acc_cls, mean_iu, fwavacc = \
label_accuracy_score(
np.argmax(target, -1), np.argmax(score, -1), self.n_classes)
metrics.append((acc, acc_cls, mean_iu, fwavacc))
metrics = np.mean(metrics, axis=0)
with open(osp.join(self.out, 'log.csv'), 'a') as f:
elapsed_time = (
datetime.datetime.now(pytz.timezone('UTC')) -
self.timestamp_start).total_seconds()
log = [self.epoch] + [loss.numpy()] + \
metrics.tolist() + [''] * 5 + [elapsed_time]
log = map(str, log)
f.write(','.join(log) + '\n')
if self.epoch % self.val_epoch == 0:
self.validate()
# @tf.function
def validate(self):
global val_size
cross_entropy = tf.losses.CategoricalCrossentropy()
val_loss = 0
visualizations = []
label_trues, label_preds = [], []
for data, target in tqdm.tqdm(
self.val_dataset, total=self.val_size,
desc=f'Valid epoch={self.epoch}', ncols=80, leave=False):
score = self.model(data, training=False)
loss = cross_entropy(target, score)
val_loss += loss.numpy()
target = target.numpy()
score = score.numpy()
for img, lt, lp in zip(data, np.argmax(target, -1), np.argmax(score, -1)):
label_trues.append(lt)
label_preds.append(lp)
# if len(visualizations) < 9:
# viz = visualize_segmentation(
# lbl_pred=lp, lbl_true=lt, img=img,
# n_class=self.n_classes)
# visualizations.append(viz)
metrics = label_accuracy_score(
label_trues, label_preds, self.n_classes)
out = osp.join(self.out, 'visualization_viz')
if not osp.exists(out):
os.makedirs(out)
out_file = osp.join(out, 'epoch%08d.jpg' % self.epoch)
# scipy.misc.imsave(out_file, get_tile_image(visualizations))
val_loss /= self.val_size
with open(osp.join(self.out, 'log.csv'), 'a') as f:
elapsed_time = (
datetime.datetime.now(pytz.timezone('UTC')) -
self.timestamp_start).total_seconds()
log = [self.epoch] + [''] * 5 + \
[val_loss] + list(metrics) + [elapsed_time]
log = map(str, log)
f.write(','.join(log) + '\n')
mean_iu = metrics[2]
is_best = mean_iu > self.best_mean_iu
if is_best:
self.best_mean_iu = mean_iu
self.model.save(osp.join(self.out, 'best_model.h5'))
# if is_best:
# shutil.copy(osp.join(self.out, 'checkpoint.pth.tar'),
# osp.join(self.out, 'model_best.pth.tar'))
def train(self):
for epoch in tqdm.trange(self.epoch, self.epochs + 1,
desc='Train', ncols=80):
self.epoch = epoch
self.train_epoch()