|
a |
|
b/evaluate_2d.py |
|
|
1 |
import torch |
|
|
2 |
import torch.nn as nn |
|
|
3 |
from torch.autograd import Variable |
|
|
4 |
import torch.optim as optim |
|
|
5 |
import torchvision |
|
|
6 |
from torchvision import datasets, models |
|
|
7 |
from torchvision import transforms as T |
|
|
8 |
from torch.utils.data import DataLoader, Dataset |
|
|
9 |
import numpy as np |
|
|
10 |
import matplotlib.pyplot as plt |
|
|
11 |
import os |
|
|
12 |
import time |
|
|
13 |
import pandas as pd |
|
|
14 |
from skimage import io, transform |
|
|
15 |
import matplotlib.image as mpimg |
|
|
16 |
from PIL import Image |
|
|
17 |
from sklearn.metrics import roc_auc_score |
|
|
18 |
import torch.nn.functional as F |
|
|
19 |
import scipy |
|
|
20 |
import random |
|
|
21 |
import pickle |
|
|
22 |
import scipy.io as sio |
|
|
23 |
import itertools |
|
|
24 |
from scipy.ndimage.interpolation import shift |
|
|
25 |
import copy |
|
|
26 |
import warnings |
|
|
27 |
warnings.filterwarnings("ignore") |
|
|
28 |
plt.ion() |
|
|
29 |
|
|
|
30 |
from utils import * |
|
|
31 |
|
|
|
32 |
def evaluate(model, dataloader, data_size, batch_size, phase, dice_loss = dice_loss, noisy_labels = False): |
|
|
33 |
model.train(False) |
|
|
34 |
running_loss = 0 |
|
|
35 |
running_dice_score_class_0 = 0 |
|
|
36 |
running_dice_score_class_1 = 0 |
|
|
37 |
running_dice_score_class_2 = 0 |
|
|
38 |
phase = phase |
|
|
39 |
|
|
|
40 |
for i,data in enumerate(dataloader[phase]): |
|
|
41 |
input_1, segF,segP, segT,_ = data |
|
|
42 |
input_1 = Variable(input_1).cuda() |
|
|
43 |
output = model(input_1) |
|
|
44 |
true = Variable(segments(segF, segP, segT)).cuda() |
|
|
45 |
if noisy_labels: |
|
|
46 |
loss = entropy_loss(true,output) |
|
|
47 |
else: |
|
|
48 |
loss = dice_loss(true,output) |
|
|
49 |
running_loss += loss.data[0] * batch_size |
|
|
50 |
dice_score_batch = dice_score(true,output) |
|
|
51 |
running_dice_score_class_0 += dice_score_batch[0] * batch_size |
|
|
52 |
running_dice_score_class_1 += dice_score_batch[1] * batch_size |
|
|
53 |
running_dice_score_class_2 += dice_score_batch[2] * batch_size |
|
|
54 |
preds = predict(output) |
|
|
55 |
if i == 11 or i == 4: |
|
|
56 |
for k in range(batch_size): |
|
|
57 |
for j in range(3): |
|
|
58 |
print('True Map') |
|
|
59 |
plt.imshow(input_1[k,1,:,:].data.cpu().numpy()) |
|
|
60 |
plt.show() |
|
|
61 |
plt.imshow(true[k,j,:,:].data.cpu().numpy()) |
|
|
62 |
plt.show() |
|
|
63 |
print('Predicted Map') |
|
|
64 |
plt.imshow(preds[j][k,:,:].cpu().numpy()) |
|
|
65 |
plt.show() |
|
|
66 |
|
|
|
67 |
loss = running_loss/data_sizes[phase] |
|
|
68 |
dice_score_0 = running_dice_score_class_0/data_sizes[phase] |
|
|
69 |
dice_score_1 = running_dice_score_class_1/data_sizes[phase] |
|
|
70 |
dice_score_2 = running_dice_score_class_2/data_sizes[phase] |
|
|
71 |
print('{} loss: {:.4f}, Dice Score (class 0): {:.4f}, Dice Score (class 1): {:.4f},Dice Score (class 2): {:.4f}'.format(phase,loss, dice_score_0, dice_score_1, dice_score_2)) |
|
|
72 |
return loss, dice_score_0, dice_score_1, dice_score_2 |
|
|
73 |
|
|
|
74 |
def evaluate_exp(model, dataloader, data_size, batch_size, phase, dice_loss = dice_loss): |
|
|
75 |
model.train(False) |
|
|
76 |
running_loss = 0 |
|
|
77 |
running_dice_score_class_0 = 0 |
|
|
78 |
running_dice_score_class_1 = 0 |
|
|
79 |
running_dice_score_class_2 = 0 |
|
|
80 |
phase = phase |
|
|
81 |
|
|
|
82 |
for i,data in enumerate(dataloader[phase]): |
|
|
83 |
input_1, segF,segP, segT,_ = data |
|
|
84 |
input_1 = Variable(input_1).cuda() |
|
|
85 |
input_1[:,:7,:,:] = input_1[:, [6,5,4,3,2,1,0], :, :] |
|
|
86 |
output = model(input_1) |
|
|
87 |
true = Variable(segments(segF, segP, segT)).cuda() |
|
|
88 |
loss = dice_loss(true,output) |
|
|
89 |
running_loss += loss.data[0] * batch_size |
|
|
90 |
dice_score_batch = dice_score(true,output) |
|
|
91 |
running_dice_score_class_0 += dice_score_batch[0] * batch_size |
|
|
92 |
running_dice_score_class_1 += dice_score_batch[1] * batch_size |
|
|
93 |
running_dice_score_class_2 += dice_score_batch[2] * batch_size |
|
|
94 |
preds = predict(output) |
|
|
95 |
if i == 11 or i == 4: |
|
|
96 |
for k in range(batch_size): |
|
|
97 |
for j in range(3): |
|
|
98 |
print('True Map') |
|
|
99 |
plt.imshow(input_1[k,1,:,:].data.cpu().numpy()) |
|
|
100 |
plt.show() |
|
|
101 |
plt.imshow(true[k,j,:,:].data.cpu().numpy()) |
|
|
102 |
plt.show() |
|
|
103 |
print('Predicted Map') |
|
|
104 |
plt.imshow(preds[j][k,:,:].cpu().numpy()) |
|
|
105 |
plt.show() |
|
|
106 |
|
|
|
107 |
loss = running_loss/data_sizes[phase] |
|
|
108 |
dice_score_0 = running_dice_score_class_0/data_sizes[phase] |
|
|
109 |
dice_score_1 = running_dice_score_class_1/data_sizes[phase] |
|
|
110 |
dice_score_2 = running_dice_score_class_2/data_sizes[phase] |
|
|
111 |
print('{} loss: {:.4f}, Dice Score (class 0): {:.4f}, Dice Score (class 1): {:.4f},Dice Score (class 2): {:.4f}'.format(phase,loss, dice_score_0, dice_score_1, dice_score_2)) |
|
|
112 |
return loss, dice_score_0, dice_score_1, dice_score_2 |
|
|
113 |
|
|
|
114 |
def evaluate_patches(model, dataloader, data_size, batch_size, phase, patch = True): |
|
|
115 |
model.eval() |
|
|
116 |
running_loss = 0 |
|
|
117 |
running_dice_score_class_0 = 0 |
|
|
118 |
running_dice_score_class_1 = 0 |
|
|
119 |
running_dice_score_class_2 = 0 |
|
|
120 |
phase = phase |
|
|
121 |
|
|
|
122 |
for i,data in enumerate(dataloader[phase]): |
|
|
123 |
input, seg,_ = data |
|
|
124 |
if patch: |
|
|
125 |
input = Variable(input[:,:,1:49,1:49]).cuda() |
|
|
126 |
true = Variable(seg[:,:,1:49,1:49]).cuda() |
|
|
127 |
else: |
|
|
128 |
input = Variable(input).cuda() |
|
|
129 |
true = Variable(seg).cuda() |
|
|
130 |
output = model(input) |
|
|
131 |
loss = dice_loss(true,output) |
|
|
132 |
running_loss += loss.data[0] * batch_size |
|
|
133 |
dice_score_batch = dice_score(true,output) |
|
|
134 |
running_dice_score_class_0 += dice_score_batch[0] * batch_size |
|
|
135 |
running_dice_score_class_1 += dice_score_batch[1] * batch_size |
|
|
136 |
running_dice_score_class_2 += dice_score_batch[2] * batch_size |
|
|
137 |
preds = predict(output) |
|
|
138 |
if i == 11: |
|
|
139 |
for k in range(batch_size): |
|
|
140 |
for j in range(3): |
|
|
141 |
print('True Map') |
|
|
142 |
plt.imshow(input[k,1,:,:].data.cpu().numpy()) |
|
|
143 |
plt.show() |
|
|
144 |
plt.imshow(true[k,j+1,:,:].data.cpu().numpy()) |
|
|
145 |
plt.show() |
|
|
146 |
print('Predicted Map') |
|
|
147 |
plt.imshow(preds[j][k,:,:].cpu().numpy()) |
|
|
148 |
plt.show() |
|
|
149 |
|
|
|
150 |
loss = running_loss/data_sizes[phase] |
|
|
151 |
dice_score_0 = running_dice_score_class_0/data_sizes[phase] |
|
|
152 |
dice_score_1 = running_dice_score_class_1/data_sizes[phase] |
|
|
153 |
dice_score_2 = running_dice_score_class_2/data_sizes[phase] |
|
|
154 |
print('{} loss: {:.4f}, Dice Score (class 1): {:.4f}, Dice Score (class 2): {:.4f},Dice Score (class 3): {:.4f}'.format(phase,loss, dice_score_0, dice_score_1, dice_score_2)) |
|
|
155 |
return loss, dice_score_0, dice_score_1, dice_score_2 |
|
|
156 |
|