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
e1ad6adc
Unverified
Commit
e1ad6adc
authored
May 24, 2023
by
Minh-Long Luu (刘明龙)
Committed by
GitHub
May 24, 2023
Browse files
Improve "Illustration of Transform" docs (#7621)
Co-authored-by:
Nicolas Hug
<
contact@nicolas-hug.com
>
parent
e5bf7cf8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
64 additions
and
46 deletions
+64
-46
gallery/plot_transforms.py
gallery/plot_transforms.py
+64
-46
No files found.
gallery/plot_transforms.py
View file @
e1ad6adc
...
...
@@ -49,19 +49,24 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs):
plt
.
tight_layout
()
####################################
# Geometric Transforms
# --------------------
# Geometric image transformation refers to the process of altering the geometric properties of an image,
# such as its shape, size, orientation, or position.
# It involves applying mathematical operations to the image pixels or coordinates to achieve the desired transformation.
#
# Pad
#
---
#
~~~
# The :class:`~torchvision.transforms.Pad` transform
# (see also :func:`~torchvision.transforms.functional.pad`)
#
fi
ll
s
image borders with some pixel values.
#
pads a
ll image borders with some pixel values.
padded_imgs
=
[
T
.
Pad
(
padding
=
padding
)(
orig_img
)
for
padding
in
(
3
,
10
,
30
,
50
)]
plot
(
padded_imgs
)
####################################
# Resize
#
------
#
~~~~~~
# The :class:`~torchvision.transforms.Resize` transform
# (see also :func:`~torchvision.transforms.functional.resize`)
# resizes an image.
...
...
@@ -70,7 +75,7 @@ plot(resized_imgs)
####################################
# CenterCrop
#
----------
#
~~~~~~~~~~
# The :class:`~torchvision.transforms.CenterCrop` transform
# (see also :func:`~torchvision.transforms.functional.center_crop`)
# crops the given image at the center.
...
...
@@ -79,46 +84,13 @@ plot(center_crops)
####################################
# 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.
(
top_left
,
top_right
,
bottom_left
,
bottom_right
,
center
)
=
T
.
FiveCrop
(
size
=
(
100
,
100
))(
orig_img
)
plot
([
top_left
,
top_right
,
bottom_left
,
bottom_right
,
center
])
####################################
# Grayscale
# ---------
# The :class:`~torchvision.transforms.Grayscale` transform
# (see also :func:`~torchvision.transforms.functional.to_grayscale`)
# converts an image to grayscale
gray_img
=
T
.
Grayscale
()(
orig_img
)
plot
([
gray_img
],
cmap
=
'gray'
)
####################################
# Random transforms
# -----------------
# The following transforms are random, which means that the same transfomer
# instance will produce different result each time it transforms a given image.
#
# ColorJitter
# ~~~~~~~~~~~
# The :class:`~torchvision.transforms.ColorJitter` transform
# randomly changes the brightness, saturation, and other properties of an image.
jitter
=
T
.
ColorJitter
(
brightness
=
.
5
,
hue
=
.
3
)
jitted_imgs
=
[
jitter
(
orig_img
)
for
_
in
range
(
4
)]
plot
(
jitted_imgs
)
####################################
# GaussianBlur
# ~~~~~~~~~~~~
# The :class:`~torchvision.transforms.GaussianBlur` transform
# (see also :func:`~torchvision.transforms.functional.gaussian_blur`)
# performs gaussian blur transform on an image.
blurrer
=
T
.
GaussianBlur
(
kernel_size
=
(
5
,
9
),
sigma
=
(
0.1
,
5
))
blurred_imgs
=
[
blurrer
(
orig_img
)
for
_
in
range
(
4
)]
plot
(
blurred_imgs
)
####################################
# RandomPerspective
# ~~~~~~~~~~~~~~~~~
...
...
@@ -181,6 +153,45 @@ resize_cropper = T.RandomResizedCrop(size=(32, 32))
resized_crops
=
[
resize_cropper
(
orig_img
)
for
_
in
range
(
4
)]
plot
(
resized_crops
)
####################################
# Photometric Transforms
# ----------------------
# Photometric image transformation refers to the process of modifying the photometric properties of an image,
# such as its brightness, contrast, color, or tone.
# These transformations are applied to change the visual appearance of an image
# while preserving its geometric structure.
#
# Except :class:`~torchvision.transforms.Grayscale`, the following transforms are random,
# which means that the same transform
# instance will produce different result each time it transforms a given image.
#
# Grayscale
# ~~~~~~~~~
# The :class:`~torchvision.transforms.Grayscale` transform
# (see also :func:`~torchvision.transforms.functional.to_grayscale`)
# converts an image to grayscale
gray_img
=
T
.
Grayscale
()(
orig_img
)
plot
([
gray_img
],
cmap
=
'gray'
)
####################################
# ColorJitter
# ~~~~~~~~~~~
# The :class:`~torchvision.transforms.ColorJitter` transform
# randomly changes the brightness, contrast, saturation, hue, and other properties of an image.
jitter
=
T
.
ColorJitter
(
brightness
=
.
5
,
hue
=
.
3
)
jitted_imgs
=
[
jitter
(
orig_img
)
for
_
in
range
(
4
)]
plot
(
jitted_imgs
)
####################################
# GaussianBlur
# ~~~~~~~~~~~~
# The :class:`~torchvision.transforms.GaussianBlur` transform
# (see also :func:`~torchvision.transforms.functional.gaussian_blur`)
# performs gaussian blur transform on an image.
blurrer
=
T
.
GaussianBlur
(
kernel_size
=
(
5
,
9
),
sigma
=
(
0.1
,
5
))
blurred_imgs
=
[
blurrer
(
orig_img
)
for
_
in
range
(
4
)]
plot
(
blurred_imgs
)
####################################
# RandomInvert
# ~~~~~~~~~~~~
...
...
@@ -244,6 +255,11 @@ equalized_imgs = [equalizer(orig_img) for _ in range(4)]
plot
(
equalized_imgs
)
####################################
# Augmentation Transforms
# -----------------------
# The following transforms are combinations of multiple transforms,
# either geometric or photometric, or both.
#
# AutoAugment
# ~~~~~~~~~~~
# The :class:`~torchvision.transforms.AutoAugment` transform
...
...
@@ -261,7 +277,7 @@ plot(imgs, row_title=row_title)
####################################
# RandAugment
# ~~~~~~~~~~~
# The :class:`~torchvision.transforms.RandAugment`
transform automatically augments the data
.
# The :class:`~torchvision.transforms.RandAugment`
is an alternate version of AutoAugment
.
augmenter
=
T
.
RandAugment
()
imgs
=
[
augmenter
(
orig_img
)
for
_
in
range
(
4
)]
plot
(
imgs
)
...
...
@@ -269,7 +285,9 @@ plot(imgs)
####################################
# TrivialAugmentWide
# ~~~~~~~~~~~~~~~~~~
# The :class:`~torchvision.transforms.TrivialAugmentWide` transform automatically augments the data.
# The :class:`~torchvision.transforms.TrivialAugmentWide` is an alternate implementation of AutoAugment.
# However, instead of transforming an image multiple times, it transforms an image only once
# using a random transform from a given list with a random strength number.
augmenter
=
T
.
TrivialAugmentWide
()
imgs
=
[
augmenter
(
orig_img
)
for
_
in
range
(
4
)]
plot
(
imgs
)
...
...
@@ -277,18 +295,18 @@ plot(imgs)
####################################
# AugMix
# ~~~~~~
# The :class:`~torchvision.transforms.AugMix` transform
automatically augments the data
.
# The :class:`~torchvision.transforms.AugMix` transform
interpolates between augmented versions of an image
.
augmenter
=
T
.
AugMix
()
imgs
=
[
augmenter
(
orig_img
)
for
_
in
range
(
4
)]
plot
(
imgs
)
####################################
# Randomly-applied
t
ransforms
# Randomly-applied
T
ransforms
# ---------------------------
#
#
Some
transforms are randomly-applied given a probability ``p``. That is,
the
# t
ransformed image may actually be the same as the original one, eve
n
w
he
n
# called with the same transform
er
instance!
#
The following
transforms are randomly-applied given a probability ``p``. That is,
given ``p = 0.5``,
# t
here is a 50% chance to return the original image, and a 50% chance to retur
n
t
he
transformed image,
#
even when
called with the same transform instance!
#
# RandomHorizontalFlip
# ~~~~~~~~~~~~~~~~~~~~
...
...
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