Source: vis/utils/utils.py#L0
Global Variables
- slicer
reverse_enumerate
reverse_enumerate(iterable)
Enumerate over an iterable in reverse order while retaining proper indexes, without creating any copies.
listify
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.
add_defaults_to_kwargs
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.
get_identifier
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.
apply_modifications
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
.
random_array
random_array(shape, mean=128.0, std=20.0)
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
.
find_layer_idx
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.
deprocess_input
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 theinput_array
.
Returns:
The rescaled input_array
.
stitch_images
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.
get_img_shape
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.
load_img
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.
lookup_imagenet_labels
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.
draw_text
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.
bgr2rgb
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
normalize
normalize(array, min_value=0.0, max_value=1.0)
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)