openvino系列教程之人脸检测 mobilenetv2

news/2024/7/10 0:24:21 标签: openvino, 人工智能, 目标检测

        OpenVINO(开放式视觉推理和神经网络优化)是英特尔推出的一款用于加速计算机视觉应用开发的软件。它基于英特尔的深度学习技术,提供了一套完整的工具链,包括模型优化器、运行时库等,帮助开发者快速实现高性能的计算机视觉算法。OpenVINO支持多种硬件平台,如CPU、GPU、FPGA等,可以广泛应用于智能安防、工业检测、无人驾驶等领域。通过使用OpenVINO,开发者可以轻松地将深度学习模型部署到各种设备上,实现高效、低功耗的计算机视觉应用。

一、python环境安装

conda create -n vino2021 python=3.8 -y
conda activate vino2021
pip install opencv-python==4.5.4.58
pip install openvino==2021.4.1  # 建议最好使用这个版本

为什么这里建议openvino使用版本和本文一致?因为openvino向上向下兼容性很一般。

二、openvino推理流程简介

一般地,模型推理包含三大步骤:

  • 图像预处理
  • 推理
  • 后处理

        openvino也遵从上面步骤流程。其中,图像预处理可能包含:图像resize、将BGR转成RGB次序、将CHW转成HWC等等。这些工作,使用opencv可以快速实现。例如:

import cv2
src = cv2.imread("d:/Data/15.jpg")
src_ = cv2.cvtColor(src, cv2.COLOR_BGR2RGB) # 将BGR转成RGB次序 
image = cv2.resize(src, (256, 256)) # 图像resize
image = image.transpose(2, 0, 1) # 将CHW转成HWC

        至于模型推理,其实不管是openvino,还是tensorrt,或者是onnxruntime等,都有推理引擎组件,使用的时候是需要使用模型将推理引擎初始化就OK;然后往引擎里面塞入图像数据就行了。这里举个例子:

# 读取模型
model_xml = "data/face-detection-0200.xml"
model_bin = "data/face-detection-0200.bin"
net = ie.read_network(model=model_xml)
# 加载模型到CPU中
exec_net = ie.load_network(network=net, device_name="CPU")
# 推理(这里相当于将image塞进推理引擎了)
res = exec_net.infer(inputs={input_blob: [image]})

        最后,推理引擎会输出特征图,也就是推理结果,我们写一个后处理代码就行了。例如:对于目标检测,我需要手写NMS;对于分割,我们需要手写上采样。

res = res[output_blob]
dets = res.reshape(-1, 7)
sh, sw, _ = src.shape
for det in dets:
    conf = det[2]
    if conf > 0.5:
        # calss_id...
        xmin = int(det[3] * sw)
        ymin = int(det[4] * sh)
        xmax = int(det[5] * sw)
        ymax = int(det[6] * sh)

        本文openvino的使用套路其实很固定,上文说道模型推理一般是三大步骤,这里openvino推理给细化成8个步骤。openvino一般完整代码步骤如下:

  • Step1:初始化推理引擎
  • Step2:从xml文件读取模型网络,从bin文件读取模型参数;或者直接从onnx文件同时读取模               型和参数
  • Step3:配置网络的输出、输入(图像预处理)
  • Step4:加载模型到设备
  • Step5:创建推理请求
  • Step6:准备输入
  • Step7:推理
  • Step8:后处理

        上述8个步骤,看似有点啰嗦,其实一般其中几个步骤就够了,以下给一个例子,可以参考下(看看就行不必执行):

import cv2
from openvino.inference_engine import IECore
import numpy as np
from timeit import default_timer as timer

