Skip to content

Commit 3b045b5

Browse files
committed
Skeleton for rotated bounding box tutorial
1 parent fb3926e commit 3b045b5

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

docs/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def __init__(self, src_dir):
8787
"plot_transforms_illustrations.py",
8888
"plot_transforms_e2e.py",
8989
"plot_cutmix_mixup.py",
90+
"plot_rotated_box_transforms.py",
9091
"plot_custom_transforms.py",
9192
"plot_tv_tensors.py",
9293
"plot_custom_tv_tensors.py",

gallery/assets/leaning_tower.jpg

1.25 MB
Loading
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
"""
2+
===============================================================
3+
Transforms on Rotated Bounding Boxes
4+
===============================================================
5+
6+
Introduction.
7+
8+
First, some code to set everything up.
9+
"""
10+
11+
# %%
12+
from PIL import Image
13+
from pathlib import Path
14+
import matplotlib.pyplot as plt
15+
16+
17+
import torch
18+
import torchvision.transforms.functional as F
19+
from torchvision import tv_tensors
20+
from torchvision.transforms import v2
21+
from torchvision.utils import draw_bounding_boxes
22+
from helpers import plot
23+
24+
plt.rcParams["figure.figsize"] = [10, 5]
25+
plt.rcParams["savefig.bbox"] = "tight"
26+
27+
# if you change the seed, make sure that the randomly-applied transforms
28+
# properly show that the image can be both transformed and *not* transformed!
29+
torch.manual_seed(0)
30+
31+
# If you're trying to run that on Colab, you can download the assets and the
32+
# helpers from https://github.com/pytorch/vision/tree/main/gallery/
33+
orig_img = Image.open(Path('../assets') / 'leaning_tower.jpg')
34+
35+
# %%
36+
# Rotated Bounding Boxes
37+
# ----------------------
38+
# Brief intro into what rotated bounding boxes are. Brief description of the
39+
# image.
40+
41+
orig_box = tv_tensors.BoundingBoxes(
42+
[
43+
[860.0, 1100, 570, 1840, -7],
44+
],
45+
format="CXCYWHR",
46+
canvas_size=(orig_img.size[1], orig_img.size[0]),
47+
clamping_mode="hard",
48+
)
49+
# TODO: why is this necessary?
50+
orig_box = v2.ConvertBoundingBoxFormat("xyxyxyxy")(orig_box)
51+
52+
plot([(orig_img, orig_box)])
53+
54+
# %%
55+
# Image Rotation
56+
# ---------------
57+
# We can rotate the image itself, and the already rotated bounding boxes are
58+
# rotated appropriately.
59+
60+
out_img, out_box = v2.RandomRotation(degrees=(0, 180), expand=True)(orig_img, orig_box)
61+
plot([(out_img, out_box)])
62+
63+
# %%
64+
# Image Padding
65+
# -------------
66+
# The rotated bounding boxes also respect padding transforms.
67+
padded_imgs_and_boxes = [
68+
v2.Pad(padding=padding)(orig_img, orig_box) for padding in (10, 30, 50, 100)
69+
]
70+
plot([(orig_img, orig_box)] + padded_imgs_and_boxes)
71+
72+
# %%
73+
# Image Resizing
74+
# --------------
75+
# The rotated bounding boxes are resized along with the image.
76+
resized_imgs_and_boxes = [v2.Resize(size=size)(orig_img, orig_box) for size in (30, 50, 100, orig_img.size)]
77+
plot([(orig_img, orig_box)] + resized_imgs_and_boxes)
78+
79+
# %%
80+
# Image Rotation
81+
# --------------
82+
rotater = v2.RandomRotation(degrees=(0, 180))
83+
rotated_imgs = [rotater((orig_img, orig_box)) for _ in range(4)]
84+
plot([(orig_img, orig_box)] + rotated_imgs)
85+
86+
# %%
87+
# Elastic Transform
88+
# -----------------
89+
plot([v2.ElasticTransform(alpha=250.0)(orig_img, orig_box)])
90+
91+
# %%
92+
# Clamping Modes
93+
# --------------
94+
# Explain hard and soft, with appropriate links to documentation. Talk about
95+
# defaults. Link to to-be-written-tutorial on mode-setting in general.
96+
soft_box = orig_box.clone()
97+
soft_box.clamping_mode = "soft"
98+
99+
hard_center_crops_and_boxes = [
100+
v2.CenterCrop(size=size)(orig_img, orig_box)
101+
for size in (800, 1200, 2000, orig_img.size)
102+
]
103+
104+
soft_center_crops_and_boxes = [
105+
v2.CenterCrop(size=size)(orig_img, soft_box)
106+
for size in (800, 1200, 2000, orig_img.size)
107+
]
108+
109+
plot([[(orig_img, orig_box)] + hard_center_crops_and_boxes,
110+
[(orig_img, soft_box)] + soft_center_crops_and_boxes])
111+

0 commit comments

Comments
 (0)