forked from rykov8/ssd_keras
-
Notifications
You must be signed in to change notification settings - Fork 85
/
ssd_data.py
703 lines (593 loc) · 25.3 KB
/
ssd_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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
"""Some utils for data augmentation."""
import numpy as np
import matplotlib.pyplot as plt
import cv2
import random
import os
eps = 1e-10
class BaseGTUtility(object):
"""Base class for handling datasets.
Derived classes should implement the following attributes and call the init methode:
gt_path str
image_path str
classes list of str, first class is normaly 'Background'
image_names list of str
data list of array (boxes, n * xy + one_hot_class)
optional attributes are:
text list of list of str
"""
def __init__(self):
self.gt_path = ''
self.image_path = ''
self.classes = []
self.image_names = []
self.data = []
def init(self):
self.num_classes = len(self.classes)
self.classes_lower = [s.lower() for s in self.classes]
self.colors = plt.cm.hsv(np.linspace(0, 1, len(self.classes)+1)).tolist()
# statistics
stats = np.zeros(self.num_classes)
num_without_annotation = 0
for i in range(len(self.data)):
#stats += np.sum(self.data[i][:,-self.num_classes:], axis=0)
if len(self.data[i]) == 0:
num_without_annotation += 1
else:
unique, counts = np.unique(self.data[i][:,-1].astype(np.int16), return_counts=True)
stats[unique] += counts
self.stats = stats
self.num_without_annotation = num_without_annotation
self.num_samples = len(self.image_names)
self.num_images = len(self.data)
self.num_objects = sum(self.stats)
if self.num_samples == 0:
print('WARNING: empty dataset')
def __str__(self):
if not hasattr(self, 'stats'):
self.init()
s = ''
for i in range(self.num_classes):
s += '%-16s %8i\n' % (self.classes[i], self.stats[i])
s += '\n'
s += '%-16s %8i\n' % ('images', self.num_images)
s += '%-16s %8i\n' % ('objects', self.num_objects)
s += '%-16s %8.2f\n' % ('per image', self.num_objects/self.num_images)
s += '%-16s %8i\n' % ('no annotation', self.num_without_annotation)
return s
def plot_gt(self, boxes, show_labels=True):
# if parameter is sample index
if type(boxes) in [int]:
boxes = self.data[boxes]
ax = plt.gca()
im = plt.gci()
w, h = im.get_size()
for box in boxes:
class_idx = int(box[-1])
color = self.colors[class_idx]
is_polygon = len(box)-1 > 4
if is_polygon:
xy = box[:-1].reshape((-1,2))
else:
xmin, ymin, xmax, ymax = box[:4]
xy = np.array([[xmin, ymin], [xmax, ymin], [xmax, ymax], [xmin, ymax]])
xy = xy * [h, w]
ax.add_patch(plt.Polygon(xy, fill=False, edgecolor=color, linewidth=1))
if show_labels:
label_name = self.classes[class_idx]
if is_polygon:
angle = np.arctan((xy[1,0]-xy[0,0])/(xy[1,1]-xy[0,1]+eps))
if angle < 0:
angle += np.pi
angle = angle/np.pi*180-90
else:
angle = 0
ax.text(xy[0,0], xy[0,1], label_name, bbox={'facecolor':color, 'alpha':0.5}, rotation=angle)
def plot_input(self, input_img):
img = np.copy(input_img)
mean = np.array([104,117,123])
img += mean[np.newaxis, np.newaxis, :]
img = img[:, :, (2,1,0)]
img /= 255
plt.imshow(img)
def sample(self, idx=None, preserve_aspect_ratio=False, aspect_ratio=1.0):
'''Draw a random sample form the dataset.
'''
if idx is None:
idx = np.random.randint(0, len(self.image_names))
file_path = os.path.join(self.image_path, self.image_names[idx])
img = cv2.imread(file_path)
if preserve_aspect_ratio:
img = pad_image(img, aspect_ratio)
img = img[:, :, (2,1,0)] / 255
return idx, img, self.data[idx]
def sample_random_batch(self, batch_size=32, input_size=(512,512), seed=1337, preserve_aspect_ratio=False):
'''Draws a batch of random samples from the dataset.
# Arguments
batch_size: The batch size.
input_size: Tuple with height and width of model input.
seed: Seed for drawing the sample indices.
preserve_aspect_ratio: Boolean flag for padding the images with
random pixels and preserving the aspect ratio of the content.
# Return
idxs: List of the sample idices in the dataset.
inputs: List of preprocessed input images (BGR).
images: List of normalized images for visualization (RGB).
data: List of Ground Truth data, arrays with bounding boxes and class label.
'''
h, w = input_size
aspect_ratio = w/h
if seed is not None:
np.random.seed(seed)
idxs = np.random.randint(0, self.num_samples, batch_size)
inputs = []
images = []
data = []
for i in idxs:
img_path = os.path.join(self.image_path, self.image_names[i])
img = cv2.imread(img_path)
if type(img) != np.ndarray:
print('image not found: %s' % (img_path,))
if preserve_aspect_ratio:
img, gt = pad_image(img, aspect_ratio, self.data[i])
else:
gt = self.data[i]
inputs.append(preprocess(img, input_size))
img = cv2.resize(img, (w,h), cv2.INTER_LINEAR).astype('float32') # should we do resizing
img = img[:, :, (2,1,0)] / 255 # BGR to RGB
images.append(img)
data.append(gt)
inputs = np.asarray(inputs)
return idxs, inputs, images, data
def sample_batch(self, batch_size, batch_index, input_size=(512,512), preserve_aspect_ratio=False):
h, w = input_size
aspect_ratio = w/h
idxs = np.arange(min(batch_size*batch_index, self.num_samples),
min(batch_size*(batch_index+1), self.num_samples))
if len(idxs) == 0:
print('WARNING: empty batch')
inputs = []
data = []
for i in idxs:
img_path = os.path.join(self.image_path, self.image_names[i])
img = cv2.imread(img_path)
if preserve_aspect_ratio:
img = pad_image(img, aspect_ratio)
inputs.append(preprocess(img, input_size))
data.append(self.data[i])
inputs = np.asarray(inputs)
return inputs, data
def subset(self, start_idx=0, end_idx=-1):
gtu = BaseGTUtility()
gtu.gt_path = self.gt_path
gtu.image_path = self.image_path
gtu.classes = self.classes
gtu.image_names = self.image_names[start_idx:end_idx]
gtu.data = self.data[start_idx:end_idx]
if hasattr(gtu, 'text'):
gtu.text = self.text[start_idx:end_idx]
gtu.init()
return gtu
def split(self, split=0.8):
gtu1 = BaseGTUtility()
gtu1.gt_path = self.gt_path
gtu1.image_path = self.image_path
gtu1.classes = self.classes
gtu2 = BaseGTUtility()
gtu2.gt_path = self.gt_path
gtu2.image_path = self.image_path
gtu2.classes = self.classes
n = int(round(split * len(self.image_names)))
gtu1.image_names = self.image_names[:n]
gtu2.image_names = self.image_names[n:]
gtu1.data = self.data[:n]
gtu2.data = self.data[n:]
if hasattr(self, 'text'):
gtu1.text = self.text[:n]
gtu2.text = self.text[n:]
gtu1.init()
gtu2.init()
return gtu1, gtu2
def merge(self, gtu2):
gtu1 = self
if len(set(gtu1.classes)^set(gtu2.classes)) > 0:
raise Exception('Classes are different')
gtu = BaseGTUtility()
gtu.classes = gtu1.classes
# if image_path are the same
if gtu1.image_path == gtu2.image_path:
gtu.image_path = gtu1.image_path
gtu.image_names = gtu1.image_names + gtu2.image_names
else:
s1 = gtu1.image_path.split(os.path.sep)
s2 = gtu2.image_path.split(os.path.sep)
lmin = min(len(s1),len(s1))
for i in range(lmin):
if s1[i] != s2[i]:
break
if i == lmin-1:
i = lmin
prefix1 = os.path.join('', *s1[i:])
prefix2 = os.path.join('', *s2[i:])
gtu.image_path = os.path.join('', *s1[:i])
gtu.image_names = \
[os.path.join(prefix1, n) for n in gtu1.image_names] + \
[os.path.join(prefix2, n) for n in gtu2.image_names]
gtu.data = gtu1.data + gtu2.data
if hasattr(gtu1, 'text') and hasattr(gtu2, 'text'):
gtu.text = gtu1.text + gtu2.text
gtu.init()
return gtu
def convert(self, new_classes, conversion_map=None):
classes_lower = [s.lower() for s in self.classes]
new_classes_lower = [s.lower() for s in new_classes]
# do renaming if conversion map is provided
if conversion_map is not None:
for i in range(len(conversion_map)):
m_old = conversion_map[i][0].lower()
m_new = conversion_map[i][1].lower()
if m_old in classes_lower:
idx_old = classes_lower.index(m_old)
classes_lower[idx_old] = m_new
old_to_new = []
for i in range(len(classes_lower)):
if classes_lower[i] in new_classes_lower:
old_to_new.append(new_classes_lower.index(classes_lower[i]))
else:
old_to_new.append(None)
gtu = BaseGTUtility()
gtu.gt_path = self.gt_path
gtu.image_path = self.image_path
gtu.classes = new_classes
num_old_classes = len(self.classes)
num_new_classes = len(new_classes)
gtu.image_names = []
gtu.data = []
if hasattr(self, 'text'):
gtu.text = []
for i in range(len(self.image_names)):
boxes = []
for j in range(len(self.data[i])):
#old_class_idx = np.argmax(self.data[i][j,-num_old_classes:])
old_class_idx = int(self.data[i][j,-1])
new_class_idx = old_to_new[old_class_idx]
if new_class_idx is not None:
#class_one_hot = [0] * num_new_classes
#class_one_hot[new_class_idx] = 1
box = self.data[i][j,:-1]
box = list(box) + [new_class_idx]
boxes.append(box)
if len(boxes) > 0:
boxes = np.asarray(boxes)
gtu.data.append(boxes)
gtu.image_names.append(self.image_names[i])
if hasattr(self, 'text'):
gtu.text.append(self.text[i])
gtu.init()
return gtu
class InputGenerator(object):
"""Model input generator for data augmentation."""
# TODO
# flag to protect bounding boxes from cropping?
# crop range > 1.0? crop_area_range=[0.75, 1.25]
def __init__(self, gt_util, prior_util, batch_size, input_size,
preserve_aspect_ratio=True,
augmentation=False,
saturation_var=0.5,
brightness_var=0.5,
contrast_var=0.5,
lighting_std=0.5,
hflip_prob=0.5,
vflip_prob=0.0,
do_crop=True,
add_noise=True,
crop_area_range=[0.75, 1.0],
aspect_ratio_range=[4./3., 3./4.]):
self.__dict__.update(locals())
self.num_batches = gt_util.num_samples // batch_size
self.num_samples = self.num_batches * batch_size
self.color_jitter = []
if saturation_var:
self.color_jitter.append(self.saturation)
if brightness_var:
self.color_jitter.append(self.brightness)
if contrast_var:
self.color_jitter.append(self.contrast)
def __str__(self):
f = '%-20s %s\n'
s = ''
s += f % ('input_size', self.input_size)
s += f % ('batch_size', self.batch_size)
s += f % ('num_samples', self.gt_util.num_samples)
s += f % ('num_batches', self.num_batches)
return s
def grayscale(self, rgb):
return rgb.dot([0.299, 0.587, 0.114])
def saturation(self, rgb):
gs = self.grayscale(rgb)
alpha = 2 * np.random.random() * self.saturation_var
alpha += 1 - self.saturation_var
rgb = rgb * alpha + (1 - alpha) * gs[:, :, None]
return np.clip(rgb, 0, 255)
def brightness(self, rgb):
alpha = 2 * np.random.random() * self.brightness_var
alpha += 1 - self.saturation_var
rgb = rgb * alpha
return np.clip(rgb, 0, 255)
def contrast(self, rgb):
gs = self.grayscale(rgb).mean() * np.ones_like(rgb)
alpha = 2 * np.random.random() * self.contrast_var
alpha += 1 - self.contrast_var
rgb = rgb * alpha + (1 - alpha) * gs
return np.clip(rgb, 0, 255)
def lighting(self, img):
cov = np.cov(img.reshape(-1, 3) / 255.0, rowvar=False)
eigval, eigvec = np.linalg.eigh(cov)
noise = np.random.randn(3) * self.lighting_std
noise = eigvec.dot(eigval * noise) * 255
img += noise
return np.clip(img, 0, 255)
def noise(self, img):
img_size = img.shape[:2]
scale = np.random.randint(16)
noise = np.array(np.random.exponential(scale, img_size), dtype=np.int) * np.random.randint(-1,2, size=img_size)
#noise = np.array(np.random.normal(0, scale, img_size), dtype=np.int)
noise = np.repeat(noise[:, :, np.newaxis], 3, axis=2)
img = img + noise
return np.clip(img, 0, 255)
def horizontal_flip(self, img, y, hflip_prob):
if np.random.random() < hflip_prob:
img = img[:, ::-1]
num_coords = y.shape[1] - 1
if num_coords == 8: # polygon case
y[:,[0,2,4,6]] = 1 - y[:,[2,0,6,4]]
y[:,[1,3,5,7]] = y[:,[3,1,7,5]]
else:
y[:,[0,2]] = 1 - y[:,[2,0]]
return img, y
def vertical_flip(self, img, y, vflip_prob):
if np.random.random() < vflip_prob:
img = img[::-1]
num_coords = y.shape[1] - 1
if num_coords == 8: # polynom case
y[:,[0,2,4,6]] = y[:,[6,4,2,0]]
y[:,[1,3,5,7]] = 1 - y[:,[7,5,3,1]]
else:
y[:,[1,3]] = 1 - y[:,[3,1]]
return img, y
def random_sized_crop(self, img, target):
img_h, img_w = img.shape[:2]
# make sure that we can preserve the aspect ratio
ratio_range = self.aspect_ratio_range
random_ratio = ratio_range[0] + np.random.random() * (ratio_range[1] - ratio_range[0])
# a = w/h, w_i-w >= 0, h_i-h >= 0 leads to LP: max. h s.t. h <= w_i/a, h <= h_i
max_h = min(img_w/random_ratio, img_h)
max_w = max_h * random_ratio
# scale the area
crop_range = self.crop_area_range
random_scale = crop_range[0] + np.random.random() * (crop_range[1] - crop_range[0])
target_area = random_scale * max_w * max_h
w = np.round(np.sqrt(target_area * random_ratio))
h = np.round(np.sqrt(target_area / random_ratio))
x = np.random.random() * (img_w - w)
y = np.random.random() * (img_h - h)
w_rel = w / img_w
h_rel = h / img_h
x_rel = x / img_w
y_rel = y / img_h
w, h, x, y = int(w), int(h), int(x), int(y)
# crop image and transform boxes
new_img = img[y:y+h, x:x+w]
num_coords = target.shape[1] - 1
new_target = []
if num_coords == 8: # polynom case
for box in target:
new_box = np.copy(box)
new_box[0:8:2] -= x_rel
new_box[0:8:2] /= w_rel
new_box[1:8:2] -= y_rel
new_box[1:8:2] /= h_rel
#new_box[0:8:2] = np.clip(new_box[0:8:2], 0, 1) # horizontal clip
#new_box[1:8:2] = np.clip(new_box[1:8:2], 0, 1) # vertical clip
if (new_box[0] < 1 and new_box[6] < 1 and new_box[2] > 0 and new_box[4] > 0 and
new_box[1] < 1 and new_box[3] < 1 and new_box[5] > 0 and new_box[7] > 0):
new_target.append(new_box)
new_target = np.asarray(new_target)
else:
for box in target:
xmin = (box[0] - x_rel) / w_rel
ymin = (box[1] - y_rel) / h_rel
xmax = (box[2] - x_rel) / w_rel
ymax = (box[3] - y_rel) / h_rel
box_w = xmax - xmin
box_h = ymax - ymin
# add only boxes if width and height is larger then 10% of original width and height
a = 0.1
if (xmax-0 > a*box_w and 1-xmin > a*box_w and
ymax-0 > a*box_h and 1-ymin > a*box_h):
new_box = np.copy(box)
new_box[:4] = np.clip([xmin, ymin, xmax, ymax], 0, 1)
new_target.append(new_box)
new_target = np.asarray(new_target).reshape(-1, target.shape[1])
return new_img, new_target
def get_sample(self, idx, encode=True, debug=False):
h, w = self.input_size
img_name = self.gt_util.image_names[idx]
img_path = os.path.join(self.gt_util.image_path, img_name)
img = cv2.imread(img_path)
y = np.copy(self.gt_util.data[idx])
if debug:
raw_img = img.astype(np.float32)
raw_y = np.copy(y)
if self.augmentation:
if self.do_crop:
for _ in range(10): # tries to crop without losing ground truth
img_tmp, y_tmp = self.random_sized_crop(img, y)
if len(y_tmp) > 0:
break
if len(y_tmp) > 0:
img = img_tmp
y = y_tmp
if self.preserve_aspect_ratio:
aspect_ratio = w/h
img, y = pad_image(img, aspect_ratio, y)
img = cv2.resize(img, (w,h), cv2.INTER_LINEAR)
img = img.astype(np.float32)
if self.augmentation:
random.shuffle(self.color_jitter)
for jitter in self.color_jitter: # saturation, brightness, contrast
img = jitter(img)
if self.lighting_std:
img = self.lighting(img)
if self.hflip_prob > 0:
img, y = self.horizontal_flip(img, y, self.hflip_prob)
if self.vflip_prob > 0:
img, y = self.vertical_flip(img, y, self.vflip_prob)
if self.add_noise:
img = self.noise(img)
if debug:
plt.figure(figsize=(12,6))
# origal gt image
plt.subplot(121)
dbg_img = np.copy(raw_img)[:,:,(2,1,0)] / 255
plt.imshow(dbg_img)
self.gt_util.plot_gt(raw_y)
# network input image
plt.subplot(122)
dbg_img = np.copy(img)[:,:,(2,1,0)] / 255
plt.imshow(dbg_img)
self.gt_util.plot_gt(y)
plt.show()
mean = np.array([104,117,123])
img -= mean[np.newaxis, np.newaxis, :]
#img = img / 25.6
if encode:
y = self.prior_util.encode(y)
return img, y
def get_dataset(self, num_parallel_calls=1, seed=1337):
import tensorflow as tf
if seed is not None:
np.random.seed(seed)
ds = tf.data.Dataset.range(self.num_samples).repeat(-1).shuffle(self.num_samples)
ds = ds.map(lambda x: tf.py_function(self.get_sample, [x,], ['float32', 'float32']), num_parallel_calls=num_parallel_calls, deterministic=False)
ds = ds.batch(self.batch_size).prefetch(tf.data.experimental.AUTOTUNE)
return ds
def generate(self, encode=True, debug=False, seed=1337):
if seed is not None:
np.random.seed(seed)
inputs, targets = [], []
while True:
idxs = np.arange(self.gt_util.num_samples)
np.random.shuffle(idxs)
idxs = idxs[:self.num_samples]
for j, idx in enumerate(idxs):
img, y = self.get_sample(idx, encode=encode, debug=debug)
inputs.append(img)
targets.append(y)
#if len(targets) == self.batch_size or j == len(idxs)-1: # last batch in epoch can be smaller then batch_size
if len(targets) == self.batch_size:
yield np.array(inputs, dtype=np.float32), np.array(targets, dtype=np.float32)
inputs, targets = [], []
elif j == len(idxs)-1:
# forgett last batch
inputs, targets = [], []
break
def pad_image(img, aspect_ratio, gt_data=None):
"""Padds an image with random pixels to get one with specific
aspect ratio while avoiding distortion of the image content.
"""
src_h, src_w, src_c = img.shape
if src_h * aspect_ratio > src_w:
new_w = int(src_h * aspect_ratio)
new_img = np.random.rand(src_h, new_w, src_c) * 255
padding = int((new_w-src_w)/2)
new_img[:,padding:padding+src_w,:] = img
if gt_data is not None:
new_gt_data = np.copy(gt_data)
new_gt_data[:,0:-1:2] = new_gt_data[:,0:-1:2] * src_w/new_w + padding/new_w
return new_img, new_gt_data
else:
return new_img
else:
new_h = int(src_w / aspect_ratio)
new_img = np.random.rand(new_h, src_w, src_c) * 255
padding = int((new_h-src_h)/2)
new_img[padding:padding+src_h,:,:] = img
if gt_data is not None:
new_gt_data = np.copy(gt_data)
new_gt_data[:,1:-1:2] = new_gt_data[:,1:-1:2] * src_h/new_h + padding/new_h
return new_img, new_gt_data
else:
return new_img
def preprocess(img, size):
"""Precprocess an image for ImageNet models.
# Arguments
img: Input Image
size: Target image size (height, width).
# Return
Resized and mean subtracted BGR image.
"""
h, w = size
img = cv2.resize(img, (w,h), cv2.INTER_LINEAR)
img = img.astype(np.float32)
mean = np.array([104,117,123])
img -= mean[np.newaxis, np.newaxis, :]
return img
def preprocess_image(file_name, size=(300,300), lib='skimage'):
"""Preprocess a given image for models trained on ImageNet.
Does the following steps: load, resize, subtract mean
# Arguments
file_name: Path to source image file.
size: Target image size (height, width).
lib: Name of the library being usesd
'pil', 'scipy', 'skimage', 'opencv'
# Returns
BGR image as numpy array
"""
if lib == 'pil':
import PIL
img = PIL.Image.open(file_name)
img = img.resize(size, PIL.Image.BILINEAR)
img = np.array(img, dtype='float32')
img = img[:,:,(2,1,0)] # RGB to BGR
elif lib == 'scipy':
import scipy.misc
img = scipy.misc.imread(file_name).astype('float32')
img = scipy.misc.imresize(img, size).astype('float32')
img = img[:,:,(2,1,0)] # RGB to BGR
elif lib == 'opencv':
import cv2
h, w = size
img = cv2.imread(file_name)
img = cv2.resize(img, (w,h), cv2.INTER_LINEAR).astype('float32')
img = img.astype('float32')
else:
# same as in caffe implementation
import skimage.io
import skimage.transform
img = skimage.io.imread(file_name)
img = img.astype('float32')
img = img/255.
img_min = img.min()
img_max = img.max()
img = (img - img_min) / (img_max - img_min)
img = skimage.transform.resize(img, size, order=1) # interpolation order, default is linear
img = img * (img_max - img_min) + img_min
img = img.astype('float32')
img *= 255 # the reference model operates on images in [0,255] range instead of [0,1]
img = img[:,:,(2,1,0)] # RGB to BGR
#from IPython.display import display
#plt.imshow(img[:, :, (2,1,0)]/255.)
#display(plt.gcf())
#plt.close()
#mean = np.array([103.939, 116.779, 123.68])
mean = np.array([104,117,123])
img -= mean[np.newaxis, np.newaxis, :] # subtract mean
#print((img.shape, img.max(), input_img.min(), img[1,150,150]))
return img
# %%timeit
# pil: 3.57 ms ± 17 µs
# scipy: 5.33 ms ± 15.2 µs
# opencv: 2.24 ms ± 11.2 µs
# skimage: 8.81 ms ± 25.3 µs
# per loop (mean ± std. dev. of 7 runs, 100 loops each)