-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
171 lines (113 loc) · 6.11 KB
/
utils.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
import os
import numpy as np
from skimage.transform import resize
from scipy.ndimage import rotate, zoom
import random
def norm_01(x):
return np.nan_to_num((x - np.amin(x, axis=(1, 2, 3), keepdims=True)) / (
np.amax(x, axis=(1, 2, 3), keepdims=True) - np.amin(x, axis=(1, 2, 3), keepdims=True)))
class dataGenerator_allStack:
def __init__(self, data_dir, data_list, batch_size, augment):
# input the loaction of the data
self.data_dir = data_dir
self.data_list = data_list
self.batch_size = batch_size
self.aug = augment
def _rescale(self, imageStack, MIN=0, MAX=1):
if imageStack[0].max() !=1:
ImageScale = []
for stack in range(imageStack.shape[0]):
temp = imageStack[stack,...]
tempScale = np.interp(temp, (temp.min(), temp.max()), (MIN, MAX))
ImageScale.append(tempScale.astype('float64'))
else:
ImageScale = imageStack
return np.asarray(ImageScale)
def _aug_img(self, img, msk):
# Flip the image horizontally or vertically
img_hori, msk_hori = np.fliplr(img), np.fliplr(msk)
img_ver, msk_ver = np.flipud(img), np.flipud(msk)
# Rotate the image by a random multiple of 90 degrees
angle = np.random.choice([90, 180, 270])
img_rot = rotate(img, angle, axes=(2, 1),reshape=True, mode='reflect')
msk_rot = rotate(msk, angle, axes=(2, 1),reshape=True, mode='reflect')
# Choose a random transformation
img_trans = [img_hori, img_ver, img_rot]
msk_trans = [msk_hori, msk_ver, msk_rot]
i = random.randint(0, len(img_trans)-1)
return img_trans[i], msk_trans[i].astype('int')
def imageLoader(self):
while True:
for index, dataset_name in enumerate(self.data_list):
print('load dataset:', dataset_name)
temp_dataset = np.load(self.data_dir + dataset_name)
imgs, msks = temp_dataset['img'],temp_dataset['mask']
L = imgs.shape[0]
batch_start = 0
batch_end = self.batch_size
while batch_start < L:
limit = min(batch_end, L)
# take data out and rescale
img_temp, msk_temp = (imgs[batch_start:limit]), (msks[batch_start:limit])
if self.aug == True:
img_temp, msk_temp = self._aug_img(img_temp, msk_temp)
# expand dimension for the model
img_temp = np.expand_dims(img_temp, axis=3)
msk_temp = np.expand_dims(msk_temp, axis=3)
yield(img_temp, msk_temp)
batch_start += self.batch_size
batch_end += self.batch_size
class dataGenerator_allStack_ft:
def __init__(self, data_dir, data_list, batch_size, augment):
# input the loaction of the data
self.data_dir = data_dir
self.data_list = data_list
self.batch_size = batch_size
self.aug = augment
def _rescale(self, imageStack, MIN=0, MAX=1):
if imageStack[0].max() !=1:
ImageScale = []
for stack in range(imageStack.shape[0]):
temp = imageStack[stack,...]
tempScale = np.interp(temp, (temp.min(), temp.max()), (MIN, MAX))
ImageScale.append(tempScale.astype('float64'))
else:
ImageScale = imageStack
return np.asarray(ImageScale)
def _aug_img(self, img, msk, m_msk):
# Flip the image horizontally or vertically
img_hori, msk_hori, m_msk_hori = np.fliplr(img), np.fliplr(msk), np.fliplr(m_msk)
img_ver, msk_ver, m_msk_ver = np.flipud(img), np.flipud(msk), np.flipud(m_msk)
# Rotate the image by a random multiple of 90 degrees
angle = np.random.choice([90, 180, 270])
img_rot = rotate(img, angle, axes=(2, 1),reshape=True, mode='reflect')
msk_rot = rotate(msk, angle, axes=(2, 1),reshape=True, mode='reflect')
m_msk_rot = rotate(m_msk, angle, axes=(2, 1),reshape=True, mode='reflect')
# Choose a random transformation
img_trans = [img_hori, img_ver, img_rot]
msk_trans = [msk_hori, msk_ver, msk_rot]
m_msk_trans = [m_msk_hori, m_msk_ver, m_msk_rot]
i = random.randint(0, len(img_trans)-1)
return img_trans[i], msk_trans[i].astype('int'), m_msk_trans[i].astype('int')
def imageLoader(self):
while True:
for index, dataset_name in enumerate(self.data_list):
print('load dataset:', dataset_name)
temp_dataset = np.load(self.data_dir + dataset_name)
imgs, msks, m_msks = temp_dataset['img'],temp_dataset['mask'], temp_dataset['m_mask']
L = imgs.shape[0]
batch_start = 0
batch_end = self.batch_size
while batch_start < L:
limit = min(batch_end, L)
# take data out and rescale
img_temp, msk_temp, m_msk_temp = (imgs[batch_start:limit]), (msks[batch_start:limit]), (m_msks[batch_start:limit])
if self.aug == True:
img_temp, msk_temp, m_msk_temp = self._aug_img(img_temp, msk_temp, m_msk_temp)
# expand dimension for the model
img_temp = np.expand_dims(img_temp, axis=3)
msk_temp = np.expand_dims(msk_temp, axis=3)
m_msk_temp = np.expand_dims(m_msk_temp, axis=3)
yield(img_temp, msk_temp, m_msk_temp) # output the unpacked dataset
batch_start += self.batch_size
batch_end += self.batch_size