-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_pipeline.py
219 lines (193 loc) · 8.48 KB
/
test_pipeline.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
import numpy as np
import md
import os
from PIL import Image
import matplotlib
import scipy.misc
from options.test_options import TestOptions
from data import CreateDataLoader
from models import create_model
from util.visualizer import save_images
from util import html
from PIL import Image
from torchvision.transforms import functional as F
import shutil
# Data_root_3d = '/data0/geyunhao/MR2CT/ZS10307488/'
# Data_root_3d = '/data0/geyunhao/MR2CT_SAMPLING/ZS18111863/'
# Data_root_3d = '/data0/geyunhao/MR2CT_SAMPLING/ZS18158187/'
Data_root_3d = '/data0/geyunhao/MR2CT_X/' # whole testing data 46 patients
# Data_sam_root = '/data0/geyunhao/MR2CT_SAMPLING/'
# Data_root_3d = '/data0/geyunhao/MR2CT/'
Data_root_2d = '/home/geyunhao/Mapping/Mapping/pytorch-CycleGAN-and-pix2pix-master/datasets/test_pipline/'
DATA_NAME = 'test' # train, test, val, ex
IMAGE_TYPE = '.png'
'''
Turn the 3D slice to 2D slice and satisfy the structure of cycleGAN
'''
# save to .png
target_path_mr = Data_root_2d + DATA_NAME + 'A'
target_path_nct = Data_root_2d + DATA_NAME + 'B'
if not os.path.exists(target_path_mr):
os.makedirs(target_path_mr)
if not os.path.exists(target_path_nct):
os.makedirs(target_path_nct)
'''
load model
'''
opt = TestOptions().parse()
model = create_model(opt)
model.setup(opt)
for roots, dirs, files in os.walk(Data_root_3d):
for file in files:
file_path = os.path.join(roots, file)
if 'IP' in file:
'''
remove the last type
'''
shutil.rmtree(target_path_mr)
if not os.path.exists(target_path_mr):
os.makedirs(target_path_mr)
mr_3d = md.read_image(file_path) # (x,y,z)
mr_3d_np = mr_3d.to_numpy() # (z,y,x)
for i in range(mr_3d_np.shape[0]): # z
'''
get part of data
'''
# the approximately leg part in mri is range (0-60)
# the approximately pelvicum part in mri is range (70-230)
# the approximately lib part in mri is range (200-360)
# if i >= 70 and i <= 230:
'''
whole body
'''
slice0 = np.expand_dims(mr_3d_np[i, :, :], 0) # get slice data without decay (1,y,x)
'''
resize the mr
'''
# resize[1, 384, 549] to [1, 384, 548] for net_G
slice0 = slice0[:, :, :-1]
# resize[1, 384, 548] to [1, 512, 512]
slice0 = slice0[:, :, 18:530]
slice0 = np.pad(slice0, ((0, 0), (64, 64), (0, 0)), 'constant', constant_values=0)
'''
normalize 0-255
'''
mrIP_intensity_min = np.float32(0.0)
mrIP_intensity_max = np.float32(400.0)
# cut off the image
slice0[slice0 > mrIP_intensity_max] = mrIP_intensity_max
slice0[slice0 < mrIP_intensity_min] = mrIP_intensity_min
slice0 = (slice0 - mrIP_intensity_min) / (mrIP_intensity_max - mrIP_intensity_min) * 255
# nct_ref.from_numpy(slice0) # put slice data into ref
# im = Image.fromarray(slice0)
target_path = target_path_mr
target_filename = file_path.split('/')[-2] + '_'+ str(i) + IMAGE_TYPE
if not os.path.exists(target_path + '/' + target_filename):
scipy.misc.imsave(target_path + '/' + target_filename, slice0[0])
# im.save(target_path + '/' + target_filename)
'''
test
'''
opt = TestOptions().parse()
opt.nThreads = 1 # test code only supports nThreads = 1
opt.batchSize = 1 # test code only supports batchSize = 1
opt.serial_batches = True # no shuffle
opt.no_flip = True # no flip
opt.display_id = -1 # no visdom display
data_loader = CreateDataLoader(opt)
dataset = data_loader.load_data()
# # create website
# web_dir = os.path.join(opt.results_dir, opt.name, '%s_%s' % (opt.phase, opt.which_epoch))
# webpage = html.HTML(web_dir,
# 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.which_epoch))
nct_3d_samp = mr_3d # 384 * 549
for i, data in enumerate(dataset):
# if i >= opt.how_many:
if i >= dataset.__len__():
break
model.set_input(data)
model.test()
visuals = model.get_current_visuals()
img_path = model.get_image_paths()
if i % 5 == 0:
print('processing (%04d)-th image... %s' % (i, img_path))
# save_images(webpage, visuals, img_path, aspect_ratio=opt.aspect_ratio, width=opt.display_winsize)
'''
save to .mhd
'''
out_slice_np = visuals['fake_B'].squeeze(0).cpu().numpy()
out_slice_PIL = Image.fromarray(out_slice_np[0])
out_slice_np = np.array(F.resize(out_slice_PIL, (512, 512), 3))[np.newaxis, :]
# out_slice_np = np.array(F.resize(out_slice_PIL, (384, 549), 3))[np.newaxis, :]
out_slice_np = np.pad(out_slice_np[:, 64: 448, :], ((0, 0), (0, 0), (18, 19)), 'constant',
constant_values=-1) # recover to(1*384*548)
if i == 0:
test_output_array = out_slice_np
else:
test_output_array = np.concatenate((test_output_array, out_slice_np), axis=0)
nct_3d_samp.from_numpy(test_output_array)
# md.write_image(mr_3d_samp, webpage.web_dir + '/fake_nfct_' + img_path[0].split('_')[-2] + '.mhd')
# md.write_image(nct_3d_samp, webpage.web_dir + '/' + opt.name + '.mhd')
md.write_image(nct_3d_samp, './results/pure_cyclegan/' + file_path.split('/')[-2] + '.mhd')
print(file_path.split('/')[-2] +" tested!")
# webpage.save()
#
# '''
# test and save image
# '''
# opt = TestOptions().parse()
# opt.nThreads = 1 # test code only supports nThreads = 1
# opt.batchSize = 1 # test code only supports batchSize = 1
# opt.serial_batches = True # no shuffle
# opt.no_flip = True # no flip
# opt.display_id = -1 # no visdom display
# data_loader = CreateDataLoader(opt)
# dataset = data_loader.load_data()
# model = create_model(opt)
# model.setup(opt)
# # create website
# web_dir = os.path.join(opt.results_dir, opt.name, '%s_%s' % (opt.phase, opt.which_epoch))
# webpage = html.HTML(web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.which_epoch))
#
# # test
# Data_root_3d = '/data0/geyunhao/MR2CT_SAMPLING/'
# # nct_3d_samp = md.read_image(Data_root_3d + 'ZS10307488/nfct.nii.gz')
# # nct_3d_samp = md.read_image(Data_root_3d + 'ZS18111863/t1_wfi_wb_IP_Tra_9.nii') #384 * 549
# nct_3d_samp = md.read_image(Data_root_3d + 'ZS18158187/T1_WFI_WB_IP_Tra_8.nii') # 384 * 549
#
# # for j in range(4): # crop 4 times for the randomcrop in training
# # opt.test_crop_mode == str(j)
#
# for i, data in enumerate(dataset):
# # if i >= opt.how_many:
# if i >= dataset.__len__():
# break
# model.set_input(data)
# model.test()
# visuals = model.get_current_visuals()
# img_path = model.get_image_paths()
# if i % 5 == 0:
# print('processing (%04d)-th image... %s' % (i, img_path))
# # save_images(webpage, visuals, img_path, aspect_ratio=opt.aspect_ratio, width=opt.display_winsize)
#
# '''
# save to .mhd
# '''
# out_slice_np = visuals['fake_B'].squeeze(0).cpu().numpy()
# out_slice_PIL = Image.fromarray(out_slice_np[0])
# out_slice_np = np.array(F.resize(out_slice_PIL, (512, 512), 3))[np.newaxis, :]
# # out_slice_np = np.array(F.resize(out_slice_PIL, (384, 549), 3))[np.newaxis, :]
# out_slice_np = np.pad(out_slice_np[:, 64: 448, :], ((0, 0), (0, 0), (18, 19)), 'constant',
# constant_values=-1) # recover to(1*384*548)
# if i == 0:
# test_output_array = out_slice_np
# else:
# test_output_array = np.concatenate((test_output_array, out_slice_np), axis=0)
#
# nct_3d_samp.from_numpy(test_output_array)
# # md.write_image(mr_3d_samp, webpage.web_dir + '/fake_nfct_' + img_path[0].split('_')[-2] + '.mhd')
# md.write_image(nct_3d_samp, webpage.web_dir + '/' + opt.name + '.mhd')
#
# webpage.save()
#
#