gluoncv.utils

We implemented a broad range of utility functions which cover visualization, file handler, download and training helpers.

Utility functions.

Visualization

Image Visualization

gluoncv.utils.viz.plot_image(img, ax=None, reverse_rgb=False)[source]

Visualize image.

Parameters:
  • img (numpy.ndarray or mxnet.nd.NDArray) – Image with shape H, W, 3.
  • ax (matplotlib axes, optional) – You can reuse previous axes if provided.
  • reverse_rgb (bool, optional) – Reverse RGB<->BGR orders if True.
Returns:

The ploted axes.

Return type:

matplotlib axes

Examples

from matplotlib import pyplot as plt ax = plot_image(img) plt.show()

gluoncv.utils.viz.get_color_pallete(npimg, dataset='pascal_voc')[source]

Visualize image.

Parameters:
  • npimg (numpy.ndarray) – Single channel image with shape H, W, 1.
  • dataset (str, default: 'pascal_voc') – The dataset that model pretrained on. (‘pascal_voc’, ‘ade20k’)
Returns:

out_img – Image with color pallete

Return type:

PIL.Image

Bounding Box Visualization

gluoncv.utils.viz.plot_bbox(img, bboxes, scores=None, labels=None, thresh=0.5, class_names=None, colors=None, ax=None, reverse_rgb=False, absolute_coordinates=True)[source]

Visualize bounding boxes.

Parameters:
  • img (numpy.ndarray or mxnet.nd.NDArray) – Image with shape H, W, 3.
  • bboxes (numpy.ndarray or mxnet.nd.NDArray) – Bounding boxes with shape N, 4. Where N is the number of boxes.
  • scores (numpy.ndarray or mxnet.nd.NDArray, optional) – Confidence scores of the provided bboxes with shape N.
  • labels (numpy.ndarray or mxnet.nd.NDArray, optional) – Class labels of the provided bboxes with shape N.
  • thresh (float, optional, default 0.5) – Display threshold if scores is provided. Scores with less than thresh will be ignored in display, this is visually more elegant if you have a large number of bounding boxes with very small scores.
  • class_names (list of str, optional) – Description of parameter class_names.
  • colors (dict, optional) – You can provide desired colors as {0: (255, 0, 0), 1:(0, 255, 0), …}, otherwise random colors will be substituded.
  • ax (matplotlib axes, optional) – You can reuse previous axes if provided.
  • reverse_rgb (bool, optional) – Reverse RGB<->BGR orders if True.
  • absolute_coordinates (bool) – If True, absolute coordinates will be considered, otherwise coordinates are interpreted as in range(0, 1).
Returns:

The ploted axes.

Return type:

matplotlib axes

Miscellaneous

gluoncv.utils.download(url, path=None, overwrite=False, sha1_hash=None)[source]

Download an given URL :param url: URL to download :type url: str :param path: Destination path to store downloaded file. By default stores to the

current directory with same name as in url.
Parameters:
  • overwrite (bool, optional) – Whether to overwrite destination file if already exists.
  • sha1_hash (str, optional) – Expected sha1 hash in hexadecimal digits. Will ignore existing file when hash is specified but doesn’t match.
Returns:

The file path of the downloaded file.

Return type:

str

gluoncv.utils.makedirs(path)[source]

Create directory recursively if not exists. Similar to makedir -p, you can skip checking existance before this function.

Parameters:path (str) – Path of the desired dir

Random wrapper.

gluoncv.utils.random.seed(a=None)[source]

Seed the generator for python builtin random, numpy.random, mxnet.random.

This method is to control random state for mxnet related random functions.

Note that this function cannot guarantee 100 percent reproducibility due to hardware settings.

Parameters:a (int or 1-d array_like, optional) – Initialize internal state of the random number generator. If seed is not None or an int or a long, then hash(seed) is used instead. Note that the hash values for some types are nondeterministic.

Training Helpers

class gluoncv.utils.PolyLRScheduler(baselr, niters, nepochs, power=0.9)[source]

Poly like Learning Rate Scheduler It returns a new learning rate by:

lr = baselr * (1 - iter/maxiter) ^ power
Parameters:
  • baselr (float) – Base learning rate.
  • niters (int) – Number of iterations in each epoch.
  • nepochs (int) – Number of training epochs.
  • power (float) – Power of poly function.
gluoncv.utils.set_lr_mult(net, pattern, mult=1.0, verbose=False)[source]

Reset lr_mult to new value for all parameters that match pattern

Parameters:
  • net (mxnet.gluon.Block) – The network whose parameters are going to be adjusted.
  • pattern (str) – Regex matching pattern for targeting parameters.
  • mult (float, default 1.0) – The new learning rate multiplier.
  • verbose (bool) – Print which parameters being modifed if set True.
Returns:

Original network with learning rate multipliers modified.

Return type:

mxnet.gluon.Block

Bounding Box Utils

gluoncv.utils.bbox_iou(bbox_a, bbox_b)[source]

Calculate Intersection-Over-Union(IOU) of two bounding boxes.

Parameters:
Returns:

An ndarray with shape \((N, M)\) indicates IOU between each pairs of bounding boxes in bbox_a and bbox_b.

Return type:

numpy.ndarray