-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrainer.py
223 lines (176 loc) · 8.66 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
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
import os
from time import time
import torch
import numpy as np
from tqdm import tqdm
from utils import TranscriptEncoder, classes
class Train:
"""
Trainer class which defines model training and evaluation methods.
"""
def __init__(self, model, train_iterator, valid_iterator, loss, metric, optimizer, lr_scheduler, config):
super().__init__()
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
self.model = model
self.train_iterator = train_iterator
self.valid_iterator = valid_iterator
self.optimizer = optimizer
self.lr_scheduler = lr_scheduler
self.transcript_encoder = TranscriptEncoder(classes)
self.metric = metric
self.epochs = config["epochs"]
self.loss = loss
self.config = config
self.model.to(self.device)
def _eval_metrics(self, y_pred, y_true):
"""
Calculate evaluation metrics given predictions and ground truths.
"""
precious, recall, hmean = self.metric(y_pred, y_true)
return np.array([precious, recall, hmean])
def train_epoch(self, epoch):
"""Train a single epoch."""
self.model.train()
epoch_loss, total_metrics = 0, np.zeros(3)
for i, batch in tqdm(enumerate(self.train_iterator), total=len(self.train_iterator), position=0, leave=True):
# image_paths, images, bboxs, transcripts, score_map, geo_map, mapping = batch
images, score_map, geo_map, training_mask = batch
images = images.to(self.device)
score_map = score_map.to(self.device)
geo_map = geo_map.to(self.device)
training_mask = training_mask.to(self.device)
self.optimizer.zero_grad()
# Forward pass
# pred_score_map, pred_geo_map, pred_recog, pred_boxes, pred_mapping, indices = self.model(images, bboxs, mapping)
# Forward pass
pred_score_map, pred_geo_map = self.model(images)
# Calculate loss
loss = self.loss(score_map, pred_score_map, geo_map, pred_geo_map, training_mask)
# Backward pass
loss.backward()
self.optimizer.step()
epoch_loss += loss.item()
# transcripts = transcripts[indices]
# pred_boxes = pred_boxes[indices]
# pred_mapping = mapping[indices]
# pred_fns = [image_paths[i] for i in pred_mapping]
# labels, label_lengths = self.transcript_encoder.encode(transcripts.tolist())
# labels, label_lengths = labels.to(self.device), label_lengths.to(self.device)
# recog = (labels, label_lengths)
# # Calculate loss
# loss = self.loss(score_map, pred_score_map, geo_map, pred_geo_map, recog, pred_recog)
# # Backward pass
# loss.backward()
# self.optimizer.step()
# epoch_loss += loss.item()
# print(f'### LOSS: {loss.item()}')
# pred_transcripts = []
# if len(pred_mapping) > 0:
# pred, lengths = pred_recog
# _, pred = pred.max(2)
# for idx in range(lengths.numel()):
# l = lengths[idx]
# p = pred[:l, idx]
# txt = self.transcript_encoder.decode(p, l)
# pred_transcripts.append(txt)
# pred_transcripts = np.array(pred_transcripts)
# total_metrics += self._eval_metrics(
# (pred_boxes, pred_transcripts, pred_fns),
# (bboxs, transcripts, pred_fns)
# )
return (
epoch_loss / len(self.train_iterator)
)
# return (
# epoch_loss / len(self.train_iterator),
# total_metrics[0] / len(self.train_iterator), # precision
# total_metrics[1] / len(self.train_iterator), # recall
# total_metrics[2] / len(self.train_iterator) # f1-score
# )
def eval_epoch(self):
"""Validate after training a single epoch."""
self.model.eval()
# total_metrics = np.zeros(3)
val_loss = 0
with torch.no_grad():
for i, batch in tqdm(enumerate(self.valid_iterator), total=len(self.valid_iterator), position=0, leave=True):
# image_paths, images, bboxs, transcripts, score_map, geo_map, mapping = batch
images, score_map, geo_map, training_mask = batch
images = images.to(self.device)
score_map = score_map.to(self.device)
geo_map = geo_map.to(self.device)
training_mask = training_mask.to(self.device)
# Forward pass
# pred_score_map, pred_geo_map, pred_recog, pred_boxes, pred_mapping, indices = self.model(images, bboxs, mapping)
# Forward pass
pred_score_map, pred_geo_map = self.model(images)
# Calculate loss
val_loss += self.loss(score_map, pred_score_map, geo_map, pred_geo_map, training_mask)
# pred_transcripts = []
# pred_fns = []
# if len(pred_mapping) > 0:
# pred_mapping = pred_mapping[indices]
# pred_boxes = pred_boxes[indices]
# pred_fns = [image_paths[i] for i in pred_mapping]
# pred, lengths = pred_recog
# _, pred = pred.max(2)
# for i in range(lengths.numel()):
# l = lengths[i]
# p = pred[:l, i]
# t = self.transcript_encoder.decode(p, l)
# pred_transcripts.append(t)
# pred_transcripts = np.array(pred_transcripts)
# gt_fns = [image_paths[i] for i in mapping]
# total_metrics += self._eval_metrics((pred_boxes, pred_transcripts, pred_fns),
# (bboxs, transcripts, gt_fns))
return val_loss / len(self.valid_iterator)
# return (
# total_metrics[0] / len(self.train_iterator), # precision
# total_metrics[1] / len(self.train_iterator), # recall
# total_metrics[2] / len(self.train_iterator) # f1-score
# )
@staticmethod
def epoch_time(start_time, end_time):
"""Measure single epoch time based on epoch's start and end times."""
elapsed_time = end_time - start_time
elapsed_mins = int(elapsed_time / 60)
elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
return elapsed_mins, elapsed_secs
def _save_model(self, name, model):
"""Save the given model at given path."""
if not os.path.isdir(self.config["model_save_path"]):
os.makedirs(self.config["model_save_path"], exist_ok=True)
torch.save(
model.state_dict(),
os.path.join(self.config["model_save_path"], name)
)
def train(self):
"""Train the model for given numner of epochs."""
best_val_loss = float('inf')
for epoch in range(self.epochs):
# Epoch start time
start_time = time()
# Train
# loss, train_precision, train_recall, train_f1 = self.train_epoch(epoch)
train_loss = self.train_epoch(epoch)
# Evaluate
# val_precision, val_recall, val_f1 =
val_loss = self.eval_epoch()
self.lr_scheduler.step(val_loss)
# Epoch start time
end_time = time()
epoch_mins, epoch_secs = self.epoch_time(start_time, end_time)
# Save the model when loss improves and at last epoch
if val_loss < best_val_loss:
print(f"Loss reduced from previous best {best_val_loss} to {val_loss}. Saving the model!")
self._save_model(f"FOTS_epoch{epoch+1}.pt", self.model)
best_val_loss = val_loss
if epoch+1 == self.epochs:
self._save_model(f"FOTS_epoch{epoch+1}.pt", self.model)
# Log the training progress per epoch
# print(f'Epoch: {epoch+1:02} | Time: {epoch_mins}m {epoch_secs}s')
# print(f'\t Train Loss: {loss:.3f} | Train Precision: {train_precision:7.3f} | Train Recall: {train_recall:7.3f} | Train F1: {train_f1:7.3f}')
# print(f'\t Val. Precision: {val_precision:7.3f} | Val. Recall: {val_recall:7.3f} | Val F1: {val_f1:7.3f}')
print(f'Epoch: {epoch+1:02} | Time: {epoch_mins}m {epoch_secs}s')
print(f'\t Train Loss: {train_loss:.3f}')
print(f'\t Val. Loss: {val_loss:.3f}\n')