-
Notifications
You must be signed in to change notification settings - Fork 12
/
evaluate_skin.py
160 lines (115 loc) · 4.04 KB
/
evaluate_skin.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from __future__ import division
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import torch
import torch.optim as optim
from torch.utils.data import DataLoader
from loader import *
import glob
import numpy as np
import copy
import yaml
from sklearn.metrics import f1_score
from tqdm import tqdm
from model.TransMUNet import TransMUNet
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score
from matplotlib import pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
from scipy.ndimage.morphology import binary_fill_holes, binary_opening
# In[2]:
## Hyper parameters
config = yaml.load(open('./config_skin.yml'), Loader=yaml.FullLoader)
number_classes = int(config['number_classes'])
input_channels = 3
best_val_loss = np.inf
patience = 0
device = 'cuda' if torch.cuda.is_available() else 'cpu'
data_path = config['path_to_data']
test_dataset = isic_loader(path_Data = data_path, train = False, Test = True)
test_loader = DataLoader(test_dataset, batch_size = 1, shuffle= True)
# In[3]:
Net = TransMUNet(n_classes = number_classes)
Net = Net.to(device)
Net.load_state_dict(torch.load(config['saved_model'], map_location='cpu')['model_weights'])
# ## Quntitative performance
# In[4]:
predictions = []
gt = []
with torch.no_grad():
print('val_mode')
val_loss = 0
Net.eval()
for itter, batch in tqdm(enumerate(test_loader)):
img = batch['image'].to(device, dtype=torch.float)
msk = batch['mask']
msk_pred = Net(img)
gt.append(msk.numpy()[0, 0])
msk_pred = msk_pred.cpu().detach().numpy()[0, 0]
msk_pred = np.where(msk_pred>=0.43, 1, 0)
msk_pred = binary_opening(msk_pred, structure=np.ones((6,6))).astype(msk_pred.dtype)
msk_pred = binary_fill_holes(msk_pred, structure=np.ones((6,6))).astype(msk_pred.dtype)
predictions.append(msk_pred)
predictions = np.array(predictions)
gt = np.array(gt)
y_scores = predictions.reshape(-1)
y_true = gt.reshape(-1)
y_scores2 = np.where(y_scores>0.47, 1, 0)
y_true2 = np.where(y_true>0.5, 1, 0)
#F1 score
F1_score = f1_score(y_true2, y_scores2, labels=None, average='binary', sample_weight=None)
print ("\nF1 score (F-measure) or DSC: " +str(F1_score))
confusion = confusion_matrix(np.int32(y_true), y_scores2)
print (confusion)
accuracy = 0
if float(np.sum(confusion))!=0:
accuracy = float(confusion[0,0]+confusion[1,1])/float(np.sum(confusion))
print ("Accuracy: " +str(accuracy))
specificity = 0
if float(confusion[0,0]+confusion[0,1])!=0:
specificity = float(confusion[0,0])/float(confusion[0,0]+confusion[0,1])
print ("Specificity: " +str(specificity))
sensitivity = 0
if float(confusion[1,1]+confusion[1,0])!=0:
sensitivity = float(confusion[1,1])/float(confusion[1,1]+confusion[1,0])
print ("Sensitivity: " +str(sensitivity))
# ## Visualization section
# In[5]:
def save_sample(img, msk, msk_pred, th=0.3, name=''):
img2 = img.detach().cpu().numpy()[0]
img2 = np.einsum('kij->ijk', img2)
msk2 = msk.detach().cpu().numpy()[0,0]
mskp = msk_pred.detach().cpu().numpy()[0,0]
msk2 = np.where(msk2>0.5, 1., 0)
mskp = np.where(mskp>=th, 1., 0)
plt.figure(figsize=(7,15))
plt.subplot(3,1,1)
plt.imshow(img2/255.)
plt.axis('off')
plt.subplot(3,1,2)
plt.imshow(msk2*255, cmap= 'gray')
plt.axis('off')
plt.subplot(3,1,3)
plt.imshow(mskp*255, cmap = 'gray')
plt.axis('off')
plt.savefig('./results/'+name+'.png')
# In[6]:
predictions = []
gt = []
N = 5 ## Number of samples to visualize
with torch.no_grad():
print('val_mode')
val_loss = 0
Net.eval()
for itter, batch in tqdm(enumerate(test_loader)):
img = batch['image'].to(device, dtype=torch.float)
msk = batch['mask']
msk_pred = Net(img)
gt.append(msk.numpy())
predictions.append(msk_pred.cpu().detach().numpy())
save_sample(img, msk, msk_pred, th=0.5, name=str(itter+1))
if itter+1==N:
break