注意:此帖子曾于 2022 年 11 月发布了早期版本。鉴于即将于 2023 年 3 月随 PyTorch 2.0 共同发布的 torchvision 0.15 版本,我们已更新此帖子,提供最新信息。
TorchVision 正在扩展其 Transforms API!以下是新内容
- 您不仅可以将它们用于图像分类,还可以用于目标检测、实例分割和语义分割以及视频分类。
- 您可以使用新的函数式 transforms 来转换视频、边界框和分割掩码。
该 API 与之前的版本完全向后兼容,并保持一致,以帮助迁移和采用。我们现在以 Beta 版本在 torchvision.transforms.v2
命名空间中发布此新 API,我们非常希望收到您的早期反馈,以改进其功能。如果您有任何问题或建议,请联系我们。
当前 Transforms 的限制
TorchVision 现有的 Transforms API(即 V1)仅支持单个图像。因此,它只能用于分类任务
from torchvision import transforms
trans = transforms.Compose([
transforms.ColorJitter(contrast=0.5),
transforms.RandomRotation(30),
transforms.CenterCrop(480),
])
imgs = trans(imgs)
上述方法不支持目标检测和分割。此限制使得所有非分类计算机视觉任务成为“二等公民”,因为无法使用 Transforms API 执行必要的增强。从历史上看,这使得使用 TorchVision 的基本原语训练高精度模型变得困难,从而导致我们的模型库与 SoTA (State of the Art) 相差几个点。
为了规避这一限制,TorchVision 在其参考脚本中提供了自定义实现,展示了如何在每个任务中执行增强。尽管这种做法使我们能够训练出高精度的分类、目标检测和分割模型,但这种方法并不理想,导致无法从 TorchVision 二进制文件中导入这些 transforms。
新的 Transforms API
Transforms V2 API 支持视频、边界框和分割掩码,这意味着它为许多计算机视觉任务提供了原生支持。新解决方案是即插即用的替代方案。
import torchvision.transforms.v2 as transforms
# Exactly the same interface as V1:
trans = transforms.Compose([
transforms.ColorJitter(contrast=0.5),
transforms.RandomRotation(30),
transforms.CenterCrop(480),
])
imgs, bboxes, labels = trans(imgs, bboxes, labels)
新的 Transform 类可以接收任意数量的输入,无需强制特定的顺序或结构
# Already supported:
trans(imgs) # Image Classification
trans(videos) # Video Tasks
trans(imgs, bboxes, labels) # Object Detection
trans(imgs, bboxes, masks, labels) # Instance Segmentation
trans(imgs, masks) # Semantic Segmentation
trans({"image": imgs, "box": bboxes, "tag": labels}) # Arbitrary Structure
# Future support:
trans(imgs, bboxes, labels, keypoints) # Keypoint Detection
trans(stereo_images, disparities, masks) # Depth Perception
trans(image1, image2, optical_flows, masks) # Optical Flow
trans(imgs_or_videos, labels) # MixUp/CutMix-style Transforms
Transform 类确保对所有输入应用相同的随机 transforms,以确保结果一致。
函数式 API 已更新,以支持所有输入所需的信号处理内核(如调整大小、裁剪、仿射变换、填充等)
from torchvision.transforms.v2 import functional as F
# High-level dispatcher, accepts any supported input type, fully BC
F.resize(inpt, size=[224, 224])
# Image tensor kernel
F.resize_image_tensor(img_tensor, size=[224, 224], antialias=True)
# PIL image kernel
F.resize_image_pil(img_pil, size=[224, 224], interpolation=BILINEAR)
# Video kernel
F.resize_video(video, size=[224, 224], antialias=True)
# Mask kernel
F.resize_mask(mask, size=[224, 224])
# Bounding box kernel
F.resize_bounding_box(bbox, size=[224, 224], spatial_size=[256, 256])
在底层,该 API 使用 Tensor 子类化来包装输入,附加有用的元数据并分派到正确的内核。为了使您的数据与这些新的 transforms 兼容,您可以使用提供的 dataset wrapper(适用于大多数 torchvision 内置数据集),或者您可以手动将数据包装到 Datapoints 中。
from torchvision.datasets import wrap_dataset_for_transforms_v2
ds = CocoDetection(..., transforms=v2_transforms)
ds = wrap_dataset_for_transforms_v2(ds) # data is now compatible with transforms v2!
# Or wrap your data manually using the lower-level Datapoint classes:
from torchvision import datapoints
imgs = datapoints.Image(images)
vids = datapoints.Video(videos)
masks = datapoints.Mask(target["masks“])
bboxes = datapoints.BoundingBox(target["boxes“], format=”XYXY”, spatial_size=imgs.shape)
除了新的 API,我们现在还提供了可导入的多种数据增强实现,这些增强用于 SoTA (State of the Art) 研究中,例如 Large Scale Jitter(大规模抖动)、AutoAugmentation(自动增强) 方法以及 几种 新的几何、颜色和类型转换 transforms。
该 API 继续支持 PIL 和 Tensor 两种图像后端,支持单张或批量输入,并在函数式和类 API 上保持 JIT 可脚本性。新的 API 已被验证可达到与之前实现相同的精度。
一个端到端示例
以下是使用下面这张图像的新 API 示例。它适用于 PIL 图像和 Tensors。有关更多示例和教程,请查看我们的图库!
from torchvision import io, utils
from torchvision import datapoints
from torchvision.transforms import v2 as T
from torchvision.transforms.v2 import functional as F
# Defining and wrapping input to appropriate Tensor Subclasses
path = "COCO_val2014_000000418825.jpg"
img = datapoints.Image(io.read_image(path))
# img = PIL.Image.open(path)
bboxes = datapoints.BoundingBox(
[[2, 0, 206, 253], [396, 92, 479, 241], [328, 253, 417, 332],
[148, 68, 256, 182], [93, 158, 170, 260], [432, 0, 438, 26],
[422, 0, 480, 25], [419, 39, 424, 52], [448, 37, 456, 62],
[435, 43, 437, 50], [461, 36, 469, 63], [461, 75, 469, 94],
[469, 36, 480, 64], [440, 37, 446, 56], [398, 233, 480, 304],
[452, 39, 463, 63], [424, 38, 429, 50]],
format=datapoints.BoundingBoxFormat.XYXY,
spatial_size=F.get_spatial_size(img),
)
labels = [59, 58, 50, 64, 76, 74, 74, 74, 74, 74, 74, 74, 74, 74, 50, 74, 74]
# Defining and applying Transforms V2
trans = T.Compose(
[
T.ColorJitter(contrast=0.5),
T.RandomRotation(30),
T.CenterCrop(480),
]
)
img, bboxes, labels = trans(img, bboxes, labels)
# Visualizing results
viz = utils.draw_bounding_boxes(F.to_image_tensor(img), boxes=bboxes)
F.to_pil_image(viz).show()
开发里程碑和未来工作
目前的开发进度如下
- 设计 API
- 编写用于转换视频、边界框、掩码和标签的内核
- 使用新 API 重写所有现有的 Transform 类(稳定版 + 参考实现)
- 图像分类
- 视频分类
- 目标检测
- 实例分割
- 语义分割
- 验证新 API 在所有支持的任务和后端上的精度
- 速度基准测试和性能优化(进行中 - 计划于 12 月完成)
- 从原型阶段毕业(计划于第一季度完成)
- 增加对深度感知、关键点检测、光流等的支持(未来)
- 增加对 MixUp 和 CutMix 等批量 transforms 的平滑支持
我们非常希望收到您的反馈,以改进其功能。如果您有任何问题或建议,请联系我们。