-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_data.py
224 lines (149 loc) · 7.07 KB
/
get_data.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
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import random
import scipy.io as sio
import glob
import os
def random_crop(lr_img, hr_img, hr_crop_size=256, scale=1):
lr_crop_size = hr_crop_size // scale
#lr_img = lr_img[::scale, ::scale, :]
lr_img_shape = tf.shape(input=lr_img)[:2]
lr_w = tf.random.uniform(shape=(), maxval=lr_img_shape[1] - lr_crop_size + 1, dtype=tf.int32)
lr_h = tf.random.uniform(shape=(), maxval=lr_img_shape[0] - lr_crop_size + 1, dtype=tf.int32)
hr_w = lr_w * scale
hr_h = lr_h * scale
lr_img_cropped = lr_img[lr_h:lr_h + lr_crop_size, lr_w:lr_w + lr_crop_size,:]
hr_img_cropped = hr_img[hr_h:hr_h + hr_crop_size, hr_w:hr_w + hr_crop_size,:]
#lr_img_cropped = tf.expand_dims(lr_img_cropped, 2)
return lr_img_cropped, hr_img_cropped
def fixed_crop(lr_img, hr_img, hr_crop_size=256, scale=1):
lr_crop_size = hr_crop_size // scale
#lr_img = lr_img[::scale, ::scale, :]
lr_img_shape = tf.shape(input=lr_img)[:2]
lr_w = (lr_img_shape[1]-lr_crop_size)// 2
lr_h = (lr_img_shape[0]-lr_crop_size)// 2
hr_w = lr_w * scale
hr_h = lr_h * scale
lr_img_cropped = lr_img[lr_h:lr_h + lr_crop_size, lr_w:lr_w + lr_crop_size]
hr_img_cropped = hr_img[hr_h:hr_h + hr_crop_size, hr_w:hr_w + hr_crop_size]
#lr_img_cropped = tf.expand_dims(lr_img_cropped, 2)
return lr_img_cropped, hr_img_cropped
def random_cropv2(lr_img, lr_h,lr_w, lr_crop_size=256,scale = 1):
lr_crop_size = lr_crop_size // scale
lr_w = lr_w //scale
lr_h = lr_h //scale
lr_img_cropped = lr_img[lr_h:lr_h + lr_crop_size, lr_w:lr_w + lr_crop_size]
#lr_img_cropped = tf.expand_dims(lr_img_cropped, 2)
return lr_img_cropped
def fixed_cropv2(lr_img, hr_crop_size=256, scale=1):
lr_crop_size = int(hr_crop_size // scale)
#lr_img = lr_img[::scale, ::scale, :]
lr_img_shape = np.shape(lr_img)[:2]
lr_w = int((lr_img_shape[1]-lr_crop_size)// 2)
lr_h = int((lr_img_shape[0]-lr_crop_size)// 2)
lr_img_cropped = lr_img[lr_h:lr_h + lr_crop_size, lr_w:lr_w + lr_crop_size]
#lr_img_cropped = tf.expand_dims(lr_img_cropped, 2)
return lr_img_cropped
def random_flip(lr_img, hr_img):
rn = tf.random.uniform(shape=(), maxval=1)
return tf.cond(pred=rn < 0.5,
true_fn=lambda: (lr_img, hr_img),
false_fn=lambda: (tf.image.flip_left_right(lr_img),
tf.image.flip_left_right(hr_img)))
def random_rotate(lr_img, hr_img):
rn = tf.random.uniform(shape=(), maxval=4, dtype=tf.int32)
return tf.image.rot90(lr_img, rn), tf.image.rot90(hr_img, rn)
def _preprocess_npy_zstack(inp, label, path, train_config):
shapes = tf.shape(input=inp)
sized = tf.cast(shapes[0],tf.float32)
inp = tf.reshape(inp, [sized, sized, 5])
label = tf.reshape(label, [sized, sized, 3])
df_zstack = inp
if train_config.input == 'only_df':
input = df_zstack[:,:,:1]
elif train_config.input == 'df_zstack1':
input = df_zstack[:, :, :3]
elif train_config.input == 'df_zstack2':
input = df_zstack[:, :, :5]
if train_config.inverted_input is True:
input = 1-input
#(input, label) = random_crop(input, label, train_config.image_size, 1) # opt.scale
if train_config.testbool is False:
(input, label) = random_crop(input, label, train_config.image_size,1)#opt.scale
(input, label) = random_flip(input, label)
(input, label) = random_rotate(input, label)
else:
(input, label) = fixed_crop(input, label, train_config.image_size,1)#opt.scale
return input, label, path
# Load the numpy files
def map_func(inp_path):
#print(inp_path)
inp = np.load(inp_path)
lr_h = 0
inp = inp[lr_h:inp.shape[0]-lr_h, lr_h:inp.shape[1]-lr_h,:]
# print(inp.shape)
tar = np.load(inp_path.decode('utf-8').replace('input','label'))
# print(tar.shape)
avg_max = 0.1760
df_zstack = inp[:,:,:5]
# bf_in = inp[:,:,5:]
inp = df_zstack/avg_max
# inp = np.concatenate((df_zstack,bf_in), axis=2)
# print(inp.shape)
return inp, tar, inp_path
def get_dataset_iterator_bacteria_npy(filename, train_config,valid_config, data_type):
K = tf.reduce_sum(input_tensor=tf.cast(tf.logical_or(tf.equal(data_type, 'train'), tf.equal(data_type, 'valid')), tf.int32))
def fshuffle(train_config): return 99999
def fnoshuffle(): return 1
shuffle_sz = tf.cast(tf.cond(pred=tf.equal(K, 1), true_fn=lambda: fshuffle(train_config), false_fn=fnoshuffle), dtype=tf.int64)
# print(filename)
image_list = glob.glob(filename)#+glob.glob(filename[1])
###########TRAINING ONLY
if train_config.testbool is False:
random.shuffle(image_list)
dataset = tf.data.Dataset.from_tensor_slices(image_list)
dataset = dataset.shuffle(shuffle_sz, reshuffle_each_iteration=True)
dataset = dataset.map(lambda item1 : tf.numpy_function(
map_func, [item1], [tf.float32, tf.float32, tf.string]),
num_parallel_calls=train_config.n_threads)
dataset = dataset.map(lambda x, y, z : _preprocess_npy_zstack(x, y, z, train_config), num_parallel_calls=train_config.n_threads)
def batch_sz_train(train_config): return train_config.batch_size
def batch_sz_valid(valid_config): return valid_config.batch_size
if train_config.testbool is False:
BS = tf.cast(tf.cond(pred=tf.equal(K,1),true_fn=lambda: batch_sz_train(train_config),false_fn=lambda: batch_sz_valid(valid_config)), dtype=tf.int64)
else:
BS = tf.cast(1, dtype=tf.int64)
dataset = dataset.batch(BS, drop_remainder=True)
dataset = dataset.prefetch(1)
iterator = tf.compat.v1.data.make_initializable_iterator(dataset)
return iterator
def save_single_image(img, tit, vmin,vmax,image_ID='img0', image_save_path='./'):
fig, ax = plt.subplots()
ax.set_title(tit)
x1 = plt.imshow(img, cmap='gray',vmin=vmin, vmax=vmax)
plt.colorbar(x1, ax=ax)
plt.tight_layout()
plt.savefig(f'{image_save_path}/{image_ID}.png', bbox_inches='tight')
plt.close(fig)
plt.clf()
def save_pure_image(img, image_ID='img0', image_save_path='./'):
imgk = img * 255
pil_img = Image.fromarray(imgk.astype(np.uint8))
if not os.path.exists(image_save_path):
os.mkdir(image_save_path)
# asd = os.path.join(f'{image_save_path}')
# pil_img.save(os.path.join(f'{image_save_path}',f'{image_ID}.jpg'))
pil_img.save(f'{image_save_path}/{image_ID}.jpg')
def save_single_image_mat(img, image_ID='img0', image_save_path='./'):
if not os.path.exists(image_save_path):
os.mkdir(image_save_path)
# asd = os.path.join(f'{image_save_path}')
sio.savemat(f'{image_save_path}/{image_ID}.mat',
{f'img': np.array(img)})
# sio.savemat(os.path.join(f'{image_save_path}',f'{image_ID}.mat'),
# {f'img': np.array(img)})
def save_label_recon_image_mat(img, lab, image_ID='img0', image_save_path='./'):
sio.savemat(f'{image_save_path}/{image_ID}.mat',
{f'out': np.array(img),f'tar': np.array(lab)})