Illustration of transforms¶
This example illustrates some of the various transforms available in the torchvision.transforms.v2 module.
from PIL import Image from pathlib import Path import matplotlib.pyplot as plt import torch from torchvision.transforms import v2 plt.rcParams["savefig.bbox"] = 'tight' # if you change the seed, make sure that the randomly-applied transforms # properly show that the image can be both transformed and *not* transformed! torch.manual_seed(0) # If you're trying to run that on collab, you can download the assets and the # helpers from https://github.com/pytorch/vision/tree/main/gallery/ from helpers import plot orig_img = Image.open(Path('../assets') / 'astronaut.jpg')
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.
Randomly transforms the morphology of objects in images and produces a
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.
Grayscale, the following transforms are random,
which means that the same transform
instance will produce different result each time it transforms a given image.
randomly changes the brightness, contrast, saturation, hue, and other properties of an image.
The following transforms are combinations of multiple transforms, either geometric or photometric, or both.
policies = [v2.AutoAugmentPolicy.CIFAR10, v2.AutoAugmentPolicy.IMAGENET, v2.AutoAugmentPolicy.SVHN] augmenters = [v2.AutoAugment(policy) for policy in policies] imgs = [ [augmenter(orig_img) for _ in range(4)] for augmenter in augmenters ] row_title = [str(policy).split('.')[-1] for policy in policies] plot([[orig_img] + row for row in imgs], row_title=row_title)
RandAugment is an alternate version of AutoAugment.
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.
AugMix transform interpolates between augmented versions of an image.
The following transforms are randomly-applied given a probability
p. That is, given
p = 0.5,
there is a 50% chance to return the original image, and a 50% chance to return the transformed image,
even when called with the same transform instance!
randomly applies a list of transforms, with a given probability.
Total running time of the script: (0 minutes 8.751 seconds)