-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathunimodal.py
253 lines (241 loc) · 12.8 KB
/
unimodal.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
"""Implements training pipeline for unimodal comparison."""
from sklearn.metrics import accuracy_score, f1_score
import torch
from torch import nn
from utils.AUPRC import AUPRC
from eval_scripts.performance import eval_affect
from eval_scripts.complexity import all_in_one_train, all_in_one_test
from eval_scripts.robustness import relative_robustness, effective_robustness, single_plot
from tqdm import tqdm
import pickle
softmax = nn.Softmax(dim=-1)
def train(encoder, head, train_dataloader, valid_dataloader, total_epochs, early_stop=False, optimtype=torch.optim.RMSprop, lr=0.001, weight_decay=0.0, criterion=nn.CrossEntropyLoss(), auprc=False, save_encoder='encoder.pt', save_head='head.pt', modalnum=0, task='classification', track_complexity=True):
"""Train unimodal module.
Args:
encoder (nn.Module): Unimodal encodder for the modality
head (nn.Module): Takes in the unimodal encoder output and produces the final prediction.
train_dataloader (torch.utils.data.DataLoader): Training data dataloader
valid_dataloader (torch.utils.data.DataLoader): Validation set dataloader
total_epochs (int): Total number of epochs
early_stop (bool, optional): Whether to apply early-stopping or not. Defaults to False.
optimtype (torch.optim.Optimizer, optional): Type of optimizer to use. Defaults to torch.optim.RMSprop.
lr (float, optional): Learning rate. Defaults to 0.001.
weight_decay (float, optional): Weight decay of optimizer. Defaults to 0.0.
criterion (nn.Module, optional): Loss module. Defaults to nn.CrossEntropyLoss().
auprc (bool, optional): Whether to compute AUPRC score or not. Defaults to False.
save_encoder (str, optional): Path of file to save model with best validation performance. Defaults to 'encoder.pt'.
save_head (str, optional): Path fo file to save head with best validation performance. Defaults to 'head.pt'.
modalnum (int, optional): Which modality to apply encoder to. Defaults to 0.
task (str, optional): Type of task to try. Supports "classification", "regression", or "multilabel". Defaults to 'classification'.
track_complexity (bool, optional): Whether to track the model's complexity or not. Defaults to True.
"""
def _trainprocess():
model = nn.Sequential(encoder, head)
op = optimtype(model.parameters(), lr=lr, weight_decay=weight_decay)
bestvalloss = 10000
bestacc = 0
bestf1 = 0
patience = 0
for epoch in range(total_epochs):
totalloss = 0.0
totals = 0
for j in train_dataloader:
op.zero_grad()
out = model(j[modalnum].float().to(torch.device("cuda:0" if torch.cuda.is_available() else "cpu")))
if type(criterion) == torch.nn.modules.loss.BCEWithLogitsLoss:
loss = criterion(out, j[-1].float().to(torch.device("cuda:0" if torch.cuda.is_available() else "cpu")))
else:
loss = criterion(out, j[-1].to(torch.device("cuda:0" if torch.cuda.is_available() else "cpu")))
totalloss += loss * len(j[-1])
totals += len(j[-1])
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 8)
op.step()
print("Epoch "+str(epoch)+" train loss: "+str(totalloss/totals))
with torch.no_grad():
totalloss = 0.0
pred = []
true = []
pts = []
for j in valid_dataloader:
out = model(j[modalnum].float().to(torch.device("cuda:0" if torch.cuda.is_available() else "cpu")))
if type(criterion) == torch.nn.modules.loss.BCEWithLogitsLoss:
loss = criterion(out, j[-1].float().to(torch.device("cuda:0" if torch.cuda.is_available() else "cpu")))
else:
loss = criterion(out, j[-1].to(torch.device("cuda:0" if torch.cuda.is_available() else "cpu")))
totalloss += loss*len(j[-1])
if task == "classification":
pred.append(torch.argmax(out, 1))
elif task == "multilabel":
pred.append(torch.sigmoid(out).round())
true.append(j[-1])
if auprc:
# pdb.set_trace()
sm = softmax(out)
pts += [(sm[i][1].item(), j[-1][i].item())
for i in range(j[-1].size(0))]
if pred:
pred = torch.cat(pred, 0).cpu().numpy()
true = torch.cat(true, 0).cpu().numpy()
totals = true.shape[0]
valloss = totalloss/totals
if task == "classification":
acc = accuracy_score(true, pred)
print("Epoch "+str(epoch)+" valid loss: "+str(valloss) +
" acc: "+str(acc))
if acc > bestacc:
patience = 0
bestacc = acc
print("Saving Best")
torch.save(encoder, save_encoder)
torch.save(head, save_head)
else:
patience += 1
elif task == "multilabel":
f1_micro = f1_score(true, pred, average="micro")
f1_macro = f1_score(true, pred, average="macro")
print("Epoch "+str(epoch)+" valid loss: "+str(valloss) +
" f1_micro: "+str(f1_micro)+" f1_macro: "+str(f1_macro))
if f1_macro > bestf1:
patience = 0
bestf1 = f1_macro
print("Saving Best")
torch.save(encoder, save_encoder)
torch.save(head, save_head)
else:
patience += 1
elif task == "regression":
print("Epoch "+str(epoch)+" valid loss: "+str(valloss))
if valloss < bestvalloss:
patience = 0
bestvalloss = valloss
print("Saving Best")
torch.save(encoder, save_encoder)
torch.save(head, save_head)
else:
patience += 1
if early_stop and patience > 7:
break
if auprc:
print("AUPRC: "+str(AUPRC(pts)))
if track_complexity:
all_in_one_train(_trainprocess, [encoder, head])
else:
_trainprocess()
def single_test(encoder, head, test_dataloader, auprc=False, modalnum=0, task='classification', criterion=None, save_preds=None):
"""Test unimodal model on one dataloader.
Args:
encoder (nn.Module): Unimodal encoder module
head (nn.Module): Module which takes in encoded unimodal input and predicts output.
test_dataloader (torch.utils.data.DataLoader): Data Loader for test set.
auprc (bool, optional): Whether to output AUPRC or not. Defaults to False.
modalnum (int, optional): Index of modality to consider for the test with the given encoder. Defaults to 0.
task (str, optional): Type of task to try. Supports "classification", "regression", or "multilabel". Defaults to 'classification'.
criterion (nn.Module, optional): Loss module. Defaults to None.
Returns:
dict: Dictionary of (metric, value) relations.
"""
model = nn.Sequential(encoder, head)
with torch.no_grad():
pred = []
true = []
totalloss = 0
pts = []
softmax_preds = []
for j in test_dataloader:
out = model(j[modalnum].float().to(torch.device("cuda:0" if torch.cuda.is_available() else "cpu")))
if save_preds:
for o in out:
softmax_preds.append(softmax(o).detach().cpu().numpy())
if criterion is not None:
loss = criterion(out, j[-1].to(torch.device("cuda:0" if torch.cuda.is_available() else "cpu")))
totalloss += loss*len(j[-1])
if task == "classification":
pred.append(torch.argmax(out, 1))
elif task == "multilabel":
pred.append(torch.sigmoid(out).round())
elif task == "posneg-classification":
prede = []
oute = out.cpu().numpy().tolist()
for i in oute:
if i[0] > 0:
prede.append(1)
elif i[0] < 0:
prede.append(-1)
else:
prede.append(0)
pred.append(torch.LongTensor(prede))
true.append(j[-1])
if auprc:
# pdb.set_trace()
sm = softmax(out)
pts += [(sm[i][1].item(), j[-1][i].item())
for i in range(j[-1].size(0))]
if save_preds:
with open(save_preds, 'wb') as f:
pickle.dump(softmax_preds, f)
if pred:
pred = torch.cat(pred, 0).cpu().numpy()
true = torch.cat(true, 0).cpu().numpy()
totals = true.shape[0]
if auprc:
print("AUPRC: "+str(AUPRC(pts)))
if criterion is not None:
print("loss: " + str(totalloss / totals))
if task == "classification":
acc = accuracy_score(true, pred)
print("acc: "+str(acc))
return acc
elif task == "multilabel":
print(" f1_micro: "+str(f1_score(true, pred, average="micro")) +
" f1_macro: "+str(f1_score(true, pred, average="macro")))
elif task == "posneg-classification":
trueposneg = true
accs = eval_affect(trueposneg, pred)
acc2 = eval_affect(trueposneg, pred, exclude_zero=False)
print("acc: "+str(accs) + ', ' + str(acc2))
else:
return {'MSE': (totalloss / totals).item()}
def test(encoder, head, test_dataloaders_all, dataset='default', method_name='My method', auprc=False, modalnum=0, task='classification', criterion=None, no_robust=False, save_preds=None):
"""Test unimodal model on all provided dataloaders.
Args:
encoder (nn.Module): Encoder module
head (nn.Module): Module which takes in encoded unimodal input and predicts output.
test_dataloaders_all (dict): Dictionary of noisetype, dataloader to test.
dataset (str, optional): Dataset to test on. Defaults to 'default'.
method_name (str, optional): Method name. Defaults to 'My method'.
auprc (bool, optional): Whether to output AUPRC scores or not. Defaults to False.
modalnum (int, optional): Index of modality to test on. Defaults to 0.
task (str, optional): Type of task to try. Supports "classification", "regression", or "multilabel". Defaults to 'classification'.
criterion (nn.Module, optional): Loss module. Defaults to None.
no_robust (bool, optional): Whether to not apply robustness methods or not. Defaults to False.
"""
if no_robust:
return single_test(encoder, head, test_dataloaders_all, auprc, modalnum, task, criterion, save_preds=save_preds)
def _testprocess():
single_test(encoder, head, test_dataloaders_all[list(
test_dataloaders_all.keys())[0]][0], auprc, modalnum, task, criterion)
all_in_one_test(_testprocess, [encoder, head])
for noisy_modality, test_dataloaders in test_dataloaders_all.items():
print("Testing on noisy data ({})...".format(noisy_modality))
robustness_curve = dict()
for test_dataloader in tqdm(test_dataloaders):
single_test_result = single_test(
encoder, head, test_dataloader, auprc, modalnum, task, criterion)
for k, v in single_test_result.items():
curve = robustness_curve.get(k, [])
curve.append(v)
robustness_curve[k] = curve
for measure, robustness_result in robustness_curve.items():
robustness_key = '{} {}'.format(dataset, noisy_modality)
print("relative robustness ({}, {}): {}".format(noisy_modality, measure, str(
relative_robustness(robustness_result, robustness_key))))
if len(robustness_curve) != 1:
robustness_key = '{} {}'.format(robustness_key, measure)
print("effective robustness ({}, {}): {}".format(noisy_modality, measure, str(
effective_robustness(robustness_result, robustness_key))))
fig_name = '{}-{}-{}-{}'.format(method_name,
robustness_key, noisy_modality, measure)
single_plot(robustness_result, robustness_key, xlabel='Noise level',
ylabel=measure, fig_name=fig_name, method=method_name)
print("Plot saved as "+fig_name)