-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
executable file
·375 lines (327 loc) · 16.2 KB
/
inference.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import torch,monai
import sys,os,json
import SimpleITK as sitk
import numpy as np
import torch.nn as nn
from datautils import resampleVolume,adjust_image_direction
from MPUM import MPUM
from tqdm import tqdm
from collections import OrderedDict
current_dir = os.path.dirname(os.path.abspath(__file__))
def resample_image(input_image, reference_image):
def dataset_config_to_rulematrix(x):
with open(x,"r") as f:
x = json.load(f)
rules = torch.zeros(len(x),215)
for key in x:
rules[x[key]["labelindex"]][x[key]["modelindex"]] = 1
rules[0] = 1 - torch.sum(rules[1:],dim=0)
return rules
# 创建一个重采样过滤器
resampler = sitk.ResampleImageFilter()
resampler.SetReferenceImage(reference_image)
# 设置插值方法,通常使用线性插值
resampler.SetInterpolator(sitk.sitkLinear)
# 设置输出使用参考图像的spacing, origin和direction
resampler.SetOutputSpacing(reference_image.GetSpacing())
resampler.SetOutputOrigin(reference_image.GetOrigin())
resampler.SetOutputDirection(reference_image.GetDirection())
# 设置输出图像的size
resampler.SetSize(reference_image.GetSize())
resampler.SetDefaultPixelValue(0)
# 执行重采样
return resampler.Execute(input_image)
# def inference(config, nii_path,output_seg_path,output_stdct_path=None,check=True,modelname=None,dataset_mapping_to_model=None):
# '''
# dataset_mapping_to_model: .json file path
# '''
# ct_path = nii_path
# assert "." not in output_seg_path, "output_seg_path should be a dir path, not a file path"
# print(f"提供的NIfTI路径是:{ct_path}")
# # file_path = os.path.dirname(os.path.abspath(__file__))
# orict_itk = sitk.ReadImage(os.path.join(ct_path))
# ct_itk = sitk.ReadImage(os.path.join(ct_path))
# print("----------------direction check and spacing check------------------------")
# print("before processing, spacing:",ct_itk.GetSpacing())
# print("before processing, direction:",ct_itk.GetDirection())
# new_direction = (-1, 0, 0, 0, -1, 0, 0, 0, 1)
# new_spacing = (2,2,2) # 保持间距不变
# direction_check = np.mean(np.abs(np.array(ct_itk.GetDirection()) - np.array(new_direction)))
# spacing_check = np.mean(np.abs(np.array(ct_itk.GetSpacing()) - np.array([2,2,2])))
# print("----------------pre-process <LUCID Standard Protocol>------------------------")
# ct_itk = resampleVolume(new_spacing,ct_itk,resamplemethod=sitk.sitkLinear)
# ct_itk = adjust_image_direction(ct_itk, new_direction)
# print("after processing, spacing:",ct_itk.GetSpacing())
# print("after processing, direction:",ct_itk.GetDirection())
# if output_stdct_path is not None:
# output_stdct_path_ = os.path.dirname(output_stdct_path)
# if not os.path.exists(output_stdct_path_):
# os.makedirs(output_stdct_path_)
# print(f"目录已创建:{output_stdct_path_}")
# sitk.WriteImage(ct_itk, output_stdct_path)
# print("standard protocol nii has been write in ",output_stdct_path)
# else:
# print("if need to save CT.nii.gz file in standard protocol (1.5mm), use arg <output_stdct_path>")
# def scale_intensity_range(ct, a_min, a_max, b_min, b_max, clip):
# if clip:
# ct = torch.clamp(ct, min=a_min, max=a_max)
# # 线性缩放
# ct = (ct - a_min) / (a_max - a_min) * (b_max - b_min) + b_min
# return ct
# ct = sitk.GetArrayFromImage(ct_itk)
# ct = torch.tensor(ct).float().unsqueeze(0).unsqueeze(0)
# print(ct.shape)
# if config["modality"] == 'ct':
# ct = scale_intensity_range(ct, a_min=-1000, a_max=1000, b_min=0.0, b_max=1.0, clip=True)
# elif config["modality"] == 'pet':
# ct = scale_intensity_range(ct, a_min=0, a_max=20, b_min=0.0, b_max=1.0, clip=True)
# elif config["modality"] == "mr":
# ct = scale_intensity_range(ct, a_min=0, a_max=3000, b_min=0.0, b_max=1.0, clip=True)
# print("single model mode!!")
# tissueclip = torch.load("../tissueclip_RN101.pth").to("cuda:0")
# print(config)
# model = MPUM(config,tissueclip,1)
# print(config["ckpt"])
# ckpt = torch.load(config["ckpt"],map_location="cpu")
# from collections import OrderedDict
# new_state_dict = OrderedDict()
# for k, v in ckpt['model'].items(): # 假设权重存储在'ckpt['model']'中
# name = k[7:] if k.startswith('module.') else k # 移除 'module.' 前缀
# new_state_dict[name] = v
# model.load_state_dict(new_state_dict)
# model = model.to("cuda:0")
# model = model.eval()
# print(ct.shape)
# print("----------------sliding_window_inference------------------------")
# with torch.no_grad():
# wb_pred = monai.inferers.sliding_window_inference(
# ct,(128,128,128),
# sw_batch_size=1,
# predictor=model,
# overlap=0.5,
# mode="gaussian",
# sw_device="cuda:0",
# device="cpu",
# progress=True)
# # wb_pred = torch.sigmoid(wb_pred)
# print("----------------post-process------------------------")
# if dataset_mapping_to_model is not None:
# dataset_to_model_config = dataset_config_to_rulematrix(dataset_mapping_to_model)
# b,c,z,w,y = wb_pred.shape
# pred = torch.matmul(dataset_to_model_config.float(), wb_pred.view(b,c,-1)).view(b,-1,z,w,y)
# for ii,value in enumerate(torch.sum(dataset_to_model_config,dim=1)):
# if value == 1:
# continue
# pred[0,ii],_ = torch.max(wb_pred[0][dataset_to_model_config[ii]==1],dim=0)
# wb_pred = pred.float()
# with open(dataset_mapping_to_model,"r") as f:
# d = json.load(f)
# readme = {}
# for key in d:
# readme[len(readme)] = key
# else:
# from categories import prediction as label215
# readme = label215
# combined = torch.argmax(wb_pred[0],dim=0).detach().cpu()
# sitk_image = sitk.GetImageFromArray(combined)
# # 设置方向和像素间距
# sitk_image.SetDirection(ct_itk.GetDirection())
# sitk_image.SetSpacing(ct_itk.GetSpacing())
# sitk_image.SetOrigin(ct_itk.GetOrigin())
# resampler = sitk.ResampleImageFilter()
# resampler.SetReferenceImage(orict_itk)
# resampler.SetInterpolator(sitk.sitkNearestNeighbor)
# sitk_image = resampler.Execute(sitk_image)
# array = sitk.GetArrayFromImage(sitk_image)
# print(np.max(array))
# print(array.shape)
# if not os.path.exists(output_seg_path):
# os.makedirs(output_seg_path)
# print(f"目录已创建:{output_seg_path}")
# sitk.WriteImage(sitk_image, os.path.join(output_seg_path,"merge.nii.gz"))
# print("create nii.gz ",os.path.join(output_seg_path,"merge.nii.gz"))
# with open(os.path.join(output_seg_path,"readme.json"),"w") as f:
# json.dump(readme,f)
def inference(config, nii_path,output_seg_path,output_stdct_path=None,check=True,modelname=None,dataset_mapping_to_model=None):
'''
dataset_mapping_to_model: .json file path
'''
config["tissuenumber"] = 215
ct_path = nii_path
# assert "." not in output_seg_path, "output_seg_path should be a dir path, not a file path"
print(f"提供的NIfTI路径是:{ct_path}")
# file_path = os.path.dirname(os.path.abspath(__file__))
orict_itk = sitk.ReadImage(os.path.join(ct_path))
ct_itk = sitk.ReadImage(os.path.join(ct_path))
print("----------------direction check and spacing check------------------------")
print("before processing, spacing:",ct_itk.GetSpacing())
print("before processing, direction:",ct_itk.GetDirection())
new_direction = (-1, 0, 0, 0, -1, 0, 0, 0, 1)
new_spacing = (2,2,2) # 保持间距不变
direction_check = np.mean(np.abs(np.array(ct_itk.GetDirection()) - np.array(new_direction)))
spacing_check = np.mean(np.abs(np.array(ct_itk.GetSpacing()) - np.array([2,2,2])))
print("----------------pre-process <LUCID Standard Protocol>------------------------")
ct_itk = resampleVolume(new_spacing,ct_itk,resamplemethod=sitk.sitkLinear)
ct_itk = adjust_image_direction(ct_itk, new_direction)
print("after processing, spacing:",ct_itk.GetSpacing())
print("after processing, direction:",ct_itk.GetDirection())
if output_stdct_path is not None:
output_stdct_path_ = os.path.dirname(output_stdct_path)
if not os.path.exists(output_stdct_path_):
os.makedirs(output_stdct_path_)
print(f"目录已创建:{output_stdct_path_}")
sitk.WriteImage(ct_itk, output_stdct_path)
print("standard protocol nii has been write in ",output_stdct_path)
else:
print("if need to save CT.nii.gz file in standard protocol (1.5mm), use arg <output_stdct_path>")
def scale_intensity_range(ct, a_min, a_max, b_min, b_max, clip):
if clip:
ct = torch.clamp(ct, min=a_min, max=a_max)
# 线性缩放
ct = (ct - a_min) / (a_max - a_min) * (b_max - b_min) + b_min
return ct
ct = sitk.GetArrayFromImage(ct_itk)
ct = torch.tensor(ct).float().unsqueeze(0).unsqueeze(0)
print(ct.shape)
if config["modality"] == 'ct':
ct = scale_intensity_range(ct, a_min=-1000, a_max=1000, b_min=0.0, b_max=1.0, clip=True)
elif config["modality"] == 'pet':
ct = scale_intensity_range(ct, a_min=0, a_max=20, b_min=0.0, b_max=1.0, clip=True)
elif config["modality"] == "mr":
ct = scale_intensity_range(ct, a_min=0, a_max=3000, b_min=0.0, b_max=1.0, clip=True)
if isinstance(config["ckpt"],list):
print("multi model mode!!")
wb_preds = []
for modelnum,oneckpt in enumerate(config["ckpt"]):
print("model {} inference...".format(modelnum))
tissueclip = torch.load(os.path.join(current_dir,"tissueclip_RN101.pth")).to("cuda:0")
model = MPUM(config,tissueclip,1)
ckpt = torch.load(oneckpt,map_location="cpu")
new_state_dict = OrderedDict()
for k, v in ckpt['model'].items(): # 假设权重存储在'ckpt['model']'中
name = k[7:] if k.startswith('module.') else k # 移除 'module.' 前缀
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
model = model.to("cuda:0")
model = model.eval()
print(ct.shape)
print("----------------sliding_window_inference------------------------")
with torch.no_grad():
wb_pred = monai.inferers.sliding_window_inference(
ct,(128,128,128),
sw_batch_size=1,
predictor=model,
overlap=0.5,
mode="gaussian",
sw_device="cuda:0",
device="cpu",
progress=True)
wb_pred = torch.sigmoid(wb_pred)
if config["tissue"] == "all":
pass
elif config["tissue"] == "brain":
wb_pred[:,1:132] = 0
wb_preds.append(wb_pred)
wb_pred = 0
for www in wb_preds:
wb_pred += www
if dataset_mapping_to_model is not None:
dataset_to_model_config = dataset_config_to_rulematrix(dataset_mapping_to_model)
b,c,z,w,y = wb_pred.shape
pred = torch.matmul(dataset_to_model_config.float(), wb_pred.view(b,c,-1)).view(b,-1,z,w,y)
for ii,value in enumerate(torch.sum(dataset_to_model_config,dim=1)):
if value == 1:
continue
pred[0,ii],_ = torch.max(wb_pred[0][dataset_to_model_config[ii]==1],dim=0)
wb_pred = pred.float()
with open(dataset_mapping_to_model,"r") as f:
d = json.load(f)
readme = {}
for key in d:
readme[len(readme)] = key
else:
from categories import prediction as label215
readme = label215
combined = torch.argmax(wb_pred[0],dim=0).detach().cpu()
sitk_image = sitk.GetImageFromArray(combined)
# 设置方向和像素间距
sitk_image.SetDirection(ct_itk.GetDirection())
sitk_image.SetSpacing(ct_itk.GetSpacing())
sitk_image.SetOrigin(ct_itk.GetOrigin())
resampler = sitk.ResampleImageFilter()
resampler.SetReferenceImage(orict_itk)
resampler.SetInterpolator(sitk.sitkNearestNeighbor)
sitk_image = resampler.Execute(sitk_image)
array = sitk.GetArrayFromImage(sitk_image)
print(np.max(array))
print(array.shape)
if not os.path.exists(output_seg_path):
os.makedirs(output_seg_path)
print(f"目录已创建:{output_seg_path}")
sitk.WriteImage(sitk_image, os.path.join(output_seg_path,"merge.nii.gz"))
print("create nii.gz ",os.path.join(output_seg_path,"merge.nii.gz"))
else:
print("single model mode!!")
tissueclip = torch.load(os.path.join(current_dir,"tissueclip_RN101.pth")).to("cuda:0")
print(config)
model = MPUM(config,tissueclip,1)
print(config["ckpt"])
ckpt = torch.load(config["ckpt"],map_location="cpu")
new_state_dict = OrderedDict()
for k, v in ckpt['model'].items(): # 假设权重存储在'ckpt['model']'中
name = k[7:] if k.startswith('module.') else k # 移除 'module.' 前缀
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
model = model.to("cuda:0")
model = model.eval()
print(ct.shape)
print("----------------sliding_window_inference------------------------")
with torch.no_grad():
wb_pred = monai.inferers.sliding_window_inference(
ct,(128,128,128),
sw_batch_size=1,
predictor=model,
overlap=0.5,
mode="gaussian",
sw_device="cuda:0",
device="cpu",
progress=True)
# wb_pred = torch.sigmoid(wb_pred)
print("----------------post-process------------------------")
if dataset_mapping_to_model is not None:
dataset_to_model_config = dataset_config_to_rulematrix(dataset_mapping_to_model)
b,c,z,w,y = wb_pred.shape
pred = torch.matmul(dataset_to_model_config.float(), wb_pred.view(b,c,-1)).view(b,-1,z,w,y)
for ii,value in enumerate(torch.sum(dataset_to_model_config,dim=1)):
if value == 1:
continue
pred[0,ii],_ = torch.max(wb_pred[0][dataset_to_model_config[ii]==1],dim=0)
wb_pred = pred.float()
with open(dataset_mapping_to_model,"r") as f:
d = json.load(f)
readme = {}
for key in d:
readme[len(readme)] = key
else:
from categories import prediction as label215
readme = label215
combined = torch.argmax(wb_pred[0],dim=0).detach().cpu()
sitk_image = sitk.GetImageFromArray(combined)
# 设置方向和像素间距
sitk_image.SetDirection(ct_itk.GetDirection())
sitk_image.SetSpacing(ct_itk.GetSpacing())
sitk_image.SetOrigin(ct_itk.GetOrigin())
resampler = sitk.ResampleImageFilter()
resampler.SetReferenceImage(orict_itk)
resampler.SetInterpolator(sitk.sitkNearestNeighbor)
sitk_image = resampler.Execute(sitk_image)
array = sitk.GetArrayFromImage(sitk_image)
print(np.max(array))
print(array.shape)
if not os.path.exists(output_seg_path):
os.makedirs(output_seg_path)
print(f"目录已创建:{output_seg_path}")
sitk.WriteImage(sitk_image, os.path.join(output_seg_path,"merge.nii.gz"))
print("create nii.gz ",os.path.join(output_seg_path,"merge.nii.gz"))
with open(os.path.join(output_seg_path,"readme.json"),"w") as f:
json.dump(readme,f)