ResNet50

模型描述

ResNet50 v1.5 模型是 原始 ResNet50 v1 模型的修改版本。

v1 和 v1.5 之間的區別在於,在需要下采樣的瓶頸塊中,v1 在第一個 1×1 卷積中步長為 2,而 v1.5 在 3×3 卷積中步長為 2。

這一差異使得 ResNet50 v1.5 的準確度(top1 約 0.5%)略高於 v1,但效能略有下降(約 5% imgs/秒)。

該模型的初始化如 深入研究整流器:超越 ImageNet 分類中的人類水平效能中所述

該模型在 Volta、Turing 和 NVIDIA Ampere GPU 架構上使用 Tensor Cores 進行混合精度訓練。因此,研究人員可以獲得比不使用 Tensor Cores 訓練快 2 倍以上的結果,同時享受混合精度訓練的優勢。該模型針對每個 NGC 每月容器版本進行測試,以確保隨時間推移保持一致的準確性和效能。

請注意,ResNet50 v1.5 模型可以使用 TorchScript、ONNX Runtime 或 TensorRT 作為執行後端,部署到 NVIDIA Triton Inference Server 進行推理。有關詳細資訊,請檢視 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

參考文獻

使用 Tensor Cores 進行混合精度訓練的 ResNet50 模型。

模型型別: 視覺
提交者: NVIDIA