Skip to content

Commit 8491129

Browse files
Add illustrations for CenterCrop and FiveCrop in gallery example (#3706)
Co-authored-by: Nicolas Hug <[email protected]>
1 parent 77ccd87 commit 8491129

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

gallery/plot_transforms.py

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
orig_img = Image.open(Path('assets') / 'astronaut.jpg')
1818

1919

20-
def plot(img, title="", with_orig=True, **kwargs):
20+
def plot(img, title: str = "", with_orig: bool = True, **kwargs):
2121
def _plot(img, title, **kwargs):
2222
plt.figure().suptitle(title, fontsize=25)
2323
plt.imshow(np.asarray(img), **kwargs)
2424
plt.axis('off')
2525

2626
if with_orig:
27-
_plot(orig_img, "Original image")
27+
_plot(orig_img, "Original Image")
2828
_plot(img, title, **kwargs)
2929

3030

@@ -35,7 +35,7 @@ def _plot(img, title, **kwargs):
3535
# (see also :func:`~torchvision.transforms.functional.pad`)
3636
# fills image borders with some pixel values.
3737
padded_img = T.Pad(padding=30)(orig_img)
38-
plot(padded_img, "Padded image")
38+
plot(padded_img, "Padded Image")
3939

4040
####################################
4141
# Resize
@@ -44,15 +44,38 @@ def _plot(img, title, **kwargs):
4444
# (see also :func:`~torchvision.transforms.functional.resize`)
4545
# resizes an image.
4646
resized_img = T.Resize(size=30)(orig_img)
47-
plot(resized_img, "Resized image")
47+
plot(resized_img, "Resized Image")
48+
49+
####################################
50+
# CenterCrop
51+
# ----------
52+
# The :class:`~torchvision.transforms.CenterCrop` transform
53+
# (see also :func:`~torchvision.transforms.functional.center_crop`)
54+
# crops the given image at the center.
55+
center_cropped_img = T.CenterCrop(size=(100, 100))(orig_img)
56+
plot(center_cropped_img, "Center Cropped Image")
57+
58+
59+
####################################
60+
# FiveCrop
61+
# --------
62+
# The :class:`~torchvision.transforms.FiveCrop` transform
63+
# (see also :func:`~torchvision.transforms.functional.five_crop`)
64+
# crops the given image into four corners and the central crop.
65+
(img1, img2, img3, img4, img5) = T.FiveCrop(size=(100, 100))(orig_img)
66+
plot(img1, "Top Left Corner Image")
67+
plot(img2, "Top Right Corner Image", with_orig=False)
68+
plot(img3, "Bottom Left Corner Image", with_orig=False)
69+
plot(img4, "Bottom Right Corner Image", with_orig=False)
70+
plot(img5, "Center Image", with_orig=False)
4871

4972
####################################
5073
# ColorJitter
5174
# -----------
5275
# The :class:`~torchvision.transforms.ColorJitter` transform
5376
# randomly changes the brightness, saturation, and other properties of an image.
5477
jitted_img = T.ColorJitter(brightness=.5, hue=.3)(orig_img)
55-
plot(jitted_img, "Jitted image")
78+
plot(jitted_img, "Jitted Image")
5679

5780
####################################
5881
# Grayscale
@@ -61,7 +84,7 @@ def _plot(img, title, **kwargs):
6184
# (see also :func:`~torchvision.transforms.functional.to_grayscale`)
6285
# converts an image to grayscale
6386
gray_img = T.Grayscale()(orig_img)
64-
plot(gray_img, "Grayscale image", cmap='gray')
87+
plot(gray_img, "Grayscale Image", cmap='gray')
6588

6689
####################################
6790
# RandomPerspective
@@ -70,7 +93,7 @@ def _plot(img, title, **kwargs):
7093
# (see also :func:`~torchvision.transforms.functional.perspective`)
7194
# performs random perspective transform on an image.
7295
perspectived_img = T.RandomPerspective(distortion_scale=0.6, p=1.0)(orig_img)
73-
plot(perspectived_img, "Perspective transformed image")
96+
plot(perspectived_img, "Perspective transformed Image")
7497

7598
####################################
7699
# RandomRotation
@@ -79,7 +102,7 @@ def _plot(img, title, **kwargs):
79102
# (see also :func:`~torchvision.transforms.functional.rotate`)
80103
# rotates an image with random angle.
81104
rotated_img = T.RandomRotation(degrees=(30, 70))(orig_img)
82-
plot(rotated_img, "Rotated image")
105+
plot(rotated_img, "Rotated Image")
83106

84107
####################################
85108
# RandomAffine
@@ -88,16 +111,16 @@ def _plot(img, title, **kwargs):
88111
# (see also :func:`~torchvision.transforms.functional.affine`)
89112
# performs random affine transform on an image.
90113
affined_img = T.RandomAffine(degrees=(30, 70), translate=(0.1, 0.3), scale=(0.5, 0.75))(orig_img)
91-
plot(affined_img, "Affine transformed image")
114+
plot(affined_img, "Affine transformed Image")
92115

93116
####################################
94117
# RandomCrop
95118
# ----------
96119
# The :class:`~torchvision.transforms.RandomCrop` transform
97120
# (see also :func:`~torchvision.transforms.functional.crop`)
98121
# crops an image at a random location.
99-
crop = T.RandomCrop(size=(128, 128))(orig_img)
100-
plot(crop, "Random crop")
122+
crop_img = T.RandomCrop(size=(128, 128))(orig_img)
123+
plot(crop_img, "Random cropped Image")
101124

102125
####################################
103126
# RandomResizedCrop
@@ -106,8 +129,8 @@ def _plot(img, title, **kwargs):
106129
# (see also :func:`~torchvision.transforms.functional.resized_crop`)
107130
# crops an image at a random location, and then resizes the crop to a given
108131
# size.
109-
resized_crop = T.RandomResizedCrop(size=(32, 32))(orig_img)
110-
plot(resized_crop, "Random resized crop")
132+
resized_crop_img = T.RandomResizedCrop(size=(32, 32))(orig_img)
133+
plot(resized_crop_img, "Random resized cropped Image")
111134

112135
####################################
113136
# RandomHorizontalFlip
@@ -119,8 +142,8 @@ def _plot(img, title, **kwargs):
119142
# .. note::
120143
# Since the transform is applied randomly, the two images below may actually be
121144
# the same.
122-
random_hflip = T.RandomHorizontalFlip(p=0.5)(orig_img)
123-
plot(random_hflip, "Random horizontal flip")
145+
random_hflip_img = T.RandomHorizontalFlip(p=0.5)(orig_img)
146+
plot(random_hflip_img, "Random horizontal flipped Image")
124147

125148
####################################
126149
# RandomVerticalFlip
@@ -132,8 +155,8 @@ def _plot(img, title, **kwargs):
132155
# .. note::
133156
# Since the transform is applied randomly, the two images below may actually be
134157
# the same.
135-
random_vflip = T.RandomVerticalFlip(p=0.5)(orig_img)
136-
plot(random_vflip, "Random vertical flip")
158+
random_vflip_img = T.RandomVerticalFlip(p=0.5)(orig_img)
159+
plot(random_vflip_img, "Random vertical flipped Image")
137160

138161
####################################
139162
# RandomApply
@@ -144,8 +167,8 @@ def _plot(img, title, **kwargs):
144167
# .. note::
145168
# Since the transform is applied randomly, the two images below may actually be
146169
# the same.
147-
random_apply = T.RandomApply(transforms=[T.RandomCrop(size=(64, 64))], p=0.5)(orig_img)
148-
plot(random_apply, "Random Apply transform")
170+
random_apply_img = T.RandomApply(transforms=[T.RandomCrop(size=(64, 64))], p=0.5)(orig_img)
171+
plot(random_apply_img, "Random Apply transformed Image")
149172

150173
####################################
151174
# GaussianBlur
@@ -154,4 +177,4 @@ def _plot(img, title, **kwargs):
154177
# (see also :func:`~torchvision.transforms.functional.gaussian_blur`)
155178
# performs gaussianblur transform on an image.
156179
gaus_blur_img = T.GaussianBlur(kernel_size=(5, 9), sigma=(0.4, 3.0))(orig_img)
157-
plot(gaus_blur_img, "Gaussian Blur of image")
180+
plot(gaus_blur_img, "Gaussian Blurred Image")

0 commit comments

Comments
 (0)