轉換範例¶
此範例說明 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
轉換(另請參閱 pad()
)用一些像素值填充所有影像邊框。
padded_imgs = [v2.Pad(padding=padding)(orig_img) for padding in (3, 10, 30, 50)]
plot([orig_img] + padded_imgs)

Resize¶
Resize
轉換(另請參閱 resize()
)調整影像大小。
resized_imgs = [v2.Resize(size=size)(orig_img) for size in (30, 50, 100, orig_img.size)]
plot([orig_img] + resized_imgs)

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)

FiveCrop¶
FiveCrop
轉換(另請參閱 five_crop()
)將給定的影像裁剪成四個角落和中心裁剪。
(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¶
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)

RandomRotation¶
RandomRotation
轉換(另請參閱 rotate()
)以隨機角度旋轉影像。
rotater = v2.RandomRotation(degrees=(0, 180))
rotated_imgs = [rotater(orig_img) for _ in range(4)]
plot([orig_img] + rotated_imgs)

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)

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)

RandomCrop¶
RandomCrop
轉換(另請參閱 crop()
)在隨機位置裁剪影像。
cropper = v2.RandomCrop(size=(128, 128))
crops = [cropper(orig_img) for _ in range(4)]
plot([orig_img] + crops)

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)

光度轉換¶
光度影像轉換是指修改影像的光度屬性的過程,例如其亮度、對比度、色彩或色調。這些轉換的應用旨在改變影像的視覺外觀,同時保留其幾何結構。
除了 Grayscale
之外,以下轉換都是隨機的,這表示即使使用相同的轉換實例轉換給定影像,每次也會產生不同的結果。
Grayscale¶
Grayscale
轉換(另請參閱 to_grayscale()
)將影像轉換為灰階
gray_img = v2.Grayscale()(orig_img)
plot([orig_img, gray_img], cmap='gray')

ColorJitter¶
ColorJitter
轉換隨機變更影像的亮度、對比度、飽和度、色調和其他屬性。
jitter = v2.ColorJitter(brightness=.5, hue=.3)
jittered_imgs = [jitter(orig_img) for _ in range(4)]
plot([orig_img] + jittered_imgs)

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)

RandomInvert¶
RandomInvert
轉換(另請參閱 invert()
)隨機反轉給定影像的顏色。
inverter = v2.RandomInvert()
invertered_imgs = [inverter(orig_img) for _ in range(4)]
plot([orig_img] + invertered_imgs)

RandomPosterize¶
RandomPosterize
轉換(另請參閱 posterize()
)透過減少每個顏色通道的位元數來隨機色調分離影像。
posterizer = v2.RandomPosterize(bits=2)
posterized_imgs = [posterizer(orig_img) for _ in range(4)]
plot([orig_img] + posterized_imgs)

RandomSolarize¶
RandomSolarize
轉換(另請參閱 solarize()
)透過反轉高於閾值的所有像素值來隨機太陽化影像。
solarizer = v2.RandomSolarize(threshold=192.0)
solarized_imgs = [solarizer(orig_img) for _ in range(4)]
plot([orig_img] + solarized_imgs)

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)

RandomAutocontrast¶
RandomAutocontrast
轉換(另請參閱 autocontrast()
)隨機將自動對比應用於給定影像。
autocontraster = v2.RandomAutocontrast()
autocontrasted_imgs = [autocontraster(orig_img) for _ in range(4)]
plot([orig_img] + autocontrasted_imgs)

RandomEqualize¶
RandomEqualize
轉換(另請參閱 equalize()
)隨機均衡化給定影像的直方圖。
equalizer = v2.RandomEqualize()
equalized_imgs = [equalizer(orig_img) for _ in range(4)]
plot([orig_img] + equalized_imgs)

JPEG¶
JPEG
轉換(另請參閱 jpeg()
)以隨機壓縮程度將 JPEG 壓縮應用於給定影像。

擴增轉換¶
以下轉換是多個轉換的組合,可以是幾何轉換或光度轉換,或兩者兼具。
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)

RandAugment¶
RandAugment
是 AutoAugment 的替代版本。
augmenter = v2.RandAugment()
imgs = [augmenter(orig_img) for _ in range(4)]
plot([orig_img] + imgs)

TrivialAugmentWide¶
TrivialAugmentWide
是 AutoAugment 的替代實作。但是,它不是多次轉換影像,而是僅使用來自給定清單的隨機轉換和隨機強度數字來轉換影像一次。
augmenter = v2.TrivialAugmentWide()
imgs = [augmenter(orig_img) for _ in range(4)]
plot([orig_img] + imgs)

AugMix¶
AugMix
轉換在影像的擴增版本之間進行插值。

隨機應用轉換¶
以下轉換是以給定機率 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)

RandomVerticalFlip¶
RandomVerticalFlip
轉換(另請參閱 vflip()
)以給定機率對影像執行垂直翻轉。
vflipper = v2.RandomVerticalFlip(p=0.5)
transformed_imgs = [vflipper(orig_img) for _ in range(4)]
plot([orig_img] + transformed_imgs)

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)

指令碼的總執行時間: (0 分鐘 6.437 秒)