-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlate_fusion_trainer.py
147 lines (106 loc) · 5 KB
/
late_fusion_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
import torch
import torch.nn as nn
import time
import os
import numpy as np
import copy
from trainer import ModelTrainer
class LateFusionTrainer(ModelTrainer):
def __init__(self, dataloader: dict, model: nn.Module, device, model_name):
super(LateFusionTrainer, self).__init__(dataloader, model, device, model_name)
def train(self, optimizer, scheduler, criterion, num_epochs, report_len=500):
since = time.time()
best_wts = copy.deepcopy(self.model.state_dict())
best_acc = float('-inf')
patience = 8
patience_counter = 0
for epoch in range(num_epochs):
print(f'\nEpoch: {epoch+1}/{num_epochs}')
self.model.train()
torch.enable_grad()
epoch_acc = []
epoch_loss = []
for i, data in enumerate(self.dataloader['train']):
optimizer.zero_grad()
data = {x: data[x].to(self.device) for x in data}
inputs = {
'input_ids': data['input_ids'],
'attention_masks': data['attention_masks'],
'image': data['image']
}
labels = data['label']
outputs = self.model(inputs)
preds = outputs > 0.5
acc = (preds.squeeze() == labels).float().sum() / len(labels)
epoch_acc.append(acc.item())
loss = criterion(outputs, labels.unsqueeze(-1).float())
epoch_loss.append(loss.item())
if i % report_len == 0:
self.report(i, np.mean(epoch_loss), np.mean(epoch_acc))
loss.backward()
optimizer.step()
scheduler.step()
print(f'\nEpoch: {epoch+1}/{num_epochs} done, loss: {np.mean(epoch_loss)}, acc: {np.mean(epoch_acc)}')
with torch.no_grad():
self.model.eval()
accuracies = []
for _, data in enumerate(self.dataloader['val']):
data = {x: data[x].to(self.device) for x in data}
inputs = {
'input_ids': data['input_ids'],
'attention_masks': data['attention_masks'],
'image': data['image']
}
labels = data['label']
outputs = self.model(inputs)
preds = outputs > 0.5
acc = (preds.squeeze() == labels).float().sum() / len(labels)
accuracies.append(acc.item())
val_acc = np.mean(accuracies)
print(f'\nValidaton accuracies for epoch {epoch} is {val_acc}')
print(f'\nBest Accuracy so far is {best_acc}')
if val_acc > best_acc:
patience_counter = 0
best_acc = val_acc
best_wts = copy.deepcopy(self.model.state_dict())
self.save_model()
else:
patience_counter += 1
if patience_counter > patience:
print('\nI ran out of patience')
break
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
print('Best val Acc: {:4f}'.format(best_acc))
# load best model weights
self.model.load_state_dict(best_wts)
def test(self):
if os.path.isfile(self.save_path):
model_dict = torch.load(self.save_path)
self.model.load_state_dict(model_dict)
with torch.no_grad():
self.model.eval()
results = []
for _, data in enumerate(self.dataloader['test']):
data = {x: data[x].to(self.device) for x in data}
inputs = {
'input_ids': data['input_ids'],
'attention_masks': data['attention_masks'],
'image': data['image']
}
labels = data['label']
outputs = self.model(inputs)
preds = outputs > 0.5
preds = preds.squeeze().int().detach().cpu().numpy()
labels = labels.detach().cpu().numpy()
acc, prec, recall, f1_score = self.get_testing_stats(labels, preds)
results.append((acc, prec, recall, f1_score))
test_acc = np.mean([item[0] for item in results])
test_prec = np.mean([item[1] for item in results if item[1]] != None)
test_recall = np.mean([item[2] for item in results if item[2] != None])
test_f1_score = np.mean([item[3] for item in results if item[3] != None])
print(f'\nTest Accuracy is {test_acc}')
print(f'\nTest Precision is {test_prec}')
print(f'\nTest Recall is {test_recall}')
print(f'\nTest F1 score is {test_f1_score}')