forked from apple1986/SAMatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathval_2D.py
More file actions
129 lines (121 loc) · 5.04 KB
/
val_2D.py
File metadata and controls
129 lines (121 loc) · 5.04 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
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
import numpy as np
import torch
from medpy import metric
from scipy.ndimage import zoom
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, 10000
import pywt
def test_single_volume(image, label, net, classes, patch_size=[256, 256], cuda_num= "cuda:0"):
# image = image.permute(1,0,2,3)
# label = label.permute(1,0,2,3)
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)
##################################
# coeffs = pywt.dwt2(slice, 'bior1.1')
# cA, (cH, cV, cD) = coeffs
# l = cA
# h = cH+ cV+ cD
# l = zoom(l, (256 / 128, 256 / 128), order=0)
# h = zoom(h, (256 / 128, 256 / 128), order=0)
# # input_1 = torch.from_numpy(l).float()
# # input_2 = torch.from_numpy(h).float()
# input_1 = torch.from_numpy(l).unsqueeze(
# 0).unsqueeze(0).float().cuda()
# input_2 = torch.from_numpy(h).unsqueeze(
# 0).unsqueeze(0).float().cuda()
######################################
input = torch.from_numpy(slice).unsqueeze(
0).unsqueeze(0).float().to(cuda_num)
net.eval()
with torch.no_grad():
out = torch.argmax(torch.softmax(
net(input), 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_synapse(image, label, net, 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()
net.eval()
with torch.no_grad():
out = torch.argmax(torch.softmax(
net(input), 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(0, classes):
metric_list.append(calculate_metric_percase(
prediction == i, label == i))
return metric_list
def test_single_volume_BUSI(image, label, net, classes, patch_size=[256, 256], cuda_num="cuda:0"):
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
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().to(cuda_num)
net.eval()
with torch.no_grad():
out_put = net(input)
if len(out_put) != 1:
out_put = out_put[0]
out = torch.argmax(torch.softmax(
out_put, 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 = pred
metric_list = []
for i in range(1, classes): # ignore the background
metric_list.append(calculate_metric_percase(
prediction == i, label == i))
return metric_list
def test_single_volume_ds(image, label, net, 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()
net.eval()
with torch.no_grad():
output_main, _, _, _ = net(input)
out = torch.argmax(torch.softmax(
output_main, 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