-
Notifications
You must be signed in to change notification settings - Fork 30
/
image_utils.py
executable file
·390 lines (266 loc) · 12.8 KB
/
image_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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import numpy as np
import cv2
import os
import glob
import scipy.io as sio
import Queue as queue
import threading
from math import ceil
from threadsafe_iter import threadsafe_generator
def round(number, places=0):
place = 10**places
rounded = (int(number*place + 0.5if number>=0 else -0.5))/place
if rounded == int(rounded):
rounded = int(rounded)
return rounded
class Imageobject(object):
def __init__(self, image_folder, batch_size = 50, target_img_size = 256, queue_size = 10,
ext ='.png', channel_order ='RGB',
data_format ='channels_last'):
""" Image class.
Args:
image_folder: path to image file.
batch_size: image number per batch for neural network
target_img_size: target image size after resizing as the input of the neural network.
ext: valid image file extension, should be string or tuple of string
queue_size: queue size for retrieving batch ready for neural network.
channel_order: r, g, b order of color image. ('RGB' or 'BGR')
data_format: batch dimension format
channels_first: [batch, channel, height, width] (Caffe format)
channels_last: [batch, height, width, channel] (Tensorflow format)
Return:
slide class for retrieving image batches and result reconstruction
"""
self.image_folder = image_folder
self.batch_size = batch_size
self.target_img_size = target_img_size
self.queue_size = queue_size
self.channel_order = channel_order
self.data_format = data_format
self.ext = ext
self.q = queue.Queue(maxsize=self.queue_size)
self.threads = None
# multi thread
def retrieve_images_to_queue_thread(self, rotation=False, thread_num=16):
""" Generate image batch in multiple threads with specified augmentations and put in queue.
Args:
rotation: whether to add rotation augmentation in image batch.
thread_num: threads for retrieving.
Return:
queue of image batch.
"""
if type(self.ext) is str:
image_path_list = glob.glob(self.image_folder + os.path.sep + '*' + self.ext)
elif(type(self.ext) is tuple):
image_path_list = []
for ext_t in self.ext:
if type(ext_t) is str:
image_path_list.extend(glob.glob(self.image_folder + os.path.sep + '*' + ext_t))
print('Found '+ str(len(image_path_list)) + ' images.')
# threading
self.threads = []
# split into chunks of thread_num
if (len(image_path_list) < thread_num):
path_list_chunks = [image_path_list[index::len(image_path_list)] for index in range(len(image_path_list))]
else:
path_list_chunks = [image_path_list[index::thread_num] for index in range(thread_num)]
for i in range(len(path_list_chunks)):
thread = threading.Thread(target=self.retrieve_images_to_queue_thread_target,
args=(path_list_chunks[i], rotation, threading.Lock()))
thread.start()
self.threads.append(thread)
return self.q
def retrieve_images_to_queue_thread_target(self, image_path_list, rotation, lock):
self._retrieve_images_from_paths(image_path_list, rotation, lock)
# thread finished control
lock.acquire()
self.threads.remove(threading.current_thread())
if len(self.threads) == 0:
self.q.put(None) # put None for queue iter stop
lock.release()
@threadsafe_generator
def _retrieve_images_from_paths(self,image_path_list, rotation, lock):
img_batch = []
path_batch = []
for ind, image_path in enumerate(image_path_list):
if not rotation: # multi tile batch
batch_count = ceil(len(image_path_list) / float(self.batch_size))
lock.acquire()
img = cv2.imread(image_path)
lock.release()
img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (self.target_img_size, self.target_img_size))
img = self.preprocess_img(img)
if img is None: # white tile
continue
if self.channel_order is 'RGB':
pass
elif self.channel_order is 'BGR':
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
else:
raise Exception('Invalid channel order!')
img_batch.append(img)
path_batch.append(image_path)
if len(img_batch) == self.batch_size or ind == len(image_path_list) - 1:
batch_ind = (ind + 1) // self.batch_size
img_batch_ = np.stack(img_batch, axis=0)
path_batch_ = path_batch
if self.data_format == 'channels_first':
img_batch_ = np.moveaxis(img_batch_, 3, 1)
self.q.put((img_batch_, path_batch_, batch_ind, batch_count))
img_batch = []
path_batch = []
else: # same tile batch
batch_count = len(image_path_list)
lock.acquire()
img = cv2.imread(image_path)
lock.release()
img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (self.target_img_size, self.target_img_size))
img = self.preprocess_img(img)
if img is None: # white tile
continue
# rotation
rotation_time = 0
if rotation:
rotation_time = 4
if self.channel_order is 'RGB':
pass
elif self.channel_order is 'BGR':
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
else:
raise Exception('Invalid channel order!')
for rot in range(rotation_time):
img_batch.append(np.rot90(img, rot))
img_batch_ = np.stack(img_batch, axis=0)
path_batch_ = [image_path]
if self.data_format == 'channels_first':
img_batch_ = np.moveaxis(img_batch_, 3, 1)
self.q.put((img_batch_, path_batch_, ind, batch_count))
img_batch = []
path_batch = []
# last batch due to None img
if len(img_batch) is not 0:
img_batch_ = np.stack(img_batch, axis=0)
path_batch_ = np.stack(path_batch, axis=0)
if self.data_format == 'channels_first':
img_batch_ = np.moveaxis(img_batch_, 3, 1)
self.q.put((img_batch_, path_batch_, batch_count, batch_count))
@staticmethod
def preprocess_img(img):
""" Preprocess image here.
Args:
img: image before preprocessing.
Return:
image after preprocessing.
"""
return img
def reconstruct_classification_queue_to_file(self, data_queue, result_folder, suffix, save_raw=True):
""" Reconstruct classification results on top of images.
Args:
data_queue: queue contains the results after neural network (same format as the retrieving).
result_folder: folder to save the results.
result_suffix: suffix for result file.
save_raw: flag if to save the raw results before argmax.
"""
for data_out in iter(data_queue.get, None):
results, paths= data_out
if len(paths) == 1: # same-tile_augmentation
results_mean = np.mean(results, axis=0)
predictions = np.expand_dims(np.argmax(results_mean,-1),0)
elif len(paths) == len(results): # multi-tiles
pass
else:
raise Exception('Invalid result dimension!')
for ind in range(len(predictions)):
pred = predictions[ind]
path = paths[ind]
raw = cv2.cvtColor(cv2.imread(path),cv2.COLOR_BGR2RGB)
tile_gray_coded = np.zeros((raw.shape[0],raw.shape[1]), np.uint8)
tile_gray_coded[:] = self.gray_code(pred)
tile_color_coded = np.zeros(raw.shape, np.uint8)
tile_color_coded[:] = self.color_code(pred)
result_path_base = result_folder + os.path.sep + os.path.splitext(os.path.basename(path))[0]
rgb_result = cv2.cvtColor((tile_color_coded*0.2+raw*0.8).astype(np.uint8), cv2.COLOR_RGB2BGR)
cv2.imwrite(result_path_base + '_result_rgb_' + suffix + self.ext, rgb_result)
cv2.imwrite(result_path_base + '_result_mask_' + suffix + self.ext, tile_gray_coded)
if save_raw:
mdict = {}
mdict['Raw'] = results
sio.savemat(result_path_base + '_result_data_' + suffix + '.mat', mdict)
def reconstruct_segmentation_queue_to_file(self, data_queue, result_folder, result_suffix, save_raw=True):
""" Reconstruct segmentation results on top of images.
Args:
data_queue: queue contains the results after neural network (same format as the retrieving).
result_folder: folder to save the results.
result_suffix: suffix for result file.
save_raw: flag if to save the raw results before argmax.
"""
for data_out in iter(data_queue.get, None):
results, paths= data_out
if self.data_format == 'channels_first':
results = np.moveaxis(results, 1, -1)
if len(paths) == 1: # same-tile_augmentation
for i in range(4):
results[i, :, :, :] = np.rot90(results[i, :, :, :], -i)
results_mean = np.mean(results, axis=0)
predictions = np.expand_dims(np.argmax(results_mean,-1),0)
elif len(paths) == len(results): # multi-tiles
predictions = np.argmax(results, -1)
else:
raise Exception('Invalid result dimension!')
for ind in range(len(predictions)):
pred = predictions[ind]
path = paths[ind]
raw = cv2.cvtColor(cv2.imread(path),cv2.COLOR_BGR2RGB)
tile_gray_coded = cv2.resize(self.gray_code(pred),(raw.shape[1],raw.shape[0]), interpolation=cv2.INTER_NEAREST)
tile_color_coded = cv2.resize(self.color_code(pred), (raw.shape[1],raw.shape[0]), interpolation=cv2.INTER_NEAREST)
result_path_base = result_folder + os.path.sep + os.path.splitext(os.path.basename(path))[0]
rgb_result = cv2.cvtColor((tile_color_coded*0.2+raw*0.8).astype(np.uint8), cv2.COLOR_RGB2BGR)
cv2.imwrite(result_path_base + '_result_rgb_' + result_suffix + self.ext, rgb_result)
cv2.imwrite(result_path_base + '_result_mask_' + result_suffix + self.ext, tile_gray_coded)
if save_raw:
mdict = {}
mdict['Raw'] = results
sio.savemat(result_path_base + '_result_data_' + result_suffix + '.mat', mdict)
@staticmethod
def gray_code(index):
color_list = {'0':43,
'1':172,
'2':86,
'3':215,
'4':129}
if np.isscalar(index): # classification
if str(index) in color_list:
return color_list[str(index)]
else:
return 255
else: # segmentation
result = np.zeros(index.shape)
inds = np.unique(index)
for ind in inds:
if str(ind) in color_list:
result[index == ind] = color_list[str(ind)]
else:
result[index == ind] = 255
return result
@staticmethod
def color_code(index):
color_list = {'0':np.array((255, 255, 0), np.uint8),
'1':np.array((255, 0, 0), np.uint8),
'2':np.array((0, 255, 0), np.uint8),
'3':np.array((0, 0, 255), np.uint8)}
if np.isscalar(index): # classification
if str(index) in color_list:
return color_list[str(index)]
else:
return np.array((0,0,0), np.uint8)
else: # segmentation
result = np.zeros(index.shape+(3,),np.uint8)
inds = np.unique(index)
for ind in inds:
if str(ind) in color_list:
result[index == ind] = color_list[str(ind)]
else:
result[index == ind] = np.array((0, 0, 0), np.uint8)
return result