-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathval_2d.py
More file actions
99 lines (88 loc) · 3.73 KB
/
val_2d.py
File metadata and controls
99 lines (88 loc) · 3.73 KB
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
import numpy as np
import torch
from medpy import metric
from scipy.ndimage import zoom
import pdb
def calculate_metric_percase(pred, gt):
pred[pred > 0] = 1
gt[gt > 0] = 1
if pred.sum() > 0:
dice = metric.binary.dc(pred, gt)
hd95 = metric.binary.hd95(pred, gt)
return dice, hd95
else:
return 0, 0
def test_single_volume_mean(image, label, model, model2, classes, patch_size=[256, 256]):
image, label = image.squeeze(0).cpu().detach(
).numpy(), label.squeeze(0).cpu().detach().numpy()
prediction = np.zeros_like(label)
for ind in range(image.shape[0]):
slice = image[ind, :, :]
x, y = slice.shape[0], slice.shape[1]
slice = zoom(slice, (patch_size[0] / x, patch_size[1] / y), order=0)
input = torch.from_numpy(slice).unsqueeze(0).unsqueeze(0).float().cuda()
model.eval()
model2.eval()
with torch.no_grad():
output1 = model(input)
output2 = model2(input)
if len(output1)>1:
output1 = output1[0]
if len(output2)>1:
output2 = output2[0]
mean_prob = (torch.softmax(output1, dim=1) + torch.softmax(output2, dim=1)) / 2
out = torch.argmax(mean_prob, dim=1).squeeze(0)
out = out.cpu().detach().numpy()
pred = zoom(out, (x / patch_size[0], y / patch_size[1]), order=0)
prediction[ind] = pred
metric_list = []
for i in range(1, classes):
metric_list.append(calculate_metric_percase(prediction == i, label == i))
return metric_list
def test_single_volume(image, label, model, classes, patch_size=[256, 256]):
image, label = image.squeeze(0).cpu().detach(
).numpy(), label.squeeze(0).cpu().detach().numpy()
prediction = np.zeros_like(label)
for ind in range(image.shape[0]):
slice = image[ind, :, :]
x, y = slice.shape[0], slice.shape[1]
slice = zoom(slice, (patch_size[0] / x, patch_size[1] / y), order=0)
input = torch.from_numpy(slice).unsqueeze(0).unsqueeze(0).float().cuda()
model.eval()
with torch.no_grad():
output = model(input)
if len(output)>1:
output = output[0]
out = torch.argmax(torch.softmax(output, dim=1), dim=1).squeeze(0)
out = out.cpu().detach().numpy()
pred = zoom(out, (x / patch_size[0], y / patch_size[1]), order=0)
prediction[ind] = pred
metric_list = []
for i in range(1, classes):
metric_list.append(calculate_metric_percase(prediction == i, label == i))
return metric_list
def test_single_volume_cross(image, label, model_l, model_r, classes, patch_size=[256, 256]):
image, label = image.squeeze(0).cpu().detach(
).numpy(), label.squeeze(0).cpu().detach().numpy()
prediction = np.zeros_like(label)
for ind in range(image.shape[0]):
slice = image[ind, :, :]
x, y = slice.shape[0], slice.shape[1]
slice = zoom(slice, (patch_size[0] / x, patch_size[1] / y), order=0)
input = torch.from_numpy(slice).unsqueeze(0).unsqueeze(0).float().cuda()
model_r.eval()
model_l.eval()
with torch.no_grad():
output_l = model_l(input)
output_r = model_r(input)
output = (output_l + output_r) / 2
if len(output)>1:
output = output[0]
out = torch.argmax(torch.softmax(output, dim=1), dim=1).squeeze(0)
out = out.cpu().detach().numpy()
pred = zoom(out, (x / patch_size[0], y / patch_size[1]), order=0)
prediction[ind] = pred
metric_list = []
for i in range(1, classes):
metric_list.append(calculate_metric_percase(prediction == i, label == i))
return metric_list