detectron2.utils — detectron2 0.6 documentation (original) (raw)

detectron2

detectron2.utils.colormap module

An awesome colormap for really neat visualizations. Copied from Detectron, and removed gray colors.

detectron2.utils.colormap. colormap(rgb=False, maximum=255)[source]

Parameters

Returns

ndarray – a float32 array of Nx3 colors, in range [0, 255] or [0, 1]

detectron2.utils.colormap. random_color(rgb=False, maximum=255)[source]

Parameters

Returns

ndarray – a vector of 3 numbers

detectron2.utils.colormap. random_colors(N, rgb=False, maximum=255)[source]

Parameters

Returns

ndarray – a list of random_color

detectron2.utils.comm module

This file contains primitives for multi-gpu communication. This is useful when doing distributed training.

detectron2.utils.comm. get_world_size() → int[source]

detectron2.utils.comm. get_rank() → int[source]

detectron2.utils.comm. create_local_process_group(num_workers_per_machine: int) → None[source]

Create a process group that contains ranks within the same machine.

Detectron2’s launch() in engine/launch.py will call this function. If you start workers without launch(), you’ll have to also call this. Otherwise utilities like get_local_rank() will not work.

This function contains a barrier. All processes must call it together.

Parameters

num_workers_per_machine – the number of worker processes per machine. Typically the number of GPUs.

detectron2.utils.comm. get_local_process_group()[source]

Returns

A torch process group which only includes processes that are on the same machine as the current process. This group can be useful for communication within a machine, e.g. a per-machine SyncBN.

detectron2.utils.comm. get_local_rank() → int[source]

Returns

The rank of the current process within the local (per-machine) process group.

detectron2.utils.comm. get_local_size() → int[source]

Returns

The size of the per-machine process group, i.e. the number of processes per machine.

detectron2.utils.comm. is_main_process() → bool[source]

detectron2.utils.comm. synchronize()[source]

Helper function to synchronize (barrier) among all processes when using distributed training

detectron2.utils.comm. all_gather(data, group=None)[source]

Run all_gather on arbitrary picklable data (not necessarily tensors).

Parameters

Returns

list[data] – list of data gathered from each rank

detectron2.utils.comm. gather(data, dst=0, group=None)[source]

Run gather on arbitrary picklable data (not necessarily tensors).

Parameters

Returns

list[data]

on dst, a list of data gathered from each rank. Otherwise,

an empty list.

detectron2.utils.comm. shared_random_seed()[source]

Returns

int – a random number that is the same across all workers. If workers need a shared RNG, they can use this shared seed to create one.

All workers must call this function, otherwise it will deadlock.

detectron2.utils.comm. reduce_dict(input_dict, average=True)[source]

Reduce the values in the dictionary from all processes so that process with rank 0 has the reduced results.

Parameters

Returns

a dict with the same keys as input_dict, after reduction.

detectron2.utils.events module

detectron2.utils.logger module

detectron2.utils.logger. setup_logger(output=None, distributed_rank=0, *, color=True, name='detectron2', abbrev_name=None, enable_propagation: bool = False, configure_stdout: bool = True)[source]

Initialize the detectron2 logger and set its verbosity level to “DEBUG”.

Parameters

Returns

logging.Logger – a logger

detectron2.utils.logger. log_first_n(lvl, msg, n=1, *, name=None, key='caller')[source]

Log only for the first n times.

Parameters

detectron2.utils.logger. log_every_n(lvl, msg, n=1, *, name=None)[source]

Log once per n times.

Parameters

detectron2.utils.logger. log_every_n_seconds(lvl, msg, n=1, *, name=None)[source]

Log no more than once per n seconds.

Parameters

detectron2.utils.registry module

class detectron2.utils.registry. Registry(*args, **kwds)[source]

Bases: collections.abc.Iterable, typing.Generic

The registry that provides name -> object mapping, to support third-party users’ custom modules.

To create a registry (e.g. a backbone registry):

BACKBONE_REGISTRY = Registry('BACKBONE')

To register an object:

@BACKBONE_REGISTRY.register() class MyBackbone(): ...

Or:

BACKBONE_REGISTRY.register(MyBackbone)

__init__(name: str) → None[source]

Parameters

name (str) – the name of this registry

register(obj: Any = None) → Any[source]

Register the given object under the the name obj.__name__. Can be used as either a decorator or not. See docstring of this class for usage.

get(name: str) → Any[source]

detectron2.utils.registry. locate(name: str) → Any[source]

Locate and return an object x using an input string {x.__module__}.{x.__qualname__}, such as “module.submodule.class_name”.

Raise Exception if it cannot be found.

detectron2.utils.memory module

detectron2.utils.memory. retry_if_cuda_oom(func)[source]

Makes a function retry itself after encountering pytorch’s CUDA OOM error. It will first retry after calling torch.cuda.empty_cache().

If that still fails, it will then retry by trying to convert inputs to CPUs. In this case, it expects the function to dispatch to CPU implementation. The return values may become CPU tensors as well and it’s user’s responsibility to convert it back to CUDA tensor if needed.

Parameters

func – a stateless callable that takes tensor-like objects as arguments

Returns

a callable which retries func if OOM is encountered.

Examples:

output = retry_if_cuda_oom(some_torch_function)(input1, input2)

output may be on CPU even if inputs are on GPU

Note

  1. When converting inputs to CPU, it will only look at each argument and check if it has .device and .to for conversion. Nested structures of tensors are not supported.
  2. Since the function might be called more than once, it has to be stateless.

detectron2.utils.analysis module

detectron2.utils.visualizer module

class detectron2.utils.visualizer. ColorMode(value)[source]

Bases: enum.Enum

Enum of different color modes to use for instance visualizations.

IMAGE = 0

Picks a random color for every instance and overlay segmentations with low opacity.

SEGMENTATION = 1

Let instances of the same category have similar colors (from metadata.thing_colors), and overlay them with high opacity. This provides more attention on the quality of segmentation.

IMAGE_BW = 2

Same as IMAGE, but convert all areas without masks to gray-scale. Only available for drawing per-instance mask predictions.

class detectron2.utils.visualizer. VisImage(img, scale=1.0)[source]

Bases: object

__init__(img, scale=1.0)[source]

Parameters

reset_image(img)[source]

Parameters

img – same as in __init__

save(filepath)[source]

Parameters

filepath (str) – a string that contains the absolute path, including the file name, where the visualized image will be saved.

get_image()[source]

Returns

ndarray – the visualized image of shape (H, W, 3) (RGB) in uint8 type. The shape is scaled w.r.t the input image using the given scale argument.

class detectron2.utils.visualizer. Visualizer(img_rgb, metadata=None, scale=1.0, instance_mode=<ColorMode.IMAGE: 0>)[source]

Bases: object

Visualizer that draws data about detection/segmentation on images.

It contains methods like draw_{text,box,circle,line,binary_mask,polygon}that draw primitive objects to images, as well as high-level wrappers likedraw_{instance_predictions,sem_seg,panoptic_seg_predictions,dataset_dict}that draw composite data in some pre-defined style.

Note that the exact visualization style for the high-level wrappers are subject to change. Style such as color, opacity, label contents, visibility of labels, or even the visibility of objects themselves (e.g. when the object is too small) may change according to different heuristics, as long as the results still look visually reasonable.

To obtain a consistent style, you can implement custom drawing functions with the abovementioned primitive methods instead. If you need more customized visualization styles, you can process the data yourself following their format documented in tutorials (Use Models, Use Custom Datasets). This class does not intend to satisfy everyone’s preference on drawing styles.

This visualizer focuses on high rendering quality rather than performance. It is not designed to be used for real-time applications.

