编译 李翊玮

使用OpenVINO™ Toolkit运行神经网路时,需先将其神经网路转换为中间表达式Intermediate Representation又称 IR文件。为此,您需要 模型优化器Model Optimizer,它是OpenVINO™ Toolkit中的命令行工具。获得它的最简单方法是通过PyPi:

pip install openvino-dev

模型优化器直接支持TensorFlow模型,因此可直接在终端中使用以下命令:

mo --input_model v3-small_224_1.0_float.pb --input_shape “[1,224,224,3]”

这表示您正将 v3-small_224_1.0_float.pb 模型转换为一个大小为 224x224 的 RGB 图像。 当然,您可以指定更多参数,如预处理步骤或所需的模型精度 (FP32 或 FP16):

mo --input_model v3-small_224_1.0_float.pb --input_shape "[1,224,224,3]" --mean_values="[127.5,127.5,127.5]" --scale_values="[127.5]" --data_type FP16

您的模型会将所有像素归一化为 [-1,1] 值范围,并且将使用 FP16 执行推理。 运行后,你应该会看到下面的东西,其中包含所有显式和隐式参数,如模型路径、输入形状、所选精度、通道回归、平均值和比例值、转换参数等等:

将TensorFlow模型导出到IR...这可能需要几分钟时间。

模型优化器参数:

Common parameters:

- Path to the Input Model: /home/adrian/repos/openvino_notebooks/notebooks/101-tensorflow-to-openvino/model/v3-small_224_1.0_float.pb

- Path for generated IR: /home/adrian/repos/openvino_notebooks/notebooks/101-tensorflow-to-openvino/model

- IR output name: v3-small_224_1.0_float

- 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,224,224,3]

- Mean values: [127.5,127.5,127.5]

- Scale values: [127.5]

- Scale factor: Not specified

- Precision of IR: FP16

- Enable fusing: True

- Enable grouped convolutions fusing: True

- Move mean values to preprocess section: None

- Reverse input channels: False

TensorFlow specific parameters:

- Input model in text protobuf format: False

- Path to model dump for TensorBoard: None

- List of shared libraries with TensorFlow custom layers implementation: None

- Update the configuration file with input/output node names: None

- Use configuration file used to generate the model with Object Detection API: None

- Use the config file: None

- Inference Engine found in: /home/adrian/repos/openvino_notebooks/openvino_env/lib/python3.8/site-packages/openvino

Inference Engine version: 2021.4.1-3926-14e67d86634-releases/2021/4

Model Optimizer version: 2021.4.1-3926-14e67d86634-releases/2021/4

[ SUCCESS ] Generated IR version 11 model.

[ SUCCESS ] XML file: /home/adrian/repos/openvino_notebooks/notebooks/101-tensorflow-to-openvino/model/v3-small_224_1.0_float.xml

[ SUCCESS ] BIN file: /home/adrian/repos/openvino_notebooks/notebooks/101-tensorflow-to-openvino/model/v3-small_224_1.0_float.bin

[ SUCCESS ] Total execution time: 9.97 seconds.

It's been a while, check for a new version of Intel(R) Distribution of OpenVINO(TM) toolkit here https://software.intel.com/content/www/us/en/develop/tools/openvino-toolkit/download.html?cid=other&source=prod&campid=ww_2022_bu_IOTG_OpenVINO-2022-1&content=upg_all&medium=organic or on the GitHub*

[ 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),IR由两个文件组成:.xml 和 .bin。

现在,您已准备好将此网络加载到推理引擎并运行推理。 下面的代码假定您的模型用于 ImageNet 分类。

import cv2

import numpy as np

from openvino.inference_engine import IECore

# Load the model

ie = IECore()

net = ie.read_network(model="v3-small_224_1.0_float.xml", weights="v3-small_224_1.0_float.bin")

exec_net = ie.load_network(network=net, device_name="CPU")

input_key = next(iter(exec_net.input_info))

output_key = next(iter(exec_net.outputs.keys()))

# Load the image

# The MobileNet network expects images in RGB format

image = cv2.cvtColor(cv2.imread(filename="image.jpg"), code=cv2.COLOR_BGR2RGB)

# resize to MobileNet image shape

input_image = cv2.resize(src=image, dsize=(224, 224))

# reshape to network input shape

input_image = np.expand_dims(input_image.transpose(2, 0, 1), axis=0)

# Do inference

result = exec_net.infer(inputs={input_key: input_image})[output_key]

result_index = np.argmax(result)

# Convert the inference result to a class name.

imagenet_classes = open("imagenet_2012.txt").read().splitlines()

# The model description states that for this model, class 0 is background,

# so we add background at the beginning of imagenet_classes

imagenet_classes = ['background'] + imagenet_classes

print(imagenet_classes[result_index])