# ---------------------------Step 1. Initialize inference engine core--------------------------------------------------
ie = IECore()
device = "CPU"
# ---------------------------Step 2. Read a model in OpenVINO Intermediate Representation or ONNX format---------------
model_xml = "data/face-detection-0200.xml"
model_bin = "data/face-detection-0200.bin"
net = ie.read_network(model=model_xml)
# ---------------------------Step 3. Configure input & output----------------------------------------------------------
input_blob = next(iter(net.input_info))
output_blob = next(iter(net.outputs))
n, c, h, w = net.inputs[input_blob].shape
print("outputs's shape = ", net.outputs[output_blob].shape)

src = cv2.imread("d:/Data/6.jpg")
#src_ = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
image = cv2.resize(src, (w, h))
image = image.transpose(2, 0, 1)
# ---------------------------Step 4. Loading model to the device-------------------------------------------------------
exec_net = ie.load_network(network=net, device_name=device)
# ---------------------------Step 5. Create infer request--------------------------------------------------------------
# ---------------------------Step 6. Prepare input---------------------------------------------------------------------
# ---------------------------Step 7. Do inference----------------------------------------------------------------------
tic = timer()
res = exec_net.infer(inputs={input_blob: [image]})
toc = timer()
print("the cost time is(ms): ", 1000*(toc - tic))
print("the latance is:", exec_net.requests[0].latency)
# ---------------------------Step 8. Process output--------------------------------------------------------------------

以上步骤范式比较固定,部署别的模型,你会发现很多代码都一样,复制粘贴而已,需要改的无非就是模型的输入和后处理。

三、人脸检测网络

        这个人脸检测模型backbone是mobilev2,人脸检测头是SSD目标检测的head,在此模型的训练期间,训练图像的大小调整为 256x256。上一节我们知道,模型部署只需要三步:图像预处理、推理、后处理;由于推理openvino帮咱们干了,咱们只需要写好模型输入和输出就行了。

输入信息

        在模型文件中,输入的名称为: `input`, 输入图像的shape为: `1, 3, 256, 256` 输入图像次序为 `B, C, H, W`, 其中:

  • `B` - batch size
  • `C` - 图像通道数,一般为3
  • `H` - image height
  • `W` - image width

输入图像的次序为: `BGR`.

输出信息

        网络输出特征图的shape为: `1, 1, 200, 7`,其中200表示候选目标数量.每一个候选目标是一个7维的向量,存储顺序为: [`image_id`, `label`, `conf`, `x_min`, `y_min`, `x_max`, `y_max`], 其中:

  • `image_id` - 图像在这个batch中的ID,不用管,因为本文是单batch推理
  •  `label` - 预测的类别ID(0 - face)
  •  `conf` - 置信度
  •  (`x_min`, `y_min`) - 矩形bbox左上角的点坐标
  •  (`x_max`, `y_max`) - 矩形bbox右下角的点坐标

四、源码测试

下面代码中,需要三个文件:输入图像,模型xml、bin文件,下载方法在文末。

import cv2
from openvino.inference_engine import IECore
import numpy as np
from timeit import default_timer as timer

# ---------------------------Step 1. Initialize inference engine core--------------------------------------------------
ie = IECore()
device = "CPU"
# ---------------------------Step 2. Read a model in OpenVINO Intermediate Representation or ONNX format---------------
model_xml = "data/face-detection-0200.xml"
model_bin = "data/face-detection-0200.bin"
net = ie.read_network(model=model_xml)
# ---------------------------Step 3. Configure input & output----------------------------------------------------------
input_blob = next(iter(net.input_info))
output_blob = next(iter(net.outputs))
n, c, h, w = net.inputs[input_blob].shape
print("outputs's shape = ", net.outputs[output_blob].shape)

