關鍵點變換¶
本示例說明了如何定義和使用關鍵點。在本教程中,我們使用這張來自前哥倫布時期陶瓷雕像的圖片。該影像被指定為“公共領域”(https://www.metmuseum.org/art/collection/search/502727)。
注意
關鍵點支援已在 TorchVision 0.23 中釋出,目前為 BETA 功能。我們不期望 API 會發生更改,但可能存在一些罕見的邊緣情況。如果您發現任何問題,請在我們的 bug 跟蹤器上報告:https://github.com/pytorch/vision/issues?q=is:open+is:issue
首先,一些設定程式碼
from PIL import Image
from pathlib import Path
import matplotlib.pyplot as plt
import torch
from torchvision.tv_tensors import KeyPoints
from torchvision.transforms import v2
from helpers import plot
plt.rcParams["figure.figsize"] = [10, 5]
plt.rcParams["savefig.bbox"] = "tight"
# if you change the seed, make sure that the transformed output
# still make sense
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/
orig_img = Image.open(Path('../assets') / 'pottery.jpg')
建立關鍵點¶
關鍵點透過例項化 KeyPoints 類來建立。
orig_pts = KeyPoints(
[
[
[445, 700], # nose
[320, 660],
[370, 660],
[420, 660], # left eye
[300, 620],
[420, 620], # left eyebrow
[475, 665],
[515, 665],
[555, 655], # right eye
[460, 625],
[560, 600], # right eyebrow
[370, 780],
[450, 760],
[540, 780],
[450, 820], # mouth
],
],
canvas_size=(orig_img.size[1], orig_img.size[0]),
)
plot([(orig_img, orig_pts)])

變換圖示¶
rotater = v2.RandomRotation(degrees=(0, 180), expand=True)
rotated_imgs = [rotater((orig_img, orig_pts)) for _ in range(4)]
plot([(orig_img, orig_pts)] + rotated_imgs)

使用 Pad
padded_imgs_and_points = [
v2.Pad(padding=padding)(orig_img, orig_pts)
for padding in (30, 50, 100, 200)
]
plot([(orig_img, orig_pts)] + padded_imgs_and_points)

使用 Resize
resized_imgs = [
v2.Resize(size=size)(orig_img, orig_pts)
for size in (300, 500, 1000, orig_img.size)
]
plot([(orig_img, orig_pts)] + resized_imgs)
perspective_transformer = v2.RandomPerspective(distortion_scale=0.6, p=1.0)
perspective_imgs = [perspective_transformer(orig_img, orig_pts) for _ in range(4)]
plot([(orig_img, orig_pts)] + perspective_imgs)

使用 CenterCrop
center_crops_and_points = [
v2.CenterCrop(size=size)(orig_img, orig_pts)
for size in (300, 500, 1000, orig_img.size)
]
plot([(orig_img, orig_pts)] + center_crops_and_points)
rotater = v2.RandomRotation(degrees=(0, 180))
rotated_imgs = [rotater((orig_img, orig_pts)) for _ in range(4)]
plot([(orig_img, orig_pts)] + rotated_imgs)

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