|
a |
|
b/code/deepseg.py |
|
|
1 |
import os, time, math |
|
|
2 |
import nibabel as nib, numpy as np |
|
|
3 |
import tensorflow as tf |
|
|
4 |
import glob |
|
|
5 |
from image_utils import * |
|
|
6 |
|
|
|
7 |
def deeplearningseg(model_path, test_dir, atlas_dir): |
|
|
8 |
|
|
|
9 |
with tf.Session() as sess: |
|
|
10 |
|
|
|
11 |
sess.run(tf.global_variables_initializer()) |
|
|
12 |
|
|
|
13 |
# Import the computation graph and restore the variable values |
|
|
14 |
saver = tf.train.import_meta_graph('{0}.meta'.format(model_path)) |
|
|
15 |
saver.restore(sess, '{0}'.format(model_path)) |
|
|
16 |
|
|
|
17 |
# Process each subject subdirectory |
|
|
18 |
table_time = [] |
|
|
19 |
|
|
|
20 |
|
|
|
21 |
if os.path.exists('{0}/subjnames.txt'.format(test_dir)): |
|
|
22 |
os.system('rm {0}/*.txt'.format(test_dir)) |
|
|
23 |
os.system('touch {0}/subjnames.txt'.format(test_dir)) |
|
|
24 |
for data in sorted(os.listdir(test_dir)): |
|
|
25 |
|
|
|
26 |
print(data) |
|
|
27 |
|
|
|
28 |
data_dir = os.path.join(test_dir, data) |
|
|
29 |
|
|
|
30 |
if not os.path.isdir(data_dir): |
|
|
31 |
print(' {0} is not a valid directory, Skip'.format(data_dir)) |
|
|
32 |
continue |
|
|
33 |
|
|
|
34 |
file = open('{0}/subjnames.txt'.format(test_dir),'a') |
|
|
35 |
file.write('{0}\n'.format(data)) |
|
|
36 |
file.close() |
|
|
37 |
|
|
|
38 |
if os.path.exists('{0}/PHsegmentation_ED.gipl'.format(data_dir)): |
|
|
39 |
os.system('rm {0}/*.gipl'.format(data_dir)) |
|
|
40 |
if os.path.exists('{0}/lvsa_.nii.gz'.format(data_dir)): |
|
|
41 |
os.system('rm {0}/lvsa_*.nii.gz'.format(data_dir)) |
|
|
42 |
os.system('rm {0}/seg_*.nii.gz'.format(data_dir)) |
|
|
43 |
|
|
|
44 |
originalnii = glob.glob('{0}/*.nii'.format(data_dir)) |
|
|
45 |
if not originalnii: |
|
|
46 |
print(' original nifit image does not exist, use lvsa.nii.gz') |
|
|
47 |
originalnii = glob.glob('{0}/*.nii.gz'.format(data_dir)) |
|
|
48 |
imagePreprocessing(originalnii[0], data_dir, atlas_dir) |
|
|
49 |
else: |
|
|
50 |
print(' start image preprocessing ...') |
|
|
51 |
imagePreprocessing(originalnii[0], data_dir, atlas_dir) |
|
|
52 |
|
|
|
53 |
# Process ED and ES time frames |
|
|
54 |
image_ED_name = '{0}/lvsa_{1}.nii.gz'.format(data_dir, 'ED') |
|
|
55 |
image_ES_name = '{0}/lvsa_{1}.nii.gz'.format(data_dir, 'ES') |
|
|
56 |
|
|
|
57 |
if not os.path.exists(image_ED_name) or not os.path.exists(image_ES_name): |
|
|
58 |
print(' Image {0} or {1} does not exist. Skip.'.format(image_ED_name, image_ES_name)) |
|
|
59 |
continue |
|
|
60 |
|
|
|
61 |
if os.path.exists('{0}/{1}'.format(data_dir, 'dofs')) or \ |
|
|
62 |
os.path.exists('{0}/{1}'.format(data_dir, 'segs')) or \ |
|
|
63 |
os.path.exists('{0}/{1}'.format(data_dir, 'tmps')) or \ |
|
|
64 |
os.path.exists('{0}/{1}'.format(data_dir, 'sizes')) or \ |
|
|
65 |
os.path.exists('{0}/{1}'.format(data_dir, 'motion')) or \ |
|
|
66 |
os.path.exists('{0}/{1}'.format(data_dir, 'vtks')): |
|
|
67 |
|
|
|
68 |
os.system('rm -rf {0}/{1}'.format(data_dir, 'dofs')) |
|
|
69 |
os.system('rm -rf {0}/{1}'.format(data_dir, 'segs')) |
|
|
70 |
os.system('rm -rf {0}/{1}'.format(data_dir, 'tmps')) |
|
|
71 |
os.system('rm -rf {0}/{1}'.format(data_dir, 'sizes')) |
|
|
72 |
os.system('rm -rf {0}/{1}'.format(data_dir, 'motion')) |
|
|
73 |
os.system('rm -rf {0}/{1}'.format(data_dir, 'vtks')) |
|
|
74 |
|
|
|
75 |
os.mkdir('{0}/{1}'.format(data_dir, 'dofs')) |
|
|
76 |
os.mkdir('{0}/{1}'.format(data_dir, 'segs')) |
|
|
77 |
os.mkdir('{0}/{1}'.format(data_dir, 'tmps')) |
|
|
78 |
os.mkdir('{0}/{1}'.format(data_dir, 'sizes')) |
|
|
79 |
os.mkdir('{0}/{1}'.format(data_dir, 'motion')) |
|
|
80 |
os.mkdir('{0}/{1}'.format(data_dir, 'vtks')) |
|
|
81 |
|
|
|
82 |
else: |
|
|
83 |
|
|
|
84 |
os.mkdir('{0}/{1}'.format(data_dir, 'dofs')) |
|
|
85 |
os.mkdir('{0}/{1}'.format(data_dir, 'segs')) |
|
|
86 |
os.mkdir('{0}/{1}'.format(data_dir, 'tmps')) |
|
|
87 |
os.mkdir('{0}/{1}'.format(data_dir, 'sizes')) |
|
|
88 |
os.mkdir('{0}/{1}'.format(data_dir, 'motion')) |
|
|
89 |
os.mkdir('{0}/{1}'.format(data_dir, 'vtks')) |
|
|
90 |
|
|
|
91 |
for fr in ['ED', 'ES']: |
|
|
92 |
|
|
|
93 |
image_name = '{0}/lvsa_{1}.nii.gz'.format(data_dir, fr) |
|
|
94 |
|
|
|
95 |
# Read the image |
|
|
96 |
print(' Reading {} ...'.format(image_name)) |
|
|
97 |
nim = nib.load(image_name) |
|
|
98 |
image = nim.get_data() |
|
|
99 |
|
|
|
100 |
imageOrg = np.squeeze(image, axis=-1).astype(np.int16) |
|
|
101 |
tmp = imageOrg |
|
|
102 |
|
|
|
103 |
X, Y, Z = image.shape[:3] |
|
|
104 |
|
|
|
105 |
print(' Segmenting {0} frame ...'.format(fr)) |
|
|
106 |
|
|
|
107 |
# print(' Segmenting {0} frame {1} ...'.format(fr, slice)) |
|
|
108 |
start_seg_time = time.time() |
|
|
109 |
|
|
|
110 |
for slice in range(Z): |
|
|
111 |
|
|
|
112 |
image = imageOrg[:,:,slice] |
|
|
113 |
|
|
|
114 |
if image.ndim == 2: |
|
|
115 |
image = np.expand_dims(image, axis=2) |
|
|
116 |
|
|
|
117 |
# Intensity rescaling |
|
|
118 |
image = rescale_intensity(image, (1, 99)) |
|
|
119 |
# Pad the image size to be a factor of 16 so that the downsample and upsample procedures |
|
|
120 |
# in the network will result in the same image size at each resolution level. |
|
|
121 |
X2, Y2 = int(math.ceil(X / 16.0)) * 16, int(math.ceil(Y / 16.0)) * 16 |
|
|
122 |
x_pre, y_pre = int((X2 - X) / 2), int((Y2 - Y) / 2) |
|
|
123 |
x_post, y_post = (X2 - X) - x_pre, (Y2 - Y) - y_pre |
|
|
124 |
image = np.pad(image, ((x_pre, x_post), (y_pre, y_post), (0, 0)), 'constant') |
|
|
125 |
|
|
|
126 |
# Transpose the shape to NXYC |
|
|
127 |
image = np.transpose(image, axes=(2, 0, 1)).astype(np.float32) |
|
|
128 |
image = np.expand_dims(image, axis=-1) |
|
|
129 |
|
|
|
130 |
# Evaluate the networ |
|
|
131 |
prob, pred = sess.run(['probE:0', 'predR:0'], feed_dict={'image:0': image, 'training:0': False}) |
|
|
132 |
|
|
|
133 |
# Transpose and crop the segmentation to recover the original size |
|
|
134 |
pred = np.transpose(pred, axes=(1, 2, 0)) |
|
|
135 |
|
|
|
136 |
pred = pred[x_pre:x_pre + X, y_pre:y_pre + Y] |
|
|
137 |
pred = np.squeeze(pred, axis=-1).astype(np.int16) |
|
|
138 |
tmp[:,:,slice] = pred |
|
|
139 |
|
|
|
140 |
seg_time = time.time() - start_seg_time |
|
|
141 |
print(' Segmentation time = {:3f}s'.format(seg_time)) |
|
|
142 |
table_time += [seg_time] |
|
|
143 |
|
|
|
144 |
pred = tmp |
|
|
145 |
|
|
|
146 |
nim2 = nib.Nifti1Image(pred, nim.affine) |
|
|
147 |
nim2.header['pixdim'] = nim.header['pixdim'] |
|
|
148 |
nib.save(nim2, '{0}/segs/seg_lvsa_{1}.nii.gz'.format(data_dir, fr)) |
|
|
149 |
|
|
|
150 |
print('Average segmentation time = {:.3f}s per frame'.format(np.mean(table_time))) |