src = cv2.imread("d:/Data/15.jpg")
#src_ = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
image = cv2.resize(src, (w, h))
image = image.transpose(2, 0, 1)
# ---------------------------Step 4. Loading model to the device-------------------------------------------------------
exec_net = ie.load_network(network=net, device_name=device)
# ---------------------------Step 5. Create infer request--------------------------------------------------------------
# ---------------------------Step 6. Prepare input---------------------------------------------------------------------
# ---------------------------Step 7. Do inference----------------------------------------------------------------------
tic = timer()
res = exec_net.infer(inputs={input_blob: [image]})
toc = timer()
print("the cost time is(ms): ", 1000*(toc - tic))
print("the latance is:", exec_net.requests[0].latency)
# ---------------------------Step 8. Process output--------------------------------------------------------------------
res = res[output_blob]
dets = res.reshape(-1, 7)
sh, sw, _ = src.shape
for det in dets:
    conf = det[2]
    if conf > 0.5:
        # calss_id...
        xmin = int(det[3] * sw)
        ymin = int(det[4] * sh)
        xmax = int(det[5] * sw)
        ymax = int(det[6] * sh)
        cv2.putText(src, str(round(conf, 3)), (xmin, ymin), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 1, 7)
        cv2.rectangle(src, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
cv2.imshow("src", src)
cv2.waitKey(0)
cv2.destroyAllWindows()

效果图如图:

模型数据+图片:链接:https://pan.baidu.com/s/1srtz0WUr9liwyTb55hZv_w?pwd=1234 
提取码:1234


http://www.niftyadmin.cn/n/5099946.html

相关文章

04. 模仿stm32驱动开发

04. 模仿stm32驱动开发 STM32寄存器结构体 STM32寄存器结构体 使用结构体将外设的所有寄存器都放到了一起,将这个结构体抽象为外设 start.s .global _start .global _bss_start _bss_start:.word __bss_start.global _bss_end _bss_end:.word __bss_end_start: // …

基于SSM+Vue的汽车服务商城系统

末尾获取源码 开发语言:Java Java开发工具:JDK1.8 后端框架:SSM 前端:Vue 数据库:MySQL5.7和Navicat管理工具结合 服务器:Tomcat8.5 开发软件:IDEA / Eclipse 是否Maven项目:是 目录…

蓝桥等考C++组别一级009

第一部分:选择题 1、C L1 (15分) C诞生在( )。 A.AT&T贝尔实验室 B.卡文迪什实验室 C.莫斯科大学的物理实验室 D.麻省理工学院的林肯实验室 正确答案:A 2、C L1 &…

前端项目架构

1.uniapp移动端APP/uniapp微信小程序 uniapp项目首先要搭建网络请求接口request和路由。 配置项目图标 手机打开开发者模式 手机下载基座调试 分包 如果是微信小程序,因为有项目大小限制,所以前期架构需要分包。 2.vue2后台管理系统 脚手架搭建完成…

sqlalchemy更新json 字段的部分字段

需求描述: 我们有个json字段,存储的数据形如下,现在需要修改love {"dob":"21","subject":{"love":"programming"}}工程结构 main.py from sqlalchemy import Column, String, Integer,c…

【LeetCode:2530. 执行 K 次操作后的最大分数 | 堆】

🚀 算法题 🚀 🌲 算法刷题专栏 | 面试必备算法 | 面试高频算法 🍀 🌲 越难的东西,越要努力坚持,因为它具有很高的价值,算法就是这样✨ 🌲 作者简介:硕风和炜,…

【前端学习】—函数防抖(十)

【前端学习】—函数防抖&#xff08;十&#xff09; 一、什么是函数防抖 函数防抖&#xff1a;事件被触发n秒后再执行回调&#xff0c;如果在这n秒内又被触发&#xff0c;则重新计时。 二、代码实现 <script>const searchElement document.getElementById("searc…

如何评估大语言模型是否可信?这里总结了七大维度

源自&#xff1a;机器之心发布 作者&#xff1a;刘扬&#xff0c;Kevin Yao 实际部署中&#xff0c;如何 “对齐”&#xff08;alignment&#xff09;大型语言模型&#xff08;LLM&#xff0c;Large Language Model&#xff09;&#xff0c;即让模型行为与人类意图相一致…