当前位置:   article > 正文

如何转换PyTorch模型并使用OpenVINO™工具包运行它_openvino转torch模型

openvino转torch模型

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

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

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

pip install openvino-dev[pytorch,onnx]

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

  1. from fastseg import MobileV3Large
  2. model = MobileV3Large.from_pretrained().cpu().eval()
  3. dummy_input = torch.randn(1, 3, 512, 1024)
  4. 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]"

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

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运行。执行后,您应该看到如下所示的内容,其中包含所有显式和隐式参数,例如模型路径、输入形状、精度、平均值和小数位数值、转换参数等等:

  1. Model Optimizer arguments:
  2. Common parameters:
  3. - Path to the Input Model: /home/adrian/repos/openvino_notebooks/notebooks/102-pytorch-onnx-to-openvino/fastseg.onnx
  4. - Path for generated IR: /home/adrian/repos/openvino_notebooks/notebooks/102-pytorch-onnx-to-openvino/
  5. - IR output name: fastseg
  6. - Log level: ERROR
  7. - Batch: Not specified, inherited from the model
  8. - Input layers: Not specified, inherited from the model
  9. - Output layers: Not specified, inherited from the model
  10. - Input shapes: [1,3,512,1024]
  11. - Source layout: Not specified
  12. - Target layout: Not specified
  13. - Layout: Not specified
  14. - Mean values: [123.675,116.28,103.53]
  15. - Scale values: [58.395,57.12,57.375]
  16. - Scale factor: Not specified
  17. - Precision of IR: FP16
  18. - Enable fusing: True
  19. - User transformations: Not specified
  20. - Reverse input channels: False
  21. - Enable IR generation for fixed input shape: False
  22. - Use the transformations config file: None
  23. Advanced parameters:
  24. - Force the usage of legacy Frontend of Model Optimizer for model conversion into IR: False
  25. - Force the usage of new Frontend of Model Optimizer for model conversion into IR: False
  26. OpenVINO runtime found in: /home/adrian/repos/openvino_notebooks/venv/lib/python3.9/site-packages/openvino
  27. OpenVINO runtime version: 2022.1.0-7019-cdb9bec7210-releases/2022/1
  28. Model Optimizer version: 2022.1.0-7019-cdb9bec7210-releases/2022/1
  29. [ SUCCESS ] Generated IR version 11 model.
  30. [ SUCCESS ] XML file: /home/adrian/repos/openvino_notebooks/notebooks/102-pytorch-onnx-to-openvino/fastseg.xml
  31. [ SUCCESS ] BIN file: /home/adrian/repos/openvino_notebooks/notebooks/102-pytorch-onnx-to-openvino/fastseg.bin
  32. [ SUCCESS ] Total execution time: 0.55 seconds.
  33. [ SUCCESS ] Memory consumed: 112 MB.
  34. [ 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.
  35. Find more information about API v2.0 and IR v11 at https://docs.openvino.ai

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

  1. import cv2
  2. import numpy as np
  3. from openvino.runtime import Coreimage_filename = "data/street.jpg"
  4. image = cv2.cvtColor(cv2.imread(image_filename), cv2.COLOR_BGR2RGB)resized_image = cv2.resize(image, (1024, 512))
  5. # Convert the resized images to network input shape
  6. input_image = np.expand_dims(np.transpose(resized_image, (2, 0, 1)), 0)
  7. # Load the network in Inference Engine
  8. core = Core()
  9. model_ir = core.read_model(model="fastseg.xml")
  10. compiled_model_ir = core.compile_model(model=model_ir, device_name="CPU")
  11. # Get output layer
  12. output_layer_ir = compiled_model_ir.output(0)
  13. # Run inference on the input image
  14. res_ir = compiled_model_ir([input_image])[output_layer_ir]
  15. result_mask_ir = np.squeeze(np.argmax(res_ir, axis=1)).astype(np.uint8)

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

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

资源

本文最初发表于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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/351179
推荐阅读
相关标签
  

闽ICP备14008679号