pytorch转为openvino pytorch openvino_API

注意:本文是使用 OpenVINO 2022.1创建的。如果您想知道如何使用OpenVINO 2021.4的旧API,请查看此notebook

尽管PyTorch是AI训练的绝佳框架,可用于推理,但 OpenVINO™工具包可以在推理性能方面提供额外的好处,因为它针对此任务进行了大量优化。要使用它,您只需3个简单的步骤:安装OpenVINO、转换和优化模型并运行推理。为了向您展示整个过程,我们决定使用FastSeg模型,这是一个用于语义分割的网络,在Cityscapes数据集上进行了预训练。

OpenVINO能够以中间表示(IR)格式为网络运行推理。因此,您必须使用模型优化器(开发包中的命令行工具)转换网络。最简单的安装方法是使用PyPi:


pip install openvino-dev[pytorch,onnx]

第一步是将模型导出为ONNX格式。您需要使用Opset版本11,因为OpenVINO支持该版本。此外,对于此特定型号,不允许恒定折叠。


from fastseg import MobileV3Large

model = MobileV3Large.from_pretrained().cpu().eval()
dummy_input = torch.randn(1, 3, 512, 1024)

torch.onnx.export(model, dummy_input, "fastseg1024.onnx", opset_version=11, do_constant_folding=False)



OpenVINO直接支持ONNX,因此您可以将导出的模型加载到运行时并开始处理。但是,为了获得更好的性能,需要将模型转换并优化为IR。在终端中使用以下命令:


mo --input_model fastseg1024.onnx --input_shape "[1,3,512,1024]"

这意味着您正在转换输入大小等于1024x512x3(W x H x C)的model.onnx。当然,您可以提供其他参数,例如预处理或模型精度(FP32或FP16):


mo --input_model fastseg1024.onnx --input_shape "[1,3,512,1024]" --mean_values="[123.675,116.28,103.53]" --scale_values="[58.395,57.12,57.375]" --data_type FP16

均值的减法和除以标准差将直接内置到处理图中,推理将使用FP16运行。执行后,您应该看到如下所示的内容,其中包含所有显式和隐式参数,例如模型路径、输入形状、精度、平均值和小数位数值、转换参数等等:


Model Optimizer arguments:
Common parameters:
    - Path to the Input Model: /home/adrian/repos/openvino_notebooks/notebooks/102-pytorch-onnx-to-openvino/fastseg.onnx
    - Path for generated IR: /home/adrian/repos/openvino_notebooks/notebooks/102-pytorch-onnx-to-openvino/
    - IR output name: fastseg
    - Log level: ERROR
    - Batch: Not specified, inherited from the model
    - Input layers: Not specified, inherited from the model
    - Output layers: Not specified, inherited from the model
    - Input shapes: [1,3,512,1024]
    - Source layout: Not specified
    - Target layout: Not specified
    - Layout: Not specified
    - Mean values: [123.675,116.28,103.53]
    - Scale values: [58.395,57.12,57.375]
    - Scale factor: Not specified
    - Precision of IR: FP16
    - Enable fusing: True
    - User transformations: Not specified
    - Reverse input channels: False
    - Enable IR generation for fixed input shape: False
    - Use the transformations config file: None
Advanced parameters:
    - Force the usage of legacy Frontend of Model Optimizer for model conversion into IR: False
    - Force the usage of new Frontend of Model Optimizer for model conversion into IR: False
OpenVINO runtime found in:  /home/adrian/repos/openvino_notebooks/venv/lib/python3.9/site-packages/openvino
OpenVINO runtime version: 2022.1.0-7019-cdb9bec7210-releases/2022/1
Model Optimizer version: 2022.1.0-7019-cdb9bec7210-releases/2022/1
[ SUCCESS ] Generated IR version 11 model.
[ SUCCESS ] XML file: /home/adrian/repos/openvino_notebooks/notebooks/102-pytorch-onnx-to-openvino/fastseg.xml
[ SUCCESS ] BIN file: /home/adrian/repos/openvino_notebooks/notebooks/102-pytorch-onnx-to-openvino/fastseg.bin
[ SUCCESS ] Total execution time: 0.55 seconds. 
[ SUCCESS ] Memory consumed: 112 MB. 
[ INFO ] The model was converted to IR v11, the latest model format that corresponds to the source DL framework input/output format. While IR v11 is backwards compatible with OpenVINO Inference Engine API v1.0, please use API v2.0 (as of 2022.1) to take advantage of the latest improvements in IR v11.
Find more information about API v2.0 and IR v11 at https://docs.openvino.ai

几乎在最后的“成功”一词表示一切都已成功转换。您获得了IR,它包含两个文件:.xml.bin。您现在可以将此网络放入OpenVINO™运行时并执行推理。


import cv2 
import numpy as np 
from openvino.runtime import Coreimage_filename = "data/street.jpg"

image = cv2.cvtColor(cv2.imread(image_filename), cv2.COLOR_BGR2RGB)resized_image = cv2.resize(image, (1024, 512))

# Convert the resized images to network input shape
input_image = np.expand_dims(np.transpose(resized_image, (2, 0, 1)), 0)

# Load the network in Inference Engine
core = Core()
model_ir = core.read_model(model="fastseg.xml")
compiled_model_ir = core.compile_model(model=model_ir, device_name="CPU")

# Get output layer
output_layer_ir = compiled_model_ir.output(0)

# Run inference on the input image
res_ir = compiled_model_ir([input_image])[output_layer_ir]
result_mask_ir = np.squeeze(np.argmax(res_ir, axis=1)).astype(np.uint8)



它有效!下面的街道是分段的。您可以通过此演示自己尝试。

pytorch转为openvino pytorch openvino_pytorch转为openvino_02

或者,您可以使用此工具找到下载和安装有关您的环境的OpenVINO™工具包的最佳方式。

资源

  • 下载最新版本的OpenVINO工具包
  • 通过英特尔® DevCloud:边缘工作负载访问最新的英特尔硬件
  • 使用包含预配置软件的参考实施实现快速跟踪开发

本文最初发表于https://medium.com/openvino-toolkit/how-to-convert-pytorch-model-and-run-it-with-openvino-toolkit-c73cebcc01f5

https://www.codeproject.com/Articles/5335242/How-to-convert-PyTorch-model-and-run-it-with-OpenV