-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
391 lines (308 loc) · 14.1 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import torch
import numpy as np
import os.path as osp
import os
from contextlib import nullcontext
from torch.cuda.amp import autocast, GradScaler
from data import FreeMatchDataManager
from networks import avail_models
import pprint
from utils import (
FreeMatchOptimizer,
FreeMatchScheduler,
TensorBoardLogger,
EMA,
SelfAdaptiveThresholdLoss,
SelfAdaptiveFairnessLoss,
CELoss,
)
from sklearn.metrics import (
classification_report,
confusion_matrix,
accuracy_score,
precision_score,
recall_score,
f1_score
)
class FreeMatchTrainer:
def __init__(
self,
cfg
):
self.cfg = cfg
# Gathering the freematch training params.
self.num_train_iters = cfg.TRAINER.NUM_TRAIN_ITERS
self.num_eval_iters = cfg.TRAINER.NUM_EVAL_ITERS
self.num_warmup_iters = cfg.TRAINER.NUM_WARMUP_ITERS
self.num_log_iters = cfg.TRAINER.NUM_LOG_ITERS
self.ema_val = cfg.TRAINER.EMA_VAL
self.ulb_loss_ratio = cfg.TRAINER.ULB_LOSS_RATIO
self.ent_loss_ratio = cfg.TRAINER.ENT_LOSS_RATIO
self.device = 'cuda' if cfg.USE_CUDA else 'cpu'
if self.device == 'cuda':
torch.cuda.set_device(0)
torch.backends.cudnn.benchmark = True
# Building model and setup EMA
self.model = avail_models[cfg.MODEL.NAME](
num_classes=cfg.DATASET.NUM_CLASSES,
pretrained=cfg.MODEL.PRETRAINED,
pretrained_path=cfg.MODEL.PRETRAINED_PATH
)
self.model = self.model.to(self.device)
self.model.train()
self.net = EMA(
model=self.model,
decay=self.ema_val
)
self.net.train()
# Use Tensorboard if logging is enabled
if cfg.USE_TB:
self.tb = TensorBoardLogger(
fpath=osp.join(cfg.LOG_DIR, cfg.RUN_NAME),
filename=cfg.TB_DIR
)
# Build available dataloaders
self.dm = FreeMatchDataManager(cfg.DATASET, cfg.TRAINER.NUM_TRAIN_ITERS)
self.dm.data_statistics
# Build the optimizer and scheduler
self.optim = FreeMatchOptimizer(self.model, cfg.OPTIMIZER)
self.sched = FreeMatchScheduler(
optimizer=self.optim,
num_train_iters=self.num_train_iters,
)
# Initializing the loss functions
self.sat_criterion = SelfAdaptiveThresholdLoss(cfg.TRAINER.SAT_EMA)
self.ce_criterion = CELoss()
self.saf_criterion = SelfAdaptiveFairnessLoss()
# Initialize the class params
self.curr_iter = 0
self.best_test_iter = -1
self.best_test_acc = -1
self.p_t = torch.ones(cfg.DATASET.NUM_CLASSES) / cfg.DATASET.NUM_CLASSES
self.label_hist = torch.ones(cfg.DATASET.NUM_CLASSES) / cfg.DATASET.NUM_CLASSES
self.tau_t = self.p_t.mean()
self.amp = nullcontext
if cfg.TRAINER.AMP_ENABLED:
self.scaler = GradScaler()
self.amp = autocast
# Load Model if resume is true
if cfg.CONT_TRAIN:
print('Loading model from the path: %s' % cfg.RESUME)
self.__load__model__(cfg.RESUME)
if self.num_warmup_iters > 0:
print('Starting warmup training on labeled data...')
self.warmup_train()
print('Evaluating after warmup')
validate_dict = self.validate()
pprint.pprint(validate_dict, indent=4)
self.__toggle__device__()
def warmup_train(self):
# Mainly of SVHN training...
self.model.train()
# for gpu profiling
start_batch = torch.cuda.Event(enable_timing=True)
end_batch = torch.cuda.Event(enable_timing=True)
start_run = torch.cuda.Event(enable_timing=True)
end_run = torch.cuda.Event(enable_timing=True)
start_batch.record()
for batch_lb in self.dm.train_lb_dl:
if self.curr_iter >= self.num_warmup_iters:
self.curr_iter = 0
break
end_batch.record()
torch.cuda.synchronize()
start_run.record()
img_lb_w, label_lb = batch_lb['img_w'], batch_lb['label']
img_lb_w, label_lb = img_lb_w.to(self.device), label_lb.to(self.device)
with self.amp():
out = self.net(img_lb_w)
logits = out['logits']
loss = self.ce_criterion(logits, label_lb, reduction='mean')
if self.cfg.TRAINER.AMP_ENABLED:
self.scaler.scale(loss).backward()
self.scaler.step(self.optim.optimizer)
self.scaler.update()
else:
loss.backward()
self.optim.step()
end_run.record()
torch.cuda.synchronize()
log_dict = {
'warmup/loss': loss.item(),
'warmup/lr': self.optim.optimizer.param_groups[0]['lr'],
'warmup/fetch_time': start_batch.elapsed_time(end_batch) / 1000,
'warmup/run_time': start_run.elapsed_time(end_run) / 1000
}
if (self.curr_iter + 1) % self.num_log_iters == 0:
pprint.pprint(log_dict, indent=4)
self.curr_iter += 1
del log_dict
start_batch.record()
self.model.eval()
probs = list()
with torch.no_grad():
for _, batch in enumerate(self.dm.test_dl):
img_lb_w, label = batch['img_w'], batch['label']
img_lb_w, label = img_lb_w.to(self.device), label.to(self.device)
out = self.model(img_lb_w)
logits = out['logits']
probs.append(logits.softmax(dim=-1))
probs = torch.cat(probs)
max_probs, max_idx = torch.max(probs, dim=-1)
self.tau_t = max_probs.mean()
self.p_t = torch.mean(probs, dim=0)
label_hist = torch.bincount(max_idx, minlength=probs.shape[1]).to(probs.dtype)
self.label_hist = label_hist / label_hist.sum()
def train(self):
print('Starting model training...')
self.model.train()
# for gpu profiling
start_batch = torch.cuda.Event(enable_timing=True)
end_batch = torch.cuda.Event(enable_timing=True)
start_run = torch.cuda.Event(enable_timing=True)
end_run = torch.cuda.Event(enable_timing=True)
start_batch.record()
for (batch_lb, batch_ulb) in zip(self.dm.train_lb_dl, self.dm.train_ulb_dl):
if self.curr_iter >= self.num_train_iters:
break
end_batch.record()
torch.cuda.synchronize()
start_run.record()
img_lb_w, label_lb = batch_lb['img_w'], batch_lb['label']
img_ulb_w, img_ulb_s = batch_ulb['img_w'], batch_ulb['img_s']
img_lb_w, label_lb = img_lb_w.to(self.device), label_lb.to(self.device)
img_ulb_w, img_ulb_s = img_ulb_w.to(self.device), img_ulb_s.to(self.device)
num_lb = img_lb_w.shape[0]
num_ulb = img_ulb_w.shape[0]
assert num_ulb == img_ulb_s.shape[0]
img = torch.cat([img_lb_w, img_ulb_w, img_ulb_s])
with self.amp():
out = self.net(img)
logits = out['logits']
logits_lb = logits[:num_lb]
logits_ulb_w, logits_ulb_s = logits[num_lb:].chunk(2)
loss_lb = self.ce_criterion(logits_lb, label_lb, reduction='mean')
loss_sat, mask, self.tau_t, self.p_t, self.label_hist = self.sat_criterion(
logits_ulb_w, logits_ulb_s, self.tau_t, self.p_t, self.label_hist
)
loss_saf, hist_p_ulb_s = self.saf_criterion(mask, logits_ulb_s, self.p_t, self.label_hist)
loss = loss_lb + self.ulb_loss_ratio * loss_sat + self.ent_loss_ratio * loss_saf
if self.cfg.TRAINER.AMP_ENABLED:
self.scaler.scale(loss).backward()
self.scaler.step(self.optim.optimizer)
self.scaler.update()
else:
loss.backward()
self.optim.step()
self.sched.step()
self.net.update()
self.model.zero_grad()
end_run.record()
torch.cuda.synchronize()
# Logging in tensorboard
log_dict = {
'train/lb_loss': loss_lb.item(),
'train/sat_loss': loss_sat.item(),
'train/saf_loss': loss_saf.item(),
'train/total_loss': loss.item(),
'train/mask': 1 - mask.mean().item(),
'train/tau_t': self.tau_t.item(),
'train/p_t': self.p_t.mean().item(),
'train/label_hist': self.label_hist.mean().item(),
'train/label_hist_s': hist_p_ulb_s.mean().item(),
'train/lr': self.optim.optimizer.param_groups[0]['lr']
}
if (self.curr_iter + 1) % self.num_eval_iters == 0:
print('Evaluating...')
validate_dict = self.validate()
log_dict.update(validate_dict)
save_dir = osp.join(self.cfg.LOG_DIR, self.cfg.RUN_NAME, self.cfg.OUTPUT_DIR)
if not osp.exists(save_dir):
os.makedirs(save_dir)
if validate_dict['validation/accuracy'] > self.best_test_acc:
self.best_test_acc = validate_dict['validation/accuracy']
self.best_test_iter = self.curr_iter
self.__save__model__(save_dir, 'best_checkpoint.pth')
self.__save__model__(save_dir, 'last_checkpoint.pth')
log_dict.update(
{
'best_acc': self.best_test_acc,
'best_iter': self.best_test_iter
}
)
self.tb.update(log_dict, self.curr_iter)
if (self.curr_iter + 1) % self.num_log_iters == 0:
print('Iteration: %d / %d' % (self.curr_iter + 1, self.num_train_iters))
print('Fetch Time: %.3f, Run Time: %.3f' % (start_batch.elapsed_time(end_batch) / 1000, start_run.elapsed_time(end_run) / 1000 ))
pprint.pprint(log_dict, indent=4)
self.curr_iter += 1
del log_dict
start_batch.record()
@torch.no_grad()
def validate(self):
self.net.eval()
total_loss, total_num = 0, 0
labels, preds = list(), list()
for _, batch in enumerate(self.dm.test_dl):
img_lb_w, label = batch['img_w'], batch['label']
img_lb_w, label = img_lb_w.to(self.device), label.to(self.device)
out = self.net(img_lb_w)
logits = out['logits']
loss = self.ce_criterion(logits, label, reduction='mean')
labels.extend(label.cpu().tolist())
preds.extend(torch.max(logits, dim=-1)[1].cpu().tolist())
total_num += img_lb_w.shape[0]
total_loss += loss.detach().item() * img_lb_w.shape[0]
acc = accuracy_score(labels, preds)
precision = precision_score(labels, preds, average='macro')
recall = recall_score(labels, preds, average='macro')
f1 = f1_score(labels, preds, average='macro')
cf = confusion_matrix(labels, preds)
cr = classification_report(labels, preds)
print('Classification Report: \n')
print(cr)
print('Confusion Matrix \n')
print(np.array_str(cf))
self.net.train()
return {
'validation/loss': total_loss / total_num,
'validation/accuracy': acc,
'validation/precision': precision,
'validation/recall': recall,
'validation/f1': f1
}
def __save__model__(self, save_dir, save_name='latest.ckpt'):
save_dict = {
'model_state_dict': self.net.model.state_dict(),
'ema_state_dict':self.net.state_dict(),
'optimizer_state_dict': self.optim.optimizer.state_dict(),
'scheduler_state_dict': self.sched.scheduler.state_dict(),
'curr_iter': self.curr_iter,
'best_test_iter': self.best_test_iter,
'best_test_acc': self.best_test_acc,
'tau_t': self.tau_t.cpu(),
'p_t': self.p_t.cpu(),
'label_hist': self.label_hist.cpu()
}
torch.save(save_dict, osp.join(save_dir, save_name))
print('Model saved sucessfully. Path: %s' % osp.join(save_dir, save_name))
def __load__model__(self, load_path):
ckpt = torch.load(load_path)
self.net.model.load_state_dict(ckpt['model_state_dict'])
self.net.load_state_dict(ckpt['ema_state_dict'])
self.optim.optimizer.load_state_dict(ckpt['optimizer_state_dict'])
self.sched.scheduler.load_state_dict(ckpt['scheduler_state_dict'])
# Algorithm specfic loading
self.curr_iter = ckpt['curr_iter']
self.tau_t = ckpt['tau_t']
self.p_t = ckpt['p_t']
self.label_hist = ckpt['label_hist']
self.best_test_iter = ckpt['best_test_iter']
self.best_test_acc = ckpt['best_test_acc']
print('Initialized checkpoint parameters..')
print(f'Best Accuracy: {self.best_test_acc} Best Iteration: {self.best_test_iter}')
print('Model loaded from checkpoint. Path: %s' % load_path)
def __toggle__device__(self):
self.p_t = self.p_t.to(self.device)
self.tau_t = self.tau_t.to(self.device)
self.label_hist = self.label_hist.to(self.device)