如此已成功辨识! 你会得到一个图像分类结果n02099267 flat-coated retriever(平面-涂层的-猎犬)。 您可以通过原始码 demo亲自试验。

转换TensorFlow模型并使用OpenVINO运行加速推理效能相关推荐

  1. 双精度浮点数转换_模型压缩一半,精度几乎无损,TensorFlow推出半精度浮点量化工具包,还有在线Demo...

    鱼羊 发自 凹非寺  量子位 报道 | 公众号 QbitAI 近日,TensorFlow模型优化工具包又添一员大将,训练后的半精度浮点量化(float16 quantization)工具. 有了它,就 ...

  2. 模型转换、模型压缩、模型加速工具汇总

    点击上方"计算机视觉工坊",选择"星标" 干货第一时间送达 编辑丨机器学习AI算法工程 一.场景需求解读   在现实场景中,我们经常会遇到这样一个问题,即某篇论 ...

  3. TensorFlow零基础入门指南——计算模型、数据模型、运行模型!

    点上方蓝字计算机视觉联盟获取更多干货 在右上方 ··· 设为星标 ★,与你不见不散 正文一共:4110,预计阅读时间:11分钟 本篇文章主要介绍TensorFlow的基本概念,包含TensorFlow ...

  4. 设备 esp32_「ESP 教程」ESP32 如何运行 TensorFlow 模型

    人工智能之父,艾伦·图灵很早就曾预测"有一天,人们会带着电脑在公园散步,并告诉对方,今天早上我的计算机讲了个很有趣的事." 人类一直试图让机器具有智能,也就是人工智能(Artifi ...

  5. 使用OpenVINO运行PPTracking下FairMOT多目标跟踪模型

    图1  MOT 行人检测[1] 多对象追踪(Multi- Object Tracking, MOT) 在计算机视觉领域有着广泛且重要的应用.大到可以用在多目标导弹跟踪.市中心人流统计, 小到可以用在统 ...

  6. [tensorflow] 模型保存、加载与转换详解

    TensorFlow模型加载与转换详解 本次讲解主要涉及到TensorFlow框架训练时候模型文件的管理以及转换. 首先我们需要明确TensorFlow模型文件的存储格式以及文件个数: model_f ...

  7. 成功解决:Win系统下的Tensorflow使用CPU而不使用GPU运行加速

    成功解决:Win系统下的Tensorflow使用CPU而不使用GPU运行加速 目录 解决问题 解决思路 解决方法 解决问题 Win系统下的Tensorflow使用CPU而不使用GPU运行加速 解决思路 ...

  8. TensorFlow模型转换h5转pb

    在TensorFlow模型训练阶段一般使用model.save()将模型保存为h5格式,但部署阶段经常需要将训练好的模型固化为pb格式. h5模型转pb模型实现脚本: import tensorflo ...

  9. Pytorch与tensorflow模型转换

    使用pytorch_pretrained_bert将tensorflow模型转化为pytorch模型:https://blog.csdn.net/sunyueqinghit/article/detai ...

最新文章

  1. VMware Virtual SAN 互操作性:OpenStack
  2. 你了解 Java 的 jstat 命令吗?
  3. awk打印第一个字母
  4. 电气论文实现:应用转移因子法求解大规模电力网络潮流
  5. 计算机科学与技术在军中的应用,计算机科学技术的应用及发展趋势
  6. 『OpenCV3』滤波器实现及使用滤波器降噪
  7. 如何打开pr_debug调试信息
  8. Crackme015
  9. JNI_OnLoad
  10. JVM第一节:内存结构
  11. BGP十三条选路原则
  12. SAS安装时出现的问题:Diagram Control
  13. json文件编辑器android版,json editor手机版下载
  14. 极智Coding | 剖析 darknet load_weights 接口
  15. 聚合API文档阅读帮助
  16. 【最受欢迎最容易理解的初阶c语言教学】4.操作符和常见关键字
  17. jquery.webcam.js实现调用摄像头拍照兼容各个浏览器
  18. 计算机作为信息处理工具 应用于科学研究,计算机2013分春章节试题及答案.doc
  19. 数据分类分级原则、流程、方法
  20. 员工奖金需要交税吗_职工奖金个人所得税需要缴纳吗

热门文章

  1. easyui combobox多选属性
  2. 常见的SQL在线练习平台
  3. 最新爆笑段子120个,看了心境很舒服的噢(转吧转吧!)
  4. mongodb中cron定时任务
  5. 笔记:关于解释器与编译器以及机器语言与机器语言的二进制文件
  6. Source Insight 工程操作
  7. 读“金融危机十项注意”
  8. nexus3-maven私服搭建-Centos7
  9. UE Gameplay入门51(相机视锥空间的计算方法推导)
  10. 虚拟机+Ubuntu16.04:安装anaconda3