.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/transforms/plot_rotated_box_transforms.py" .. LINE NUMBERS ARE GIVEN BELOW. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_transforms_plot_rotated_box_transforms.py: =============================================================== Transforms on Rotated Bounding Boxes =============================================================== This example illustrates how to define and use rotated bounding boxes. We'll cover how to define them, demonstrate their usage with some of the existing transforms, and finally some of their unique behavior in comparision to standard bounding boxes. First, a bit of setup code: .. GENERATED FROM PYTHON SOURCE LINES 15-36 .. code-block:: Python from PIL import Image from pathlib import Path import matplotlib.pyplot as plt import torch from torchvision import tv_tensors from torchvision.transforms import v2 from helpers import plot plt.rcParams["figure.figsize"] = [10, 5] plt.rcParams["savefig.bbox"] = "tight" # if you change the seed, make sure that the randomly-applied transforms # properly show that the image can be both transformed and *not* transformed! torch.manual_seed(0) # If you're trying to run that on Colab, you can download the assets and the # helpers from https://github.com/pytorch/vision/tree/main/gallery/ orig_img = Image.open(Path('../assets') / 'leaning_tower.jpg') .. GENERATED FROM PYTHON SOURCE LINES 37-47 Creating a Rotated Bounding Box ------------------------------- Rotated bounding boxes are created by instantiating the :class:`~torchvision.tv_tensors.BoundingBoxes` class. It's the `format` parameter of the constructor that determines if a bounding box is rotated or not. In this instance, we use the :attr:`~torchvision.tv_tensors.BoundingBoxFormat` kind `CXCYWHR`. The first two values are the `x` and `y` coordinates of the center of the bounding box. The next two values are the `width` and `height` of the bounding box, and the last value is the `rotation` of the bounding box. .. GENERATED FROM PYTHON SOURCE LINES 47-59 .. code-block:: Python orig_box = tv_tensors.BoundingBoxes( [ [860.0, 1100, 570, 1840, -7], ], format="CXCYWHR", canvas_size=(orig_img.size[1], orig_img.size[0]), ) plot([(orig_img, orig_box)], bbox_width=10) .. image-sg:: /auto_examples/transforms/images/sphx_glr_plot_rotated_box_transforms_001.png :alt: plot rotated box transforms :srcset: /auto_examples/transforms/images/sphx_glr_plot_rotated_box_transforms_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 60-65 Rotation -------- Rotated bounding boxes maintain their rotation with respect to the image even when the image itself is rotated through the :class:`~torchvision.transforms.RandomRotation` transform. .. GENERATED FROM PYTHON SOURCE LINES 65-69 .. code-block:: Python rotater = v2.RandomRotation(degrees=(0, 180), expand=True) rotated_imgs = [rotater((orig_img, orig_box)) for _ in range(4)] plot([(orig_img, orig_box)] + rotated_imgs, bbox_width=10) .. image-sg:: /auto_examples/transforms/images/sphx_glr_plot_rotated_box_transforms_002.png :alt: plot rotated box transforms :srcset: /auto_examples/transforms/images/sphx_glr_plot_rotated_box_transforms_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 70-74 Padding ------- Rotated bounding boxes also maintain their properties when the image is padded using :class:`~torchvision.transforms.Pad`. .. GENERATED FROM PYTHON SOURCE LINES 74-80 .. code-block:: Python padded_imgs_and_boxes = [ v2.Pad(padding=padding)(orig_img, orig_box) for padding in (30, 50, 100, 200) ] plot([(orig_img, orig_box)] + padded_imgs_and_boxes, bbox_width=10) .. image-sg:: /auto_examples/transforms/images/sphx_glr_plot_rotated_box_transforms_003.png :alt: plot rotated box transforms :srcset: /auto_examples/transforms/images/sphx_glr_plot_rotated_box_transforms_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 81-91 Resizing -------- Rotated bounding boxes are also resized along with an image in the :class:`~torchvision.transforms.Resize` transform. Note that the bounding box looking bigger in the images with less pixels is an artifact, not reality. That is merely the rasterised representation of the bounding box's boundaries appearing bigger because we specify a fixed width of that rasterized line. When the image is, say, only 30 pixels wide, a line that is 3 pixels wide is relatively large. .. GENERATED FROM PYTHON SOURCE LINES 91-97 .. code-block:: Python resized_imgs = [ v2.Resize(size=size)(orig_img, orig_box) for size in (30, 50, 100, orig_img.size) ] plot([(orig_img, orig_box)] + resized_imgs, bbox_width=5) .. image-sg:: /auto_examples/transforms/images/sphx_glr_plot_rotated_box_transforms_004.png :alt: plot rotated box transforms :srcset: /auto_examples/transforms/images/sphx_glr_plot_rotated_box_transforms_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 98-102 Perspective ----------- The rotated bounding box is also transformed along with the image when the perspective is transformed with :class:`~torchvision.transforms.RandomPerspective`. .. GENERATED FROM PYTHON SOURCE LINES 102-106 .. code-block:: Python perspective_transformer = v2.RandomPerspective(distortion_scale=0.6, p=1.0) perspective_imgs = [perspective_transformer(orig_img, orig_box) for _ in range(4)] plot([(orig_img, orig_box)] + perspective_imgs, bbox_width=10) .. image-sg:: /auto_examples/transforms/images/sphx_glr_plot_rotated_box_transforms_005.png :alt: plot rotated box transforms :srcset: /auto_examples/transforms/images/sphx_glr_plot_rotated_box_transforms_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 107-111 Elastic Transform ----------------- The rotated bounding box is appropriately unchanged when going through the :class:`~torchvision.transforms.ElasticTransform`. .. GENERATED FROM PYTHON SOURCE LINES 111-117 .. code-block:: Python elastic_imgs = [ v2.ElasticTransform(alpha=alpha)(orig_img, orig_box) for alpha in (100.0, 500.0, 1000.0, 2000.0) ] plot([(orig_img, orig_box)] + elastic_imgs, bbox_width=10) .. image-sg:: /auto_examples/transforms/images/sphx_glr_plot_rotated_box_transforms_006.png :alt: plot rotated box transforms :srcset: /auto_examples/transforms/images/sphx_glr_plot_rotated_box_transforms_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 118-140 Crop & Clamping Modes --------------------- The :class:`~torchvision.transforms.CenterCrop` transform selectively crops the image on a center location. The behavior of the rotated bounding box depends on its `clamping_mode`. We can set the `clamping_mode` in the :class:`~torchvision.tv_tensors.BoundingBoxes` constructur, or by directly setting it after construction as we do in the example below. There are two values for `clamping_mode`: - `"soft"`: The default when constucting :class:`~torchvision.tv_tensors.BoundingBoxes`. - `"hard"`: For standard bounding boxes, both modes behave the same. We also need to document: - `clamping_mode` for individual kernels. - `clamping_mode` in :class:`~torchvision.transforms.v2.ClampBoundingBoxes`. - the new :class:`~torchvision.transforms.v2.SetClampingMode` transform. .. GENERATED FROM PYTHON SOURCE LINES 140-157 .. code-block:: Python assert orig_box.clamping_mode == "soft" hard_box = orig_box.clone() hard_box.clamping_mode = "hard" soft_center_crops_and_boxes = [ v2.CenterCrop(size=size)(orig_img, orig_box) for size in (800, 1200, 2000, orig_img.size) ] hard_center_crops_and_boxes = [ v2.CenterCrop(size=size)(orig_img, hard_box) for size in (800, 1200, 2000, orig_img.size) ] plot([[(orig_img, orig_box)] + soft_center_crops_and_boxes, [(orig_img, hard_box)] + hard_center_crops_and_boxes], bbox_width=10) .. image-sg:: /auto_examples/transforms/images/sphx_glr_plot_rotated_box_transforms_007.png :alt: plot rotated box transforms :srcset: /auto_examples/transforms/images/sphx_glr_plot_rotated_box_transforms_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.204 seconds) .. _sphx_glr_download_auto_examples_transforms_plot_rotated_box_transforms.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_rotated_box_transforms.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_rotated_box_transforms.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_rotated_box_transforms.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_