__init__(img_rgb, metadata=None, scale=1.0, instance_mode=<ColorMode.IMAGE: 0>)[source]

Parameters

draw_instance_predictions(predictions)[source]

Draw instance-level prediction results on an image.

Parameters

predictions (Instances) – the output of an instance detection/segmentation model. Following fields will be used to draw: “pred_boxes”, “pred_classes”, “scores”, “pred_masks” (or “pred_masks_rle”).

Returns

output (VisImage) – image object with visualizations.

draw_sem_seg(sem_seg, area_threshold=None, alpha=0.8)[source]

Draw semantic segmentation predictions/labels.

Parameters

Returns

output (VisImage) – image object with visualizations.

draw_panoptic_seg(panoptic_seg, segments_info, area_threshold=None, alpha=0.7)[source]

Draw panoptic prediction annotations or results.

Parameters

Returns

output (VisImage) – image object with visualizations.

draw_dataset_dict(dic)[source]

Draw annotations/segmentations in Detectron2 Dataset format.

Parameters

dic (dict) – annotation/segmentation data of one image, in Detectron2 Dataset format.

Returns

output (VisImage) – image object with visualizations.

overlay_instances(*, boxes=None, labels=None, masks=None, keypoints=None, assigned_colors=None, alpha=0.5)[source]

Parameters

Returns

output (VisImage) – image object with visualizations.

overlay_rotated_instances(boxes=None, labels=None, assigned_colors=None)[source]

Parameters

Returns

output (VisImage) – image object with visualizations.

draw_and_connect_keypoints(keypoints)[source]

Draws keypoints of an instance and follows the rules for keypoint connections to draw lines between appropriate keypoints. This follows color heuristics for line color.

Parameters

keypoints (Tensor) – a tensor of shape (K, 3), where K is the number of keypoints and the last dimension corresponds to (x, y, probability).

Returns

output (VisImage) – image object with visualizations.

draw_text(text, position, *, font_size=None, color='g', horizontal_alignment='center', rotation=0)[source]

Parameters

Returns

output (VisImage) – image object with text drawn.

draw_box(box_coord, alpha=0.5, edge_color='g', line_style='-')[source]

Parameters

Returns

output (VisImage) – image object with box drawn.

draw_rotated_box_with_label(rotated_box, alpha=0.5, edge_color='g', line_style='-', label=None)[source]

Draw a rotated box with label on its top-left corner.

Parameters

Returns

output (VisImage) – image object with box drawn.

draw_circle(circle_coord, color, radius=3)[source]

Parameters

Returns

output (VisImage) – image object with box drawn.

draw_line(x_data, y_data, color, linestyle='-', linewidth=None)[source]

Parameters

Returns

output (VisImage) – image object with line drawn.

draw_binary_mask(binary_mask, color=None, *, edge_color=None, text=None, alpha=0.5, area_threshold=10)[source]

Parameters

Returns

output (VisImage) – image object with mask drawn.

draw_soft_mask(soft_mask, color=None, *, text=None, alpha=0.5)[source]

Parameters

Returns

output (VisImage) – image object with mask drawn.

draw_polygon(segment, color, edge_color=None, alpha=0.5)[source]

Parameters

Returns

output (VisImage) – image object with polygon drawn.

get_output()[source]

Returns

output (VisImage) – the image output containing the visualizations added to the image.

detectron2.utils.video_visualizer module

class detectron2.utils.video_visualizer. VideoVisualizer(metadata, instance_mode=<ColorMode.IMAGE: 0>)[source]

Bases: object

__init__(metadata, instance_mode=<ColorMode.IMAGE: 0>)[source]

Parameters

metadata (MetadataCatalog) – image metadata.

draw_instance_predictions(frame, predictions)[source]

Draw instance-level prediction results on an image.

Parameters

Returns

output (VisImage) – image object with visualizations.

draw_sem_seg(frame, sem_seg, area_threshold=None)[source]

Parameters