Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
vision
Commits
84911291
Unverified
Commit
84911291
authored
Apr 22, 2021
by
Aditya Oke
Committed by
GitHub
Apr 22, 2021
Browse files
Add illustrations for CenterCrop and FiveCrop in gallery example (#3706)
Co-authored-by:
Nicolas Hug
<
nicolashug@fb.com
>
parent
77ccd878
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
20 deletions
+43
-20
gallery/plot_transforms.py
gallery/plot_transforms.py
+43
-20
No files found.
gallery/plot_transforms.py
View file @
84911291
...
...
@@ -17,14 +17,14 @@ import torchvision.transforms as T
orig_img
=
Image
.
open
(
Path
(
'assets'
)
/
'astronaut.jpg'
)
def
plot
(
img
,
title
=
""
,
with_orig
=
True
,
**
kwargs
):
def
plot
(
img
,
title
:
str
=
""
,
with_orig
:
bool
=
True
,
**
kwargs
):
def
_plot
(
img
,
title
,
**
kwargs
):
plt
.
figure
().
suptitle
(
title
,
fontsize
=
25
)
plt
.
imshow
(
np
.
asarray
(
img
),
**
kwargs
)
plt
.
axis
(
'off'
)
if
with_orig
:
_plot
(
orig_img
,
"Original
i
mage"
)
_plot
(
orig_img
,
"Original
I
mage"
)
_plot
(
img
,
title
,
**
kwargs
)
...
...
@@ -35,7 +35,7 @@ def plot(img, title="", with_orig=True, **kwargs):
# (see also :func:`~torchvision.transforms.functional.pad`)
# fills image borders with some pixel values.
padded_img
=
T
.
Pad
(
padding
=
30
)(
orig_img
)
plot
(
padded_img
,
"Padded
i
mage"
)
plot
(
padded_img
,
"Padded
I
mage"
)
####################################
# Resize
...
...
@@ -44,7 +44,30 @@ plot(padded_img, "Padded image")
# (see also :func:`~torchvision.transforms.functional.resize`)
# resizes an image.
resized_img
=
T
.
Resize
(
size
=
30
)(
orig_img
)
plot
(
resized_img
,
"Resized image"
)
plot
(
resized_img
,
"Resized Image"
)
####################################
# CenterCrop
# ----------
# The :class:`~torchvision.transforms.CenterCrop` transform
# (see also :func:`~torchvision.transforms.functional.center_crop`)
# crops the given image at the center.
center_cropped_img
=
T
.
CenterCrop
(
size
=
(
100
,
100
))(
orig_img
)
plot
(
center_cropped_img
,
"Center Cropped Image"
)
####################################
# FiveCrop
# --------
# The :class:`~torchvision.transforms.FiveCrop` transform
# (see also :func:`~torchvision.transforms.functional.five_crop`)
# crops the given image into four corners and the central crop.
(
img1
,
img2
,
img3
,
img4
,
img5
)
=
T
.
FiveCrop
(
size
=
(
100
,
100
))(
orig_img
)
plot
(
img1
,
"Top Left Corner Image"
)
plot
(
img2
,
"Top Right Corner Image"
,
with_orig
=
False
)
plot
(
img3
,
"Bottom Left Corner Image"
,
with_orig
=
False
)
plot
(
img4
,
"Bottom Right Corner Image"
,
with_orig
=
False
)
plot
(
img5
,
"Center Image"
,
with_orig
=
False
)
####################################
# ColorJitter
...
...
@@ -52,7 +75,7 @@ plot(resized_img, "Resized image")
# The :class:`~torchvision.transforms.ColorJitter` transform
# randomly changes the brightness, saturation, and other properties of an image.
jitted_img
=
T
.
ColorJitter
(
brightness
=
.
5
,
hue
=
.
3
)(
orig_img
)
plot
(
jitted_img
,
"Jitted
i
mage"
)
plot
(
jitted_img
,
"Jitted
I
mage"
)
####################################
# Grayscale
...
...
@@ -61,7 +84,7 @@ plot(jitted_img, "Jitted image")
# (see also :func:`~torchvision.transforms.functional.to_grayscale`)
# converts an image to grayscale
gray_img
=
T
.
Grayscale
()(
orig_img
)
plot
(
gray_img
,
"Grayscale
i
mage"
,
cmap
=
'gray'
)
plot
(
gray_img
,
"Grayscale
I
mage"
,
cmap
=
'gray'
)
####################################
# RandomPerspective
...
...
@@ -70,7 +93,7 @@ plot(gray_img, "Grayscale image", cmap='gray')
# (see also :func:`~torchvision.transforms.functional.perspective`)
# performs random perspective transform on an image.
perspectived_img
=
T
.
RandomPerspective
(
distortion_scale
=
0.6
,
p
=
1.0
)(
orig_img
)
plot
(
perspectived_img
,
"Perspective transformed
i
mage"
)
plot
(
perspectived_img
,
"Perspective transformed
I
mage"
)
####################################
# RandomRotation
...
...
@@ -79,7 +102,7 @@ plot(perspectived_img, "Perspective transformed image")
# (see also :func:`~torchvision.transforms.functional.rotate`)
# rotates an image with random angle.
rotated_img
=
T
.
RandomRotation
(
degrees
=
(
30
,
70
))(
orig_img
)
plot
(
rotated_img
,
"Rotated
i
mage"
)
plot
(
rotated_img
,
"Rotated
I
mage"
)
####################################
# RandomAffine
...
...
@@ -88,7 +111,7 @@ plot(rotated_img, "Rotated image")
# (see also :func:`~torchvision.transforms.functional.affine`)
# performs random affine transform on an image.
affined_img
=
T
.
RandomAffine
(
degrees
=
(
30
,
70
),
translate
=
(
0.1
,
0.3
),
scale
=
(
0.5
,
0.75
))(
orig_img
)
plot
(
affined_img
,
"Affine transformed
i
mage"
)
plot
(
affined_img
,
"Affine transformed
I
mage"
)
####################################
# RandomCrop
...
...
@@ -96,8 +119,8 @@ plot(affined_img, "Affine transformed image")
# The :class:`~torchvision.transforms.RandomCrop` transform
# (see also :func:`~torchvision.transforms.functional.crop`)
# crops an image at a random location.
crop
=
T
.
RandomCrop
(
size
=
(
128
,
128
))(
orig_img
)
plot
(
crop
,
"Random crop"
)
crop
_img
=
T
.
RandomCrop
(
size
=
(
128
,
128
))(
orig_img
)
plot
(
crop
_img
,
"Random crop
ped Image
"
)
####################################
# RandomResizedCrop
...
...
@@ -106,8 +129,8 @@ plot(crop, "Random crop")
# (see also :func:`~torchvision.transforms.functional.resized_crop`)
# crops an image at a random location, and then resizes the crop to a given
# size.
resized_crop
=
T
.
RandomResizedCrop
(
size
=
(
32
,
32
))(
orig_img
)
plot
(
resized_crop
,
"Random resized crop"
)
resized_crop
_img
=
T
.
RandomResizedCrop
(
size
=
(
32
,
32
))(
orig_img
)
plot
(
resized_crop
_img
,
"Random resized crop
ped Image
"
)
####################################
# RandomHorizontalFlip
...
...
@@ -119,8 +142,8 @@ plot(resized_crop, "Random resized crop")
# .. note::
# Since the transform is applied randomly, the two images below may actually be
# the same.
random_hflip
=
T
.
RandomHorizontalFlip
(
p
=
0.5
)(
orig_img
)
plot
(
random_hflip
,
"Random horizontal flip"
)
random_hflip
_img
=
T
.
RandomHorizontalFlip
(
p
=
0.5
)(
orig_img
)
plot
(
random_hflip
_img
,
"Random horizontal flip
ped Image
"
)
####################################
# RandomVerticalFlip
...
...
@@ -132,8 +155,8 @@ plot(random_hflip, "Random horizontal flip")
# .. note::
# Since the transform is applied randomly, the two images below may actually be
# the same.
random_vflip
=
T
.
RandomVerticalFlip
(
p
=
0.5
)(
orig_img
)
plot
(
random_vflip
,
"Random vertical flip"
)
random_vflip
_img
=
T
.
RandomVerticalFlip
(
p
=
0.5
)(
orig_img
)
plot
(
random_vflip
_img
,
"Random vertical flip
ped Image
"
)
####################################
# RandomApply
...
...
@@ -144,8 +167,8 @@ plot(random_vflip, "Random vertical flip")
# .. note::
# Since the transform is applied randomly, the two images below may actually be
# the same.
random_apply
=
T
.
RandomApply
(
transforms
=
[
T
.
RandomCrop
(
size
=
(
64
,
64
))],
p
=
0.5
)(
orig_img
)
plot
(
random_apply
,
"Random Apply transform"
)
random_apply
_img
=
T
.
RandomApply
(
transforms
=
[
T
.
RandomCrop
(
size
=
(
64
,
64
))],
p
=
0.5
)(
orig_img
)
plot
(
random_apply
_img
,
"Random Apply transform
ed Image
"
)
####################################
# GaussianBlur
...
...
@@ -154,4 +177,4 @@ plot(random_apply, "Random Apply transform")
# (see also :func:`~torchvision.transforms.functional.gaussian_blur`)
# performs gaussianblur transform on an image.
gaus_blur_img
=
T
.
GaussianBlur
(
kernel_size
=
(
5
,
9
),
sigma
=
(
0.4
,
3.0
))(
orig_img
)
plot
(
gaus_blur_img
,
"Gaussian Blur
of i
mage"
)
plot
(
gaus_blur_img
,
"Gaussian Blur
red I
mage"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment