ResNet50

模型描述
ResNet50 v1.5 模型是原始 ResNet50 v1 模型的改进版本。
v1 和 v1.5 之间的区别在于:在需要下采样的瓶颈模块(bottleneck blocks)中,v1 在第一个 1×1 卷积层中设置步长(stride)为 2,而 v1.5 则在 3×3 卷积层中设置步长为 2。
这种差异使得 ResNet50 v1.5 比 v1 稍微精确一些(top1 准确率高约 0.5%),但性能上会有细微的损耗(约 5% 的图像处理速度下降)。
该模型在 Volta、Turing 和 NVIDIA Ampere GPU 架构上使用 Tensor Core 进行混合精度训练。因此,研究人员获得结果的速度比不使用 Tensor Core 训练时快 2 倍以上,同时还能体验混合精度训练带来的优势。该模型会针对每个 NGC 月度容器版本进行测试,以确保在整个生命周期内保持一致的准确性和性能。
请注意,ResNet50 v1.5 模型可以通过 TorchScript、ONNX Runtime 或 TensorRT 作为执行后端,部署在NVIDIA Triton 推理服务器上进行推理。详细信息请参阅 NGC
示例
在下面的示例中,我们将使用预训练的 ResNet50 v1.5 模型对 图像 进行推理并展示结果。
要运行此示例,您需要安装一些额外的 Python 包。这些包用于图像预处理和可视化。
!pip install validators matplotlib
import torch
from PIL import Image
import torchvision.transforms as transforms
import numpy as np
import json
import requests
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
print(f'Using {device} for inference')
加载在 ImageNet 数据集上预训练的模型。
resnet50 = torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_resnet50', pretrained=True)
utils = torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_convnets_processing_utils')
resnet50.eval().to(device)
准备示例输入数据。
uris = [
'http://images.cocodataset.org/test-stuff2017/000000024309.jpg',
'http://images.cocodataset.org/test-stuff2017/000000028117.jpg',
'http://images.cocodataset.org/test-stuff2017/000000006149.jpg',
'http://images.cocodataset.org/test-stuff2017/000000004954.jpg',
]
batch = torch.cat(
[utils.prepare_input_from_uri(uri) for uri in uris]
).to(device)
运行推理。使用 pick_n_best(predictions=output, n=topN) 辅助函数来挑选模型认为概率最高的 N 个假设。
with torch.no_grad():
output = torch.nn.functional.softmax(resnet50(batch), dim=1)
results = utils.pick_n_best(predictions=output, n=5)
显示结果。
for uri, result in zip(uris, results):
img = Image.open(requests.get(uri, stream=True).raw)
img.thumbnail((256,256), Image.LANCZOS)
plt.imshow(img)
plt.show()
print(result)
详细信息
有关模型输入和输出、训练配方、推理和性能的详细信息,请访问:github 和/或 NGC
参考文献