Pytorch ONNX格式支持
ONNX是一種深度學習權重模型的表示格式,ONNX格式可以讓AI開發者在不同框架之間相互轉換模型,實作呼叫上的通用性。當前PyTorch*, Caffe2*, Apache MXNet*, Microsoft Cognitive Toolkit* 、百度飛槳都支持ONNX格式。OpenVINO的模型優化器支持把ONNX格式的模型轉換IR中間層檔案。
當前OpenVINO官方支持的ONNX模型主要包括:bert_large,bvlc_alexnet,bvlc_googlenet,bvlc_reference_caffenet,bvlc_reference_rcnn_ilsvrc13 model,inception_v1,inception_v2,resnet50,squeezenet,densenet121,emotion_ferplus,mnist,shufflenet,VGG19,zfnet512。需要注意的是這些模型升級版本并不被支持。
從OpenVINO的2019R04版本開始支持所有公開的Pytorch模型,支持的模型串列如下:

Pytorch ONNX到OpenVINO IR轉換
下面的例子演示了如何從torchvision的公開模型中轉換為ONNX,然后再轉換為IR,使用OpenVINO完成呼叫的完整程序。我們將以resnet18為例來演示。
下載模型與轉ONNX格式
要下載與使用torchvision的預訓練模型,首選需要安裝好pytorch,然后執行下面的代碼就可以下載相關支持模型:
1 import torchvision.models as models
2 resnet18 = models.resnet18(pretrained=True)
3 alexnet = models.alexnet(pretrained=True)
4 squeezenet = models.squeezenet1_0(pretrained=True)
5 vgg16 = models.vgg16(pretrained=True)
6 densenet = models.densenet161(pretrained=True)
7 inception = models.inception_v3(pretrained=True)
8 googlenet = models.googlenet(pretrained=True)
9 shufflenet = models.shufflenet_v2_x1_0(pretrained=True)
10 mobilenet = models.mobilenet_v2(pretrained=True)
11 resnext50_32x4d = models.resnext50_32x4d(pretrained=True)
12 wide_resnet50_2 = models.wide_resnet50_2(pretrained=True)
13 mnasnet = models.mnasnet1_0(pretrained=True)
這里,我們只需要執行resnet18 = models.resnet18(pretrained=True)就可以下載resnet18的模型。這些模型的輸入格式要求如下:
大小都是224x224,
RGB三通道影像,
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
下載與轉為為ONNX的代碼如下:
model = torchvision.models.resnet18(pretrained=True).eval()
dummy_input = torch.randn((1, 3, 224, 224))
torch.onnx.export(model, dummy_input, "resnet18.onnx")
轉為IR格式
Cmd至打開安裝好的OpenVINO:
deployment_tools\model_optimizer
目錄下,執行下面的命令列陳述句:
python mo_onnx.py --input_model D:\python\pytorch_tutorial\resnet18.onnx

可以看到resnet18模型已經成功轉好!
OpenVINO SDK呼叫
對轉換好的IR模型,就可以首先通過OpenVINO202R3的Python版本SDK完成加速推理預測,完整的代碼實作如下:
from __future__ import print_function
import cv2
import numpy as np
import logging as log
from openvino.inference_engine import IECore
with open('imagenet_classes.txt') as f:
labels = [line.strip() for line in f.readlines()]
def image_classification():
model_xml = "resnet18.xml"
model_bin = "resnet18.bin"
# Plugin initialization for specified device and load extensions library if specified
log.info("Creating Inference Engine")
ie = IECore()
# Read IR
log.info("Loading network files:\n\t{}\n\t{}".format(model_xml, model_bin))
net = ie.read_network(model=model_xml, weights=model_bin)
log.info("Preparing input blobs")
input_blob = next(iter(net.inputs))
out_blob = next(iter(net.outputs))
# Read and pre-process input images
n, c, h, w = net.inputs[input_blob].shape
images = np.ndarray(shape=(n, c, h, w))
src = cv2.imread("D:/images/messi.jpg")
image = cv2.resize(src, (w, h))
image = np.float32(image) / 255.0
image[:, :, ] -= (np.float32(0.485), np.float32(0.456), np.float32(0.406))
image[:, :, ] /= (np.float32(0.229), np.float32(0.224), np.float32(0.225))
image = image.transpose((2, 0, 1))
# Loading model to the plugin
log.info("Loading model to the plugin")
exec_net = ie.load_network(network=net, device_name="CPU")
# Start sync inference
log.info("Starting inference in synchronous mode")
res = exec_net.infer(inputs={input_blob: [image]})
# Processing output blob
log.info("Processing output blob")
res = res[out_blob]
label_index = np.argmax(res, 1)
label_txt = labels[label_index[0]]
cv2.putText(src, label_txt, (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 255), 2, 8)
cv2.imshow("ResNet18-from Pytorch image classification", src)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
image_classification()
運行結果如下:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/170093.html
標籤:英特爾技術
上一篇:親測有效 | OpenVINO支持ONNX格式檔案直接讀取了
下一篇:高德地圖通過覆寫瓦片形式接入Google地圖,造成加載地圖時候,高德和谷歌地圖同時加載,請問怎么能去掉高德的加載,只加載谷歌地圖?
