1、视频
https://www.bilibili.com/video/BV1EZ4y1u7Sd/
2、百度网盘下载
可以在百度网盘下载需要用到的文件。
2、安装相关软件
2.1 安装 CMake
下载之后默认安装。
2.2 安装 Microsoft Visual Studio 2017
操作选中 “.NET 桌面开发”、“使用 C++ 的桌面开发”、“通用 Windows 平台开发”,然后进行安装,安装完成后重启。务必安装在 C 盘,因为 OpenVINO 默认 Visual Studio 在 C 盘。
2.3 安装 OpenVINO
2.3.1 OpenVINO 简介
OpenVINO 的全称是:Open Visual Inference & Neural Network Optimization
OpenVINO 工具套件主要包括用于优化神经网络模型的工具 Model Optimizer 和用于加速推理计算的软件包 Inference Engine。
相关概念:
- Model Optimizer:负责硬件无关的模型架构优化。
- Inference Engine:负责硬件相关的指令级别的优化。
- *.xml: 描述神经网络的结构
- *.bin: 存储模型权重参数
2.3.2 进行安装
操作下载之后全部默认安装。
3、设置环境变量
设置环境变量,是为了让应用程序(*.exe)能够找到 hddl 库、Inference Engine 库和 Open CV 库。
操作添加系统变量:
- HDDL_INSTALL_DIR:
C:\Program Files (x86)\IntelSWTools\openvino_2019.3.334\inference_engine\external\hddl
- IE_DIR:
C:\Program Files (x86)\IntelSWTools\openvino_2019.3.334\deployment_tools\inference_engine
- OPENCV_DIR:
C:\Program Files (x86)\IntelSWTools\openvino_2019.3.334\opencv
操作然后将以下 5 条路径添加到 Path 变量中:
- %IE_DIR%\bin\intel64\Release
- %IE_DIR%\bin\intel64\Debug
- %OPENCV_DIR%\lib
- %OPENCV_DIR%\bin
- %HDDL_INSTALL_DIR%\bin
4、运行演示程序
运行演示程序可以验证 OpenVINO 工具套件是否安装成功,下面的例子中选择一个运行即可。
输入命令 cd C:\Program Files (x86)\IntelSWTools\openvino_2019.3.334\deployment_tools\demo
,首先进入 demo 目录。因为下面的操作(4.1、4.2)都在该目录下进行。
输入命令 pip install networkx==2.3
,必须指定 networkx 为 2.3 版本,如果高于 2.3,运行 demo_benchmark_app.bat 会出现 AttributeError: ‘Graph’ object has no attribute ‘node’ 错误。
4.1 演示程序1:demo_benchmark_app.bat
这个程序可以测试出 AI 加速硬件(这里我们指定的是 CPU)的运行时间(Duration)、延时(Latency)和吞吐量(Throughput)。整个程序的工作流程为:下载 squeeznet 模型(*.bin) -> 使用 Inference Engine 进行推理 -> 给出硬件的表现数据。
输入命令 demo_benchmark_app.bat -d=CPU
,如果你之前没有下载 squeezenet 模型,那么 demo_benchmark_app.bat 会自动下载 squeezenet 模型。
运行成功截图:
4.2 演示程序2:demo_security_barrier_camera.bat
该演示程序为车牌识别演示程序,如本地不存在对应模型,该程序会自动下载。整个程序的工作流程为:下载 vehicle-license-plate-detection-barrier 模型(*.bin) -> 使用 Inference Engine 进行推理 -> 弹出识别窗口。
输入命令 demo_security_barrier_camera.bat -d=CPU
,指定 AI 模型在 CPU 上运行。
运行成功截图:
5、优化模型
我们以之前 cats_dogs 项目用的 ssd_inception_v2_coco_2018_01_28 模型为例子。
5.1 准备冻结图模型
我们接下来要使用的模型存放在: E:\1-tf_train\workspaces\cats_dogs\pre_trained_model\ssd_inception_v2_coco_2018_01_28\frozen_inference_graph.pb
5.2 准备数据
操作 下载一个商场监控的视频:https://github.com/intel-iot-devkit/sample-videos/raw/master/store-aisle-detection.mp4,然后保存到 E:\1-tf_train\workspaces\cats_dogs\videos\store-aisle-detection.mp4
5.3 优化模型
我们已经有了模型的冻结图文件 (*.pb),现在需要对该模型进行优化。Model Optimizer 会将基于多种流行的深度学习框架(如 Caffe、MXNet、TensorFlow、ONNX 等训练好的模型)转换为统一的能够被 Inference Engine 读取、加载并执行推理计算的中间表达(Intermediate Representation),简称 IR 文件。
输入命令 cd C:\Program Files (x86)\IntelSWTools\openvino_2019.3.334\deployment_tools\model_optimizer
,进入优化模型脚本 mo_tf.py 所在目录。
输入命令 python mo_tf.py --input_model=E:\1-tf_train\workspaces\cats_dogs\pre_trained_model\ssd_inception_v2_coco_2018_01_28\frozen_inference_graph.pb --tensorflow_object_detection_api_pipeline_config=E:\1-tf_train\workspaces\cats_dogs\pre_trained_model\ssd_inception_v2_coco_2018_01_28\pipeline.config --tensorflow_use_custom_operations_config=“C:\Program Files (x86)\IntelSWTools\openvino_2019.3.334\deployment_tools\model_optimizer\extensions\front\tf\ssd_v2_support.json” --reverse_input_channels --data_type=FP16 --output_dir=E:\1-tf_train\workspaces\cats_dogs\optimized_model --model_name=ssd_inception_v2_coco
- –input_model:待优化的冻结图模型,*.pb 文件
- –tensorflow_use_custom_operations_config:描述转换规则的文件,要和模型相匹配,*.json 文件。在 C:\Program Files (x86)\IntelSWTools\openvino_2019.3.334\deployment_tools\model_optimizer\extensions\front\tf 目录下可找到。
- –tensorflow_object_detection_api_pipeline_config:TensorFlow 冻结图的 pipeline.config 文件
- –reverse_input_channels:如果模型是基于 RGB 通道顺序来训练的模型,就需要添加该命令。因为 Inference Engine 是按照 BGR 通道顺序来读取图像数据的。
- –data_type:指定数据类型为 FP16
- –output_dir:IR 模型的保存路径
- –model_name:IR 模型的名字
优化成功的截图:
5.4 编译源程序
安装 OpenVINO 时附带了很多样例程序,源代码都已经写好了,程序的功能就调用优化后的模型,进行图像识别,所以我们就跳过写源代码这一步,直接进行编译。
输入命令 C:\Program Files (x86)\IntelSWTools\openvino_2019.3.334\deployment_tools\inference_engine\demos
,首先进入 demos 文件夹。
输入命令 build_demos_msvc.bat
,对 demos 文件夹里的所有项目进行编译。编译结果保存在:C:\Users\66\Documents\Intel\OpenVINO\omz_demos_build\intel64\Release 目录下。
得到可执行程序:
但我们仅仅需要的是 human_pose_estimation_demo.exe 这个程序。
5.5 编译生成 cpu_extension.lib
指定 CPU 来进行推理时,需要加载 CPU 扩展库,因为这个扩展库实现了 MKLDNN 库中没有实现的自定义网络层。我们现在先编译好,为以后使用做好准备。
输入命令 cd C:\Program Files (x86)\IntelSWTools\openvino_2019.3.334\deployment_tools\inference_engine\samples
,进入 samples 目录。
输入命令 build_samples_msvc.bat
,对 samples 目录下的所有项目进行编译。
操作
- 用 Visual Studio 2017 打开 C:\Users\66\Documents\Intel\OpenVINO\inference_engine_samples_build\Samples.sln -> 解决方案配置 -> 选择 Debug -> 右键 ALL_BUILD -> 生成 -> 得到 Debug 模式下的 Samples 可执行文件和相关库。
- 将 C:\Users\66\Documents\Intel\OpenVINO\inference_engine_samples_build\intel64\Debug\cpu_extension.lib 复制到 C:\Program Files(x86)\IntelSWTTools\openvino_2019.3.334\inference_engine\lib\intel64\Debug 目录下;
- 将 C:\Users\66\Documents\Intel\OpenVINO\inference_engine_samples_build\intel64\Debug\cpu_extension.dll 复制到 C:\Program Files(x86)\IntelSWTTools\openvino_2019.3.334\inference_engine\bin\intel64\Debug 目录下;
- 将 C:\Users\66\Documents\Intel\OpenVINO\inference_engine_samples_build\intel64\Release\cpu_extension.lib 复制到 C:\Program Files(x86)\IntelSWTTools\openvino_2019.3.334\inference_engine\lib\intel64\Release 目录下;
- 将 C:\Users\66\Documents\Intel\OpenVINO\inference_engine_samples_build\intel64\Release\cpu_extension.dll 复制到 C:\Program Files(x86)\IntelSWTTools\openvino_2019.3.334\inference_engine\bin\intel64\Release 目录下;
5.6 复制标签文件
操作 将百度网盘中的标签文件 ssd_inception_v2_coco.labels 复制到 E:\1-tf_train\workspaces\cats_dogs\optimized_model 目录下,务必保证该标签文件名和之前优化后模型的名字一样。
5.7 运行优化后的模型
输入命令 cd C:\Users\66\Documents\Intel\OpenVINO\omz_demos_build\intel64\Release
,进入 Release 目录
输入命令 object_detection_demo_ssd_async.exe -i=E:\1-tf_train\workspaces\cats_dogs\videos\store-aisle-detection.mp4 -m=E:\1-tf_train\workspaces\cats_dogs\optimized_model\ssd_inception_v2_coco.xml -d=CPU -t=0.6
- -i:视频所在位置
- -m:已经优化好的模型(*.xml 文件)
运行结果:
6、直接下载优化好的模型
在 GitHub 上 opencv/open_model_zoo 这个仓库里,已经有优化好的模型,我们可以直接下载下来用。
6.1 下载模型
以下载 human_pose_estimation 模型为例,这个模型的功能是描绘出人体的动作。
输入命令 cd C:\Program Files (x86)\IntelSWTools\openvino_2019.3.334\deployment_tools\tools\model_downloader
,进入 downloader.py 这个脚本所在目录,我们以用这个脚本来下载已经优化好的模型。
输入命令python downloader.py --name=human-pose-estimation-0001
,执行命令,进行下载。
下载完成后,模型会保存在 C:\Program Files (x86)\IntelSWTools\openvino_2019.3.334\deployment_tools\open_model_zoo\tools\downloader\intel\human-pose-estimation-0001\FP16 目录下。
6.2 编译源程序
程序的功能为:调用优化好的模型,即 IR 模型,然后识别图像中人的姿势。这个程序安装 OpenVINO 时自带,所以就不需要我们来写了,只需要编译即可。在 5.4 节我们已经编译过了,所以在 C:\Users\66\Documents\Intel\OpenVINO\omz_demos_build\intel64\Release 目录可以直接找到编译好的程序,即 human_pose_estimation_demo.exe。
6.2 测试模型
6.2.1 对一个视频进行测试
视频就使用我们在 5.2 节下载的视频。
输入命令 cd C:\Users\66\Documents\Intel\OpenVINO\omz_demos_build\intel64\Release
,进入 Release 目录。
输入命令 human_pose_estimation_demo.exe -d=CPU -i=E:\1-tf_train\workspaces\cats_dogs\videos\store-aisle-detection.mp4 -m="C:\Program Files (x86)\IntelSWTools\openvino_2019.3.334\deployment_tools\open_model_zoo\tools\downloader\intel\human-pose-estimation-0001\FP16\human-pose-estimation-0001.xml"
测试结果截图: