torchvision.models¶
模型子包包含了针对不同任务定义的模型,包括:图像分类、像素级语义分割、目标检测、实例分割、人体关键点检测和视频分类。
分类¶
模型子包包含以下图像分类模型架构的定义:
- AlexNet
- VGG
- ResNet
- SqueezeNet
- DenseNet
- Inception v3
- GoogLeNet
- ShuffleNet v2
- MobileNetV2
- MobileNetV3
- ResNeXt
- 宽残差网络
- MNASNet
你可以通过调用其构造函数来构建一个具有随机权重的模型:
import torchvision.models as models
resnet18 = models.resnet18()
alexnet = models.alexnet()
vgg16 = models.vgg16()
squeezenet = models.squeezenet1_0()
densenet = models.densenet161()
inception = models.inception_v3()
googlenet = models.googlenet()
shufflenet = models.shufflenet_v2_x1_0()
mobilenet_v2 = models.mobilenet_v2()
mobilenet_v3_large = models.mobilenet_v3_large()
mobilenet_v3_small = models.mobilenet_v3_small()
resnext50_32x4d = models.resnext50_32x4d()
wide_resnet50_2 = models.wide_resnet50_2()
mnasnet = models.mnasnet1_0()
我们提供预训练模型,使用PyTorch torch.utils.model_zoo。
这些可以通过传递pretrained=True来构建:
import torchvision.models as models
resnet18 = models.resnet18(pretrained=True)
alexnet = models.alexnet(pretrained=True)
squeezenet = models.squeezenet1_0(pretrained=True)
vgg16 = models.vgg16(pretrained=True)
densenet = models.densenet161(pretrained=True)
inception = models.inception_v3(pretrained=True)
googlenet = models.googlenet(pretrained=True)
shufflenet = models.shufflenet_v2_x1_0(pretrained=True)
mobilenet_v2 = models.mobilenet_v2(pretrained=True)
mobilenet_v3_large = models.mobilenet_v3_large(pretrained=True)
mobilenet_v3_small = models.mobilenet_v3_small(pretrained=True)
resnext50_32x4d = models.resnext50_32x4d(pretrained=True)
wide_resnet50_2 = models.wide_resnet50_2(pretrained=True)
mnasnet = models.mnasnet1_0(pretrained=True)
实例化一个预训练模型将会将其权重下载到缓存目录中。
此目录可以通过TORCH_MODEL_ZOO环境变量进行设置。详情请参见torch.utils.model_zoo.load_url()。
一些模型使用具有不同训练和评估行为的模块,例如批处理归一化。要在这两种模式之间切换,请根据需要使用model.train()或model.eval()。有关详细信息,请参阅train()或eval()。
所有预训练模型都期望输入图像进行相同的归一化处理,
即形状为(3 x H x W)的3通道RGB图像的小批量,
其中H和W至少为224。
图像需要加载到[0, 1]范围内,然后使用mean = [0.485, 0.456, 0.406]和std = [0.229, 0.224, 0.225]进行归一化。
你可以使用以下转换来进行归一化:
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
一个这样的归一化示例可以在ImageNet示例中找到 这里
获取mean和std值的过程大致相当于:
import torch
from torchvision import datasets, transforms as T
transform = T.Compose([T.Resize(256), T.CenterCrop(224), T.ToTensor()])
dataset = datasets.ImageNet(".", split="train", transform=transform)
means = []
stds = []
for img in subset(dataset):
means.append(torch.mean(img))
stds.append(torch.std(img))
mean = torch.mean(torch.tensor(means))
std = torch.mean(torch.tensor(stds))
不幸的是,所使用的具体 subset 已经丢失。有关更多信息,请参见 此讨论 或 这些实验。
ImageNet 单中心裁切错误率(224x224)
| 模型 | Acc@1 | Acc@5 |
|---|---|---|
| AlexNet | 56.522 | 79.066 |
| VGG-11 | 69.020 | 88.628 |
| VGG-13 | 69.928 | 89.246 |
| VGG-16 | 71.592 | 90.382 |
| VGG-19 | 72.376 | 90.876 |
| 带批归一化的 VGG-11 | 70.370 | 89.810 |
| 带批归一化的 VGG-13 | 71.586 | 90.374 |
| 带批归一化的 VGG-16 | 73.360 | 91.516 |
| 带批归一化的 VGG-19 | 74.218 | 91.842 |
| ResNet-18 | 69.758 | 89.078 |
| ResNet-34 | 73.314 | 91.420 |
| ResNet-50 | 76.130 | 92.862 |
| ResNet-101 | 77.374 | 93.546 |
| ResNet-152 | 78.312 | 94.046 |
| SqueezeNet 1.0模型 | 58.092 | 80.420 |
| SqueezeNet 1.1版本 | 58.178 | 80.624 |
| Densenet-121 | 74.434 | 91.972 |
| Densenet-169 | 75.600 | 92.806 |
| Densenet-201 | 76.896 | 93.370 |
| Densenet-161 | 77.138 | 93.560 |
| Inception V3模型 | 77.294 | 93.450 |
| GoogleNet | 69.778 | 89.530 |
| ShuffleNet V2 x1.0 | 69.362 | 88.316 |
| ShuffleNet V2 x0.5 | 60.552 | 81.746 |
| MobileNet V2 | 71.878 | 90.286 |
| MobileNet V3 大型版 | 74.042 | 91.340 |
| MobileNet V3 小型版 | 67.668 | 87.402 |
| ResNeXt-50-32x4d | 77.618 | 93.698 |
| ResNeXt-101-32x8d | 79.312 | 94.526 |
| 宽残差网络50-2 | 78.468 | 94.086 |
| 宽残差网络-101-2 | 78.848 | 94.284 |
| MNASNet 1.0 | 73.456 | 91.510 |
| MNASNet 0.5 | 67.734 | 87.490 |
Alexnet¶
-
torchvision.models.alexnet(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.alexnet.AlexNet[source]¶ AlexNet模型架构来自 “一个奇怪的技巧…” 论文。
Parameters:
VGG¶
-
torchvision.models.vgg11(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.vgg.VGG[source]¶ VGG 11层模型(配置“A”)来自 “Very Deep Convolutional Networks For Large-Scale Image Recognition” <https://arxiv.org/pdf/1409.1556.pdf>._
Parameters:
-
torchvision.models.vgg11_bn(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.vgg.VGG[source]¶ VGG 11层模型(配置“A”)带批处理规范化 “Very Deep Convolutional Networks For Large-Scale Image Recognition” <https://arxiv.org/pdf/1409.1556.pdf>._
Parameters:
-
torchvision.models.vgg13(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.vgg.VGG[source]¶ VGG 13层模型(配置“B”) “Very Deep Convolutional Networks For Large-Scale Image Recognition” <https://arxiv.org/pdf/1409.1556.pdf>._
Parameters:
-
torchvision.models.vgg13_bn(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.vgg.VGG[source]¶ VGG 13层模型(配置“B”)带批处理规范化 “Very Deep Convolutional Networks For Large-Scale Image Recognition” <https://arxiv.org/pdf/1409.1556.pdf>._
Parameters:
-
torchvision.models.vgg16(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.vgg.VGG[source]¶ VGG 16层模型(配置“D”) “Very Deep Convolutional Networks For Large-Scale Image Recognition” <https://arxiv.org/pdf/1409.1556.pdf>._
Parameters:
-
torchvision.models.vgg16_bn(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.vgg.VGG[source]¶ VGG 16层模型(配置“D”)带批处理规范化 “Very Deep Convolutional Networks For Large-Scale Image Recognition” <https://arxiv.org/pdf/1409.1556.pdf>._
Parameters:
-
torchvision.models.vgg19(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.vgg.VGG[source]¶ VGG 19层模型(配置“E”) “Very Deep Convolutional Networks For Large-Scale Image Recognition” <https://arxiv.org/pdf/1409.1556.pdf>._
Parameters:
ResNet¶
-
torchvision.models.resnet18(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.resnet.ResNet[source]¶ ResNet-18模型来自 “深度残差学习用于图像识别”。
Parameters:
-
torchvision.models.resnet34(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.resnet.ResNet[source]¶ ResNet-34模型来自 “深度残差学习用于图像识别”。
Parameters:
-
torchvision.models.resnet50(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.resnet.ResNet[source]¶ ResNet-50模型来自 “深度残差学习用于图像识别”。
Parameters:
-
torchvision.models.resnet101(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.resnet.ResNet[source]¶ ResNet-101模型来自 “深度残差学习用于图像识别”。
Parameters:
-
torchvision.models.resnet152(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.resnet.ResNet[source]¶ ResNet-152模型来自 “深度残差学习用于图像识别”。
Parameters:
SqueezeNet¶
-
torchvision.models.squeezenet1_0(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.squeezenet.SqueezeNet[source]¶ SqueezeNet模型架构来自“SqueezeNet: 与AlexNet相当的准确性,但参数减少50倍且模型大小<0.5MB”论文。
Parameters:
-
torchvision.models.squeezenet1_1(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.squeezenet.SqueezeNet[source]¶ SqueezeNet 1.1 模型来自 官方 SqueezeNet 仓库。 SqueezeNet 1.1 的计算量比 SqueezeNet 1.0 少 2.4 倍,参数也略少, 但不牺牲准确性。
Parameters:
DenseNet¶
-
torchvision.models.densenet121(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.densenet.DenseNet[source]¶ Densenet-121模型来自 “密集连接的卷积网络”。
Parameters:
-
torchvision.models.densenet169(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.densenet.DenseNet[source]¶ Densenet-169模型来自 “密集连接的卷积网络”。
Parameters:
-
torchvision.models.densenet161(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.densenet.DenseNet[source]¶ Densenet-161模型来自 “密集连接的卷积网络”。
Parameters:
-
torchvision.models.densenet201(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.densenet.DenseNet[source]¶ Densenet-201模型来自 “密集连接的卷积网络”。
Parameters:
Inception v3¶
-
torchvision.models.inception_v3(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.inception.Inception3[source]¶ Inception v3 模型架构来自 “重新思考计算机视觉中的 Inception 架构”。
注意
重要:与其它模型不同,inception_v3 模型期望张量的大小为 N x 3 x 299 x 299,因此请确保您的图片尺寸与此相符。
Parameters:
注意
这需要安装scipy
GoogLeNet¶
-
torchvision.models.googlenet(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.googlenet.GoogLeNet[source]¶ GoogLeNet (Inception v1) 模型架构来自 “通过卷积网络深入研究”。
Parameters:
注意
这需要安装scipy
ShuffleNet v2¶
-
torchvision.models.shufflenet_v2_x0_5(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.shufflenetv2.ShuffleNetV2[source]¶ 构建具有0.5x输出通道的ShuffleNetV2,如在 “ShuffleNet V2:高效CNN架构设计的实际指南”中所述。
Parameters:
-
torchvision.models.shufflenet_v2_x1_0(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.shufflenetv2.ShuffleNetV2[source]¶ 构建具有1.0x输出通道的ShuffleNetV2,如在 “ShuffleNet V2:高效CNN架构设计的实际指南”中所述。
Parameters:
-
torchvision.models.shufflenet_v2_x1_5(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.shufflenetv2.ShuffleNetV2[source]¶ 构建了一个具有1.5倍输出通道的ShuffleNetV2,如在 “ShuffleNet V2:高效CNN架构设计的实际指南”中所述。
Parameters:
-
torchvision.models.shufflenet_v2_x2_0(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.shufflenetv2.ShuffleNetV2[source]¶ 构建具有2.0x输出通道的ShuffleNetV2,如在 “ShuffleNet V2:高效CNN架构设计的实际指南”中所述。
Parameters:
MobileNet v2¶
-
torchvision.models.mobilenet_v2(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.mobilenetv2.MobileNetV2[source]¶ 构建了MobileNetV2架构,源自 “MobileNetV2:倒置残差和线性瓶颈”。
Parameters:
MobileNet v3¶
-
torchvision.models.mobilenet_v3_large(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.mobilenetv3.MobileNetV3[source]¶ 构建一个大型的MobileNetV3架构,来自 “搜索MobileNetV3”。
Parameters:
-
torchvision.models.mobilenet_v3_small(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.mobilenetv3.MobileNetV3[source]¶ 构建了一个小型的MobileNetV3架构,来自 “搜索MobileNetV3”。
Parameters:
ResNext¶
-
torchvision.models.resnext50_32x4d(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.resnet.ResNet[source]¶ ResNeXt-50 32x4d 模型来自 “聚合残差变换用于深度神经网络”。
Parameters:
-
torchvision.models.resnext101_32x8d(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.resnet.ResNet[source]¶ ResNeXt-101 32x8d 模型来自 “聚合残差变换用于深度神经网络”。
Parameters:
宽残差网络¶
-
torchvision.models.wide_resnet50_2(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.resnet.ResNet[source]¶ Wide ResNet-50-2 模型来自 “宽残差网络”。
该模型与ResNet相同,除了每个模块中的瓶颈通道数是其两倍。外部1x1卷积层的通道数保持不变,例如,ResNet-50的最后一层模块有2048-512-2048个通道,在Wide ResNet-50-2中则为2048-1024-2048。
Parameters:
-
torchvision.models.wide_resnet101_2(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.resnet.ResNet[source]¶ Wide ResNet-101-2 模型来自 “宽残差网络”。
该模型与ResNet相同,除了每个模块中的瓶颈通道数是其两倍。外部1x1卷积层的通道数保持不变,例如,ResNet-50的最后一层模块有2048-512-2048个通道,在Wide ResNet-50-2中则为2048-1024-2048。
Parameters:
MNASNet¶
-
torchvision.models.mnasnet0_5(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.mnasnet.MNASNet[source]¶ MNASNet 与深度乘数为 0.5 的模型来自 “MnasNet: Platform-Aware Neural Architecture Search for Mobile”。
Parameters:
-
torchvision.models.mnasnet0_75(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.mnasnet.MNASNet[source]¶ MNASNet 与深度乘数为 0.75 的模型来自 “MnasNet: Platform-Aware Neural Architecture Search for Mobile”。
Parameters:
-
torchvision.models.mnasnet1_0(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.mnasnet.MNASNet[source]¶ MNASNet 与深度乘数为 1.0 的模型来自 “MnasNet: Platform-Aware Neural Architecture Search for Mobile”。
Parameters:
-
torchvision.models.mnasnet1_3(pretrained: bool = False, progress: bool = True, **kwargs) → torchvision.models.mnasnet.MNASNet[source]¶ MNASNet,深度乘数为1.3,来自 “MnasNet: Platform-Aware Neural Architecture Search for Mobile”。
Parameters:
量化模型¶
以下架构为 INT8 精度量化模型提供支持。你可以通过调用其构造函数来获取具有随机权重的模型:
import torchvision.models as models
googlenet = models.quantization.googlenet()
inception_v3 = models.quantization.inception_v3()
mobilenet_v2 = models.quantization.mobilenet_v2()
mobilenet_v3_large = models.quantization.mobilenet_v3_large()
resnet18 = models.quantization.resnet18()
resnet50 = models.quantization.resnet50()
resnext101_32x8d = models.quantization.resnext101_32x8d()
shufflenet_v2_x0_5 = models.quantization.shufflenet_v2_x0_5()
shufflenet_v2_x1_0 = models.quantization.shufflenet_v2_x1_0()
shufflenet_v2_x1_5 = models.quantization.shufflenet_v2_x1_5()
shufflenet_v2_x2_0 = models.quantization.shufflenet_v2_x2_0()
获取预训练的量化模型只需几行代码:
import torchvision.models as models
model = models.quantization.mobilenet_v2(pretrained=True, quantize=True)
model.eval()
# run the model with quantized inputs and weights
out = model(torch.rand(1, 3, 224, 224))
我们为以下模型提供预训练的量化权重:
| 模型 | Acc@1 | Acc@5 |
|---|---|---|
| MobileNet V2 | 71.658 | 90.150 |
| MobileNet V3 大型版 | 73.004 | 90.858 |
| ShuffleNet V2 | 68.360 | 87.582 |
| ResNet 18模型 | 69.494 | 88.882 |
| ResNet 50模型 | 75.920 | 92.814 |
| ResNeXt 101 32x8d | 78.986 | 94.480 |
| Inception V3模型 | 77.176 | 93.354 |
| GoogleNet | 69.826 | 89.404 |
语义分割¶
模型子包包含以下语义分割模型架构的定义:
与图像分类模型一样,所有预训练模型都期望输入图像以相同的方式进行归一化。
图像需要加载到[0, 1]的范围内,然后使用
mean = [0.485, 0.456, 0.406]和std = [0.229, 0.224, 0.225]进行归一化。
它们是在最小尺寸为520的调整大小的图像上进行训练的。
预训练模型是在COCO train2017数据集的一个子集上进行训练的,该子集包含了Pascal VOC数据集中存在的20个类别。您可以在references/segmentation/coco_utils.py中查看子集是如何选择的。预训练模型输出的类别如下所示:
['__background__', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
在COCO val2017上评估的预训练模型的准确率如下所示:
| 网络 | 平均交并比 | 全局像素级精度 |
|---|---|---|
| FCN ResNet50模型 | 60.5 | 91.4 |
| FCN ResNet101模型 | 63.7 | 91.9 |
| DeepLabV3 ResNet50 | 66.4 | 92.4 |
| DeepLabV3 ResNet101 | 67.4 | 92.4 |
| DeepLabV3 移动网V3-Large | 60.3 | 91.2 |
| LR-ASPP 移动网 V3 大型版 | 57.9 | 91.2 |
全卷积网络¶
-
torchvision.models.segmentation.fcn_resnet50(pretrained=False, progress=True, num_classes=21, aux_loss=None, **kwargs)[source]¶ 构建一个具有 ResNet-50 主干网络的全卷积网络模型。
Parameters:
DeepLabV3¶
-
torchvision.models.segmentation.deeplabv3_resnet50(pretrained=False, progress=True, num_classes=21, aux_loss=None, **kwargs)[source]¶ 构建一个具有 ResNet-50 主干网络的 DeepLabV3 模型。
Parameters:
-
torchvision.models.segmentation.deeplabv3_resnet101(pretrained=False, progress=True, num_classes=21, aux_loss=None, **kwargs)[source]¶ 构建一个具有 ResNet-101 主干网络的 DeepLabV3 模型。
Parameters:
目标检测、实例分割和人体关键点检测¶
模型子包包含以下检测模型架构的定义:
用于检测、实例分割和关键点检测的预训练模型由 torchvision 中的分类模型初始化。
模型期望一个长度为Tensor[C, H, W]的列表,在范围0-1内。
模型在内部调整图像大小,使其最小尺寸为800。可以通过向模型构造函数传递选项min_size来更改此设置。
对于目标检测和实例分割,预训练模型返回以下类别的预测:
COCO_INSTANCE_CATEGORY_NAMES = [ '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella', 'N/A', 'N/A', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'N/A', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table', 'N/A', 'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush' ]
以下是根据COCO train2017数据集训练并在COCO val2017上评估的模型准确率的总结。
| 网络 | 边界框 AP0.5 | 待翻译文本似乎不完整或有误,请提供完整的待翻译文本以便准确翻译。 | 关键点 AP3 |
|---|---|---|---|
| 更快的R-CNN ResNet-50 FPN | 37.0 | ||
| 更快的R-CNN MobileNetV3-Large FPN | 32.8 | ||
| 更快的 R-CNN 移动网 V3-Large 320 FPN | 22.8 | ||
| RetinaNet ResNet-50 FPN | 36.4 | ||
| 带掩码的R-CNN基于ResNet-50 FPN模型 | 37.9 | 34.6 |
对于人体关键点检测,预训练模型的准确率如下所示
| 网络 | 边界框 AP0.5 | 待翻译文本似乎不完整或有误,请提供完整的待翻译文本以便准确翻译。 | 关键点 AP3 |
|---|---|---|---|
| 关键点 R-CNN 带 ResNet-50 和 FPN | 54.6 | 65.0 |
对于人体关键点检测,预训练模型以以下顺序返回关键点:
COCO_PERSON_KEYPOINT_NAMES = [ 'nose', 'left_eye', 'right_eye', 'left_ear', 'right_ear', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle' ]
运行时特性¶
目标检测、实例分割和关键点检测模型的实现非常高效。
在下面的表格中,我们使用了 8 块 V100 GPU,CUDA 版本为 10.0,CUDNN 版本为 7.4 来报告结果。训练过程中,每块 GPU 的批大小为 2,在测试时批大小为 1。
在测试时,我们报告模型评估和后处理(包括在图像中粘贴掩码)的时间,但不包括计算精确率-召回率的时间。
| 网络 | 训练时间(秒/迭代) | 测试时间(秒/次) | 内存(GB) |
|---|---|---|---|
| 更快的R-CNN ResNet-50 FPN | 0.2288 | 0.0590 | 5.2 |
| 更快的R-CNN MobileNetV3-Large FPN | 0.1020 | 0.0415 | 1.0 |
| 更快的 R-CNN 移动网 V3-Large 320 FPN | 0.0978 | 0.0376 | 0.6 |
| RetinaNet ResNet-50 FPN | 0.2514 | 0.0939 | 4.1 |
| 带掩码的R-CNN基于ResNet-50 FPN模型 | 0.2728 | 0.0903 | 5.4 |
| 关键点 R-CNN 带 ResNet-50 和 FPN | 0.3789 | 0.1242 | 6.8 |
Faster R-CNN¶
-
torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=False, progress=True, num_classes=91, pretrained_backbone=True, trainable_backbone_layers=None, **kwargs)[source]¶ 构建一个具有 ResNet-50-FPN 主干网络的 Faster R-CNN 模型。
模型的输入预期为张量列表,每个张量的形状为
[C, H, W],对应每一张图片,并且应在0-1范围内。不同的图片可以有不同的尺寸。模型的行为会根据它是在训练模式还是评估模式而变化。
在训练过程中,模型期望输入张量以及目标(字典列表),包含:
- boxes (
FloatTensor[N, 4]): the ground-truth boxes in[x1, y1, x2, y2]format, with0 <= x1 < x2 <= Wand0 <= y1 < y2 <= H. - labels (
Int64Tensor[N]): the class label for each ground-truth box
模型在训练过程中返回一个
Dict[Tensor],其中包含RPN和R-CNN的分类和回归损失。在推理过程中,模型只需要输入张量,并返回后处理的预测结果作为
List[Dict[Tensor]],每个输入图像一个。Dict的字段如下:- boxes (
FloatTensor[N, 4]): the predicted boxes in[x1, y1, x2, y2]format, with0 <= x1 < x2 <= Wand0 <= y1 < y2 <= H. - labels (
Int64Tensor[N]): the predicted labels for each image - scores (
Tensor[N]): the scores or each prediction
Faster R-CNN 可以导出为 ONNX,适用于批量大小固定且输入图像尺寸固定的场景。
Example:
>>> model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True) >>> # For training >>> images, boxes = torch.rand(4, 3, 600, 1200), torch.rand(4, 11, 4) >>> labels = torch.randint(1, 91, (4, 11)) >>> images = list(image for image in images) >>> targets = [] >>> for i in range(len(images)): >>> d = {} >>> d['boxes'] = boxes[i] >>> d['labels'] = labels[i] >>> targets.append(d) >>> output = model(images, targets) >>> # For inference >>> model.eval() >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)] >>> predictions = model(x) >>> >>> # optionally, if you want to export the model to ONNX: >>> torch.onnx.export(model, x, "faster_rcnn.onnx", opset_version = 11)
Parameters: - boxes (
-
torchvision.models.detection.fasterrcnn_mobilenet_v3_large_fpn(pretrained=False, progress=True, num_classes=91, pretrained_backbone=True, trainable_backbone_layers=None, **kwargs)[source]¶ 构建一个具有高分辨率MobileNetV3-Large FPN主干网络的Faster R-CNN模型。 它与使用ResNet-50 FPN主干网络的Faster R-CNN工作方式类似。有关更多详细信息,请参见fasterrcnn_resnet50_fpn。
Example:
>>> model = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_fpn(pretrained=True) >>> model.eval() >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)] >>> predictions = model(x)
Parameters:
-
torchvision.models.detection.fasterrcnn_mobilenet_v3_large_320_fpn(pretrained=False, progress=True, num_classes=91, pretrained_backbone=True, trainable_backbone_layers=None, **kwargs)[source]¶ 构建一个低分辨率的Faster R-CNN模型,采用针对移动应用场景优化的MobileNetV3-Large FPN主干网络。 它与使用ResNet-50 FPN主干网络的Faster R-CNN工作方式类似。有关更多详细信息,请参见fasterrcnn_resnet50_fpn。
Example:
>>> model = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_320_fpn(pretrained=True) >>> model.eval() >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)] >>> predictions = model(x)
Parameters:
RetinaNet¶
-
torchvision.models.detection.retinanet_resnet50_fpn(pretrained=False, progress=True, num_classes=91, pretrained_backbone=True, trainable_backbone_layers=None, **kwargs)[source]¶ 构建一个具有 ResNet-50-FPN 主干网络的 RetinaNet 模型。
模型的输入预期为张量列表,每个张量的形状为
[C, H, W],对应每一张图片,并且应在0-1范围内。不同的图片可以有不同的尺寸。模型的行为会根据它是在训练模式还是评估模式而变化。
在训练过程中,模型期望输入张量以及目标(字典列表),包含:
- boxes (
FloatTensor[N, 4]): the ground-truth boxes in[x1, y1, x2, y2]format, with0 <= x1 < x2 <= Wand0 <= y1 < y2 <= H. - labels (
Int64Tensor[N]): the class label for each ground-truth box
模型在训练过程中返回一个
Dict[Tensor],其中包含分类和回归损失。在推理过程中,模型只需要输入张量,并返回后处理的预测结果作为
List[Dict[Tensor]],每个输入图像一个。Dict的字段如下:- boxes (
FloatTensor[N, 4]): the predicted boxes in[x1, y1, x2, y2]format, with0 <= x1 < x2 <= Wand0 <= y1 < y2 <= H. - labels (
Int64Tensor[N]): the predicted labels for each image - scores (
Tensor[N]): the scores or each prediction
Example:
>>> model = torchvision.models.detection.retinanet_resnet50_fpn(pretrained=True) >>> model.eval() >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)] >>> predictions = model(x)
Parameters: - boxes (
Mask R-CNN¶
-
torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=False, progress=True, num_classes=91, pretrained_backbone=True, trainable_backbone_layers=None, **kwargs)[source]¶ 构建一个带有 ResNet-50-FPN 主干网络的 Mask R-CNN 模型。
模型的输入预期为张量列表,每个张量的形状为
[C, H, W],对应每一张图片,并且应在0-1范围内。不同的图片可以有不同的尺寸。模型的行为会根据它是在训练模式还是评估模式而变化。
在训练过程中,模型期望输入张量以及目标(字典列表),包含:
- boxes (
FloatTensor[N, 4]): the ground-truth boxes in[x1, y1, x2, y2]format, with0 <= x1 < x2 <= Wand0 <= y1 < y2 <= H. - labels (
Int64Tensor[N]): the class label for each ground-truth box - masks (
UInt8Tensor[N, H, W]): the segmentation binary masks for each instance
模型在训练过程中返回一个
Dict[Tensor],其中包含RPN和R-CNN的分类和回归损失,以及掩码损失。在推理过程中,模型只需要输入张量,并返回后处理的预测结果作为
List[Dict[Tensor]],每个输入图像一个。Dict的字段如下:- boxes (
FloatTensor[N, 4]): the predicted boxes in[x1, y1, x2, y2]format, with0 <= x1 < x2 <= Wand0 <= y1 < y2 <= H. - labels (
Int64Tensor[N]): the predicted labels for each image - scores (
Tensor[N]): the scores or each prediction - masks (
UInt8Tensor[N, 1, H, W]): the predicted masks for each instance, in0-1range. In order to obtain the final segmentation masks, the soft masks can be thresholded, generally with a value of 0.5 (mask >= 0.5)
Mask R-CNN 可以导出为 ONNX,用于批量大小固定且输入图像尺寸固定的场景。
Example:
>>> model = torchvision.models.detection.maskrcnn_resnet50_fpn(pretrained=True) >>> model.eval() >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)] >>> predictions = model(x) >>> >>> # optionally, if you want to export the model to ONNX: >>> torch.onnx.export(model, x, "mask_rcnn.onnx", opset_version = 11)
Parameters: - boxes (
关键点R-CNN¶
-
torchvision.models.detection.keypointrcnn_resnet50_fpn(pretrained=False, progress=True, num_classes=2, num_keypoints=17, pretrained_backbone=True, trainable_backbone_layers=None, **kwargs)[source]¶ 构建一个具有 ResNet-50-FPN 主干网络的 Keypoint R-CNN 模型。
模型的输入预期为张量列表,每个张量的形状为
[C, H, W],对应每一张图片,并且应在0-1范围内。不同的图片可以有不同的尺寸。模型的行为会根据它是在训练模式还是评估模式而变化。
在训练过程中,模型期望输入张量以及目标(字典列表),包含:
- boxes (
FloatTensor[N, 4]): the ground-truth boxes in[x1, y1, x2, y2]format, with0 <= x1 < x2 <= Wand0 <= y1 < y2 <= H. - labels (
Int64Tensor[N]): the class label for each ground-truth box - keypoints (
FloatTensor[N, K, 3]): theKkeypoints location for each of theNinstances, in the format[x, y, visibility], wherevisibility=0means that the keypoint is not visible.
模型在训练过程中返回一个
Dict[Tensor],其中包含RPN和R-CNN的分类和回归损失,以及关键点损失。在推理过程中,模型只需要输入张量,并返回后处理的预测结果作为
List[Dict[Tensor]],每个输入图像一个。Dict的字段如下:- boxes (
FloatTensor[N, 4]): the predicted boxes in[x1, y1, x2, y2]format, with0 <= x1 < x2 <= Wand0 <= y1 < y2 <= H. - labels (
Int64Tensor[N]): the predicted labels for each image - scores (
Tensor[N]): the scores or each prediction - keypoints (
FloatTensor[N, K, 3]): the locations of the predicted keypoints, in[x, y, v]format.
Keypoint R-CNN 可以导出为 ONNX,适用于批量大小固定且输入图像尺寸固定的场景。
Example:
>>> model = torchvision.models.detection.keypointrcnn_resnet50_fpn(pretrained=True) >>> model.eval() >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)] >>> predictions = model(x) >>> >>> # optionally, if you want to export the model to ONNX: >>> torch.onnx.export(model, x, "keypoint_rcnn.onnx", opset_version = 11)
Parameters: - boxes (
视频分类¶
我们提供了在Kinetics-400上预训练的动作识别模型。
它们都是使用references/video_classification中提供的脚本进行训练的。
所有预训练模型都期望输入图像以相同的方式进行归一化处理,即每批包含3个通道的RGB视频,形状为(3 x T x H x W),其中H和W预期为112,T是一个剪辑中的视频帧数。图像必须加载到[0, 1]范围内,然后使用mean = [0.43216, 0.394666, 0.37645]和std = [0.22803, 0.22145, 0.216989]进行归一化。
注意
归一化参数与图像分类的不同对应于来自Kinetics-400的均值和标准差。
注意
目前,归一化代码可以在references/video_classification/transforms.py中找到,
参见其中的Normalize函数。请注意,它与标准图像归一化不同,
因为它假设视频是4维的。
Kinetics 数据集 1-crop 准确率(剪辑长度为 16 帧,16x112x112)
| 网络 | 剪裁准确率@1 | 剪裁准确率 top-5 |
|---|---|---|
| 三维残差网络 18 层 | 52.75 | 75.45 |
| ResNet 18 多卡版本 | 53.90 | 76.29 |
| (2+1)D ResNet | 57.50 | 78.81 |
三维残差网络 ResNet ¶
-
torchvision.models.video.r3d_18(pretrained=False, progress=True, **kwargs)[source]¶ 构建具有18层的Resnet3D模型,参见 https://arxiv.org/abs/1711.11248
Parameters: Returns: R3D-18 网络模型
返回类型: nn.Module
ResNet 混合卷积¶
-
torchvision.models.video.mc3_18(pretrained=False, progress=True, **kwargs)[source]¶ 构建具有18层混合卷积网络,详情参见 https://arxiv.org/abs/1711.11248
Parameters: Returns: MC3 网络定义
返回类型: nn.Module