torchvision.models¶
模型子包包含了针对不同任务定义的模型,包括:图像分类、像素级语义分割、目标检测、实例分割、人体关键点检测和视频分类。
分类¶
模型子包包含以下图像分类模型架构的定义:
- AlexNet
- VGG
- ResNet
- SqueezeNet
- DenseNet
- Inception v3
- GoogLeNet
- ShuffleNet v2
- MobileNet v2
- 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 = models.mobilenet_v2()
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 = models.mobilenet_v2(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)
| 网络 | 顶级错误 1 | 顶级5错误 |
|---|---|---|
| AlexNet | 43.45 | 20.91 |
| VGG-11 | 30.98 | 11.37 |
| VGG-13 | 30.07 | 10.75 |
| VGG-16 | 28.41 | 9.62 |
| VGG-19 | 27.62 | 9.12 |
| 带批归一化的 VGG-11 | 29.62 | 10.19 |
| 带批归一化的 VGG-13 | 28.45 | 9.63 |
| 带批归一化的 VGG-16 | 26.63 | 8.50 |
| 带批归一化的 VGG-19 | 25.76 | 8.15 |
| ResNet-18 | 30.24 | 10.92 |
| ResNet-34 | 26.70 | 8.58 |
| ResNet-50 | 23.85 | 7.13 |
| ResNet-101 | 22.63 | 6.44 |
| ResNet-152 | 21.69 | 5.94 |
| SqueezeNet 1.0模型 | 41.90 | 19.58 |
| SqueezeNet 1.1版本 | 41.81 | 19.38 |
| Densenet-121 | 25.35 | 7.83 |
| Densenet-169 | 24.00 | 7.00 |
| Densenet-201 | 22.80 | 6.43 |
| Densenet-161 | 22.35 | 6.20 |
| Inception V3模型 | 22.55 | 6.44 |
| GoogleNet | 30.22 | 10.47 |
| ShuffleNet V2 | 30.64 | 11.68 |
| MobileNet V2 | 28.12 | 9.71 |
| ResNeXt-50-32x4d | 22.38 | 6.30 |
| ResNeXt-101-32x8d | 20.69 | 5.47 |
| 宽残差网络50-2 | 21.49 | 5.91 |
| 宽残差网络-101-2 | 21.16 | 5.72 |
| MNASNet 1.0 | 26.49 | 8.456 |
Alexnet¶
-
torchvision.models.alexnet(pretrained=False, progress=True, **kwargs)[source]¶ AlexNet模型架构来自 “一个奇怪的技巧…” 论文。
Parameters:
VGG¶
-
torchvision.models.vgg11(pretrained=False, progress=True, **kwargs)[source]¶ VGG 11层模型(配置“A”)来自 “非常深的卷积网络用于大规模图像识别”
Parameters:
-
torchvision.models.vgg11_bn(pretrained=False, progress=True, **kwargs)[source]¶ VGG 11层模型(配置“A”)带批量归一化 “非常深的卷积网络用于大规模图像识别”
Parameters:
-
torchvision.models.vgg13(pretrained=False, progress=True, **kwargs)[source]¶ VGG 13层模型(配置“B”) “用于大规模图像识别的非常深的卷积网络”
Parameters:
-
torchvision.models.vgg13_bn(pretrained=False, progress=True, **kwargs)[source]¶ VGG 13层模型(配置“B”)带批量归一化 “非常深的卷积网络用于大规模图像识别”
Parameters:
-
torchvision.models.vgg16(pretrained=False, progress=True, **kwargs)[source]¶ VGG 16层模型(配置“D”) “用于大规模图像识别的非常深的卷积网络”
Parameters:
-
torchvision.models.vgg16_bn(pretrained=False, progress=True, **kwargs)[source]¶ VGG 16层模型(配置“D”)带批量归一化 “非常深的卷积网络用于大规模图像识别”
Parameters:
-
torchvision.models.vgg19(pretrained=False, progress=True, **kwargs)[source]¶ VGG 19层模型(配置“E”) “用于大规模图像识别的非常深的卷积网络”
Parameters:
-
torchvision.models.vgg19_bn(pretrained=False, progress=True, **kwargs)[source]¶ VGG 19层模型(配置 ‘E’)带有批量归一化 “非常深的卷积网络用于大规模图像识别”
Parameters:
ResNet¶
-
torchvision.models.resnet18(pretrained=False, progress=True, **kwargs)[source]¶ ResNet-18模型来自 “深度残差学习用于图像识别”
Parameters:
-
torchvision.models.resnet34(pretrained=False, progress=True, **kwargs)[source]¶ ResNet-34模型来自 “深度残差学习用于图像识别”
Parameters:
-
torchvision.models.resnet50(pretrained=False, progress=True, **kwargs)[source]¶ ResNet-50模型来自 “深度残差学习用于图像识别”
Parameters:
-
torchvision.models.resnet101(pretrained=False, progress=True, **kwargs)[source]¶ ResNet-101模型来自 “深度残差学习用于图像识别”
Parameters:
-
torchvision.models.resnet152(pretrained=False, progress=True, **kwargs)[source]¶ ResNet-152模型来自 “深度残差学习用于图像识别”
Parameters:
SqueezeNet¶
-
torchvision.models.squeezenet1_0(pretrained=False, progress=True, **kwargs)[source]¶ SqueezeNet模型架构来自“SqueezeNet: 与AlexNet相当的准确性,但参数减少50倍且模型大小<0.5MB”论文。
Parameters:
-
torchvision.models.squeezenet1_1(pretrained=False, progress=True, **kwargs)[source]¶ SqueezeNet 1.1 模型来自 官方 SqueezeNet 仓库。 SqueezeNet 1.1 的计算量比 SqueezeNet 1.0 少 2.4 倍,参数也略少, 但不牺牲准确性。
Parameters:
DenseNet¶
-
torchvision.models.densenet121(pretrained=False, progress=True, **kwargs)[source]¶ Densenet-121模型来自 “密集连接的卷积网络”
Parameters:
-
torchvision.models.densenet169(pretrained=False, progress=True, **kwargs)[source]¶ Densenet-169模型来自 “密集连接的卷积网络”
Parameters:
-
torchvision.models.densenet161(pretrained=False, progress=True, **kwargs)[source]¶ Densenet-161模型来自 “密集连接的卷积网络”
Parameters:
Inception v3¶
-
torchvision.models.inception_v3(pretrained=False, progress=True, **kwargs)[source]¶ Inception v3 模型架构来自 “重新思考计算机视觉中的 Inception 架构”。
注意
重要:与其它模型不同,inception_v3 模型期望张量的大小为 N x 3 x 299 x 299,因此请确保您的图片尺寸与此相符。
Parameters:
注意
这需要安装scipy
GoogLeNet¶
-
torchvision.models.googlenet(pretrained=False, progress=True, **kwargs)[source]¶ GoogLeNet (Inception v1) 模型架构来自 “通过卷积网络深入研究”。
Parameters:
注意
这需要安装scipy
ShuffleNet v2¶
-
torchvision.models.shufflenet_v2_x0_5(pretrained=False, progress=True, **kwargs)[source]¶ 构建具有0.5x输出通道的ShuffleNetV2,如在 “ShuffleNet V2:高效CNN架构设计的实际指南”中所述。
Parameters:
-
torchvision.models.shufflenet_v2_x1_0(pretrained=False, progress=True, **kwargs)[source]¶ 构建具有1.0x输出通道的ShuffleNetV2,如在 “ShuffleNet V2:高效CNN架构设计的实际指南”中所述。
Parameters:
-
torchvision.models.shufflenet_v2_x1_5(pretrained=False, progress=True, **kwargs)[source]¶ 构建了一个具有1.5倍输出通道的ShuffleNetV2,如在 “ShuffleNet V2:高效CNN架构设计的实际指南”中所述。
Parameters:
-
torchvision.models.shufflenet_v2_x2_0(pretrained=False, progress=True, **kwargs)[source]¶ 构建具有2.0x输出通道的ShuffleNetV2,如在 “ShuffleNet V2:高效CNN架构设计的实际指南”中所述。
Parameters:
MobileNet v2¶
-
torchvision.models.mobilenet_v2(pretrained=False, progress=True, **kwargs)[source]¶ 构建了MobileNetV2架构,源自 “MobileNetV2:倒置残差和线性瓶颈”。
Parameters:
ResNext¶
-
torchvision.models.resnext50_32x4d(pretrained=False, progress=True, **kwargs)[source]¶ ResNeXt-50 32x4d 模型来自 “聚合残差变换用于深度神经网络”
Parameters:
-
torchvision.models.resnext101_32x8d(pretrained=False, progress=True, **kwargs)[source]¶ ResNeXt-101 32x8d 模型来自 “聚合残差变换用于深度神经网络”
Parameters:
宽残差网络¶
-
torchvision.models.wide_resnet50_2(pretrained=False, progress=True, **kwargs)[source]¶ 来自“Wide Residual Networks”的Wide ResNet-50-2模型 “Wide Residual Networks”
该模型与ResNet相同,除了每个模块中的瓶颈通道数是其两倍。外部1x1卷积层的通道数保持不变,例如,ResNet-50的最后一层模块有2048-512-2048个通道,在Wide ResNet-50-2中则为2048-1024-2048。
Parameters:
MNASNet¶
-
torchvision.models.mnasnet0_5(pretrained=False, progress=True, **kwargs)[source]¶ MNASNet,深度乘数为0.5,来自 “MnasNet: Platform-Aware Neural Architecture Search for Mobile”. :param pretrained: 如果为True,则返回在ImageNet上预训练的模型 :type pretrained: bool :param progress: 如果为True,则在stderr中显示下载进度条 :type progress: bool
-
torchvision.models.mnasnet0_75(pretrained=False, progress=True, **kwargs)[source]¶ MNASNet,深度乘数为0.75,来自 “MnasNet: Platform-Aware Neural Architecture Search for Mobile”. :param pretrained: 如果为True,则返回在ImageNet上预训练的模型 :type pretrained: bool :param progress: 如果为True,则在stderr中显示下载进度条 :type progress: bool
-
torchvision.models.mnasnet1_0(pretrained=False, progress=True, **kwargs)[source]¶ MNASNet,深度乘数为1.0,来自 “MnasNet: Platform-Aware Neural Architecture Search for Mobile”. :param pretrained: 如果为True,则返回在ImageNet上预训练的模型 :type pretrained: bool :param progress: 如果为True,则在stderr中显示下载进度条 :type progress: bool
-
torchvision.models.mnasnet1_3(pretrained=False, progress=True, **kwargs)[source]¶ MNASNet,深度乘数为1.3,来自 “MnasNet: Platform-Aware Neural Architecture Search for Mobile”. :param pretrained: 如果为True,则返回在ImageNet上预训练的模型 :type pretrained: bool :param progress: 如果为True,则在stderr中显示下载进度条 :type progress: bool
语义分割¶
模型子包包含以下语义分割模型架构的定义:
与图像分类模型一样,所有预训练模型都期望输入图像以相同的方式进行归一化。
图像需要加载到[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 |
全卷积网络¶
目标检测、实例分割和人体关键点检测¶
模型子包包含以下检测模型架构的定义:
用于检测、实例分割和关键点检测的预训练模型由 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 | ||
| 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 |
| 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=3, **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, with values ofxbetween0andWand values ofybetween0andH - 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, with values ofxbetween0andWand values ofybetween0andH - 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 (
RetinaNet¶
-
torchvision.models.detection.retinanet_resnet50_fpn(pretrained=False, progress=True, num_classes=91, pretrained_backbone=True, **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, with values between0andHand0andW - 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, with values between0andHand0andW - 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=3, **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, with values ofxbetween0andWand values ofybetween0andH - 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, with values ofxbetween0andWand values ofybetween0andH - 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=3, **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, with values ofxbetween0andWand values ofybetween0andH - 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, with values ofxbetween0andWand values ofybetween0andH - 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