API Reference

mmdet.apis

mmdet.core

anchor

class mmdet.core.anchor.AnchorGenerator(strides, ratios, scales=None, base_sizes=None, scale_major=True, octave_base_scale=None, scales_per_octave=None, centers=None, center_offset=0.0)[source]

Standard anchor generator for 2D anchor-based detectors.

Parameters:
  • strides (list[int] | list[tuple[int, int]]) – Strides of anchors in multiple feature levels.
  • ratios (list[float]) – The list of ratios between the height and width of anchors in a single level.
  • scales (list[int] | None) – Anchor scales for anchors in a single level. It cannot be set at the same time if octave_base_scale and scales_per_octave are set.
  • base_sizes (list[int] | None) – The basic sizes of anchors in multiple levels. If None is given, strides will be used as base_sizes. (If strides are non square, the shortest stride is taken.)
  • scale_major (bool) – Whether to multiply scales first when generating base anchors. If true, the anchors in the same row will have the same scales. By default it is True in V2.0
  • octave_base_scale (int) – The base scale of octave.
  • scales_per_octave (int) – Number of scales for each octave. octave_base_scale and scales_per_octave are usually used in retinanet and the scales should be None when they are set.
  • centers (list[tuple[float, float]] | None) – The centers of the anchor relative to the feature grid center in multiple feature levels. By default it is set to be None and not used. If a list of tuple of float is given, they will be used to shift the centers of anchors.
  • center_offset (float) – The offset of center in propotion to anchors’ width and height. By default it is 0 in V2.0.

Examples

>>> from mmdet.core import AnchorGenerator
>>> self = AnchorGenerator([16], [1.], [1.], [9])
>>> all_anchors = self.grid_anchors([(2, 2)], device='cpu')
>>> print(all_anchors)
[tensor([[-4.5000, -4.5000,  4.5000,  4.5000],
        [11.5000, -4.5000, 20.5000,  4.5000],
        [-4.5000, 11.5000,  4.5000, 20.5000],
        [11.5000, 11.5000, 20.5000, 20.5000]])]
>>> self = AnchorGenerator([16, 32], [1.], [1.], [9, 18])
>>> all_anchors = self.grid_anchors([(2, 2), (1, 1)], device='cpu')
>>> print(all_anchors)
[tensor([[-4.5000, -4.5000,  4.5000,  4.5000],
        [11.5000, -4.5000, 20.5000,  4.5000],
        [-4.5000, 11.5000,  4.5000, 20.5000],
        [11.5000, 11.5000, 20.5000, 20.5000]]),         tensor([[-9., -9., 9., 9.]])]
gen_base_anchors()[source]

Generate base anchors.

Returns:
Base anchors of a feature grid in multiple
feature levels.
Return type:list(torch.Tensor)
gen_single_level_base_anchors(base_size, scales, ratios, center=None)[source]

Generate base anchors of a single level.

Parameters:
  • base_size (int | float) – Basic size of an anchor.
  • scales (torch.Tensor) – Scales of the anchor.
  • ratios (torch.Tensor) – The ratio between between the height and width of anchors in a single level.
  • center (tuple[float], optional) – The center of the base anchor related to a single feature grid. Defaults to None.
Returns:

Anchors in a single-level feature maps

Return type:

torch.Tensor

grid_anchors(featmap_sizes, device='cuda')[source]

Generate grid anchors in multiple feature levels.

Parameters:
  • featmap_sizes (list[tuple]) – List of feature map sizes in multiple feature levels.
  • device (str) – Device where the anchors will be put on.
Returns:

Anchors in multiple feature levels.

The sizes of each tensor should be [N, 4], where N = width * height * num_base_anchors, width and height are the sizes of the corresponding feature lavel, num_base_anchors is the number of anchors for that level.

Return type:

list[torch.Tensor]

num_base_anchors

total number of base anchors in a feature grid

Type:list[int]
num_levels

number of feature levels that the generator will be applied

Type:int
single_level_grid_anchors(base_anchors, featmap_size, stride=(16, 16), device='cuda')[source]

Generate grid anchors of a single level.

Note

This function is usually called by method self.grid_anchors.

Parameters:
  • base_anchors (torch.Tensor) – The base anchors of a feature grid.
  • featmap_size (tuple[int]) – Size of the feature maps.
  • stride (tuple[int], optional) – Stride of the feature map. Defaults to (16, 16).
  • device (str, optional) – Device the tensor will be put on. Defaults to ‘cuda’.
Returns:

Anchors in the overall feature maps.

Return type:

torch.Tensor

single_level_valid_flags(featmap_size, valid_size, num_base_anchors, device='cuda')[source]

Generate the valid flags of anchor in a single feature map.

Parameters:
  • featmap_size (tuple[int]) – The size of feature maps.
  • valid_size (tuple[int]) – The valid size of the feature maps.
  • num_base_anchors (int) – The number of base anchors.
  • device (str, optional) – Device where the flags will be put on. Defaults to ‘cuda’.
