技能创建器
skill-creator
创建、改进和评估自定义技能,把专业流程转化为可重复使用的 AI 能力。
transforms-and-tv-tensors【数据与研究】Build torchvision transforms.v2 pipelines for images, videos, masks, boxes, rotated boxes, keypoints, arbitrary samples, migration from v1, and TVTensor metadata safety.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Use this sub-skill when the task is about torchvision.transforms.v2, torchvision.tv_tensors, transform migration, dtype/range handling, annotation metadata, custom transforms, functionals, or transform performance.
For dataset construction, downloads, dataset wrapping, image/video IO, or visualization utilities, route to ../datasets-io-utils/. For model weights preprocessing transforms, route to ../models-and-weights/. For official training preset scripts, route to ../training-references/.
references/pipeline-recipes.md.references/tv-tensors-and-annotations.md.references/migration-and-performance.md.canvas_size, random determinism, PIL/tensor mismatch, TorchScript, or slow transforms: use references/troubleshooting.md.python sub-skills/transforms-and-tv-tensors/scripts/smoke_transform_pipeline.py from the skill root or copy the script into a project and run it there.from torchvision.transforms import v2 for new code; v2 handles images plus boxes, masks, videos, keypoints, and arbitrary nested samples.v2.ToImage() and use v2.ToDtype(torch.float32, scale=True) before v2.Normalize(...).torchvision.tv_tensors before geometry transforms, especially BoundingBoxes(..., format=..., canvas_size=(H, W)), Mask(...), and KeyPoints(..., canvas_size=(H, W)).v2.SanitizeBoundingBoxes() or v2.ClampBoundingBoxes() after crops/resizes that can remove or partially move boxes.import torch
from torchvision import tv_tensors
from torchvision.transforms import v2
image = torch.randint(0, 256, (3, 32, 40), dtype=torch.uint8)
target = {
"boxes": tv_tensors.BoundingBoxes([[2, 4, 20, 24]], format="XYXY", canvas_size=image.shape[-2:]),
"labels": torch.tensor([1]),
"mask": tv_tensors.Mask(torch.zeros((1, 32, 40), dtype=torch.uint8)),
}
transforms = v2.Compose([
v2.ToImage(),
v2.RandomHorizontalFlip(p=1.0),
v2.ToDtype(torch.float32, scale=True),
])
image, target = transforms(image, target)
(..., C, H, W) for tensor images and that videos use a supported video layout for the chosen transform.[0, 1]; uint8 images are in [0, 255].format and canvas_size, and convert with v2.ConvertBoundingBoxFormat(...) when needed.v2.Compose is not the TorchScript composition primitive, and scripting v2 classes can fall back to v1-equivalent behavior.