快捷方式

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 開發者文件

檢視文件

教程

為初學者和高階開發者提供深入的教程

檢視教程

資源

查詢開發資源並讓您的問題得到解答

檢視資源