快捷方式

transforms 的示例

注意

Colab 上试用,或 转至末尾 下载完整的示例代码。

此示例说明了 torchvision.transforms.v2 模块 中提供的一些各种 transforms。

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 Colab, 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')

几何变换

几何图像变换是指改变图像的几何属性的过程,例如其形状、大小、方向或位置。它涉及对图像像素或坐标应用数学运算以实现所需的变换。

Pad

Pad 变换(另请参阅 pad())用某些像素值填充所有图像边框。

padded_imgs = [v2.Pad(padding=padding)(orig_img) for padding in (3, 10, 30, 50)]
plot([orig_img] + padded_imgs)
plot transforms illustrations

调整大小

Resize 变换(另请参阅 resize())调整图像大小。

resized_imgs = [v2.Resize(size=size)(orig_img) for size in (30, 50, 100, orig_img.size)]
plot([orig_img] + resized_imgs)
plot transforms illustrations

CenterCrop

CenterCrop 变换(另请参阅 center_crop())在图像中心裁剪给定图像。

center_crops = [v2.CenterCrop(size=size)(orig_img) for size in (30, 50, 100, orig_img.size)]
plot([orig_img] + center_crops)
plot transforms illustrations

FiveCrop

FiveCrop 变换(另请参阅 five_crop())将给定图像裁剪成四个角和中心裁剪。

plot transforms illustrations

RandomPerspective

RandomPerspective 变换(另请参阅 perspective())对图像执行随机透视变换。

perspective_transformer = v2.RandomPerspective(distortion_scale=0.6, p=1.0)
perspective_imgs = [perspective_transformer(orig_img) for _ in range(4)]
plot([orig_img] + perspective_imgs)
plot transforms illustrations

RandomRotation

RandomRotation 变换(另请参阅 rotate())以随机角度旋转图像。

rotater = v2.RandomRotation(degrees=(0, 180))
rotated_imgs = [rotater(orig_img) for _ in range(4)]
plot([orig_img] + rotated_imgs)
plot transforms illustrations

RandomAffine

RandomAffine 变换(另请参阅 affine())对图像执行随机仿射变换。

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([orig_img] + affine_imgs)
plot transforms illustrations

ElasticTransform

ElasticTransform 变换(另请参阅 elastic_transform())随机变换图像中对象的形态,产生类似水下观察的效果。

elastic_transformer = v2.ElasticTransform(alpha=250.0)
transformed_imgs = [elastic_transformer(orig_img) for _ in range(2)]
plot([orig_img] + transformed_imgs)
plot transforms illustrations

RandomCrop

RandomCrop 变换(另请参阅 crop())在随机位置裁剪图像。

cropper = v2.RandomCrop(size=(128, 128))
crops = [cropper(orig_img) for _ in range(4)]
plot([orig_img] + crops)
plot transforms illustrations

RandomResizedCrop

RandomResizedCrop 变换(另请参阅 resized_crop())在随机位置裁剪图像,然后将裁剪后的图像调整到指定大小。

resize_cropper = v2.RandomResizedCrop(size=(32, 32))
resized_crops = [resize_cropper(orig_img) for _ in range(4)]
plot([orig_img] + resized_crops)
plot transforms illustrations

光度变换

光度图像变换是指修改图像的光度属性的过程,例如其亮度、对比度、颜色或色调。应用这些变换是为了在保留图像几何结构的同时改变图像的视觉外观。

除了 Grayscale,以下变换是随机的,这意味着同一个变换实例每次转换给定图像时都会产生不同的结果。

Grayscale

Grayscale 变换(另请参阅 to_grayscale())将图像转换为灰度。

gray_img = v2.Grayscale()(orig_img)
plot([orig_img, gray_img], cmap='gray')
plot transforms illustrations

ColorJitter

ColorJitter 变换随机改变图像的亮度、对比度、饱和度、色相和其他属性。

jitter = v2.ColorJitter(brightness=.5, hue=.3)
jittered_imgs = [jitter(orig_img) for _ in range(4)]
plot([orig_img] + jittered_imgs)
plot transforms illustrations

GaussianBlur

GaussianBlur 变换(另请参阅 gaussian_blur())对图像执行高斯模糊变换。

blurrer = v2.GaussianBlur(kernel_size=(5, 9), sigma=(0.1, 5.))
blurred_imgs = [blurrer(orig_img) for _ in range(4)]
plot([orig_img] + blurred_imgs)
plot transforms illustrations

RandomInvert

RandomInvert 变换(另请参阅 invert())随机反转给定图像的颜色。

inverter = v2.RandomInvert()
invertered_imgs = [inverter(orig_img) for _ in range(4)]
plot([orig_img] + invertered_imgs)
plot transforms illustrations

