快捷方式

變換示例

注意

Colab 上嘗試或轉到末尾下載完整的示例程式碼。

此示例展示了 torchvision.transforms.v2 模組中可用的一些不同變換。

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())使用某些畫素值填充影像的所有邊界。

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 變換(另請參見 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 變換(另請參見 five_crop())將給定的影像裁剪成四個角和中心區域。

plot transforms illustrations

隨機透視

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 變換(另請參見 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 變換(另請參見 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 變換(另請參見 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 變換(另請參見 crop())在隨機位置裁剪影像。

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

隨機調整大小裁剪

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 變換(另請參見 to_grayscale())將影像轉換為灰度

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

顏色抖動

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 變換(另請參見 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 變換(另請參見 invert())隨機反轉給定影像的顏色。

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

隨機色調分離

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 變換(另請參見 solarize())透過反轉高於閾值的所有畫素值來隨機對影像進行曝光過度處理(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 變換(另請參見 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 變換(另請參見 autocontrast())隨機對給定影像應用自動對比度。

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

隨機直方圖均衡

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 變換(另請參見 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 變換(另請參見 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 變換以給定機率隨機應用一個變換列表。

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.501 秒)

由 Sphinx-Gallery 生成的相簿

文件

訪問 PyTorch 的全面開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源