Shortcuts

SanitizeBoundingBox

class torchvision.transforms.v2.SanitizeBoundingBox(min_size: float = 1.0, labels_getter: Optional[Union[Callable[[Any], Optional[Tensor]], str]] = 'default')[source]

[BETA] Remove degenerate/invalid bounding boxes and their corresponding labels and masks.

Warning

The SanitizeBoundingBox transform is in Beta stage, and while we do not expect major breaking changes, some APIs may still change according to user feedback. Please submit any feedback you may have in this issue: https://github.com/pytorch/vision/issues/6753, and you can also check out https://github.com/pytorch/vision/issues/7319 to learn more about the APIs that we suspect might involve future changes.

This transform removes bounding boxes and their associated labels/masks that:

  • are below a given min_size: by default this also removes degenerate boxes that have e.g. X2 <= X1.

  • have any coordinate outside of their corresponding image. You may want to call ClampBoundingBox first to avoid undesired removals.

It is recommended to call it at the end of a pipeline, before passing the input to the models. It is critical to call this transform if RandomIoUCrop was called. If you want to be extra careful, you may call it after all transforms that may modify bounding boxes but once at the end should be enough in most cases.

Parameters:
  • min_size (float, optional) –

  • labels_getter (callable or str or None, optional) – indicates how to identify the labels in the input. It can be a str in which case the input is expected to be a dict, and labels_getter then specifies the key whose value corresponds to the labels. It can also be a callable that takes the same input as the transform, and returns the labels. By default, this will try to find a “labels” key in the input, if the input is a dict or it is a tuple whose second element is a dict. This heuristic should work well with a lot of datasets, including the built-in torchvision datasets.

Examples using SanitizeBoundingBox:

Transforms v2: End-to-end object detection example

Transforms v2: End-to-end object detection example
forward(*inputs: Any) Any[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources