-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdata_generator.py
More file actions
119 lines (85 loc) · 5.48 KB
/
data_generator.py
File metadata and controls
119 lines (85 loc) · 5.48 KB
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
import os
import numpy as np
from skimage.transform import resize
import nibabel as nib
def resize_3d_image(image, shape):
resized_image = resize(image, output_shape=shape)
if np.amax(resized_image) == np.amin(resized_image):
normalised_image = resized_image
else:
normalised_image = (resized_image-np.amin(resized_image))/(np.amax(resized_image)-np.amin(resized_image))
return normalised_image
def train_generator(f_path, batch_size, moving_image_shape, fixed_image_shape, with_label_inputs=True):
moving_images_path = os.path.join(f_path, 'us_images')
fixed_images_path = os.path.join(f_path, 'mr_images')
if with_label_inputs:
moving_labels_path = os.path.join(f_path, 'us_labels')
fixed_labels_path = os.path.join(f_path, 'mr_labels')
all_names = np.array(os.listdir(fixed_images_path))
while True:
batch_names = all_names[np.random.permutation(len(all_names))[:batch_size]]
moving_images_batch = np.zeros((batch_size, *moving_image_shape))
fixed_images_batch = np.zeros((batch_size, *fixed_image_shape))
if with_label_inputs:
moving_labels_batch = np.zeros((batch_size, *moving_image_shape))
fixed_labels_batch = np.zeros((batch_size, *fixed_image_shape))
for i, f_name in enumerate(batch_names):
moving_image = nib.load(os.path.join(moving_images_path, f_name)).get_fdata()
fixed_image = nib.load(os.path.join(fixed_images_path, f_name)).get_fdata()
if with_label_inputs:
moving_label = nib.load(os.path.join(moving_labels_path, f_name)).get_fdata()
fixed_label = nib.load(os.path.join(fixed_labels_path, f_name)).get_fdata()
label_to_select = np.random.randint(6) #pick one label randomly for training
moving_images_batch[i] = resize_3d_image(moving_image, moving_image_shape)
fixed_images_batch[i] = resize_3d_image(fixed_image, fixed_image_shape)
if with_label_inputs:
moving_labels_batch[i] = resize_3d_image(moving_label[:, :, :, label_to_select], moving_image_shape)
fixed_labels_batch[i] = resize_3d_image(fixed_label[:, :, :, label_to_select], fixed_image_shape)
zero_phis = np.zeros([batch_size, *moving_image_shape[:-1], 3])
if with_label_inputs:
inputs = [moving_images_batch, fixed_images_batch, moving_labels_batch, fixed_labels_batch]
outputs = [fixed_images_batch, fixed_labels_batch, zero_phis]
else:
inputs = [moving_images_batch, fixed_images_batch]
outputs = [fixed_images_batch, zero_phis]
yield (inputs, outputs)
def test_generator(f_path, batch_size, moving_image_shape, fixed_image_shape, start_index, end_index, label_num, with_label_inputs=True):
moving_images_path = os.path.join(f_path, 'us_images')
fixed_images_path = os.path.join(f_path, 'mr_images')
if with_label_inputs:
moving_labels_path = os.path.join(f_path, 'us_labels')
fixed_labels_path = os.path.join(f_path, 'mr_labels')
all_names = np.array(os.listdir(fixed_images_path))[start_index: end_index]
if start_index and end_index is not None:
n_steps = int(np.floor((end_index - start_index) / batch_size))
else:
start_index = 0
end_index = len(all_names)
n_steps =int( np.floor((end_index - start_index) / batch_size))
for step in range(n_steps):
batch_names = all_names[step*batch_size:(step*batch_size)+batch_size]
moving_images_batch = np.zeros((batch_size, *moving_image_shape))
fixed_images_batch = np.zeros((batch_size, *fixed_image_shape))
if with_label_inputs:
moving_labels_batch = np.zeros((batch_size, *moving_image_shape))
fixed_labels_batch = np.zeros((batch_size, *fixed_image_shape))
for i, f_name in enumerate(batch_names):
moving_image = nib.load(os.path.join(moving_images_path, f_name)).get_fdata()
fixed_image = nib.load(os.path.join(fixed_images_path, f_name)).get_fdata()
if with_label_inputs:
moving_label = nib.load(os.path.join(moving_labels_path, f_name)).get_fdata() # if label not available, just pass zeros
fixed_label = nib.load(os.path.join(fixed_labels_path, f_name)).get_fdata() # if label not available, just pass zeros
label_to_select = label_num #pick one label randomly for training
moving_images_batch[i] = resize_3d_image(moving_image, moving_image_shape)
fixed_images_batch[i] = resize_3d_image(fixed_image, fixed_image_shape)
if with_label_inputs:
moving_labels_batch[i] = resize_3d_image(moving_label[:, :, :, label_to_select], moving_image_shape)
fixed_labels_batch[i] = resize_3d_image(fixed_label[:, :, :, label_to_select], fixed_image_shape)
zero_phis = np.zeros([batch_size, *moving_image_shape[:-1], 3])
if with_label_inputs:
inputs = [moving_images_batch, fixed_images_batch, moving_labels_batch, fixed_labels_batch]
outputs = [fixed_images_batch, fixed_labels_batch, zero_phis]
else:
inputs = [moving_images_batch, fixed_images_batch]
outputs = [fixed_images_batch, zero_phis]
yield (inputs, outputs)