-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval_3d.py
193 lines (174 loc) · 8.84 KB
/
eval_3d.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import argparse
import os
import sys
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
import copy
import math
from tqdm import tqdm
import h5py
import SimpleITK as sitk
from PIL import Image
import numpy as np
import pandas as pd
from medpy import metric
import torch
import torch.nn.functional as F
from models import deeplabv3
from utils.utils import ensure_dir, measure_img
sep = '\\' if sys.platform[:3] == 'win' else '/'
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def get_args(known=False):
parser = argparse.ArgumentParser(description='PyTorch Implementation')
parser.add_argument('--seed', type=int, default=1, metavar='S', help='random seed (default: 1)')
parser.add_argument('--project', type=str, default=os.path.dirname(os.path.realpath(__file__)) + '/runs/UCMT', help='project path for saving results')
parser.add_argument('--backbone', type=str, default='VNet', choices=['VNet'], help='segmentation backbone')
parser.add_argument('--data_path', type=str, default='YOUR_DATA_PATH', help='path to the data')
parser.add_argument('--image_size', type=list, default=[80, 112, 112], help='the size of images for training and testing')
parser.add_argument('--labeled_percentage', type=float, default=0.1, help='the percentage of labeled data')
parser.add_argument('--is_mix', type=bool, default=True, help='cut mix')
parser.add_argument('--mix_prob', type=float, default=0.5, help='probability for amplitude mix')
parser.add_argument('--batch_size', type=int, default=8, help='number of inputs per batch')
parser.add_argument('--num_workers', type=int, default=2, help='number of workers to use for dataloader')
parser.add_argument('--in_channels', type=int, default=1, help='input channels')
parser.add_argument('--num_classes', type=int, default=2, help='number of target categories')
parser.add_argument('--pretrained', type=bool, default=True, help='using pretrained weights')
parser.add_argument('--learning_rate', type=float, default=1e-4, help='learning rate')
parser.add_argument('--intra_weights', type=list, default=[1., 1.], help='inter classes weighted coefficients in the loss function')
parser.add_argument('--inter_weight', type=float, default=1., help='inter losses weighted coefficients in the loss function')
parser.add_argument('--model_weights', type=str, default='best.pth', help='model weights')
args = parser.parse_known_args()[0] if known else parser.parse_args()
return args
def get_data_path(image_path):
image_files = [os.path.join(image_path, x, y)
for x in os.listdir(image_path) if x.endswith('TestDataset')
for y in os.listdir(os.path.join(image_path, x)) if y.endswith('image.nii.gz')]
return image_files
def get_gt_path(image_path):
image_files = [os.path.join(image_path, x, y)
for x in os.listdir(image_path) if x.endswith('TestDataset')
for y in os.listdir(os.path.join(image_path, x)) if y.endswith('label.nii.gz')]
return image_files
def load_model(model_weights, in_channels, num_classes, backbone='VNet'):
model = deeplabv3.__dict__[backbone](in_channels=in_channels, out_channels=num_classes).to(device)
print('#parameters:', sum(param.numel() for param in model.parameters()))
model.load_state_dict(torch.load(model_weights))
return model
def calculate_metric_percase(pred, gt):
pred[pred > 0] = 1
gt[gt > 0] = 1
dice = metric.binary.dc(pred, gt)
jc = metric.binary.jc(pred, gt)
asd = metric.binary.asd(pred, gt)
hd95 = metric.binary.hd95(pred, gt)
return dice, jc, hd95, asd
def test_single_case(net, image, stride_xy, stride_z, patch_size, num_classes=1):
w, h, d = image.shape
# if the size of image is less than patch_size, then padding it
add_pad = False
if w < patch_size[0]:
w_pad = patch_size[0]-w
add_pad = True
else:
w_pad = 0
if h < patch_size[1]:
h_pad = patch_size[1]-h
add_pad = True
else:
h_pad = 0
if d < patch_size[2]:
d_pad = patch_size[2]-d
add_pad = True
else:
d_pad = 0
wl_pad, wr_pad = w_pad//2,w_pad-w_pad//2
hl_pad, hr_pad = h_pad//2,h_pad-h_pad//2
dl_pad, dr_pad = d_pad//2,d_pad-d_pad//2
if add_pad:
image = np.pad(image, [(wl_pad,wr_pad),(hl_pad,hr_pad), (dl_pad, dr_pad)], mode='constant', constant_values=0)
ww,hh,dd = image.shape
sx = math.ceil((ww - patch_size[0]) / stride_xy) + 1
sy = math.ceil((hh - patch_size[1]) / stride_xy) + 1
sz = math.ceil((dd - patch_size[2]) / stride_z) + 1
# print("{}, {}, {}".format(sx, sy, sz))
score_map = np.zeros((num_classes, ) + image.shape).astype(np.float32)
cnt = np.zeros(image.shape).astype(np.float32)
for x in range(0, sx):
xs = min(stride_xy*x, ww-patch_size[0])
for y in range(0, sy):
ys = min(stride_xy * y,hh-patch_size[1])
for z in range(0, sz):
zs = min(stride_z * z, dd-patch_size[2])
test_patch = image[xs:xs+patch_size[0], ys:ys+patch_size[1], zs:zs+patch_size[2]]
test_patch = np.expand_dims(np.expand_dims(test_patch,axis=0),axis=0).astype(np.float32)
test_patch = torch.from_numpy(test_patch).cuda()
y1 = net(test_patch)['out']
y = F.softmax(y1, dim=1)
y = y.cpu().data.numpy()
y = y[0,:,:,:,:]
score_map[:, xs:xs+patch_size[0], ys:ys+patch_size[1], zs:zs+patch_size[2]] \
= score_map[:, xs:xs+patch_size[0], ys:ys+patch_size[1], zs:zs+patch_size[2]] + y
cnt[xs:xs+patch_size[0], ys:ys+patch_size[1], zs:zs+patch_size[2]] \
= cnt[xs:xs+patch_size[0], ys:ys+patch_size[1], zs:zs+patch_size[2]] + 1
score_map = score_map/np.expand_dims(cnt,axis=0)
label_map = np.argmax(score_map, axis = 0)
if add_pad:
label_map = label_map[wl_pad:wl_pad+w,hl_pad:hl_pad+h,dl_pad:dl_pad+d]
score_map = score_map[:,wl_pad:wl_pad+w,hl_pad:hl_pad+h,dl_pad:dl_pad+d]
return label_map, score_map
def test():
args = get_args()
# Project Saving Path
project_path = args.project + '_{}_label_{}/'.format(args.backbone, args.labeled_percentage)
# Load Data
with open(args.data_path + '/test.list', 'r') as f:
image_list = f.readlines()
image_list = sorted([item.replace('\n', '').split(".")[0] for item in image_list])
weights_path = project_path + 'weights/' + args.model_weights
model = load_model(model_weights=weights_path, in_channels=args.in_channels, num_classes=args.num_classes)
model.eval()
test_save_path = project_path + '/predictions/'
ensure_dir(test_save_path)
############################
# Test
############################
print('Testing')
results = {i: [] for i in range(4)}
for case in tqdm(image_list):
h5f = h5py.File(args.data_path + "/LA_h5/" + case + "/mri_norm2.h5", 'r')
image = h5f['image'][:]
label = h5f['label'][:]
prediction, score_map = test_single_case(net=model, image=image, stride_xy=18, stride_z=4, patch_size=args.image_size, num_classes=args.num_classes)
prediction_copy = copy.deepcopy(prediction)
prediction_u = measure_img(prediction, t_num=1).astype('bool')
prediction = prediction_u * prediction_copy
prediction[prediction > args.num_classes-1] = args.num_classes-1
save_path = project_path + 'predictions/'
ensure_dir(save_path)
img_itk = sitk.GetImageFromArray(image.astype(np.float32))
img_itk.SetSpacing((1, 1, 1))
prd_itk = sitk.GetImageFromArray(prediction.astype(np.float32))
prd_itk.SetSpacing((1, 1, 1))
lab_itk = sitk.GetImageFromArray(label.astype(np.float32))
lab_itk.SetSpacing((1, 1, 1))
sitk.WriteImage(prd_itk, test_save_path + case + "_pred.nii.gz")
sitk.WriteImage(img_itk, test_save_path + case + "_img.nii.gz")
sitk.WriteImage(lab_itk, test_save_path + case + "_gt.nii.gz")
result = calculate_metric_percase(pred=prediction, gt=label)
for i in range(4):
results[i].append(result[i])
# save results
data_frame = pd.DataFrame(
data={i: results[i] for i in range(4)},
index=range(1, len(image_list) + 1))
data_frame.to_csv(project_path + '/' + 'evaluation_LA.csv', index_label='Index')
result = data_frame.values
avg_score = np.mean(result, axis=0)
with open(project_path+'/performance.txt', 'w') as f:
f.writelines('metric is {} \n'.format(avg_score))
print('AVG Score:', avg_score)
print('EVAL FINISHED!')
if __name__ == '__main__':
test()