AI 开放平台

提供适配不同硬件平台的AI模型,探索更多端侧场景应用

基于此芯P1部署CenterFace

此芯: P1

此文档介绍如何使用 CIX P1 NPU SDK 将 CenterFace 转换为 CIX SOC NPU 上可以运行的模型。

整体来讲有四个步骤:
:::tip
步骤1~3 在 x86 主机 Linux 环境下执行
:::

  1. 下载 NPU SDK 并安装 NOE Compiler
  2. 下载模型文件 (代码和脚本)
  3. 编译模型
  4. 部署模型到 Orion O6

下载 NPU SDK 并安装 NOE Compiler

请参考 安装 NPU SDK 进行 NPU SDK 和 NOE Compiler 的安装.

下载模型文件

在 CIX AI Model Hub 中包含了 CenterFace 的所需文件, 请用户按照 CIX AI Model Hub 下载

cd ai_model_hub/models/ComputeVision/Face_Detection/onnx_centerface

请确认目录结构是否同下图所示。

├── centerface.cix
├── cfg
│   └── onnx_centerfacebuild.cfg # quantization config file
├── datasets
│   ├── calibration_data.npy
│   ├── data.npy
│   ├── input.bin
│   └── label.npy
├── graph.json
├── inference_npu.py   # inference on npu
├── inference_onnx.py  # inference on onnxruntime
├── model
│   ├── centerface.onnx
│   └── centerface_sim.onnx
├── out
├── ReadMe.md
└── test_data
    ├── test1.JPEG
    └── test2.JPEG

编译模型

:::tip
用户可无需从头编译模型,radxa 提供预编译好的 centerface.cix 模型(可用下面步骤下载),如果使用预编译好的模型,可以跳过“编译模型” 这一步

wget https://modelscope.cn/models/cix/ai_model_hub_24_Q4/resolve/master/models/ComputeVision/Face_Detection/onnx_centerface/centerface.cix

:::

准备 onnx 模型

  • 下载 onnx 模型

    centerface.onnx

  • 简化模型

    这里使用 onnxsim 进行模型输入固化和模型简化

    pip3 install onnxsim onnxruntime
    onnxsim centerface.onnx centerface-sim.onnx --overwrite-input-shape 1,3,640,640

编译模型

CIX SOC NPU 支持 INT8 计算,在编译模型前,我们需要使用 NOE Compiler 对模型进行 INT8 量化

  • 准备校准集

    • 使用 datasets 现有校准集

      .
      └── calibration_data.npy
    • 自行准备校准集

      test_data 目录下已经包含多张校准集的图片文件

      .
      ├── test1.jpeg
      └── test2.jpeg

      参考以下脚本生成校准文件

      import sys
      import os
      import numpy as np
      _abs_path = os.path.join(os.getcwd(), "../../../../")
      sys.path.append(_abs_path)
      from utils.image_process import preprocess_image_deeplabv3
      from utils.tools import get_file_list
      # Get a list of images from the provided path
      images_path = "test_data"
      images_list = get_file_list(images_path)
      data = []
      for image_path in images_list:
          input = preprocess_image_deeplabv3(image_path)
          data.append(input)
      # concat the data and save calib dataset
      data = np.concatenate(data, axis=0)
      np.save("datasets/calib_data_tmp.npy", data)
      print("Generate calib dataset success.")
  • 使用 NOE Compiler 量化与编译模型

    • 制作量化与编译 cfg 配置文件, 请参考以下配置

      [Common]
      mode = build
      
      [Parser]
      model_type = onnx
      model_name = centerface
      detection_postprocess = 
      model_domain = image_classification
      input_model = ./model/centerface.onnx
      input = input.1
      input_shape = [1, 3, 640, 640]
      output = 537,538,539,540
      output_dir = ./out
      
      [Optimizer]
      calibration_data = ./datasets/calibration_data.npy
      calibration_batch_size = 1
      data = ./datasets/data.npy
      label = ./datasets/label.npy
      metric_batch_size = 10
      output_dir = ./out
      dataset = widerfaceDataset
      metric = centerfaceMetric
      save_statistic_info = True
      cast_dtypes_for_lib = True
      
      [GBuilder]
      outputs = centerface.cix
      target = X2_1204MP3
      dot = graph.dot
      profile = True
      tiling = fps
    • 编译模型
      :::tip
      如果遇到 cixbuild 报错 [E] Optimizing model failed! CUDA error: no kernel image is available for execution on the device ...
      这意味着当前版本的 torch 不支持此 GPU,请完全卸载当前版本的 torch, 然后在 torch 官网下载最新版本。
      :::

      cixbuild ./onnx_centerfacebuild.cfg

模型部署

NPU 推理

将使用 NOE Compiler 编译好的 .cix 格式的模型复制到 Orion O6 开发板上进行模型验证

python3 inference_npu.py --images ./test_data/ --model_path ./centerface.cix
(.venv) radxa@orion-o6:~/ai_model_hub/models/ComputeVision/Face_Detection/onnx_centerface$ time python3 inference_npu.py --images ./test_data/ --model_path centerface.cix
npu: noe_init_context success
npu: noe_load_graph success
Input tensor count is 1.
Output tensor count is 4.
npu: noe_create_job success
npu times =  0:00:00.033083
npu times =  0:00:00.031629
npu: noe_clean_job success
npu: noe_unload_graph success
npu: noe_deinit_context success

real    0m3.034s
user    0m3.334s
sys     0m0.379s

结果保存在 output 文件夹中

centerface1.webp

CPU 推理

使用 CPU 对 onnx 模型进行推理验证正确性,可在 X86 主机上或 Orion O6 上运行

python3 inference_onnx.py --images ./test_data/ --model_path centerface_sim.onnx
(.venv) radxa@orion-o6:~/ai_model_hub/models/ComputeVision/Face_Detection/onnx_centerface$ time python3 inference_onnx.py --images ./test_data/ --model_path centerface_sim.onnx
cpu times =  0:00:00.188306
cpu times =  0:00:00.180590

real    0m3.433s
user    0m7.276s
sys     0m0.553s

结果保存在 output 文件夹中
centerface2.webp

可以看到 NPU 和 CPU 上推理的结果一致,但运行速度大幅缩短

参考文档

论文链接: CenterFace: Joint Face Detection and Alignment Using Face as Point