在 Intel® Distribution of OpenVINO™ Toolkit 中使用深度学习识别模型
本教程将探讨如何使用 Intel® Distribution of OpenVINO™ 工具包版本中包含的深度学习模型进行人脸检测、年龄、性别和情绪识别以及头部姿态估计。
引言
本教程将探讨如何使用 Intel® Distribution of OpenVINO™ 工具包版本中包含的深度学习模型进行人脸检测、年龄、性别和情绪识别以及头部姿态估计。本教程还演示了 Intel Distribution of OpenVINO 工具包的架构组件的使用,例如 Intel® Deep Learning Deployment Toolkit,它允许软件开发人员在用户应用程序中使用高级 C++ 库(称为 Inference Engine)来部署预训练模型。
本教程包括在 Up Squared* Grove* 物联网开发套件上构建和运行示例应用程序的说明。Up Squared* 平台预装了 Ubuntu* 16.04.4 桌面镜像和 Intel Distribution of OpenVINO 工具包。
图 1 说明了捕获的图像与 Inference Engine 和检测结果之间的关系。

要求
表 1. 要求
| 硬件 | 软件 | 
|---|---|
| Up Squared* Grove* 物联网开发套件 | 预装 Ubuntu* 16.04.4 桌面镜像 | 
| 带 USB 端口的网络摄像头 | Intel Distribution of OpenVINO 工具包(在 Up Squared 平台预装)。本教程同时使用了预装版本和 R3 版本 3.343。要升级或刷新您的版本,请参阅 免费下载。 | 
| 带 HDMI 接口和线缆的显示器 | |
| USB 键盘和鼠标 | |
| 具有 Internet 访问的网络连接或 Up Squared* Wi-Fi 套件 | 

运行预装的年龄和性别识别模型
UP Squared* 板上预装了 Ubuntu* 16.04.4 桌面镜像以及 Intel Distribution of OpenVINO 工具包。表 2 描述了安装的目录结构。
表 2. Intel Distribution of OpenVINO Toolkit 中的目录和关键文件
| 组件 (Component) | Location | 
|---|---|
| 根目录 | /opt/intel/computer_vision_sdk/deployment_tools | 
| Intel 模型 | /opt/intel/computer_vision_sdk/deployment_tools/intel_models | 
| setupvars.sh | /opt/intel/computer_vision_sdk/bin/setupvars.sh | 
| 构建 | /opt/intel/computer_vision_sdk/deployment_tools/inference_engine/samples/build | 
| 二进制 | /opt/intel/computer_vision_sdk/deployment_tools/inference_engine/samples/build/intel64/Release | 
| 演示脚本 | /opt/intel/computer_vision_sdk/deployment_tools/demo 包含分类和安全屏障摄像头演示脚本。 | 
构建年龄和性别识别应用程序
- 	更新存储库列表并安装先决软件包。 # Update the repository list sudo -E apt update # Install prerequisite packages sudo -E apt -y install cmake libpng12-dev libcairo2-dev libpango1.0-dev libglib2.0-dev libgtk2.0-dev libgstreamer0.10-dev libswscale-dev libavcodec-dev libavformat-dev 
- 	设置环境变量。 export ROOT_DIR=/opt/intel/computer_vision_sdk_2018.3.343/deployment_tools source $ROOT_DIR/../bin/setupvars.sh 
- 	如果构建目录存在,则进入构建目录。否则,创建构建目录。 cd $ROOT_DIR/inference_engine/samples sudo mkdir build cd $ROOT_DIR/inference_engine/samples/build 
- 	生成发布版的 make 文件,不带调试信息。 sudo cmake -DMAKE_BUILD_TYPE=Release .. 
- 	生成带调试信息的 make 文件。 sudo cmake -DMAKE_BUILD_TYPE=Debug .. 
- 	构建交互式人脸检测示例。 sudo make -j8 interactive_face_detection_sample 
- 	构建所有示例。 sudo make -j8 
- 	构建将在 Release 或 Debug 目录中生成 interactive_face_detection可执行文件。ls $ROOT_DIR/deployment_tools /inference_engine/samples/build/intel64/Release/interactive_face_detection_sample 
运行年龄和性别识别应用程序
运行应用程序并使用 -h 显示所有可用选项。
cd $ROOT_DIR/inference_engine/samples/build/intel64/Release$ ./interactive_face_detection_sample -h
InferenceEngine: 
	API version ............ 1.0
	Build .................. 10478
