English | 简体中文
这些示例演示了如何使用 Intel® OpenVINOTM integration with Tensorflow 识别和检测在图像和视频中的对象。
- 分类演示使用谷歌 Inception v3 模型对指定的图像或视频进行分类。
- 对象检测演示使用从 Darknet 转换而来的 YOLOv4 模型检测指定图像、视频、图像目录或相机输入中的对象。
继续运行示例之前,必须将 openvino_tensorflow 仓库克隆到本地设备。此步骤请运行以下命令:
$ git clone https://github.com/openvinotoolkit/openvino_tensorflow.git
$ cd openvino_tensorflow
$ git submodule init
$ git submodule update --recursive在此示例中,我们假设您已经:
- 为系统安装了 TensorFlow
- 为系统安装了Intel® OpenVINOTM integration with Tensorflow
**注意:**有关使用TF1 Inception v3模型的python分类示例,请参阅此页。
请参阅this page 使用pip快速安装。
此演示使用TF Hub 图像分类模型 InceptionV3与可能用于分类的ImageNet标签.在标签文件中,您将找到Imagenet比赛中曾使用的1,000 个类别。
安装先决条件
$ cd <path-to-openvino_tensorflow-repository>/examples
$ pip3 install -r requirements.txt现在,您可以使用如下说明运行分类示例:
$ cd <path-to-openvino_tensorflow-repository>
$ python3 examples/classification_sample.pyclassification_sample.py 在image上进行推理,其输出类似于:
military uniform 0.79601693
mortarboard 0.02091024
academic gown 0.014557127
suit 0.009166162
comic book 0.007978318
注意:使用 --no_show 标志禁用应用程序显示窗口。 默认情况下,显示窗口是启用的。
在此示例中,我们使用 Admiral Grace Hopper 的图像。如您所见,网络准确发现她穿着军装,得到了 0.79 的高分。
下一步,通过将路径传递给新输入来尝试一下。 您可以提供图像或视频或图像目录的绝对或相对路径。 例如:
$ python3 examples/classification_sample.py --input=<absolute-or-relative-path-to-your-input>如果您将新图像或视频(如my_image.png或people-detection.mp4)添加到 openvino_tensorflow 仓库现有的 <path-to-openvino_tensorflow-repository>/examples/data 目录,将会是这样:
$ python3 examples/classification_sample.py --input=examples/data/my_image.png
或
$ python3 examples/classification_sample.py --input=examples/data/people-detection.mp4使用相机作为输入使用--input=0。 这里的“0”指的是 /dev/video0 中的摄像头。 如果相机连接到不同的端口,请适当更改。
如要查看各种后端(Intel® 硬件)的更多选项,请调用:
$ python3 examples/classification_sample.py --help在此示例中,我们假设您已经:
-
为系统安装了 TensorFlow
-
为系统安装了Intel® OpenVINOTM integration with Tensorflow
注意: 参考此页转换yolov4 darknet模型及其python对象检测示例。
使用pip快速安装,请参考此页。
安装先决条件
$ cd <path-to-openvino_tensorflow-repository>/examples
$ pip3 install -r requirements.txt本演示中使用的 TensorFlow Yolo v4 darknet模型由于其大小而未打包在存储库中。 因此请按照以下说明将 DarkNet 模型转换为 TensorFlow模型,并将标签和权重下载到“openvino_tensorflow 的克隆存储库”中的“<path-to-openvino_tensorflow-repository>/examples/data”目录:
For Linux and macOS
$ cd <path-to-openvino_tensorflow-repository>/examples
$ chmod +x convert_yolov4.sh
$ ./convert_yolov4.shFor Windows
cd <path-to-openvino_tensorflow-repository>\examples
convert_yolov4.bat完成后,<path-to-openvino_tensorflow-repository>/examples/data文件夹将包含运行对象检测示例所需的以下文件:
- coco.names
- yolo_v4
使用以下说明运行对象检测示例:
$ cd <path-to-openvino_tensorflow-repository>
$ python3 examples/object_detection_sample.py注意:
使用--no_show 标志禁用应用程序显示窗口。 默认情况下,显示窗口是启用的。、
使用--rename根据图像内容重命名输入图像或图像目录,例如noexiftags-1person-1uniform-intelOpenVINO.jpg**
使用此仓库附带的默认示例图像,并且 输出类似于下面的内容并将结果写入 detections.jpg:
在此示例中,我们使用 Admiral Grace Hopper 的默认图像。如您所见,网络检测到并准确绘制了人物边界框。
接下来,通过将路径传递给您的新输入,在您自己的图像上进行尝试。 您可以提供绝对或相对路径,例如:
$ python3 examples/object_detection_sample.py --input=<absolute-or-relative-path-to-your-image>如果您将新图像(如 my_image.png)添加到 openvino_tensorflow 存储库的现有 <path-to-openvino_tensorflow-repository>/examples/data 目录,将会是这样:
$ cd <path-to-openvino_tensorflow-repository>
$ python3 examples/object_detection_sample.py --input=examples/data/my_image.png如要查看各种后端(Intel® 硬件)的更多选项,请调用:
$ python3 examples/object_detection_sample.py --help接下来,通过将路径传递到您的输入视频,在您自己的视频文件上进行尝试。 您可以提供绝对或相对路径,例如
$ python3 examples/object_detection_sample.py --input=<absolute-or-relative-path-to-your-video-file>如果您将新视频(例如,examples/data/people-detection.mp4)添加到 openvino_tensorflow 存储库中现有的 <path-to-openvino_tensorflow-repository>/examples/data 目录,它将如下所示:
$ python3 examples/object_detection_sample.py --input=examples/data/people-detection.mp4使用相机作为输入使用--input=0。 这里的“0”指的是 /dev/video0 中的摄像头。 如果相机连接到不同的端口,请适当更改。
注意: 输入为图像或图像目录的结果将写入输出图像。 对于视频或相机输入,请使用应用程序显示窗口查看结果。
运行 C++ 示例时,我们需要通过从源代码构建 TensorFlow 框架,因为示例依赖 TensorFlow 库。
在从源代码开始构建之前,您必须确保已安装 先决条件。
本演示中使用的 TensorFlow 模型由于其大小而未打包在存储库中。 因此将模型下载到“openvino_tensorflow 的克隆存储库”中的“<path-to-openvino_tensorflow-repository>/examples/data”目录并提取文件:
- 下载模型
$ curl -L "https://storage.googleapis.com/download.tensorflow.org/models/inception_v3_2016_08_28_frozen.pb.tar.gz" |
tar -C <path-to-openvino_tensorflow-repository>/examples/data -xz-运行以下命令,构建 openvino_tensorflow 及示例:
$ cd <path-to-openvino_tensorflow-repository>
$ python3 build_tf.py --output_dir <path-to-tensorflow-dir>
$ python3 build_ovtf.py --use_tensorflow_from_location <path-to-tensorflow-dir>关于详细的构建说明,请参阅 BUILD.md。
现在,classification_sample 的二进制可执行文件已构建完成。更新 LD_LIBRARY_PATH 并运行示例:
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<path-to-openvino_tensorflow-repository>/build_cmake/artifacts/lib:<path-to-openvino_tensorflow-repository>/build_cmake/artifacts/tensorflow
$ ./build_cmake/examples/classification_sample/infer_image-
下载以下模型 "https://storage.googleapis.com/download.tensorflow.org/models/inception_v3_2016_08_28_frozen.pb.tar.gz" 解压并复制到 <path-to-openvino_tensorflow-repository>\examples\data
-
运行以下指令构建openvino_tensorflow及示例
cd <path-to-openvino_tensorflow-repository>
python build_ovtf.py --use_openvino_from_location="C:\Program Files (x86)\Intel\openvino_2022.3.0" --use_tensorflow_from_location="\path\to\directory\containing\tensorflow\"- 现在,classification_sample的二进制可执行文件已构建完成。更新PATH并运行示例:
set PATH=%PATH%;<path-to-openvino_tensorflow-repository>\build_cmake\artifacts\lib;<path-to-openvino_tensorflow-repository>\build_cmake\artifacts\tensorflow
build_cmake\examples\classification_sample\Release\infer_image.exe使用此仓库附带的默认示例图像,并且 输出类似于:
military uniform (653): 0.834306
mortarboard (668): 0.0218693
academic gown (401): 0.010358
pickelhaube (716): 0.00800814
bulletproof vest (466): 0.00535091
在此示例中,我们使用 Admiral Grace Hopper 的默认图像。如您 所见,网络准确发现她穿着军装,得到了 0.8 的高分。
接下来,通过将路径传递到新图像,在您自己的图像上进行尝试。 您可以提供绝对或相对路径,例如
$ ./build_cmake/examples/classification_sample/infer_image --image=<absolute-or-relative-path-to-your-image>如果您将新图像(如: my_image.png)添加到 openvino_tensorflow 仓库的现有<path-to-openvino_tensorflow-repository>/examples/data 目录,将会是这样:
$ ./build_cmake/examples/classification_sample/infer_image --image=examples/data/my_image.png如要查看各种后端(Intel® 硬件)的更多选项,请调用:
$ ./build_cmake/examples/classification_sample/infer_image --help注意: 在以上示例中,首先执行热身运行,然后在后续运行中测量推理时间。首个运行的执行时间一般比后面运行的时间长,因为首次运行包含很多一次图表转化和优化步骤。
