• openvino系列教程之人脸检测 mobilenetv2


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

    一、python环境安装

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

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

    二、openvino推理流程简介

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

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

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

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

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

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

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

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

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

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

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

    1. import cv2
    2. from openvino.inference_engine import IECore
    3. import numpy as np
    4. from timeit import default_timer as timer
    5. # ---------------------------Step 1. Initialize inference engine core--------------------------------------------------
    6. ie = IECore()
    7. device = "CPU"
    8. # ---------------------------Step 2. Read a model in OpenVINO Intermediate Representation or ONNX format---------------
    9. model_xml = "data/face-detection-0200.xml"
    10. model_bin = "data/face-detection-0200.bin"
    11. net = ie.read_network(model=model_xml)
    12. # ---------------------------Step 3. Configure input & output----------------------------------------------------------
    13. input_blob = next(iter(net.input_info))
    14. output_blob = next(iter(net.outputs))
    15. n, c, h, w = net.inputs[input_blob].shape
    16. print("outputs's shape = ", net.outputs[output_blob].shape)
    17. src = cv2.imread("d:/Data/6.jpg")
    18. #src_ = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
    19. image = cv2.resize(src, (w, h))
    20. image = image.transpose(2, 0, 1)
    21. # ---------------------------Step 4. Loading model to the device-------------------------------------------------------
    22. exec_net = ie.load_network(network=net, device_name=device)
    23. # ---------------------------Step 5. Create infer request--------------------------------------------------------------
    24. # ---------------------------Step 6. Prepare input---------------------------------------------------------------------
    25. # ---------------------------Step 7. Do inference----------------------------------------------------------------------
    26. tic = timer()
    27. res = exec_net.infer(inputs={input_blob: [image]})
    28. toc = timer()
    29. print("the cost time is(ms): ", 1000*(toc - tic))
    30. print("the latance is:", exec_net.requests[0].latency)
    31. # ---------------------------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文件,下载方法在文末。

    1. import cv2
    2. from openvino.inference_engine import IECore
    3. import numpy as np
    4. from timeit import default_timer as timer
    5. # ---------------------------Step 1. Initialize inference engine core--------------------------------------------------
    6. ie = IECore()
    7. device = "CPU"
    8. # ---------------------------Step 2. Read a model in OpenVINO Intermediate Representation or ONNX format---------------
    9. model_xml = "data/face-detection-0200.xml"
    10. model_bin = "data/face-detection-0200.bin"
    11. net = ie.read_network(model=model_xml)
    12. # ---------------------------Step 3. Configure input & output----------------------------------------------------------
    13. input_blob = next(iter(net.input_info))
    14. output_blob = next(iter(net.outputs))
    15. n, c, h, w = net.inputs[input_blob].shape
    16. print("outputs's shape = ", net.outputs[output_blob].shape)
    17. src = cv2.imread("d:/Data/15.jpg")
    18. #src_ = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
    19. image = cv2.resize(src, (w, h))
    20. image = image.transpose(2, 0, 1)
    21. # ---------------------------Step 4. Loading model to the device-------------------------------------------------------
    22. exec_net = ie.load_network(network=net, device_name=device)
    23. # ---------------------------Step 5. Create infer request--------------------------------------------------------------
    24. # ---------------------------Step 6. Prepare input---------------------------------------------------------------------
    25. # ---------------------------Step 7. Do inference----------------------------------------------------------------------
    26. tic = timer()
    27. res = exec_net.infer(inputs={input_blob: [image]})
    28. toc = timer()
    29. print("the cost time is(ms): ", 1000*(toc - tic))
    30. print("the latance is:", exec_net.requests[0].latency)
    31. # ---------------------------Step 8. Process output--------------------------------------------------------------------
    32. res = res[output_blob]
    33. dets = res.reshape(-1, 7)
    34. sh, sw, _ = src.shape
    35. for det in dets:
    36. conf = det[2]
    37. if conf > 0.5:
    38. # calss_id...
    39. xmin = int(det[3] * sw)
    40. ymin = int(det[4] * sh)
    41. xmax = int(det[5] * sw)
    42. ymax = int(det[6] * sh)
    43. cv2.putText(src, str(round(conf, 3)), (xmin, ymin), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 1, 7)
    44. cv2.rectangle(src, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
    45. cv2.imshow("src", src)
    46. cv2.waitKey(0)
    47. cv2.destroyAllWindows()

    效果图如图:

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

  • 相关阅读:
    微信小程序——Git版本管理
    结构化思维助力Prompt创作:专业化技术讲解和实践案例
    迅为iTOP-RK3588开发板多屏同显多屏异显异触
    Servlet概念和Tomcat的安装配置
    Siri怎么重置主人声音
    Android中focusableInTouchMode会导致第一次点击事件失效
    C++ 模板泛型编程
    手把手带你编写你的第一个单元测试
    velero 迁移k8s集群资源
    【解读】基于PREEvision的诊断设计
  • 原文地址:https://blog.csdn.net/m0_72734364/article/details/133903464