跳转到主要内容
博客

torchvision 0.3:分割、检测模型、新数据集等等……

作者: 2019 年 5 月 22 日2024 年 11 月 16 日暂无评论

PyTorch 领域库(如 torchvision)提供了对常用数据集和模型的便捷访问,可用于快速创建最先进的基线。此外,它们还提供通用的抽象,以减少用户可能不得不重复编写的样板代码。torchvision 0.3 版本带来了多项新功能,包括用于语义分割、对象检测、实例分割和人体关键点检测的模型,以及计算机视觉特定的自定义 C++/CUDA 操作。

新功能包括:

参考训练/评估脚本: torchvision 现在在 references/ 文件夹下提供了用于以下任务的训练和评估脚本:分类、语义分割、对象检测、实例分割和人体关键点检测。这些脚本可作为如何训练特定模型的记录,并提供基线训练和评估脚本,以便快速启动研究。

torchvision ops: torchvision 现在包含自定义 C++/CUDA 运算符。这些运算符专门用于计算机视觉,可以更轻松地构建对象检测模型。这些运算符目前不支持 PyTorch 脚本模式,但计划在下一个版本中支持。目前支持的一些操作包括:

  • roi_pool(以及模块版本 RoIPool)
  • roi_align(以及模块版本 RoIAlign)
  • nms,用于边界框的非极大值抑制
  • box_iou,用于计算两组边界框之间的交并比指标
  • box_area,用于计算一组边界框的面积

以下是使用 torchvision ops 的几个示例

import torch
import torchvision

# create 10 random boxes
boxes = torch.rand(10, 4) * 100
# they need to be in [x0, y0, x1, y1] format
boxes[:, 2:] += boxes[:, :2]
# create a random image
image = torch.rand(1, 3, 200, 200)
# extract regions in `image` defined in `boxes`, rescaling
# them to have a size of 3x3
pooled_regions = torchvision.ops.roi_align(image, [boxes], output_size=(3, 3))
# check the size
print(pooled_regions.shape)
# torch.Size([10, 3, 3, 3])

# or compute the intersection over union between
# all pairs of boxes
print(torchvision.ops.box_iou(boxes, boxes).shape)
# torch.Size([10, 10])

新模型和数据集: torchvision 现在增加了对对象检测、实例分割和人体关键点检测模型的支持。此外,还添加了几个流行的数据集。注意:API 目前处于实验阶段,未来版本的 torchvision 可能会发生变化。新模型包括:

分割模型

0.3 版本还包含用于图像上密集像素级预测的模型。它增加了 FCN 和 DeepLabV3 分割模型,使用 ResNet50 和 ResNet101 作为骨干网络。ResNet101 骨干网络的预训练权重可用,并在 COCO train2017 的一个子集上进行了训练,该子集包含与 Pascal VOC 相同的 20 个类别。

预训练模型在 COCO val2017 的子集(包含与 Pascal VOC 相同的 20 个类别)上给出以下结果:

网络平均 IoU全局像素级准确率
FCN ResNet10163.791.9
DeepLabV3 ResNet10167.492.4

检测模型

网络box APmask APkeypoint AP
在 COCO 上训练的 Faster R-CNN ResNet-50 FPN37.0  
在 COCO 上训练的 Mask R-CNN ResNet-50 FPN37.934.6 
在 COCO 上训练的 Keypoint R-CNN ResNet-50 FPN54.6 65.0

用于对象检测、实例分割和关键点检测的模型实现速度很快,尤其是在训练期间。

在下表中,我们使用 8 块 V100 GPU,配合 CUDA 10.0 和 CUDNN 7.4 报告结果。训练期间,每块 GPU 使用 2 的批处理大小,测试期间使用 1 的批处理大小。

对于测试时间,我们报告模型评估和后处理(包括图像中的掩码粘贴)的时间,但不包括计算精确召回率的时间。

网络训练时间(秒/迭代)测试时间(秒/迭代)内存(GB)
Faster R-CNN ResNet-50 FPN0.22880.05905.2
Mask R-CNN ResNet-50 FPN0.27280.09035.4
Keypoint R-CNN ResNet-50 FPN0.37890.12426.8

您可以用几行代码加载和使用预训练的检测和分割模型

import torchvision

model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True)
# set it to evaluation mode, as the model behaves differently
# during training and during evaluation
model.eval()

image = PIL.Image.open('/path/to/an/image.jpg')
image_tensor = torchvision.transforms.functional.to_tensor(image)

# pass a list of (potentially different sized) tensors
# to the model, in 0-1 range. The model will take care of
# batching them together and normalizing
output = model([image_tensor])
# output is a list of dict, containing the postprocessed predictions

分类模型

添加了以下分类模型:

  • GoogLeNet (Inception v1)
  • MobileNet V2
  • ShuffleNet v2
  • ResNeXt-50 32x4d 和 ResNeXt-101 32x8d

数据集

添加了以下数据集:

  • Caltech101、Caltech256 和 CelebA
  • ImageNet 数据集(改进了 ImageFolder,提供类字符串)
  • 语义边界数据集
  • VisionDataset 作为所有数据集的基类

此外,我们还添加了更多图像变换、一般改进和错误修复,以及改进了文档。

请在此处查看完整的发布说明:此处,以及此入门教程:Google Colab 上的此处,其中描述了如何根据自定义数据集微调您自己的实例分割模型。

干杯!

PyTorch 团队