将 openCV 与 openVINO 结合使用的正确方法是什么?



Dislcaimer:我以前从未使用过openCV或openVINO,甚至没有使用过任何接近ML的东西。然而,我一直在研究神经网络(在线阅读材料(,因为我必须在边缘设备上使用英特尔的 openVINO。 以下是官方文档关于将 openCV 与 openVINO 一起使用的内容(使用 openVINO 的推理引擎和 openCV(。

->使用openVINO的模型优化器优化预训练模型(创建IR文件对( 使用这些 IR 文件与

openCV's dnn.readnet() //this is where the inference engine gets set? 

https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_raspbian.html

尝试挖掘更多并找到第三方参考。这里采取了不同的方法。

->中间文件(未创建 bin/xml。而是使用咖啡模型文件(

->推理引擎使用以下行显式定义

net.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)

https://www.learnopencv.com/using-openvino-with-opencv/

现在我知道要利用openCV,我们必须将其推理引擎与预训练模型一起使用。我想知道这两种方法中哪一种是正确的(或首选的(,如果相反,我没有错过任何东西。

您可以从以下位置开始使用OpenVino: https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_windows.html

您需要一组先决条件才能运行示例。OpenCV是可用于图像处理的计算机视觉软件包。

Openvino 推理需要您将任何经过训练的模型(.caffemodel,.pb 等(转换为中间表示(.xml,.bin(文件。

为了更好地理解和样本演示OpenVino,请观看视频/订阅OpenVino Youtube频道:https://www.youtube.com/channel/UCkN8KINLvP1rMkL4trkNgTg

如果您使用的拓扑结构受 OpenVino 支持,那么最好的使用方法是 openvino 附带的 opencv。为此,您需要

1.通过运行 setupvars 来初始化 openvino 环境.bat在您的 openvino 路径(C:\Program Files (x86(\IntelSWTools\openvino\bin(

2.使用模型优化器为您的模型生成 IR 文件 (xml&bin(。

3.在路径/inference_engine_samples_build/中使用推理引擎示例运行

如果拓扑不受支持,则可以执行提到的其他过程。

我遇到的最常见问题:

  • setupvar.bat 必须在同一终端内运行,或使用 os.environ["varname"] = varvalue
  • OpenCV需要在支持推理引擎(即DLDT(的情况下构建。 这里有预构建的二进制文件:https://github.com/opencv/opencv/wiki/Intel%27s-Deep-Learning-Inference-Engine-backend
  • 目标推理引擎:net.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE(
  • Target
  • NCS2: net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD(

位于OpenVino目录中的OpenCV预构建二进制文件已经具有IE支持,也是一种选择。

请注意,Neural Compute Stick 2 AKA NCS2 (OpenVino IE/VPU/MYRIAD( 需要 FP16 模型格式 (float16(。 还要尽量保持这种格式的图像,以避免转换处罚。 您可以输入以下任何格式的图像:FP32、FP16、U8

我发现本指南很有帮助:https://learnopencv.com/using-openvino-with-opencv/

下面是一个针对 NCS2 从 https://medium.com/sclable/intel-openvino-with-opencv-f5ad03363a38 的示例:

# Load the model.
net = cv2.dnn.readNet(ARCH_FPATH, MODEL_FPATH)
# Specify target device.
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD) # NCS 2
# Read an image.
print("Processing input image...")
img = cv2.imread(IMG_FPATH)
if img is None:
raise Exception(f'Image not found here: {IMG_FPATH}')
# Prepare input blob and perform inference
blob = cv2.dnn.blobFromImage(img, size=(672, 384), ddepth=cv2.CV_8U)
net.setInput(blob)
out = net.forward()
# Draw detected faces
for detect in out.reshape(-1, 7):
conf = float(detect[2])
xmin = int(detect[3] * frame.shape[1])
ymin = int(detect[4] * frame.shape[0])
xmax = int(detect[5] * frame.shape[1])
ymax = int(detect[6] * frame.shape[0])

if conf > CONF_THRESH:
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), color=(0, 255, 0))

这里有更多示例(jupyter notebook/python(: https://github.com/sclable/openvino_opencv

相关内容

最新更新