-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathISBI_validation.py
84 lines (72 loc) · 3.01 KB
/
ISBI_validation.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
import numpy as np
import torch
import pandas as pd
from torchvision.models.densenet import densenet161
import tqdm
from torch.utils.data import DataLoader
from sklearn.metrics import roc_auc_score, average_precision_score
import ISBI_data
import ISBI_rareset
from metrics import *
from nets import ResNet, ResNext, ViT, ResNet152, Densenet161, effNetB7, effNetB6
# ckpt = torch.load('weights/resnext50_32x4d-epoch=027-val_arr=7.96.ckpt', map_location=torch.device('cpu'))
model = ResNext()
#ckpt = torch.load('saved_model/ISBI-WeightedBCE-ResNext101-epoch=013-val_loss=0.0892.ckpt', map_location=torch.device('cpu'))
ckpt = torch.load('data/checkpoints/ISBI-732-ResNext101-epoch=021-val_loss=0.0805.ckpt', map_location=torch.device('cpu'))
new_dict = {k.replace('vit.', 'model.'): v for k, v in ckpt['state_dict'].items()}
model.load_state_dict(new_dict)
model.eval()
model.cuda()
testing_img_path = '../Test_Set/Test/'
testing_df = '../Test_Set/RFMiD_Testing_Labels.csv'
valset = ISBI_data.ISBIDataset(testing_df, testing_img_path, testing=True)
N = len(valset)
batch_size = 8
dataloader = DataLoader(valset, batch_size=batch_size, shuffle=False,
num_workers=24)
outs = np.zeros((N, 29))
labels = np.zeros((N, 29))
for i, (imgs, label, w) in enumerate(tqdm.tqdm(dataloader)):
idx = i * batch_size
imgs = imgs.cuda()
out = model(imgs).detach().cpu().numpy()
#out = np.round(out).astype('int').clip(1, None)
outs[idx:idx + len(out),:] = out
labels[idx:idx + len(label),:] = label.detach().cpu().numpy()
sig = torch.nn.Sigmoid()
def postPro(outs):
for i in range(outs.shape[0]):
if outs[i][0]<0.1:
outs[i,1:] = outs[i,1:] * outs[i][0]
return outs
def postPro2(outs):
for i in range(outs.shape[0]):
if outs[i][0]<0.5:
for j in range(outs.shape[1]-1):
if outs[i][j+1] > 0.6:
outs[i][0] = outs[i][0]/outs[i][j+1]
return outs
outs = sig(torch.tensor(outs)).numpy()
outs = postPro2(outs)
rounded_illness_pred = np.round(outs[:,0]).astype('int')
illness_label = labels[:,0]
acc = (rounded_illness_pred==illness_label).astype(int).sum()/len(illness_label)
print(f'Accuracy: {acc}')
illness_pred = outs[:,0]
auc1 = roc_auc_score(illness_label, illness_pred)
print(f'AUC of Challenge 1: {auc1}')
diseases_label = labels[:,1:]
diseases_pred = outs[:,1:]
auc2 = roc_auc_score(diseases_label, diseases_pred)
print(f'AUC of Challenge 2: {auc2}')
mAP = average_precision_score(diseases_label, diseases_pred)
print(f'mAP of Challenge 2: {mAP}')
C1_Score = auc1
C2_Score = mAP * 0.5 + auc2 * 0.5
final_Score = C2_Score * 0.5 + C1_Score * 0.5
print(f'C1 Score: {C1_Score} C2 Score: {C2_Score} Final Score: {final_Score}')
# df = pd.read_csv('../Training_Set/RFMiD_Training_Labels.csv')
# predict = sig(torch.tensor(outs)).numpy()
# predict_csv = pd.DataFrame(predict,columns=df.columns[1:])
# predict_csv.index+=1
# predict_csv.to_csv('results.csv', index=True)