Returns:

The valid flags of each anchor in a single level

feature map.

Return type:

torch.Tensor

valid_flags(featmap_sizes, pad_shape, device='cuda')[source]

Generate valid flags of anchors in multiple feature levels.

Parameters:
  • featmap_sizes (list(tuple)) – List of feature map sizes in multiple feature levels.
  • pad_shape (tuple) – The padded shape of the image.
  • device (str) – Device where the anchors will be put on.
Returns:

Valid flags of anchors in multiple levels.

Return type:

list(torch.Tensor)

class mmdet.core.anchor.LegacyAnchorGenerator(strides, ratios, scales=None, base_sizes=None, scale_major=True, octave_base_scale=None, scales_per_octave=None, centers=None, center_offset=0.0)[source]

Legacy anchor generator used in MMDetection V1.x.

Difference to the V2.0 anchor generator:

  1. The center offset of V1.x anchors are set to be 0.5 rather than 0.
  2. The width/height are minused by 1 when calculating the anchors’ centers and corners to meet the V1.x coordinate system.
  3. The anchors’ corners are quantized.
Parameters:
  • strides (list[int] | list[tuple[int]]) – Strides of anchors in multiple feature levels.
  • ratios (list[float]) – The list of ratios between the height and width of anchors in a single level.
  • scales (list[int] | None) – Anchor scales for anchors in a single level. It cannot be set at the same time if octave_base_scale and scales_per_octave are set.
  • base_sizes (list[int]) – The basic sizes of anchors in multiple levels. If None is given, strides will be used to generate base_sizes.
  • scale_major (bool) – Whether to multiply scales first when generating base anchors. If true, the anchors in the same row will have the same scales. By default it is True in V2.0
  • octave_base_scale (int) – The base scale of octave.
  • scales_per_octave (int) – Number of scales for each octave. octave_base_scale and scales_per_octave are usually used in retinanet and the scales should be None when they are set.
  • centers (list[tuple[float, float]] | None) – The centers of the anchor relative to the feature grid center in multiple feature levels. By default it is set to be None and not used. It a list of float is given, this list will be used to shift the centers of anchors.
  • center_offset (float) – The offset of center in propotion to anchors’ width and height. By default it is 0.5 in V2.0 but it should be 0.5 in v1.x models.

Examples

>>> from mmdet.core import LegacyAnchorGenerator
>>> self = LegacyAnchorGenerator(
>>>     [16], [1.], [1.], [9], center_offset=0.5)
>>> all_anchors = self.grid_anchors(((2, 2),), device='cpu')
>>> print(all_anchors)
[tensor([[ 0.,  0.,  8.,  8.],
        [16.,  0., 24.,  8.],
        [ 0., 16.,  8., 24.],
        [16., 16., 24., 24.]])]
gen_single_level_base_anchors(base_size, scales, ratios, center=None)[source]

Generate base anchors of a single level.

Note

The width/height of anchors are minused by 1 when calculating
the centers and corners to meet the V1.x coordinate system.
Parameters:
  • base_size (int | float) – Basic size of an anchor.
  • scales (torch.Tensor) – Scales of the anchor.
  • ratios (torch.Tensor) – The ratio between between the height. and width of anchors in a single level.
  • center (tuple[float], optional) – The center of the base anchor related to a single feature grid. Defaults to None.
Returns:

Anchors in a single-level feature map.

Return type:

torch.Tensor

mmdet.core.anchor.anchor_inside_flags(flat_anchors, valid_flags, img_shape, allowed_border=0)[source]

Check whether the anchors are inside the border.

Parameters:
  • flat_anchors (torch.Tensor) – Flatten anchors, shape (n, 4).
  • valid_flags (torch.Tensor) – An existing valid flags of anchors.
  • img_shape (tuple(int)) – Shape of current image.
  • allowed_border (int, optional) – The border to allow the valid anchor. Defaults to 0.
Returns:

Flags indicating whether the anchors are inside a

valid range.

Return type:

torch.Tensor

mmdet.core.anchor.images_to_levels(target, num_levels)[source]

Convert targets by image to targets by feature level.

[target_img0, target_img1] -> [target_level0, target_level1, …]

mmdet.core.anchor.calc_region(bbox, ratio, featmap_size=None)[source]

Calculate a proportional bbox region.

The bbox center are fixed and the new h’ and w’ is h * ratio and w * ratio.

Parameters:
  • bbox (Tensor) – Bboxes to calculate regions, shape (n, 4).
  • ratio (float) – Ratio of the output region.
  • featmap_size (tuple) – Feature map size used for clipping the boundary.
Returns:

x1, y1, x2, y2

Return type:

tuple

bbox

mask

evaluation

post_processing

fp16

optimizer

utils

mmdet.datasets

datasets

pipelines

mmdet.models

detectors

backbones

necks

dense_heads

roi_heads

losses