keras_retinanet.utils package

Submodules

keras_retinanet.utils.anchors module

keras_retinanet.utils.coco_eval module

keras_retinanet.utils.colors module

keras_retinanet.utils.colors.label_color(label)[source]

Return a color from a set of predefined colors. Contains 80 colors in total.

Args
label: The label to get the color for.
Returns

A list of three values representing a RGB color.

If no color is defined for a certain label, the color green is returned and a warning is printed.

keras_retinanet.utils.compute_overlap module

keras_retinanet.utils.compute_overlap.compute_overlap
Args
a: (N, 4) ndarray of float b: (K, 4) ndarray of float
Returns
overlaps: (N, K) ndarray of overlap between boxes and query_boxes

keras_retinanet.utils.config module

keras_retinanet.utils.eval module

keras_retinanet.utils.gpu module

Copyright 2017-2019 Fizyr (https://fizyr.com)

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

keras_retinanet.utils.gpu.setup_gpu(gpu_id)[source]

keras_retinanet.utils.image module

Copyright 2017-2018 Fizyr (https://fizyr.com)

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

class keras_retinanet.utils.image.TransformParameters(fill_mode='nearest', interpolation='linear', cval=0, relative_translation=True)[source]

Bases: object

Struct holding parameters determining how to apply a transformation to an image.

Args

fill_mode: One of: ‘constant’, ‘nearest’, ‘reflect’, ‘wrap’ interpolation: One of: ‘nearest’, ‘linear’, ‘cubic’, ‘area’, ‘lanczos4’ cval: Fill value to use with fill_mode=’constant’ relative_translation: If true (the default), interpret translation as a factor of the image size.

If false, interpret it as absolute pixels.
cvBorderMode()[source]
cvInterpolation()[source]
class keras_retinanet.utils.image.VisualEffect(contrast_factor, brightness_delta, hue_delta, saturation_factor)[source]

Bases: object

Struct holding parameters and applying image color transformation.

Args
contrast_factor: A factor for adjusting contrast. Should be between 0 and 3. brightness_delta: Brightness offset between -1 and 1 added to the pixel values. hue_delta: Hue offset between -1 and 1 added to the hue channel. saturation_factor: A factor multiplying the saturation values of each pixel.
keras_retinanet.utils.image.adjust_brightness(image, delta)[source]

Adjust brightness of an image

Args
image: Image to adjust. delta: Brightness offset between -1 and 1 added to the pixel values.
keras_retinanet.utils.image.adjust_contrast(image, factor)[source]

Adjust contrast of an image.

Args
image: Image to adjust. factor: A factor for adjusting contrast.
keras_retinanet.utils.image.adjust_hue(image, delta)[source]

Adjust hue of an image.

Args

image: Image to adjust. delta: An interval between -1 and 1 for the amount added to the hue channel.

The values are rotated if they exceed 180.
keras_retinanet.utils.image.adjust_saturation(image, factor)[source]

Adjust saturation of an image.

Args
image: Image to adjust. factor: An interval for the factor multiplying the saturation values of each pixel.
keras_retinanet.utils.image.adjust_transform_for_image(transform, image, relative_translation)[source]

Adjust a transformation for a specific image.

The translation of the matrix will be scaled with the size of the image. The linear part of the transformation will adjusted so that the origin of the transformation will be at the center of the image.

keras_retinanet.utils.image.apply_transform(matrix, image, params)[source]

Apply a transformation to an image.

The origin of transformation is at the top left corner of the image.

The matrix is interpreted such that a point (x, y) on the original image is moved to transform * (x, y) in the generated image. Mathematically speaking, that means that the matrix is a transformation from the transformed image space to the original image space.

Args
matrix: A homogeneous 3 by 3 matrix holding representing the transformation to apply. image: The image to transform. params: The transform parameters (see TransformParameters)
keras_retinanet.utils.image.compute_resize_scale(image_shape, min_side=800, max_side=1333)[source]

Compute an image scale such that the image size is constrained to min_side and max_side.

Args
min_side: The image’s min side will be equal to min_side after resizing. max_side: If after resizing the image’s max side is above max_side, resize until the max side is equal to max_side.
Returns
A resizing scale.
keras_retinanet.utils.image.preprocess_image(x, mode='caffe')[source]

Preprocess an image by subtracting the ImageNet mean.

Args

x: np.array of shape (None, None, 3) or (3, None, None). mode: One of “caffe” or “tf”.

  • caffe: will zero-center each color channel with
    respect to the ImageNet dataset, without scaling.
  • tf: will scale pixels between -1 and 1, sample-wise.
Returns
The input with the ImageNet mean subtracted.
keras_retinanet.utils.image.random_visual_effect_generator(contrast_range=(0.9, 1.1), brightness_range=(-0.1, 0.1), hue_range=(-0.05, 0.05), saturation_range=(0.95, 1.05))[source]

Generate visual effect parameters uniformly sampled from the given intervals.

Args

contrast_factor: A factor interval for adjusting contrast. Should be between 0 and 3. brightness_delta: An interval between -1 and 1 for the amount added to the pixels. hue_delta: An interval between -1 and 1 for the amount added to the hue channel.

The values are rotated if they exceed 180.
saturation_factor: An interval for the factor multiplying the saturation values of each
pixel.
keras_retinanet.utils.image.read_image_bgr(path)[source]

Read an image in BGR format.

Args
path: Path to the image.
keras_retinanet.utils.image.resize_image(img, min_side=800, max_side=1333)[source]

Resize an image such that the size is constrained to min_side and max_side.

Args
min_side: The image’s min side will be equal to min_side after resizing. max_side: If after resizing the image’s max side is above max_side, resize until the max side is equal to max_side.
Returns
A resized image.

keras_retinanet.utils.keras_version module

Copyright 2017-2018 Fizyr (https://fizyr.com)

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

keras_retinanet.utils.keras_version.assert_keras_version()[source]

Assert that the Keras version is up to date.

keras_retinanet.utils.keras_version.check_keras_version()[source]

Check that the Keras version is up to date. If it isn’t, print an error message and exit the script.

keras_retinanet.utils.keras_version.keras_version()[source]

Get the Keras version.

Returns
tuple of (major, minor, patch).
keras_retinanet.utils.keras_version.keras_version_ok()[source]

Check if the current Keras version is higher than the minimum version.

keras_retinanet.utils.model module

Copyright 2017-2018 Fizyr (https://fizyr.com)

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

keras_retinanet.utils.model.freeze(model)[source]

Set all layers in a model to non-trainable.

The weights for these layers will not be updated during training.

This function modifies the given model in-place, but it also returns the modified model to allow easy chaining with other functions.

keras_retinanet.utils.tf_version module

Copyright 2017-2019 Fizyr (https://fizyr.com)

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

keras_retinanet.utils.tf_version.assert_tf_version(minimum_tf_version=(1, 12, 0))[source]

Assert that the Keras version is up to date.

keras_retinanet.utils.tf_version.check_tf_version()[source]

Check that the Keras version is up to date. If it isn’t, print an error message and exit the script.

keras_retinanet.utils.tf_version.tf_version()[source]

Get the Tensorflow version. Returns

tuple of (major, minor, patch).
keras_retinanet.utils.tf_version.tf_version_ok(minimum_tf_version=(1, 12, 0))[source]

Check if the current Tensorflow version is higher than the minimum version.

keras_retinanet.utils.transform module

Copyright 2017-2018 Fizyr (https://fizyr.com)

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

keras_retinanet.utils.transform.change_transform_origin(transform, center)[source]
Create a new transform representing the same transformation,
only with the origin of the linear part changed.
Args
transform: the transformation matrix center: the new origin of the transformation
Returns
translate(center) * transform * translate(-center)
keras_retinanet.utils.transform.colvec(*args)[source]

Create a numpy array representing a column vector.

keras_retinanet.utils.transform.random_flip(flip_x_chance, flip_y_chance, prng=<module 'numpy.random' from '/home/docs/.pyenv/versions/3.7.9/lib/python3.7/site-packages/numpy/random/__init__.py'>)[source]

Construct a transformation randomly containing X/Y flips (or not). Args

flip_x_chance: The chance that the result will contain a flip along the X axis. flip_y_chance: The chance that the result will contain a flip along the Y axis. prng: The pseudo-random number generator to use.
Returns
a homogeneous 3 by 3 transformation matrix
keras_retinanet.utils.transform.random_rotation(min, max, prng=<module 'numpy.random' from '/home/docs/.pyenv/versions/3.7.9/lib/python3.7/site-packages/numpy/random/__init__.py'>)[source]

Construct a random rotation between -max and max. Args

min: a scalar for the minimum absolute angle in radians max: a scalar for the maximum absolute angle in radians prng: the pseudo-random number generator to use.
Returns
a homogeneous 3 by 3 rotation matrix
keras_retinanet.utils.transform.random_scaling(min, max, prng=<module 'numpy.random' from '/home/docs/.pyenv/versions/3.7.9/lib/python3.7/site-packages/numpy/random/__init__.py'>)[source]

Construct a random 2D scale matrix between -max and max. Args

min: a 2D vector containing the minimum scaling factor for X and Y. min: a 2D vector containing The maximum scaling factor for X and Y. prng: the pseudo-random number generator to use.
Returns
a homogeneous 3 by 3 scaling matrix
keras_retinanet.utils.transform.random_shear(min, max, prng=<module 'numpy.random' from '/home/docs/.pyenv/versions/3.7.9/lib/python3.7/site-packages/numpy/random/__init__.py'>)[source]

Construct a random 2D shear matrix with shear angle between -max and max. Args

min: the minimum shear angle in radians. max: the maximum shear angle in radians. prng: the pseudo-random number generator to use.
Returns
a homogeneous 3 by 3 shear matrix
keras_retinanet.utils.transform.random_transform(min_rotation=0, max_rotation=0, min_translation=(0, 0), max_translation=(0, 0), min_shear=0, max_shear=0, min_scaling=(1, 1), max_scaling=(1, 1), flip_x_chance=0, flip_y_chance=0, prng=<module 'numpy.random' from '/home/docs/.pyenv/versions/3.7.9/lib/python3.7/site-packages/numpy/random/__init__.py'>)[source]

Create a random transformation.

The transformation consists of the following operations in this order (from left to right):
  • rotation
  • translation
  • shear
  • scaling
  • flip x (if applied)
  • flip y (if applied)

Note that by default, the data generators in keras_retinanet.preprocessing.generators interpret the translation as factor of the image size. So an X translation of 0.1 would translate the image by 10% of it’s width. Set relative_translation to False in the TransformParameters of a data generator to have it interpret the translation directly as pixel distances instead.

Args
min_rotation: The minimum rotation in radians for the transform as scalar. max_rotation: The maximum rotation in radians for the transform as scalar. min_translation: The minimum translation for the transform as 2D column vector. max_translation: The maximum translation for the transform as 2D column vector. min_shear: The minimum shear angle for the transform in radians. max_shear: The maximum shear angle for the transform in radians. min_scaling: The minimum scaling for the transform as 2D column vector. max_scaling: The maximum scaling for the transform as 2D column vector. flip_x_chance: The chance (0 to 1) that a transform will contain a flip along X direction. flip_y_chance: The chance (0 to 1) that a transform will contain a flip along Y direction. prng: The pseudo-random number generator to use.
keras_retinanet.utils.transform.random_transform_generator(prng=None, **kwargs)[source]

Create a random transform generator.

Uses a dedicated, newly created, properly seeded PRNG by default instead of the global DEFAULT_PRNG.

The transformation consists of the following operations in this order (from left to right):
  • rotation
  • translation
  • shear
  • scaling
  • flip x (if applied)
  • flip y (if applied)

Note that by default, the data generators in keras_retinanet.preprocessing.generators interpret the translation as factor of the image size. So an X translation of 0.1 would translate the image by 10% of it’s width. Set relative_translation to False in the TransformParameters of a data generator to have it interpret the translation directly as pixel distances instead.

Args
min_rotation: The minimum rotation in radians for the transform as scalar. max_rotation: The maximum rotation in radians for the transform as scalar. min_translation: The minimum translation for the transform as 2D column vector. max_translation: The maximum translation for the transform as 2D column vector. min_shear: The minimum shear angle for the transform in radians. max_shear: The maximum shear angle for the transform in radians. min_scaling: The minimum scaling for the transform as 2D column vector. max_scaling: The maximum scaling for the transform as 2D column vector. flip_x_chance: The chance (0 to 1) that a transform will contain a flip along X direction. flip_y_chance: The chance (0 to 1) that a transform will contain a flip along Y direction. prng: The pseudo-random number generator to use.
keras_retinanet.utils.transform.random_translation(min, max, prng=<module 'numpy.random' from '/home/docs/.pyenv/versions/3.7.9/lib/python3.7/site-packages/numpy/random/__init__.py'>)[source]

Construct a random 2D translation between min and max. Args

min: a 2D vector with the minimum translation for each dimension max: a 2D vector with the maximum translation for each dimension prng: the pseudo-random number generator to use.
Returns
a homogeneous 3 by 3 translation matrix
keras_retinanet.utils.transform.rotation(angle)[source]

Construct a homogeneous 2D rotation matrix. Args

angle: the angle in radians
Returns
the rotation matrix as 3 by 3 numpy array
keras_retinanet.utils.transform.scaling(factor)[source]

Construct a homogeneous 2D scaling matrix. Args

factor: a 2D vector for X and Y scaling
Returns
the zoom matrix as 3 by 3 numpy array
keras_retinanet.utils.transform.shear(angle)[source]

Construct a homogeneous 2D shear matrix. Args

angle: the shear angle in radians
Returns
the shear matrix as 3 by 3 numpy array
keras_retinanet.utils.transform.transform_aabb(transform, aabb)[source]

Apply a transformation to an axis aligned bounding box.

The result is a new AABB in the same coordinate system as the original AABB. The new AABB contains all corner points of the original AABB after applying the given transformation.

Args
transform: The transformation to apply. x1: The minimum x value of the AABB. y1: The minimum y value of the AABB. x2: The maximum x value of the AABB. y2: The maximum y value of the AABB.
Returns
The new AABB as tuple (x1, y1, x2, y2)
keras_retinanet.utils.transform.translation(translation)[source]

Construct a homogeneous 2D translation matrix. # Arguments

translation: the translation 2D vector
# Returns
the translation matrix as 3 by 3 numpy array

keras_retinanet.utils.visualization module

Copyright 2017-2018 Fizyr (https://fizyr.com)

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

keras_retinanet.utils.visualization.draw_annotations(image, annotations, color=(0, 0, 0), label_to_name=None)[source]

Draws annotations in an image.

# Arguments
image : The image to draw on. annotations : A [N, 5] matrix (x1, y1, x2, y2, label) or dictionary containing bboxes (shaped [N, 4]) and labels (shaped [N]). color : The color of the boxes. By default the color from keras_retinanet.utils.colors.label_color will be used. label_to_name : (optional) Functor for mapping a label to a name.
keras_retinanet.utils.visualization.draw_box(image, box, color, thickness=1)[source]

Draws a box on an image with a given color.

# Arguments
image : The image to draw on. box : A list of 4 elements (x1, y1, x2, y2). color : The color of the box. thickness : The thickness of the lines to draw a box with.
keras_retinanet.utils.visualization.draw_boxes(image, boxes, color, thickness=1)[source]

Draws boxes on an image with a given color.

# Arguments
image : The image to draw on. boxes : A [N, 4] matrix (x1, y1, x2, y2). color : The color of the boxes. thickness : The thickness of the lines to draw boxes with.
keras_retinanet.utils.visualization.draw_caption(image, box, caption)[source]

Draws a caption above the box in an image.

# Arguments
image : The image to draw on. box : A list of 4 elements (x1, y1, x2, y2). caption : String containing the text to draw.
keras_retinanet.utils.visualization.draw_detections(image, boxes, scores, labels, color=(0, 255, 0), label_to_name=None, score_threshold=0.05)[source]

Draws detections in an image.

# Arguments
image : The image to draw on. boxes : A [N, 4] matrix (x1, y1, x2, y2). scores : A list of N classification scores. labels : A list of N labels. color : The color of the boxes. By default the color from keras_retinanet.utils.colors.label_color will be used. label_to_name : (optional) Functor for mapping a label to a name. score_threshold : Threshold used for determining what detections to draw.

Module contents