-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtrainer.py
356 lines (338 loc) · 18.9 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
import torch
import torch.nn as nn
import copy
import os
import numpy as np
from sklearn.metrics import roc_auc_score, average_precision_score, roc_curve, confusion_matrix, precision_recall_curve, precision_score
from models import binary_cross_entropy, cross_entropy_logits, entropy_logits, RandomLayer
from prettytable import PrettyTable
from domain_adaptator import ReverseLayerF
from tqdm import tqdm
class Trainer(object):
def __init__(self, model, optim, device, train_dataloader, val_dataloader, test_dataloader, opt_da=None, discriminator=None,
experiment=None, alpha=1, **config):
self.model = model
self.optim = optim
self.device = device
self.epochs = config["SOLVER"]["MAX_EPOCH"]
self.current_epoch = 0
self.train_dataloader = train_dataloader
self.val_dataloader = val_dataloader
self.test_dataloader = test_dataloader
self.is_da = config["DA"]["USE"]
self.alpha = alpha
self.n_class = config["DECODER"]["BINARY"]
if opt_da:
self.optim_da = opt_da
if self.is_da:
self.da_method = config["DA"]["METHOD"]
self.domain_dmm = discriminator
if config["DA"]["RANDOM_LAYER"] and not config["DA"]["ORIGINAL_RANDOM"]:
self.random_layer = nn.Linear(in_features=config["DECODER"]["IN_DIM"]*self.n_class, out_features=config["DA"]
["RANDOM_DIM"], bias=False).to(self.device)
torch.nn.init.normal_(self.random_layer.weight, mean=0, std=1)
for param in self.random_layer.parameters():
param.requires_grad = False
elif config["DA"]["RANDOM_LAYER"] and config["DA"]["ORIGINAL_RANDOM"]:
self.random_layer = RandomLayer([config["DECODER"]["IN_DIM"], self.n_class], config["DA"]["RANDOM_DIM"])
if torch.cuda.is_available():
self.random_layer.cuda()
else:
self.random_layer = False
self.da_init_epoch = config["DA"]["INIT_EPOCH"]
self.init_lamb_da = config["DA"]["LAMB_DA"]
self.batch_size = config["SOLVER"]["BATCH_SIZE"]
self.use_da_entropy = config["DA"]["USE_ENTROPY"]
self.nb_training = len(self.train_dataloader)
self.step = 0
self.experiment = experiment
self.best_model = None
self.best_epoch = None
self.best_auroc = 0
self.train_loss_epoch = []
self.train_model_loss_epoch = []
self.train_da_loss_epoch = []
self.val_loss_epoch, self.val_auroc_epoch = [], []
self.test_metrics = {}
self.config = config
self.output_dir = config["RESULT"]["OUTPUT_DIR"]
valid_metric_header = ["# Epoch", "AUROC", "AUPRC", "Val_loss"]
test_metric_header = ["# Best Epoch", "AUROC", "AUPRC", "F1", "Sensitivity", "Specificity", "Accuracy",
"Threshold", "Test_loss"]
if not self.is_da:
train_metric_header = ["# Epoch", "Train_loss"]
else:
train_metric_header = ["# Epoch", "Train_loss", "Model_loss", "epoch_lamb_da", "da_loss"]
self.val_table = PrettyTable(valid_metric_header)
self.test_table = PrettyTable(test_metric_header)
self.train_table = PrettyTable(train_metric_header)
self.original_random = config["DA"]["ORIGINAL_RANDOM"]
def da_lambda_decay(self):
delta_epoch = self.current_epoch - self.da_init_epoch
non_init_epoch = self.epochs - self.da_init_epoch
p = (self.current_epoch + delta_epoch * self.nb_training) / (
non_init_epoch * self.nb_training
)
grow_fact = 2.0 / (1.0 + np.exp(-10 * p)) - 1
return self.init_lamb_da * grow_fact
def train(self):
float2str = lambda x: '%0.4f' % x
for i in range(self.epochs):
self.current_epoch += 1
if not self.is_da:
train_loss = self.train_epoch()
train_lst = ["epoch " + str(self.current_epoch)] + list(map(float2str, [train_loss]))
if self.experiment:
self.experiment.log_metric("train_epoch model loss", train_loss, epoch=self.current_epoch)
else:
train_loss, model_loss, da_loss, epoch_lamb = self.train_da_epoch()
train_lst = ["epoch " + str(self.current_epoch)] + list(map(float2str, [train_loss, model_loss,
epoch_lamb, da_loss]))
self.train_model_loss_epoch.append(model_loss)
self.train_da_loss_epoch.append(da_loss)
if self.experiment:
self.experiment.log_metric("train_epoch total loss", train_loss, epoch=self.current_epoch)
self.experiment.log_metric("train_epoch model loss", model_loss, epoch=self.current_epoch)
if self.current_epoch >= self.da_init_epoch:
self.experiment.log_metric("train_epoch da loss", da_loss, epoch=self.current_epoch)
self.train_table.add_row(train_lst)
self.train_loss_epoch.append(train_loss)
auroc, auprc, val_loss = self.test(dataloader="val")
if self.experiment:
self.experiment.log_metric("valid_epoch model loss", val_loss, epoch=self.current_epoch)
self.experiment.log_metric("valid_epoch auroc", auroc, epoch=self.current_epoch)
self.experiment.log_metric("valid_epoch auprc", auprc, epoch=self.current_epoch)
val_lst = ["epoch " + str(self.current_epoch)] + list(map(float2str, [auroc, auprc, val_loss]))
self.val_table.add_row(val_lst)
self.val_loss_epoch.append(val_loss)
self.val_auroc_epoch.append(auroc)
if auroc >= self.best_auroc:
self.best_model = copy.deepcopy(self.model)
self.best_auroc = auroc
self.best_epoch = self.current_epoch
print('Validation at Epoch ' + str(self.current_epoch) + ' with validation loss ' + str(val_loss), " AUROC "
+ str(auroc) + " AUPRC " + str(auprc))
auroc, auprc, f1, sensitivity, specificity, accuracy, test_loss, thred_optim, precision = self.test(dataloader="test")
test_lst = ["epoch " + str(self.best_epoch)] + list(map(float2str, [auroc, auprc, f1, sensitivity, specificity,
accuracy, thred_optim, test_loss]))
self.test_table.add_row(test_lst)
print('Test at Best Model of Epoch ' + str(self.best_epoch) + ' with test loss ' + str(test_loss), " AUROC "
+ str(auroc) + " AUPRC " + str(auprc) + " Sensitivity " + str(sensitivity) + " Specificity " +
str(specificity) + " Accuracy " + str(accuracy) + " Thred_optim " + str(thred_optim))
self.test_metrics["auroc"] = auroc
self.test_metrics["auprc"] = auprc
self.test_metrics["test_loss"] = test_loss
self.test_metrics["sensitivity"] = sensitivity
self.test_metrics["specificity"] = specificity
self.test_metrics["accuracy"] = accuracy
self.test_metrics["thred_optim"] = thred_optim
self.test_metrics["best_epoch"] = self.best_epoch
self.test_metrics["F1"] = f1
self.test_metrics["Precision"] = precision
self.save_result()
if self.experiment:
self.experiment.log_metric("valid_best_auroc", self.best_auroc)
self.experiment.log_metric("valid_best_epoch", self.best_epoch)
self.experiment.log_metric("test_auroc", self.test_metrics["auroc"])
self.experiment.log_metric("test_auprc", self.test_metrics["auprc"])
self.experiment.log_metric("test_sensitivity", self.test_metrics["sensitivity"])
self.experiment.log_metric("test_specificity", self.test_metrics["specificity"])
self.experiment.log_metric("test_accuracy", self.test_metrics["accuracy"])
self.experiment.log_metric("test_threshold", self.test_metrics["thred_optim"])
self.experiment.log_metric("test_f1", self.test_metrics["F1"])
self.experiment.log_metric("test_precision", self.test_metrics["Precision"])
return self.test_metrics
def save_result(self):
if self.config["RESULT"]["SAVE_MODEL"]:
torch.save(self.best_model.state_dict(),
os.path.join(self.output_dir, f"best_model_epoch_{self.best_epoch}.pth"))
torch.save(self.model.state_dict(), os.path.join(self.output_dir, f"model_epoch_{self.current_epoch}.pth"))
state = {
"train_epoch_loss": self.train_loss_epoch,
"val_epoch_loss": self.val_loss_epoch,
"test_metrics": self.test_metrics,
"config": self.config
}
if self.is_da:
state["train_model_loss"] = self.train_model_loss_epoch
state["train_da_loss"] = self.train_da_loss_epoch
state["da_init_epoch"] = self.da_init_epoch
torch.save(state, os.path.join(self.output_dir, f"result_metrics.pt"))
val_prettytable_file = os.path.join(self.output_dir, "valid_markdowntable.txt")
test_prettytable_file = os.path.join(self.output_dir, "test_markdowntable.txt")
train_prettytable_file = os.path.join(self.output_dir, "train_markdowntable.txt")
with open(val_prettytable_file, 'w') as fp:
fp.write(self.val_table.get_string())
with open(test_prettytable_file, 'w') as fp:
fp.write(self.test_table.get_string())
with open(train_prettytable_file, "w") as fp:
fp.write(self.train_table.get_string())
def _compute_entropy_weights(self, logits):
entropy = entropy_logits(logits)
entropy = ReverseLayerF.apply(entropy, self.alpha)
entropy_w = 1.0 + torch.exp(-entropy)
return entropy_w
def train_epoch(self):
self.model.train()
loss_epoch = 0
num_batches = len(self.train_dataloader)
for i, (v_d, v_p, labels) in enumerate(tqdm(self.train_dataloader)):
self.step += 1
v_d, v_p, labels = v_d.to(self.device), v_p.to(self.device), labels.float().to(self.device)
self.optim.zero_grad()
v_d, v_p, f, score = self.model(v_d, v_p)
if self.n_class == 1:
n, loss = binary_cross_entropy(score, labels)
else:
n, loss = cross_entropy_logits(score, labels)
loss.backward()
self.optim.step()
loss_epoch += loss.item()
if self.experiment:
self.experiment.log_metric("train_step model loss", loss.item(), step=self.step)
loss_epoch = loss_epoch / num_batches
print('Training at Epoch ' + str(self.current_epoch) + ' with training loss ' + str(loss_epoch))
return loss_epoch
def train_da_epoch(self):
self.model.train()
total_loss_epoch = 0
model_loss_epoch = 0
da_loss_epoch = 0
epoch_lamb_da = 0
if self.current_epoch >= self.da_init_epoch:
# epoch_lamb_da = self.da_lambda_decay()
epoch_lamb_da = 1
if self.experiment:
self.experiment.log_metric("DA loss lambda", epoch_lamb_da, epoch=self.current_epoch)
num_batches = len(self.train_dataloader)
for i, (batch_s, batch_t) in enumerate(tqdm(self.train_dataloader)):
self.step += 1
v_d, v_p, labels = batch_s[0].to(self.device), batch_s[1].to(self.device), batch_s[2].float().to(
self.device)
v_d_t, v_p_t = batch_t[0].to(self.device), batch_t[1].to(self.device)
self.optim.zero_grad()
self.optim_da.zero_grad()
v_d, v_p, f, score = self.model(v_d, v_p)
if self.n_class == 1:
n, model_loss = binary_cross_entropy(score, labels)
else:
n, model_loss = cross_entropy_logits(score, labels)
if self.current_epoch >= self.da_init_epoch:
v_d_t, v_p_t, f_t, t_score = self.model(v_d_t, v_p_t)
if self.da_method == "CDAN":
reverse_f = ReverseLayerF.apply(f, self.alpha)
softmax_output = torch.nn.Softmax(dim=1)(score)
softmax_output = softmax_output.detach()
# reverse_output = ReverseLayerF.apply(softmax_output, self.alpha)
if self.original_random:
random_out = self.random_layer.forward([reverse_f, softmax_output])
adv_output_src_score = self.domain_dmm(random_out.view(-1, random_out.size(1)))
else:
feature = torch.bmm(softmax_output.unsqueeze(2), reverse_f.unsqueeze(1))
feature = feature.view(-1, softmax_output.size(1) * reverse_f.size(1))
if self.random_layer:
random_out = self.random_layer.forward(feature)
adv_output_src_score = self.domain_dmm(random_out)
else:
adv_output_src_score = self.domain_dmm(feature)
reverse_f_t = ReverseLayerF.apply(f_t, self.alpha)
softmax_output_t = torch.nn.Softmax(dim=1)(t_score)
softmax_output_t = softmax_output_t.detach()
# reverse_output_t = ReverseLayerF.apply(softmax_output_t, self.alpha)
if self.original_random:
random_out_t = self.random_layer.forward([reverse_f_t, softmax_output_t])
adv_output_tgt_score = self.domain_dmm(random_out_t.view(-1, random_out_t.size(1)))
else:
feature_t = torch.bmm(softmax_output_t.unsqueeze(2), reverse_f_t.unsqueeze(1))
feature_t = feature_t.view(-1, softmax_output_t.size(1) * reverse_f_t.size(1))
if self.random_layer:
random_out_t = self.random_layer.forward(feature_t)
adv_output_tgt_score = self.domain_dmm(random_out_t)
else:
adv_output_tgt_score = self.domain_dmm(feature_t)
if self.use_da_entropy:
entropy_src = self._compute_entropy_weights(score)
entropy_tgt = self._compute_entropy_weights(t_score)
src_weight = entropy_src / torch.sum(entropy_src)
tgt_weight = entropy_tgt / torch.sum(entropy_tgt)
else:
src_weight = None
tgt_weight = None
n_src, loss_cdan_src = cross_entropy_logits(adv_output_src_score, torch.zeros(self.batch_size).to(self.device),
src_weight)
n_tgt, loss_cdan_tgt = cross_entropy_logits(adv_output_tgt_score, torch.ones(self.batch_size).to(self.device),
tgt_weight)
da_loss = loss_cdan_src + loss_cdan_tgt
else:
raise ValueError(f"The da method {self.da_method} is not supported")
loss = model_loss + da_loss
else:
loss = model_loss
loss.backward()
self.optim.step()
self.optim_da.step()
total_loss_epoch += loss.item()
model_loss_epoch += model_loss.item()
if self.experiment:
self.experiment.log_metric("train_step model loss", model_loss.item(), step=self.step)
self.experiment.log_metric("train_step total loss", loss.item(), step=self.step)
if self.current_epoch >= self.da_init_epoch:
da_loss_epoch += da_loss.item()
if self.experiment:
self.experiment.log_metric("train_step da loss", da_loss.item(), step=self.step)
total_loss_epoch = total_loss_epoch / num_batches
model_loss_epoch = model_loss_epoch / num_batches
da_loss_epoch = da_loss_epoch / num_batches
if self.current_epoch < self.da_init_epoch:
print('Training at Epoch ' + str(self.current_epoch) + ' with model training loss ' + str(total_loss_epoch))
else:
print('Training at Epoch ' + str(self.current_epoch) + ' model training loss ' + str(model_loss_epoch)
+ ", da loss " + str(da_loss_epoch) + ", total training loss " + str(total_loss_epoch) + ", DA lambda " +
str(epoch_lamb_da))
return total_loss_epoch, model_loss_epoch, da_loss_epoch, epoch_lamb_da
def test(self, dataloader="test"):
test_loss = 0
y_label, y_pred = [], []
if dataloader == "test":
data_loader = self.test_dataloader
elif dataloader == "val":
data_loader = self.val_dataloader
else:
raise ValueError(f"Error key value {dataloader}")
num_batches = len(data_loader)
with torch.no_grad():
self.model.eval()
for i, (v_d, v_p, labels) in enumerate(data_loader):
v_d, v_p, labels = v_d.to(self.device), v_p.to(self.device), labels.float().to(self.device)
if dataloader == "val":
v_d, v_p, f, score = self.model(v_d, v_p)
elif dataloader == "test":
v_d, v_p, f, score = self.best_model(v_d, v_p)
if self.n_class == 1:
n, loss = binary_cross_entropy(score, labels)
else:
n, loss = cross_entropy_logits(score, labels)
test_loss += loss.item()
y_label = y_label + labels.to("cpu").tolist()
y_pred = y_pred + n.to("cpu").tolist()
auroc = roc_auc_score(y_label, y_pred)
auprc = average_precision_score(y_label, y_pred)
test_loss = test_loss / num_batches
if dataloader == "test":
fpr, tpr, thresholds = roc_curve(y_label, y_pred)
prec, recall, _ = precision_recall_curve(y_label, y_pred)
precision = tpr / (tpr + fpr)
f1 = 2 * precision * tpr / (tpr + precision + 0.00001)
thred_optim = thresholds[5:][np.argmax(f1[5:])]
y_pred_s = [1 if i else 0 for i in (y_pred >= thred_optim)]
cm1 = confusion_matrix(y_label, y_pred_s)
accuracy = (cm1[0, 0] + cm1[1, 1]) / sum(sum(cm1))
sensitivity = cm1[0, 0] / (cm1[0, 0] + cm1[0, 1])
specificity = cm1[1, 1] / (cm1[1, 0] + cm1[1, 1])
if self.experiment:
self.experiment.log_curve("test_roc curve", fpr, tpr)
self.experiment.log_curve("test_pr curve", recall, prec)
precision1 = precision_score(y_label, y_pred_s)
return auroc, auprc, np.max(f1[5:]), sensitivity, specificity, accuracy, test_loss, thred_optim, precision1
else:
return auroc, auprc, test_loss