Skip to content

Commit f6d1838

Browse files
committed
Skeleton for clamping modes
1 parent ffe2d2c commit f6d1838

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

gallery/transforms/plot_rotated_box_transforms.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,19 @@
5858

5959
# %%
6060
# Rotation
61-
# --------------
61+
# --------
62+
# Rotated bounding boxes maintain their rotation with respect to the image even
63+
# when the image itself is rotated through the
64+
# :class:`~torchvision.transforms.RandomRotation` transform.
6265
rotater = v2.RandomRotation(degrees=(0, 180), expand=True)
6366
rotated_imgs = [rotater((orig_img, orig_box)) for _ in range(4)]
6467
plot([(orig_img, orig_box)] + rotated_imgs, bbox_width=10)
6568

6669
# %%
6770
# Padding
68-
# -------------
69-
# The rotated bounding boxes also respect padding transforms.
71+
# -------
72+
# Rotated bounding boxes also maintain their properties when the image is padded using
73+
# :class:`~torchvision.transforms.Pad`.
7074
padded_imgs_and_boxes = [
7175
v2.Pad(padding=padding)(orig_img, orig_box)
7276
for padding in (30, 50, 100, 200)
@@ -75,28 +79,35 @@
7579

7680
# %%
7781
# Resizing
78-
# --------------
79-
# Note that the bounding box looking bigger in the small images is an artifact,
80-
# not reality. It is due to the the fact that we specify a fixed-size for the
81-
# width of the lines to draw. When the image is, say, only 30 pixels wide, a
82-
# line that is 3 pixels wide is relatively large. We could potentially try to
83-
# tweak the plotting function to avoid this appearance.
82+
# --------
83+
# Rotated bounding boxes are also resized along with an image in the
84+
# :class:`~torchvision.transforms.Resize` transform.
85+
#
86+
# Note that the bounding box looking bigger in the images with less pixels is
87+
# an artifact, not reality. That is merely the rasterised representation of the
88+
# bounding box's boundaries appearing bigger because we specify a fixed width of
89+
# that rasterized line. When the image is, say, only 30 pixels wide, a
90+
# line that is 3 pixels wide is relatively large.
8491
resized_imgs = [
8592
v2.Resize(size=size)(orig_img, orig_box)
8693
for size in (30, 50, 100, orig_img.size)
8794
]
88-
plot([(orig_img, orig_box)] + resized_imgs, bbox_width=3)
95+
plot([(orig_img, orig_box)] + resized_imgs, bbox_width=5)
8996

9097
# %%
9198
# Perspective
9299
# -----------
100+
# The rotated bounding box is also transformed along with the image when the
101+
# perspective is transformed with :class:`~torchvision.transforms.RandomPerspective`.
93102
perspective_transformer = v2.RandomPerspective(distortion_scale=0.6, p=1.0)
94103
perspective_imgs = [perspective_transformer(orig_img, orig_box) for _ in range(4)]
95104
plot([(orig_img, orig_box)] + perspective_imgs, bbox_width=10)
96105

97106
# %%
98107
# Elastic Transform
99108
# -----------------
109+
# The rotated bounding box is appropriately unchanged when going through the
110+
# :class:`~torchvision.transforms.ElasticTransform`.
100111
elastic_imgs = [
101112
v2.ElasticTransform(alpha=alpha)(orig_img, orig_box)
102113
for alpha in (100.0, 500.0, 1000.0, 2000.0)
@@ -106,14 +117,26 @@
106117
# %%
107118
# Crop & Clamping Modes
108119
# ---------------------
109-
# This section doubles as the example for Crop and for explaining clamping
110-
# modes. My rationale for doing at both at once: any meaningful examples for
111-
# cropping are going to impact the bounding box, and the only way to make
112-
# sense of that is to also explain clamping modes. We should cover:
120+
# The :class:`~torchvision.transforms.CenterCrop` transform selectively crops
121+
# the image on a center location. The behavior of the rotated bounding box
122+
# depends on its `clamping_mode`. We can set the `clamping_mode` in the
123+
# :class:`~torchvision.tv_tensors.BoundingBoxes` constructur, or by directly
124+
# setting it after construction as we do in the example below.
125+
#
126+
# There are two values for `clamping_mode`:
127+
#
128+
# - `"soft"`: The default when constucting
129+
# :class:`~torchvision.tv_tensors.BoundingBoxes`. <Insert semantic
130+
# description for soft mode.>
131+
# - `"hard"`: <Insert semantic description for hard mode.>
132+
#
133+
# For standard bounding boxes, both modes behave the same. We also need to
134+
# document:
135+
#
136+
# - `clamping_mode` for individual kernels.
137+
# - `clamping_mode` in :class:`~torchvision.transforms.v2.ClampBoundingBoxes`.
138+
# - the new :class:`~torchvision.transforms.v2.SetClampingMode` transform.
113139
#
114-
# * Clamping mode kinds: hard, soft, None. Behavior of each, when to use them.
115-
# * Clamping mode defaults for: bounding boxes, functionals, transforms in
116-
# general, ClampingBoundingBoxes() specifically.
117140
assert orig_box.clamping_mode == "soft"
118141
hard_box = orig_box.clone()
119142
hard_box.clamping_mode = "hard"

0 commit comments

Comments
 (0)