From 44cb84b7562138b1d03632a33e06da3384a2d1d2 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Fri, 25 Aug 2023 12:37:42 +0100 Subject: [PATCH 1/6] jabjabge --- docs/source/conf.py | 1 + .../others/plot_scripted_tensor_transforms.py | 2 +- gallery/transforms/helpers.py | 8 +- .../plot_transforms_illustrations.py} | 162 ++++++++---------- 4 files changed, 78 insertions(+), 95 deletions(-) rename gallery/{others/plot_transforms.py => transforms/plot_transforms_illustrations.py} (71%) diff --git a/docs/source/conf.py b/docs/source/conf.py index f2836dd5bd5..66138c2d12e 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -84,6 +84,7 @@ def __init__(self, src_dir): transforms_subsection_order = [ "plot_transforms_getting_started.py", + "plot_transforms_illustrations.py", "plot_transforms_e2e.py", "plot_cutmix_mixup.py", "plot_custom_transforms.py", diff --git a/gallery/others/plot_scripted_tensor_transforms.py b/gallery/others/plot_scripted_tensor_transforms.py index 85b332c4ca1..27e8cc166a8 100644 --- a/gallery/others/plot_scripted_tensor_transforms.py +++ b/gallery/others/plot_scripted_tensor_transforms.py @@ -62,7 +62,7 @@ def show(imgs): # -------------------------- # Most transforms natively support tensors on top of PIL images (to visualize # the effect of the transforms, you may refer to see -# :ref:`sphx_glr_auto_examples_others_plot_transforms.py`). +# :ref:`sphx_glr_auto_examples_transforms_plot_transforms_illustrations.py`). # Using tensor images, we can run the transforms on GPUs if cuda is available! import torch.nn as nn diff --git a/gallery/transforms/helpers.py b/gallery/transforms/helpers.py index 3c92df4322e..957d9bcb709 100644 --- a/gallery/transforms/helpers.py +++ b/gallery/transforms/helpers.py @@ -5,7 +5,7 @@ from torchvision.transforms.v2 import functional as F -def plot(imgs): +def plot(imgs, row_title=None, **imshow_kwargs): if not isinstance(imgs[0], list): # Make a 2d grid even if there's just 1 row imgs = [imgs] @@ -40,7 +40,11 @@ def plot(imgs): img = draw_segmentation_masks(img, masks.to(torch.bool), colors=["green"] * masks.shape[0], alpha=.65) ax = axs[row_idx, col_idx] - ax.imshow(img.permute(1, 2, 0).numpy()) + ax.imshow(img.permute(1, 2, 0).numpy(), **imshow_kwargs) ax.set(xticklabels=[], yticklabels=[], xticks=[], yticks=[]) + if row_title is not None: + for row_idx in range(num_rows): + axs[row_idx, 0].set(ylabel=row_title[row_idx]) + plt.tight_layout() diff --git a/gallery/others/plot_transforms.py b/gallery/transforms/plot_transforms_illustrations.py similarity index 71% rename from gallery/others/plot_transforms.py rename to gallery/transforms/plot_transforms_illustrations.py index 9702bc9c3ba..a349ad1430b 100644 --- a/gallery/others/plot_transforms.py +++ b/gallery/transforms/plot_transforms_illustrations.py @@ -4,8 +4,8 @@ ========================== .. note:: - Try on `collab `_ - or :ref:`go to the end ` to download the full example code. + Try on `collab `_ + or :ref:`go to the end ` to download the full example code. This example illustrates the various transforms available in :ref:`the torchvision.transforms module `. @@ -13,47 +13,25 @@ # sphinx_gallery_thumbnail_path = "../../gallery/assets/transforms_thumbnail.png" +# %% from PIL import Image from pathlib import Path import matplotlib.pyplot as plt -import numpy as np import torch -import torchvision.transforms as T - +from torchvision.transforms import v2 plt.rcParams["savefig.bbox"] = 'tight' + +# 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') + # 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) - -def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): - if not isinstance(imgs[0], list): - # Make a 2d grid even if there's just 1 row - imgs = [imgs] - - num_rows = len(imgs) - num_cols = len(imgs[0]) + with_orig - fig, axs = plt.subplots(nrows=num_rows, ncols=num_cols, squeeze=False) - for row_idx, row in enumerate(imgs): - row = [orig_img] + row if with_orig else row - for col_idx, img in enumerate(row): - ax = axs[row_idx, col_idx] - ax.imshow(np.asarray(img), **imshow_kwargs) - ax.set(xticklabels=[], yticklabels=[], xticks=[], yticks=[]) - - if with_orig: - axs[0, 0].set(title='Original image') - axs[0, 0].title.set_size(8) - if row_title is not None: - for row_idx in range(num_rows): - axs[row_idx, 0].set(ylabel=row_title[row_idx]) - - plt.tight_layout() - - # %% # Geometric Transforms # -------------------- @@ -66,8 +44,8 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # The :class:`~torchvision.transforms.Pad` transform # (see also :func:`~torchvision.transforms.functional.pad`) # pads all image borders with some pixel values. -padded_imgs = [T.Pad(padding=padding)(orig_img) for padding in (3, 10, 30, 50)] -plot(padded_imgs) +padded_imgs = [v2.Pad(padding=padding)(orig_img) for padding in (3, 10, 30, 50)] +plot([orig_img] + padded_imgs) # %% # Resize @@ -75,8 +53,8 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # The :class:`~torchvision.transforms.Resize` transform # (see also :func:`~torchvision.transforms.functional.resize`) # resizes an image. -resized_imgs = [T.Resize(size=size)(orig_img) for size in (30, 50, 100, orig_img.size)] -plot(resized_imgs) +resized_imgs = [v2.Resize(size=size)(orig_img) for size in (30, 50, 100, orig_img.size)] +plot([orig_img] + resized_imgs) # %% # CenterCrop @@ -84,8 +62,8 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # The :class:`~torchvision.transforms.CenterCrop` transform # (see also :func:`~torchvision.transforms.functional.center_crop`) # crops the given image at the center. -center_crops = [T.CenterCrop(size=size)(orig_img) for size in (30, 50, 100, orig_img.size)] -plot(center_crops) +center_crops = [v2.CenterCrop(size=size)(orig_img) for size in (30, 50, 100, orig_img.size)] +plot([orig_img] + center_crops) # %% # FiveCrop @@ -93,8 +71,8 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # 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]) +(top_left, top_right, bottom_left, bottom_right, center) = v2.FiveCrop(size=(100, 100))(orig_img) +plot([orig_img] + [top_left, top_right, bottom_left, bottom_right, center]) # %% # RandomPerspective @@ -102,9 +80,9 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # The :class:`~torchvision.transforms.RandomPerspective` transform # (see also :func:`~torchvision.transforms.functional.perspective`) # performs random perspective transform on an image. -perspective_transformer = T.RandomPerspective(distortion_scale=0.6, p=1.0) +perspective_transformer = v2.RandomPerspective(distortion_scale=0.6, p=1.0) perspective_imgs = [perspective_transformer(orig_img) for _ in range(4)] -plot(perspective_imgs) +plot([orig_img] + perspective_imgs) # %% # RandomRotation @@ -112,9 +90,9 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # The :class:`~torchvision.transforms.RandomRotation` transform # (see also :func:`~torchvision.transforms.functional.rotate`) # rotates an image with random angle. -rotater = T.RandomRotation(degrees=(0, 180)) +rotater = v2.RandomRotation(degrees=(0, 180)) rotated_imgs = [rotater(orig_img) for _ in range(4)] -plot(rotated_imgs) +plot([orig_img] + rotated_imgs) # %% # RandomAffine @@ -122,20 +100,20 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # The :class:`~torchvision.transforms.RandomAffine` transform # (see also :func:`~torchvision.transforms.functional.affine`) # performs random affine transform on an image. -affine_transfomer = T.RandomAffine(degrees=(30, 70), translate=(0.1, 0.3), scale=(0.5, 0.75)) +affine_transfomer = v2.RandomAffine(degrees=(30, 70), translate=(0.1, 0.3), scale=(0.5, 0.75)) affine_imgs = [affine_transfomer(orig_img) for _ in range(4)] -plot(affine_imgs) +plot([orig_img] + affine_imgs) # %% -# ElasticTransform +# Elasticv2ransform # ~~~~~~~~~~~~~~~~ -# The :class:`~torchvision.transforms.ElasticTransform` transform +# The :class:`~torchvision.transforms.Elasticv2ransform` transform # (see also :func:`~torchvision.transforms.functional.elastic_transform`) # Randomly transforms the morphology of objects in images and produces a # see-through-water-like effect. -elastic_transformer = T.ElasticTransform(alpha=250.0) +elastic_transformer = v2.ElasticTransform(alpha=250.0) transformed_imgs = [elastic_transformer(orig_img) for _ in range(2)] -plot(transformed_imgs) +plot([orig_img] + transformed_imgs) # %% # RandomCrop @@ -143,9 +121,9 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # The :class:`~torchvision.transforms.RandomCrop` transform # (see also :func:`~torchvision.transforms.functional.crop`) # crops an image at a random location. -cropper = T.RandomCrop(size=(128, 128)) +cropper = v2.RandomCrop(size=(128, 128)) crops = [cropper(orig_img) for _ in range(4)] -plot(crops) +plot([orig_img] + crops) # %% # RandomResizedCrop @@ -154,12 +132,12 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # (see also :func:`~torchvision.transforms.functional.resized_crop`) # crops an image at a random location, and then resizes the crop to a given # size. -resize_cropper = T.RandomResizedCrop(size=(32, 32)) +resize_cropper = v2.RandomResizedCrop(size=(32, 32)) resized_crops = [resize_cropper(orig_img) for _ in range(4)] -plot(resized_crops) +plot([orig_img] + resized_crops) # %% -# Photometric Transforms +# Photometric v2ransforms # ---------------------- # Photometric image transformation refers to the process of modifying the photometric properties of an image, # such as its brightness, contrast, color, or tone. @@ -175,17 +153,17 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # 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') +gray_img = v2.Grayscale()(orig_img) +plot([orig_img] + [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) +jitter = v2.ColorJitter(brightness=.5, hue=.3) jitted_imgs = [jitter(orig_img) for _ in range(4)] -plot(jitted_imgs) +plot([orig_img] + jitted_imgs) # %% # GaussianBlur @@ -193,9 +171,9 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # 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)) +blurrer = v2.GaussianBlur(kernel_size=(5, 9), sigma=(0.1, 5.)) blurred_imgs = [blurrer(orig_img) for _ in range(4)] -plot(blurred_imgs) +plot([orig_img] + blurred_imgs) # %% # RandomInvert @@ -203,9 +181,9 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # The :class:`~torchvision.transforms.RandomInvert` transform # (see also :func:`~torchvision.transforms.functional.invert`) # randomly inverts the colors of the given image. -inverter = T.RandomInvert() +inverter = v2.RandomInvert() invertered_imgs = [inverter(orig_img) for _ in range(4)] -plot(invertered_imgs) +plot([orig_img] + invertered_imgs) # %% # RandomPosterize @@ -214,9 +192,9 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # (see also :func:`~torchvision.transforms.functional.posterize`) # randomly posterizes the image by reducing the number of bits # of each color channel. -posterizer = T.RandomPosterize(bits=2) +posterizer = v2.RandomPosterize(bits=2) posterized_imgs = [posterizer(orig_img) for _ in range(4)] -plot(posterized_imgs) +plot([orig_img] + posterized_imgs) # %% # RandomSolarize @@ -225,9 +203,9 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # (see also :func:`~torchvision.transforms.functional.solarize`) # randomly solarizes the image by inverting all pixel values above # the threshold. -solarizer = T.RandomSolarize(threshold=192.0) +solarizer = v2.RandomSolarize(threshold=192.0) solarized_imgs = [solarizer(orig_img) for _ in range(4)] -plot(solarized_imgs) +plot([orig_img] + solarized_imgs) # %% # RandomAdjustSharpness @@ -235,9 +213,9 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # The :class:`~torchvision.transforms.RandomAdjustSharpness` transform # (see also :func:`~torchvision.transforms.functional.adjust_sharpness`) # randomly adjusts the sharpness of the given image. -sharpness_adjuster = T.RandomAdjustSharpness(sharpness_factor=2) +sharpness_adjuster = v2.RandomAdjustSharpness(sharpness_factor=2) sharpened_imgs = [sharpness_adjuster(orig_img) for _ in range(4)] -plot(sharpened_imgs) +plot([orig_img] + sharpened_imgs) # %% # RandomAutocontrast @@ -245,9 +223,9 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # The :class:`~torchvision.transforms.RandomAutocontrast` transform # (see also :func:`~torchvision.transforms.functional.autocontrast`) # randomly applies autocontrast to the given image. -autocontraster = T.RandomAutocontrast() +autocontraster = v2.RandomAutocontrast() autocontrasted_imgs = [autocontraster(orig_img) for _ in range(4)] -plot(autocontrasted_imgs) +plot([orig_img] + autocontrasted_imgs) # %% # RandomEqualize @@ -255,12 +233,12 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # The :class:`~torchvision.transforms.RandomEqualize` transform # (see also :func:`~torchvision.transforms.functional.equalize`) # randomly equalizes the histogram of the given image. -equalizer = T.RandomEqualize() +equalizer = v2.RandomEqualize() equalized_imgs = [equalizer(orig_img) for _ in range(4)] -plot(equalized_imgs) +plot([orig_img] + equalized_imgs) # %% -# Augmentation Transforms +# Augmentation v2ransforms # ----------------------- # The following transforms are combinations of multiple transforms, # either geometric or photometric, or both. @@ -270,46 +248,46 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # The :class:`~torchvision.transforms.AutoAugment` transform # automatically augments data based on a given auto-augmentation policy. # See :class:`~torchvision.transforms.AutoAugmentPolicy` for the available policies. -policies = [T.AutoAugmentPolicy.CIFAR10, T.AutoAugmentPolicy.IMAGENET, T.AutoAugmentPolicy.SVHN] -augmenters = [T.AutoAugment(policy) for policy in policies] +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(imgs, row_title=row_title) +plot([[orig_img] + row for row in imgs], row_title=row_title) # %% # RandAugment # ~~~~~~~~~~~ # The :class:`~torchvision.transforms.RandAugment` is an alternate version of AutoAugment. -augmenter = T.RandAugment() +augmenter = v2.RandAugment() imgs = [augmenter(orig_img) for _ in range(4)] -plot(imgs) +plot([orig_img] + imgs) # %% -# TrivialAugmentWide +# v2rivialAugmentWide # ~~~~~~~~~~~~~~~~~~ -# The :class:`~torchvision.transforms.TrivialAugmentWide` is an alternate implementation of AutoAugment. +# The :class:`~torchvision.transforms.v2rivialAugmentWide` 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() +augmenter = v2.TrivialAugmentWide() imgs = [augmenter(orig_img) for _ in range(4)] -plot(imgs) +plot([orig_img] + imgs) # %% # AugMix # ~~~~~~ # The :class:`~torchvision.transforms.AugMix` transform interpolates between augmented versions of an image. -augmenter = T.AugMix() +augmenter = v2.AugMix() imgs = [augmenter(orig_img) for _ in range(4)] -plot(imgs) +plot([orig_img] + imgs) # %% -# Randomly-applied Transforms +# Randomly-applied v2ransforms # --------------------------- # -# The following transforms are randomly-applied given a probability ``p``. That is, given ``p = 0.5``, +# The following transforms are randomly-applied given a probability ``p``. v2hat 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! # @@ -318,9 +296,9 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # The :class:`~torchvision.transforms.RandomHorizontalFlip` transform # (see also :func:`~torchvision.transforms.functional.hflip`) # performs horizontal flip of an image, with a given probability. -hflipper = T.RandomHorizontalFlip(p=0.5) +hflipper = v2.RandomHorizontalFlip(p=0.5) transformed_imgs = [hflipper(orig_img) for _ in range(4)] -plot(transformed_imgs) +plot([orig_img] + transformed_imgs) # %% # RandomVerticalFlip @@ -328,15 +306,15 @@ def plot(imgs, with_orig=True, row_title=None, **imshow_kwargs): # The :class:`~torchvision.transforms.RandomVerticalFlip` transform # (see also :func:`~torchvision.transforms.functional.vflip`) # performs vertical flip of an image, with a given probability. -vflipper = T.RandomVerticalFlip(p=0.5) +vflipper = v2.RandomVerticalFlip(p=0.5) transformed_imgs = [vflipper(orig_img) for _ in range(4)] -plot(transformed_imgs) +plot([orig_img] + transformed_imgs) # %% # RandomApply # ~~~~~~~~~~~ # The :class:`~torchvision.transforms.RandomApply` transform # randomly applies a list of transforms, with a given probability. -applier = T.RandomApply(transforms=[T.RandomCrop(size=(64, 64))], p=0.5) +applier = v2.RandomApply(transforms=[v2.RandomCrop(size=(64, 64))], p=0.5) transformed_imgs = [applier(orig_img) for _ in range(4)] -plot(transformed_imgs) +plot([orig_img] + transformed_imgs) From 1e576f07b3d7809822db6cac5fe41fa125bb538e Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Fri, 25 Aug 2023 14:13:01 +0100 Subject: [PATCH 2/6] Fixes --- .../transforms/plot_transforms_illustrations.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/gallery/transforms/plot_transforms_illustrations.py b/gallery/transforms/plot_transforms_illustrations.py index a349ad1430b..4d66775f38f 100644 --- a/gallery/transforms/plot_transforms_illustrations.py +++ b/gallery/transforms/plot_transforms_illustrations.py @@ -105,9 +105,9 @@ plot([orig_img] + affine_imgs) # %% -# Elasticv2ransform +# ElasticTransform # ~~~~~~~~~~~~~~~~ -# The :class:`~torchvision.transforms.Elasticv2ransform` transform +# The :class:`~torchvision.transforms.ElasticTransform` transform # (see also :func:`~torchvision.transforms.functional.elastic_transform`) # Randomly transforms the morphology of objects in images and produces a # see-through-water-like effect. @@ -137,7 +137,7 @@ plot([orig_img] + resized_crops) # %% -# Photometric v2ransforms +# 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. @@ -238,7 +238,7 @@ plot([orig_img] + equalized_imgs) # %% -# Augmentation v2ransforms +# Augmentation Transforms # ----------------------- # The following transforms are combinations of multiple transforms, # either geometric or photometric, or both. @@ -266,9 +266,9 @@ plot([orig_img] + imgs) # %% -# v2rivialAugmentWide +# TrivialAugmentWide # ~~~~~~~~~~~~~~~~~~ -# The :class:`~torchvision.transforms.v2rivialAugmentWide` is an alternate implementation of AutoAugment. +# 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 = v2.TrivialAugmentWide() @@ -284,10 +284,10 @@ plot([orig_img] + imgs) # %% -# Randomly-applied v2ransforms +# Randomly-applied Transforms # --------------------------- # -# The following transforms are randomly-applied given a probability ``p``. v2hat is, given ``p = 0.5``, +# 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! # From daaab1b3c82ce695f8d1d5d0646743b84753cc95 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Fri, 25 Aug 2023 14:30:25 +0100 Subject: [PATCH 3/6] empty From 6b2844b83ae89432957d7a7569c03d26fcf0bc06 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Fri, 25 Aug 2023 14:44:01 +0100 Subject: [PATCH 4/6] ADdress comments --- .../transforms/plot_transforms_illustrations.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/gallery/transforms/plot_transforms_illustrations.py b/gallery/transforms/plot_transforms_illustrations.py index 4d66775f38f..926366dd167 100644 --- a/gallery/transforms/plot_transforms_illustrations.py +++ b/gallery/transforms/plot_transforms_illustrations.py @@ -10,10 +10,10 @@ This example illustrates the various transforms available in :ref:`the torchvision.transforms module `. """ +# %% # sphinx_gallery_thumbnail_path = "../../gallery/assets/transforms_thumbnail.png" -# %% from PIL import Image from pathlib import Path import matplotlib.pyplot as plt @@ -23,15 +23,15 @@ 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') -# 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) - # %% # Geometric Transforms # -------------------- @@ -154,7 +154,7 @@ # (see also :func:`~torchvision.transforms.functional.to_grayscale`) # converts an image to grayscale gray_img = v2.Grayscale()(orig_img) -plot([orig_img] + [gray_img], cmap='gray') +plot([orig_img, gray_img], cmap='gray') # %% # ColorJitter @@ -162,8 +162,8 @@ # The :class:`~torchvision.transforms.ColorJitter` transform # randomly changes the brightness, contrast, saturation, hue, and other properties of an image. jitter = v2.ColorJitter(brightness=.5, hue=.3) -jitted_imgs = [jitter(orig_img) for _ in range(4)] -plot([orig_img] + jitted_imgs) +jittered_imgs = [jitter(orig_img) for _ in range(4)] +plot([orig_img] + jittered_imgs) # %% # GaussianBlur From 3095fa9206669bef567b54660680b6dd4046b2da Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Fri, 25 Aug 2023 14:48:47 +0100 Subject: [PATCH 5/6] empty From 2fcdcca5798f80a7bc0e6e252e1ce83f6131afd2 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Fri, 25 Aug 2023 14:57:26 +0100 Subject: [PATCH 6/6] nit --- gallery/transforms/plot_transforms_illustrations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gallery/transforms/plot_transforms_illustrations.py b/gallery/transforms/plot_transforms_illustrations.py index 926366dd167..95ab455d0fd 100644 --- a/gallery/transforms/plot_transforms_illustrations.py +++ b/gallery/transforms/plot_transforms_illustrations.py @@ -7,8 +7,8 @@ Try on `collab `_ or :ref:`go to the end ` to download the full example code. -This example illustrates the various transforms available in :ref:`the -torchvision.transforms module `. +This example illustrates some of the various transforms available in :ref:`the +torchvision.transforms.v2 module `. """ # %%