RandomPosterize

RandomPosterize 变换(另请参阅 posterize())通过减少每个颜色通道的位数来随机将图像量化。

posterizer = v2.RandomPosterize(bits=2)
posterized_imgs = [posterizer(orig_img) for _ in range(4)]
plot([orig_img] + posterized_imgs)
plot transforms illustrations

RandomSolarize

RandomSolarize 变换(另请参阅 solarize())通过反转高于阈值的像素值来随机对图像进行太阳化处理。

solarizer = v2.RandomSolarize(threshold=192.0)
solarized_imgs = [solarizer(orig_img) for _ in range(4)]
plot([orig_img] + solarized_imgs)
plot transforms illustrations

RandomAdjustSharpness

RandomAdjustSharpness 变换(另请参阅 adjust_sharpness())随机调整给定图像的锐度。

sharpness_adjuster = v2.RandomAdjustSharpness(sharpness_factor=2)
sharpened_imgs = [sharpness_adjuster(orig_img) for _ in range(4)]
plot([orig_img] + sharpened_imgs)
plot transforms illustrations

RandomAutocontrast

RandomAutocontrast 变换(另请参阅 autocontrast())随机对给定图像应用自动对比度。

autocontraster = v2.RandomAutocontrast()
autocontrasted_imgs = [autocontraster(orig_img) for _ in range(4)]
plot([orig_img] + autocontrasted_imgs)
plot transforms illustrations

RandomEqualize

RandomEqualize 变换(另请参阅 equalize())随机均衡给定图像的直方图。

equalizer = v2.RandomEqualize()
equalized_imgs = [equalizer(orig_img) for _ in range(4)]
plot([orig_img] + equalized_imgs)
plot transforms illustrations

JPEG

JPEG 变换(另请参阅 jpeg())以随机压缩程度对给定图像应用 JPEG 压缩。

jpeg = v2.JPEG((5, 50))
jpeg_imgs = [jpeg(orig_img) for _ in range(4)]
plot([orig_img] + jpeg_imgs)
plot transforms illustrations

增强变换

以下变换是多个变换的组合,可以是几何变换、光度变换,或者两者兼有。

AutoAugment

AutoAugment 变换根据给定的自动增强策略自动增强数据。有关可用策略,请参阅 AutoAugmentPolicy

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)
plot transforms illustrations

RandAugment

RandAugment 是 AutoAugment 的一个替代版本。

augmenter = v2.RandAugment()
imgs = [augmenter(orig_img) for _ in range(4)]
plot([orig_img] + imgs)
plot transforms illustrations

TrivialAugmentWide

TrivialAugmentWide 是 AutoAugment 的一个替代实现。但是,它不是多次变换图像,而是使用给定列表中的随机变换和随机强度数字,仅对图像进行一次变换。

augmenter = v2.TrivialAugmentWide()
imgs = [augmenter(orig_img) for _ in range(4)]
plot([orig_img] + imgs)
plot transforms illustrations

AugMix

AugMix 变换在增强的图像版本之间进行插值。

augmenter = v2.AugMix()
imgs = [augmenter(orig_img) for _ in range(4)]
plot([orig_img] + imgs)
plot transforms illustrations

随机应用变换

以下变换是给定概率 p 时随机应用的。也就是说,给定 p = 0.5,即使使用相同的变换实例调用,也有 50% 的机会返回原始图像,50% 的机会返回变换后的图像!

RandomHorizontalFlip

RandomHorizontalFlip 变换(另请参阅 hflip())以给定概率执行图像的水平翻转。

hflipper = v2.RandomHorizontalFlip(p=0.5)
transformed_imgs = [hflipper(orig_img) for _ in range(4)]
plot([orig_img] + transformed_imgs)
plot transforms illustrations

RandomVerticalFlip

RandomVerticalFlip 变换(另请参阅 vflip())以给定概率执行图像的垂直翻转。

vflipper = v2.RandomVerticalFlip(p=0.5)
transformed_imgs = [vflipper(orig_img) for _ in range(4)]
plot([orig_img] + transformed_imgs)
plot transforms illustrations

RandomApply

RandomApply 变换以给定概率随机应用一组变换。

applier = v2.RandomApply(transforms=[v2.RandomCrop(size=(64, 64))], p=0.5)
transformed_imgs = [applier(orig_img) for _ in range(4)]
plot([orig_img] + transformed_imgs)
plot transforms illustrations

脚本总运行时间: (0 分钟 6.870 秒)

由 Sphinx-Gallery 生成的画廊

文档

访问全面的 PyTorch 开发者文档

查看文档

教程

为初学者和高级开发者提供深入的教程

查看教程

资源

查找开发资源并让您的问题得到解答

查看资源