interactive_face_detection [OPTION]
Options:
    -h                         Print a usage message.
    -i "<path>"                Optional. Path to an video file. Default value is "cam" to work with camera.
    -m "<path>"                Required. Path to an .xml file with a trained face detection model.
    -m_ag "<path>"             Optional. Path to an .xml file with a trained age gender model.
    -m_hp "<path>"             Optional. Path to an .xml file with a trained head pose model.
      -l "<absolute_path>"     Required for MKLDNN (CPU)-targeted custom layers.Absolute path to a shared library with the kernels impl.
          Or
      -c "<absolute_path>"     Required for clDNN (GPU)-targeted custom kernels.Absolute path to the xml file with the kernels desc.
    -d "<device>"              Specify the target device for Face Detection (CPU, GPU, FPGA, or MYRYAD. Sample will look for a suitable plugin for device specified.
    -d_ag "<device>"           Specify the target device for Age Gender Detection (CPU, GPU, FPGA, or MYRYAD. Sample will look for a suitable plugin for device specified.
    -d_hp "<device>"           Specify the target device for Head Pose Detection (CPU, GPU, FPGA, or MYRYAD. Sample will look for a suitable plugin for device specified.
    -n_ag "<num>"              Specify number of maximum simultaneously processed faces for Age Gender Detection ( default is 16).
    -n_hp "<num>"              Specify number of maximum simultaneously processed faces for Head Pose Detection ( default is 16).
    -no_wait                   No wait for key press in the end.
    -no_show                   No show processed video.
    -pc                        Enables per-layer performance report.
    -r                         Inference results as raw values.
    -t                         Probability threshold for detections.
交互式人脸检测应用程序使用三个模型
- 训练后人脸检测模型的 .xml 文件路径
- 训练后年龄性别模型的 .xml 文件路径
- 训练后头部姿态模型的 .xml 文件路径
该应用程序支持 -i 的两种选项:网络摄像头流或视频文件路径。
使用网络摄像头运行应用程序
- 	将网络摄像头插入 UP Squared 板的 USB 端口。打开 Ubuntu 控制台并列出视频设备。 ls -ltrh /dev/video* 
- 	如果系统上没有 /dev/video 文件,请确保网络摄像头已插入 USB。 crw-rw----+ 1 root video 81, 0 Sep 27 12:48 /dev/video0 
- 	使用摄像头运行交互式人脸检测应用程序。 ./interactive_face_detection_sample -d CPU -d_ag CPU -d_hp CPU -i cam -m "$ROOT_DIR/intel_models/face-detection-adas-0001/FP32/face-detection-adas-0001.xml" -m_ag "$ROOT_DIR/intel_models/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013.xml" -m_hp "$ROOT_DIR/intel_models/head-pose-estimation-adas-0001/FP32/head-pose-estimation-adas-0001.xml" 
使用视频文件路径运行应用程序
该应用程序接受预录制视频文件。本示例使用 预录制视频。
./interactive_face_detection_sample -d CPU -d_ag CPU -d_hp CPU -i /home/upsquared/head-pose-face-detection-female-and-male.mp4 -m "$ROOT_DIR/intel_models/face-detection-adas-0001/FP32/face-detection-adas-0001.xml" -m_ag "$ROOT_DIR/intel_models/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013.xml" -m_hp "$ROOT_DIR/intel_models/head-pose-estimation-adas-0001/FP32/head-pose-estimation-adas-0001.xml"
解释检测结果
该应用程序使用 OpenCV* 显示网络摄像头或预录制视频的视觉结果。视觉结果包括
- 每个检测到的人脸周围的边界框
- 一个多色轴,跟踪边界框的中心
- 边界框左上角的文本,指示性别和估计年龄
- 渲染人脸检测、年龄和性别检测以及头部姿态所需的时间

浮点误差
CPU 只能运行单精度浮点数(FP32)模型。
错误:插件不支持 F16
如果发生错误,请更改模型路径以指向 FP32。
运行演示脚本
分类和安全屏障摄像头演示脚本位于 /opt/intel/computer_vision_sdk/deployment_tools/demo。请参阅 README.txt 以获取有关如何使用演示脚本和创建自己的年龄、性别和情绪演示脚本的说明。
在 Intel Distribution of OpenVINO Toolkit R3 中运行年龄、性别和情绪识别模型
下载 Intel Distribution of OpenVINO Toolkit R3。请遵循 Linux* 的安装说明。安装将创建表 3 中的目录结构。
表 3. Intel Distribution of OpenVINO Toolkit R3 中的目录和关键文件
| 组件 (Component) | Location | 
|---|---|
| 根目录 | /opt/intel/computer_vision_sdk_2018.3.343/deployment_tools | 
| Intel 模型 | /opt/intel/ computer_vision_sdk_2018.3.343/deployment_tools/intel_models | 
| setupvars.sh | /opt/intel/ computer_vision_sdk_2018.3.343/bin/setupvars.sh | 
| 构建 | /home/upsquared/inference_engine_samples | 
| 二进制 | /home/upsquared/inference_engine_samples/intel64/Release | 
| 演示脚本 | /opt/intel/ computer_vision_sdk_2018.3.343/deployment_tools/demo | 
构建 Intel 年龄、性别和情绪识别应用程序
- 	更新存储库列表并安装先决软件包。 # Update the repository list sudo -E apt update # Install prerequisite packages sudo -E apt -y install build-essential cmake libpng12-dev libcairo2-dev libpango1.0-dev libglib2.0-dev libgtk2.0-dev libswscale-dev libavcodec-dev libavformat-dev libgstreamer1.0-0 gstreamer1.0-plugins-base 
- 	设置环境变量。 export ROOT_DIR=/opt/intel/computer_vision_sdk_2018.3.343/deployment_tools source $ROOT_DIR/../bin/setupvars.sh 
- 	如果构建目录存在,则进入构建目录。否则,创建构建目录。 cd $ROOT_DIR/inference_engine/samples sudo mkdir build cd $ROOT_DIR/inference_engine/samples/build 
- 	生成发布版的 make 文件,不带调试信息。 sudo cmake -DMAKE_BUILD_TYPE=Release .. 
- 	生成带调试信息的 make 文件。 sudo cmake -DMAKE_BUILD_TYPE=Debug .. 
- 	构建交互式人脸检测示例。 sudo make -j8 interactive_face_detection_sample 
- 	构建所有示例。 sudo make -j8 
- 	构建将在 Release 或 Debug 目录中生成 interactive_face_detection 可执行文件。 ls $ROOT_DIR/deployment_tools /inference_engine/samples/build/intel64/Release/interactive_face_detection_sample 
运行 Intel 年龄、性别和情绪识别应用程序
运行应用程序并使用 -h 显示所有可用选项。
cd ~/inference_engine_samples/intel64/Release$ ./interactive_face_detection_sample -h
InferenceEngine: 
	API version ............ 1.2
	Build .................. 13911
interactive_face_detection [OPTION]
Options:
    -h                         Print a usage message.
    -i "<path>"                Optional. Path to an video file. Default value is "cam" to work with camera.
    -m "<path>"                Required. Path to an .xml file with a trained face detection model.
    -m_ag "<path>"             Optional. Path to an .xml file with a trained age gender model.
    -m_hp "<path>"             Optional. Path to an .xml file with a trained head pose model.
    -m_em "<path>"             Optional. Path to an .xml file with a trained emotions model.
      -l "<absolute_path>"     Required for MKLDNN (CPU)-targeted custom layers.Absolute path to a shared library with the kernels impl.
          Or
      -c "<absolute_path>"     Required for clDNN (GPU)-targeted custom kernels.Absolute path to the xml file with the kernels desc.
    -d "<device>"              Specify the target device for Face Detection (CPU, GPU, FPGA, or MYRIAD). Sample will look for a suitable plugin for device specified.
    -d_ag "<device>"           Specify the target device for Age Gender Detection (CPU, GPU, FPGA, or MYRIAD). Sample will look for a suitable plugin for device specified.
    -d_hp "<device>"           Specify the target device for Head Pose Detection (CPU, GPU, FPGA, or MYRIAD). Sample will look for a suitable plugin for device specified.
    -d_em "<device>"           Specify the target device for Emotions Detection (CPU, GPU, FPGA, or MYRIAD). Sample will look for a suitable plugin for device specified.
    -n_ag "<num>"              Specify number of maximum simultaneously processed faces for Age Gender Detection (default is 16).
    -n_hp "<num>"              Specify number of maximum simultaneously processed faces for Head Pose Detection (default is 16).
    -n_em "<num>"              Specify number of maximum simultaneously processed faces for Emotions Detection (default is 16).
    -dyn_ag                    Enable dynamic batch size for AgeGender net.
    -dyn_hp                    Enable dynamic batch size for HeadPose net.
    -dyn_em                    Enable dynamic batch size for Emotions net.
    -async                     Enable asynchronous mode
    -no_wait                   No wait for key press in the end.
    -no_show                   No show processed video.
    -pc                        Enables per-layer performance report.
    -r                         Inference results as raw values.
    -t                         Probability threshold for detections.The interactive face detection application will use three models:
交互式人脸检测应用程序使用三个模型
- 训练后人脸检测模型的 .xml 文件路径
- 训练后年龄和性别模型的 .xml 文件路径
- 训练后情绪识别模型的 .xml 文件路径
该应用程序有两个 -i 选项:网络摄像头或视频文件路径。
使用网络摄像头运行应用程序
- 	将网络摄像头插入 UP Squared 板的 USB 端口,打开 Ubuntu 控制台并列出视频设备。 ls -ltrh /dev/video* 
- 	如果系统上没有 /dev/video 文件,请确保网络摄像头已插入 USB。 crw-rw----+ 1 root video 81, 0 Sep 27 12:48 /dev/video0 
- 	使用摄像头运行情绪识别应用程序。 ./interactive_face_detection_sample -d CPU -d_em CPU -dyn_em -i cam -m "$ROOT_DIR/intel_models/face-detection-adas-0001/FP32/face-detection-adas-0001.xml" -m_ag "$ROOT_DIR/intel_models/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013.xml" -m_em "$ROOT_DIR/intel_models/emotions-recognition-retail-0003/FP32/emotions-recognition-retail-0003.xml" 
使用视频文件路径运行应用程序
该应用程序接受预录制视频文件。本示例使用 预录制视频。
./interactive_face_detection_sample -d CPU -d_em CPU -dyn_em -i /home/upsquared/head-pose-face-detection-female-and-male.mp4 -m "$ROOT_DIR/intel_models/face-detection-adas-0001/FP32/face-detection-adas-0001.xml" -m_ag "$ROOT_DIR/intel_models/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013.xml" -m_em "$ROOT_DIR/intel_models/emotions-recognition-retail-0003/FP32/emotions-recognition-retail-0003.xml"
解释检测结果
该应用程序使用 OpenCV* 显示网络摄像头或预录制视频的视觉结果。视觉结果包括
- 每个检测到的人脸周围的边界框
- 边界框顶部的文本指示每个检测到的人脸的性别、年龄和情绪信息,例如中性、快乐、悲伤、惊讶或愤怒
- 渲染图像、人脸、年龄、性别和情绪所需的时间

浮点误差
CPU 只能运行单精度浮点数(FP32)模型。
错误:插件不支持 F16
如果发生错误,请更改模型路径以指向 FP32。
运行 Intel 演示脚本
分类和安全屏障摄像头演示脚本位于 /opt/intel/ computer_vision_sdk_2018.3.343/deployment_tools/demo。请参阅 README.txt 以获取有关如何使用演示脚本和创建自己的年龄、性别和情绪演示脚本的说明。
摘要
本教程介绍了如何运行使用 Inference Engine 模型的年龄、性别和情绪识别 Intel 模型。尝试在 UP Squared* 板上运行 /opt/intel/ computer_vision_sdk_2018.3.343/deployment_tools/intel_models 中提供的其他预训练模型。
主要参考资料
- Intel® Developer Zone (Intel® DZ)
- UP Squared*
- 英特尔® OpenVINO™ 工具包发行版
- Intel Distribution of OpenVINO toolkit 发行说明
- Intel Distribution of OpenVINO toolkit 论坛
- 推理引擎
关于作者
Nancy Le 是 Intel 公司 Core & Visual Computing Group 的一名软件工程师,致力于为物联网或 IoT 项目提供 Intel Atom® 处理器支持。



