-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_reset.py
133 lines (99 loc) · 4.25 KB
/
test_reset.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
import argparse
import os
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from data_isic import DataLoaderISIC
from sklearn.metrics import roc_auc_score, balanced_accuracy_score, confusion_matrix, roc_curve
from reset import ReSeT
parser = argparse.ArgumentParser()
parser.add_argument("--batch_size", type=int, default=256)
parser.add_argument("--train_epochs", type=int, default=150)
args = parser.parse_args()
def main():
model_name = "CNN"
dim_input = sum(['feature' in col for col in pd.read_csv(f"features/{model_name}_val.csv").columns])
num_outputs = 1
emb_dim = 200
dim_output = 2
n_vect = 200
val_gen = DataLoaderISIC(
f"features/{model_name}_val.csv",
"GroundTruth.csv",
batch_size=args.batch_size,
n_vect=n_vect
)
test_gen = DataLoaderISIC(
f"features/{model_name}_test.csv",
"GroundTruth.csv",
batch_size=args.batch_size,
n_vect=n_vect
)
class_weights = torch.tensor([0.02, 0.98]).cuda()
criterion = nn.CrossEntropyLoss(class_weights)
model = ReSeT(dim_input, num_outputs, emb_dim, dim_output)
model = nn.DataParallel(model)
model = model.cuda()
model.load_state_dict(torch.load(f"models/{model_name}/{model_name}.pth"))
model.eval()
losses, total, correct, true_labels, predicted_probs = [], 0, 0, [], []
base_fpr = np.linspace(0, 1, 101)
for imgs, lbls in val_gen.test_data():
imgs = torch.Tensor(imgs).cuda()
lbls = torch.Tensor(lbls).long().cuda()
preds = model(imgs)
zero_rows_mask = torch.all(imgs == 0, dim=2)
non_zero_rows_mask = ~zero_rows_mask
preds = preds[non_zero_rows_mask]
lbls = lbls[non_zero_rows_mask]
loss = criterion(preds.view(-1, 2), lbls.view(-1))
losses.append(loss.item())
total += lbls.view(-1).shape[0]
correct += (preds.view(-1, 2).argmax(dim=1) == lbls.view(-1)).sum().item()
true_labels += lbls.view(-1).cpu().numpy().tolist()
predicted_probs += torch.softmax(preds.view(-1, 2), dim=1)[:, 1].cpu().detach().numpy().tolist()
avg_loss, avg_acc = np.mean(losses), correct / total
auc = roc_auc_score(true_labels, predicted_probs)
best_bacc = 0
best_thresh = 0
thresholds = np.linspace(0, 1, 5)
for thresh in thresholds:
balanced_acc = balanced_accuracy_score(true_labels, (np.array(predicted_probs) > thresh).astype(int))
if balanced_acc > best_bacc:
best_bacc = balanced_acc
best_thresh = thresh
print(
f"val loss {avg_loss:.3f} val acc {avg_acc:.3f} val AUC {auc:.3f}"
f" val balanced acc {best_bacc:.3f} best threshold {best_thresh:.3f}")
losses, total, correct, true_labels, predicted_probs = [], 0, 0, [], []
for imgs, lbls in test_gen.test_data():
imgs = torch.Tensor(imgs).cuda()
lbls = torch.Tensor(lbls).long().cuda()
preds = model(imgs)
zero_rows_mask = torch.all(imgs == 0, dim=2)
non_zero_rows_mask = ~zero_rows_mask
preds = preds[non_zero_rows_mask]
lbls = lbls[non_zero_rows_mask]
loss = criterion(preds.view(-1, 2), lbls.view(-1))
losses.append(loss.item())
total += lbls.view(-1).shape[0]
correct += (preds.view(-1, 2).argmax(dim=1) == lbls.view(-1)).sum().item()
true_labels += lbls.view(-1).cpu().numpy().tolist()
predicted_probs += torch.softmax(preds.view(-1, 2), dim=1)[:, 1].cpu().detach().numpy().tolist()
avg_loss, avg_acc = np.mean(losses), correct / total
if not os.path.exists("predictions"):
os.makedirs("predictions")
np.savetxt(f"predictions/{model_name}", np.array(predicted_probs), delimiter=",")
binary_predictions = (np.array(predicted_probs) > best_thresh).astype(int)
conf_matrix = confusion_matrix(true_labels, binary_predictions)
auc = roc_auc_score(true_labels, predicted_probs)
bacc = balanced_accuracy_score(true_labels, binary_predictions)
fpr, tpr, thresholds = roc_curve(true_labels, predicted_probs)
tpr = np.interp(base_fpr, fpr, tpr)
tpr[0] = 0.0
print(
f"test loss {avg_loss:.3f} test acc {avg_acc:.3f} test AUC {auc:.3f}"
f" test balanced acc {bacc:.3f}")
if __name__ == "__main__":
main()