-
Notifications
You must be signed in to change notification settings - Fork 664
/
utils.py
351 lines (267 loc) · 10.9 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
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
from __future__ import absolute_import
from __future__ import division
import os
import tempfile
import math
import json
import six
import numpy as np
import matplotlib.font_manager as fontman
from skimage import io, transform
from keras import backend as K
from keras.models import load_model
import logging
logger = logging.getLogger(__name__)
try:
import PIL as pil
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
except ImportError:
pil = None
# Globals
_CLASS_INDEX = None
def _check_pil():
if not pil:
raise ImportError('Failed to import PIL. You must install Pillow')
def _find_font_file(query):
"""Utility to find font file.
"""
return list(filter(lambda path: query.lower() in os.path.basename(path).lower(), fontman.findSystemFonts()))
def reverse_enumerate(iterable):
"""Enumerate over an iterable in reverse order while retaining proper indexes, without creating any copies.
"""
return zip(reversed(range(len(iterable))), reversed(iterable))
def listify(value):
"""Ensures that the value is a list. If it is not a list, it creates a new list with `value` as an item.
"""
if not isinstance(value, list):
value = [value]
return value
def add_defaults_to_kwargs(defaults, **kwargs):
"""Updates `kwargs` with dict of `defaults`
Args:
defaults: A dictionary of keys and values
**kwargs: The kwargs to update.
Returns:
The updated kwargs.
"""
defaults = dict(defaults)
defaults.update(kwargs)
return defaults
def get_identifier(identifier, module_globals, module_name):
"""Helper utility to retrieve the callable function associated with a string identifier.
Args:
identifier: The identifier. Could be a string or function.
module_globals: The global objects of the module.
module_name: The module name
Returns:
The callable associated with the identifier.
"""
if isinstance(identifier, six.string_types):
fn = module_globals.get(identifier)
if fn is None:
raise ValueError('Unknown {}: {}'.format(module_name, identifier))
return fn
elif callable(identifier):
return identifier
else:
raise ValueError('Could not interpret identifier')
def apply_modifications(model, custom_objects=None):
"""Applies modifications to the model layers to create a new Graph. For example, simply changing
`model.layers[idx].activation = new activation` does not change the graph. The entire graph needs to be updated
with modified inbound and outbound tensors because of change in layer building function.
Args:
model: The `keras.models.Model` instance.
Returns:
The modified model with changes applied. Does not mutate the original `model`.
"""
# The strategy is to save the modified model and load it back. This is done because setting the activation
# in a Keras layer doesnt actually change the graph. We have to iterate the entire graph and change the
# layer inbound and outbound nodes with modified tensors. This is doubly complicated in Keras 2.x since
# multiple inbound and outbound nodes are allowed with the Graph API.
model_path = os.path.join(tempfile.gettempdir(), next(tempfile._get_candidate_names()) + '.h5')
try:
model.save(model_path)
return load_model(model_path, custom_objects=custom_objects)
finally:
os.remove(model_path)
def random_array(shape, mean=128., std=20.):
"""Creates a uniformly distributed random array with the given `mean` and `std`.
Args:
shape: The desired shape
mean: The desired mean (Default value = 128)
std: The desired std (Default value = 20)
Returns: Random numpy array of given `shape` uniformly distributed with desired `mean` and `std`.
"""
x = np.random.random(shape)
# normalize around mean=0, std=1
x = (x - np.mean(x)) / (np.std(x) + K.epsilon())
# and then around the desired mean/std
x = (x * std) + mean
return x
def find_layer_idx(model, layer_name):
"""Looks up the layer index corresponding to `layer_name` from `model`.
Args:
model: The `keras.models.Model` instance.
layer_name: The name of the layer to lookup.
Returns:
The layer index if found. Raises an exception otherwise.
"""
layer_idx = None
for idx, layer in enumerate(model.layers):
if layer.name == layer_name:
layer_idx = idx
break
if layer_idx is None:
raise ValueError("No layer with name '{}' within the model".format(layer_name))
return layer_idx
def deprocess_input(input_array, input_range=(0, 255)):
"""Utility function to scale the `input_array` to `input_range` throwing away high frequency artifacts.
Args:
input_array: An N-dim numpy array.
input_range: Specifies the input range as a `(min, max)` tuple to rescale the `input_array`.
Returns:
The rescaled `input_array`.
"""
# normalize tensor: center on 0., ensure std is 0.1
input_array = input_array.copy()
input_array -= input_array.mean()
input_array /= (input_array.std() + K.epsilon())
input_array *= 0.1
# clip to [0, 1]
input_array += 0.5
input_array = np.clip(input_array, 0, 1)
# Convert to `input_range`
return (input_range[1] - input_range[0]) * input_array + input_range[0]
def stitch_images(images, margin=5, cols=5):
"""Utility function to stitch images together with a `margin`.
Args:
images: The array of 2D images to stitch.
margin: The black border margin size between images (Default value = 5)
cols: Max number of image cols. New row is created when number of images exceed the column size.
(Default value = 5)
Returns:
A single numpy image array comprising of input images.
"""
if len(images) == 0:
return None
h, w, c = images[0].shape
n_rows = int(math.ceil(len(images) / cols))
n_cols = min(len(images), cols)
out_w = n_cols * w + (n_cols - 1) * margin
out_h = n_rows * h + (n_rows - 1) * margin
stitched_images = np.zeros((out_h, out_w, c), dtype=images[0].dtype)
for row in range(n_rows):
for col in range(n_cols):
img_idx = row * cols + col
if img_idx >= len(images):
break
stitched_images[(h + margin) * row: (h + margin) * row + h,
(w + margin) * col: (w + margin) * col + w, :] = images[img_idx]
return stitched_images
def get_img_shape(img):
"""Returns image shape in a backend agnostic manner.
Args:
img: An image tensor of shape: `(channels, image_dims...)` if data_format='channels_first' or
`(image_dims..., channels)` if data_format='channels_last'.
Returns:
Tuple containing image shape information in `(samples, channels, image_dims...)` order.
"""
if isinstance(img, np.ndarray):
shape = img.shape
else:
shape = K.int_shape(img)
if K.image_data_format() == 'channels_last':
shape = list(shape)
shape.insert(1, shape[-1])
shape = tuple(shape[:-1])
return shape
def load_img(path, grayscale=False, target_size=None):
"""Utility function to load an image from disk.
Args:
path: The image file path.
grayscale: True to convert to grayscale image (Default value = False)
target_size: (w, h) to resize. (Default value = None)
Returns:
The loaded numpy image.
"""
img = io.imread(path, grayscale)
if target_size:
img = transform.resize(img, target_size, preserve_range=True).astype('uint8')
return img
def lookup_imagenet_labels(indices):
"""Utility function to return the image net label for the final `dense` layer output index.
Args:
indices: Could be a single value or an array of indices whose labels should be looked up.
Returns:
Image net label corresponding to the image category.
"""
global _CLASS_INDEX
if _CLASS_INDEX is None:
with open(os.path.join(os.path.dirname(__file__), '../../resources/imagenet_class_index.json')) as f:
_CLASS_INDEX = json.load(f)
indices = listify(indices)
return [_CLASS_INDEX[str(idx)][1] for idx in indices]
def draw_text(img, text, position=(10, 10), font='FreeSans.ttf', font_size=14, color=(0, 0, 0)):
"""Draws text over the image. Requires PIL.
Args:
img: The image to use.
text: The text string to overlay.
position: The text (x, y) position. (Default value = (10, 10))
font: The ttf or open type font to use. (Default value = 'FreeSans.ttf')
font_size: The text font size. (Default value = 12)
color: The (r, g, b) values for text color. (Default value = (0, 0, 0))
Returns: Image overlayed with text.
"""
_check_pil()
font_files = _find_font_file(font)
if len(font_files) == 0:
logger.warn("Failed to lookup font '{}', falling back to default".format(font))
font = ImageFont.load_default()
else:
font = ImageFont.truetype(font_files[0], font_size)
# Don't mutate original image
img = Image.fromarray(img)
draw = ImageDraw.Draw(img)
draw.text(position, text, fill=color, font=font)
return np.asarray(img)
def bgr2rgb(img):
"""Converts an RGB image to BGR and vice versa
Args:
img: Numpy array in RGB or BGR format
Returns: The converted image format
"""
return img[..., ::-1]
def normalize(array, min_value=0., max_value=1.):
"""Normalizes the numpy array to (min_value, max_value)
Args:
array: The numpy array
min_value: The min value in normalized array (Default value = 0)
max_value: The max value in normalized array (Default value = 1)
Returns:
The array normalized to range between (min_value, max_value)
"""
arr_min = np.min(array)
arr_max = np.max(array)
normalized = (array - arr_min) / (arr_max - arr_min + K.epsilon())
return (max_value - min_value) * normalized + min_value
class _BackendAgnosticImageSlice(object):
"""Utility class to make image slicing uniform across various `image_data_format`.
"""
def __getitem__(self, item_slice):
"""Assuming a slice for shape `(samples, channels, image_dims...)`
"""
if K.image_data_format() == 'channels_first':
return item_slice
else:
# Move channel index to last position.
item_slice = list(item_slice)
item_slice.append(item_slice.pop(1))
return tuple(item_slice)
"""Slice utility to make image slicing uniform across various `image_data_format`.
Example:
conv_layer[utils.slicer[:, filter_idx, :, :]] will work for both `channels_first` and `channels_last` image
data formats even though, in tensorflow, slice should be conv_layer[utils.slicer[:, :, :, filter_idx]]
"""
slicer = _BackendAgnosticImageSlice()