-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathval.py
197 lines (147 loc) · 6.35 KB
/
val.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
194
195
196
197
"""
测试脚本
"""
import os
import copy
import collections
from time import time
import torch
import numpy as np
import pandas as pd
import scipy.ndimage as ndimage
import SimpleITK as sitk
import skimage.measure as measure
import skimage.morphology as morphology
from net.ResUNet import ResUNet
from utilities.calculate_metrics import Metirc
import parameter as para
os.environ['CUDA_VISIBLE_DEVICES'] = para.gpu
# 为了计算dice_global定义的两个变量
dice_intersection = 0.0
dice_union = 0.0
file_name = [] # 文件名称
time_pre_case = [] # 单例数据消耗时间
# 定义评价指标
liver_score = collections.OrderedDict()
liver_score['dice'] = []
liver_score['jacard'] = []
liver_score['voe'] = []
liver_score['fnr'] = []
liver_score['fpr'] = []
liver_score['assd'] = []
liver_score['rmsd'] = []
liver_score['msd'] = []
# 定义网络并加载参数
net = torch.nn.DataParallel(ResUNet(training=False)).cuda()
net.load_state_dict(torch.load(para.module_path))
net.eval()
for file_index, file in enumerate(os.listdir(para.test_ct_path)):
start = time()
file_name.append(file)
# 将CT读入内存
ct = sitk.ReadImage(os.path.join(para.test_ct_path, file), sitk.sitkInt16)
ct_array = sitk.GetArrayFromImage(ct)
origin_shape = ct_array.shape
# 将灰度值在阈值之外的截断掉
ct_array[ct_array > para.upper] = para.upper
ct_array[ct_array < para.lower] = para.lower
# min max 归一化
ct_array = ct_array.astype(np.float32)
ct_array = ct_array / 200
# 对CT使用双三次算法进行插值,插值之后的array依然是int16
ct_array = ndimage.zoom(ct_array, (1, para.down_scale, para.down_scale), order=3)
# 对slice过少的数据使用padding
too_small = False
if ct_array.shape[0] < para.size:
depth = ct_array.shape[0]
temp = np.ones((para.size, int(512 * para.down_scale), int(512 * para.down_scale))) * para.lower
temp[0: depth] = ct_array
ct_array = temp
too_small = True
# 滑动窗口取样预测
start_slice = 0
end_slice = start_slice + para.size - 1
count = np.zeros((ct_array.shape[0], 512, 512), dtype=np.int16)
probability_map = np.zeros((ct_array.shape[0], 512, 512), dtype=np.float32)
with torch.no_grad():
while end_slice < ct_array.shape[0]:
ct_tensor = torch.FloatTensor(ct_array[start_slice: end_slice + 1]).cuda()
ct_tensor = ct_tensor.unsqueeze(dim=0).unsqueeze(dim=0)
outputs = net(ct_tensor)
count[start_slice: end_slice + 1] += 1
probability_map[start_slice: end_slice + 1] += np.squeeze(outputs.cpu().detach().numpy())
# 由于显存不足,这里直接保留ndarray数据,并在保存之后直接销毁计算图
del outputs
start_slice += para.stride
end_slice = start_slice + para.size - 1
if end_slice != ct_array.shape[0] - 1:
end_slice = ct_array.shape[0] - 1
start_slice = end_slice - para.size + 1
ct_tensor = torch.FloatTensor(ct_array[start_slice: end_slice + 1]).cuda()
ct_tensor = ct_tensor.unsqueeze(dim=0).unsqueeze(dim=0)
outputs = net(ct_tensor)
count[start_slice: end_slice + 1] += 1
probability_map[start_slice: end_slice + 1] += np.squeeze(outputs.cpu().detach().numpy())
del outputs
pred_seg = np.zeros_like(probability_map)
pred_seg[probability_map >= (para.threshold * count)] = 1
if too_small:
temp = np.zeros((depth, 512, 512), dtype=np.float32)
temp += pred_seg[0: depth]
pred_seg = temp
# 将金标准读入内存
seg = sitk.ReadImage(os.path.join(para.test_seg_path, file.replace('volume', 'segmentation')), sitk.sitkUInt8)
seg_array = sitk.GetArrayFromImage(seg)
seg_array[seg_array > 0] = 1
# 对肝脏进行最大连通域提取,移除细小区域,并进行内部的空洞填充
pred_seg = pred_seg.astype(np.uint8)
liver_seg = copy.deepcopy(pred_seg)
liver_seg = measure.label(liver_seg, 4)
props = measure.regionprops(liver_seg)
max_area = 0
max_index = 0
for index, prop in enumerate(props, start=1):
if prop.area > max_area:
max_area = prop.area
max_index = index
liver_seg[liver_seg != max_index] = 0
liver_seg[liver_seg == max_index] = 1
liver_seg = liver_seg.astype(np.bool)
morphology.remove_small_holes(liver_seg, para.maximum_hole, connectivity=2, in_place=True)
liver_seg = liver_seg.astype(np.uint8)
# 计算分割评价指标
liver_metric = Metirc(seg_array, liver_seg, ct.GetSpacing())
liver_score['dice'].append(liver_metric.get_dice_coefficient()[0])
liver_score['jacard'].append(liver_metric.get_jaccard_index())
liver_score['voe'].append(liver_metric.get_VOE())
liver_score['fnr'].append(liver_metric.get_FNR())
liver_score['fpr'].append(liver_metric.get_FPR())
liver_score['assd'].append(liver_metric.get_ASSD())
liver_score['rmsd'].append(liver_metric.get_RMSD())
liver_score['msd'].append(liver_metric.get_MSD())
dice_intersection += liver_metric.get_dice_coefficient()[1]
dice_union += liver_metric.get_dice_coefficient()[2]
# 将预测的结果保存为nii数据
pred_seg = sitk.GetImageFromArray(liver_seg)
pred_seg.SetDirection(ct.GetDirection())
pred_seg.SetOrigin(ct.GetOrigin())
pred_seg.SetSpacing(ct.GetSpacing())
sitk.WriteImage(pred_seg, os.path.join(para.pred_path, file.replace('volume', 'pred')))
speed = time() - start
time_pre_case.append(speed)
print(file_index, 'this case use {:.3f} s'.format(speed))
print('-----------------------')
# 将评价指标写入到exel中
liver_data = pd.DataFrame(liver_score, index=file_name)
liver_data['time'] = time_pre_case
liver_statistics = pd.DataFrame(index=['mean', 'std', 'min', 'max'], columns=list(liver_data.columns))
liver_statistics.loc['mean'] = liver_data.mean()
liver_statistics.loc['std'] = liver_data.std()
liver_statistics.loc['min'] = liver_data.min()
liver_statistics.loc['max'] = liver_data.max()
writer = pd.ExcelWriter('./result.xlsx')
liver_data.to_excel(writer, 'liver')
liver_statistics.to_excel(writer, 'liver_statistics')
writer.save()
# 打印dice global
print('dice global:', dice_intersection / dice_union)