-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrainer.py
231 lines (194 loc) · 10.2 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
224
225
226
227
228
229
230
231
import datetime
import json
from collections import defaultdict
import numpy as np
import pandas as pd
import torch
from tqdm import tqdm
from evaluation.evaluate import evaluate_model
from evaluation.metrics import compile_metrics
from paths import *
from utils import save_evaluation_results_for_args
__all__ = ['NIMSTrainer']
class NIMSTrainer:
"""
Provides functionality regarding training including save/loading models and experiment configurations.
"""
def __init__(self, model, criterion, dice_criterion, optimizer, scheduler, device, train_loader, valid_loader, test_loader,
experiment_name, args, normalization=None):
self.args = args
self.model = model
self.model_name = args.model
self.criterion = criterion
self.dice_criterion = dice_criterion
self.optimizer = optimizer
self.scheduler = scheduler
self.device = device
self.num_epochs = args.num_epochs
self.num_classes = args.num_classes
self.train_loader = train_loader
self.valid_loader = valid_loader
self.test_loader = test_loader
self.input_data = args.input_data
self.reference = args.reference
self.experiment_name = experiment_name
self.normalization = normalization
self.rain_thresholds = args.rain_thresholds
self.trained_weight = {'model': None,
'model_name': args.model,
'n_blocks': args.n_blocks, # U-Net related
'start_channels': args.start_channels, # U-Net related
'no_residual': args.no_residual, # U-Net related
'no_skip': args.no_skip, # U-Net related
'use_tte': args.use_tte, # U-Net related
'num_layers': args.num_layers, # ConvLSTM related
'hidden_dim': args.hidden_dim, # ConvLSTM related
'kernel_size': args.kernel_size, # ConvLSTM related
'start_dim': args.start_dim, # MetNet related
'window_size': args.window_size,
'model_utc': args.model_utc,
'dry_sampling_rate': args.dry_sampling_rate,
'global_sampling_rate': args.global_sampling_rate,
'num_epochs': args.num_epochs,
'batch_size': args.batch_size,
'optimizer': args.optimizer,
'lr': args.lr,
'wd': args.wd,
'custom_name': args.custom_name,
'rain_thresholds': args.rain_thresholds,
'num_classes': args.num_classes,
'norm_max': normalization['max_values'] if normalization else None,
'norm_min': normalization['min_values'] if normalization else None}
self.device_idx = int(args.device)
self.model.to(self.device)
# Hotfix - intermediate evaluation on test set
self.intermediate_test = args.intermediate_test
if args.intermediate_test:
print("Intermediate evaluation on test set is enabled")
else:
print("Omitting intermediate evaluation on test set")
self.log_dict = {
'complete': False,
'history': [],
}
self.log_json_path = get_train_log_json_path(self.experiment_name, makedirs=True)
self.log_txt_path = get_train_log_txt_path(self.experiment_name, makedirs=True)
self.history_path = get_train_log_history_path(self.experiment_name, makedirs=True)
def save_trained_weight(self, epoch: int = None, step: int = None):
"""
Save trained weights and experiment configurations to appropriate path.
"""
if sum([epoch is not None, step is not None]) != 1:
raise ValueError('Only one of `epoch` or `step` must be specified to save model')
trained_weight_path = get_trained_model_path(self.experiment_name, epoch=epoch, step=step, makedirs=True)
if os.path.isfile(trained_weight_path):
os.remove(trained_weight_path)
torch.save(self.trained_weight, trained_weight_path)
def train(self):
"""
Train the model by `self.num_epochs`.
"""
self.model.train()
for epoch in range(1, self.num_epochs + 1):
epoch_log_dict = dict()
epoch_log_dict['epoch'] = epoch
epoch_log = 'Epoch {:3d} / {:3d} (GPU {})'.format(epoch, self.num_epochs, self.device_idx)
self._log(epoch_log.center(40).center(80, '='))
# Run training epoch
loss, confusion, metrics = self._epoch(self.train_loader, mode='train')
correct = confusion[np.diag_indices_from(confusion)].sum()
accuracy = (correct / confusion.sum()).item()
epoch_log_dict['loss'] = loss
epoch_log_dict['accuracy'] = accuracy
# Save model
self.trained_weight['model'] = self.model.state_dict()
self.save_trained_weight(epoch=epoch, step=None)
# Save/log train evaluation metrics
summary = save_evaluation_results_for_args(confusion, metrics, epoch, self.args, "train", loss=loss)
self._log('Train Metrics '.center(40).center(80, '#'))
self._log(summary)
self.log_dict['history'].append(epoch_log_dict)
# Validation evaluation
_, loss, confusion, metrics = evaluate_model(self.model, self.valid_loader, self.args.rain_thresholds,
self.criterion, self.device, self.normalization)
summary = save_evaluation_results_for_args(confusion, metrics, epoch, self.args, "val", loss=loss)
self._log('Validation Metrics '.center(40).center(80, '#'))
self._log(summary)
# Test evaluation
_, loss, confusion, metrics = evaluate_model(self.model, self.test_loader, self.args.rain_thresholds,
self.criterion, self.device, self.normalization)
summary = save_evaluation_results_for_args(confusion, metrics, epoch, self.args, "test", loss=loss)
self._log('Test Metrics '.center(40).center(80, '#'))
self._log(summary)
with open(self.log_json_path, 'w') as f:
json.dump(self.log_dict, f)
history = pd.DataFrame(self.log_dict['history'])
history.to_csv(self.history_path)
if self.scheduler:
self.scheduler.step()
# Save history
self.log_dict['complete'] = True
with open(self.log_json_path, 'w') as f:
json.dump(self.log_dict, f)
print('Log files saved to the following paths:')
print(self.log_txt_path)
print(self.log_json_path)
print(self.history_path)
def _log(self, text):
print(text)
with open(self.log_txt_path, 'a') as f:
f.write(text + '\n')
def _epoch(self, data_loader, mode):
"""
Run a single epoch of inference or training (`mode="train"`) based on the supplied `mode`.
:param data_loader:
:param mode: "train" | "eval"
"""
pbar = tqdm(data_loader)
total_loss = 0
total_samples = 0
confusion_matrix = np.zeros((self.num_classes, self.num_classes), dtype=np.int)
metrics_by_threshold = defaultdict(list) # metrics_by_threshold[threshold][step]: DataFrame
for i, (images, target, t) in enumerate(pbar):
# Note, StandardDataset retrieves timestamps in Tensor format due to collation issue, for now
timestamps = []
for e in t:
origin = datetime(year=e[0], month=e[1], day=e[2], hour=e[3])
lead_time = e[4].item()
timestamps.append((origin, lead_time))
# Apply normalizations
if self.normalization:
with torch.no_grad():
for i, (max_val, min_val) in enumerate(zip(self.normalization['max_values'],
self.normalization['min_values'])):
if min_val < 0:
images[:, :, i, :, :] = images[:, :, i, :, :] / max(-min_val, max_val)
else:
images[:, :, i, :, :] = (images[:, :, i, :, :] - min_val) / (max_val - min_val)
images = images.type(torch.FloatTensor).to(self.device)
target = target.type(torch.LongTensor).to(self.device)
output = self.model(images, t)
# Obtain predictions and compute evaluation metrics
loss, pred_labels, target_labels = self.criterion(output, target, timestamps, mode=mode)
if self.dice_criterion !=None:
loss += self.dice_criterion(pred_labels, target_labels, self.device)
_, predictions = output.detach().cpu().topk(1, dim=1, largest=True,
sorted=True) # (batch_size, height, width)
step_confusion, step_metrics_by_threshold = compile_metrics(data_loader.dataset, predictions.numpy(),
timestamps, self.args.rain_thresholds)
confusion_matrix += step_confusion
for threshold, metrics in step_metrics_by_threshold.items():
metrics_by_threshold[threshold].append(metrics)
if loss is None:
continue
total_loss += loss.item() * images.shape[0]
total_samples += images.shape[0]
# Apply backprop
if mode == 'train':
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
# Collate evaluation results
metrics_by_threshold = {t: pd.concat(metrics) for t, metrics in metrics_by_threshold.items()}
average_loss = total_loss / total_samples
return average_loss, confusion_matrix, metrics_by_threshold