Hailo-8系列——Hailo Dataflow Compiler使用经验分享

Hailo Dataflow Compiler用于将深度学习模型编译为能够在Hailo-8上运行的HEF文件。
安装需求:系统内存大于16G
为了便于安装可以采用在featurize中租用的方式安装文件

转换为HEF文件需要大概三步:
1、编写一个深度学习模型,转换为HAR文件
2、量化模型,通过加入代表数据集,转换为量化的HAR文件
3、编译转换为HEF文件

首先,更新,安装必要的环境

sudo apt-get update  # 避免报错
sudo apt-get install python3-dev graphviz libgraphviz-dev pkg-config 

然后,在安装hailo_dataflow_compiler

pip install  hailo_dataflow_compiler-3.27.0-py3-none-linux_x86_64.whl

安装成功后,根据手册编译一个模型

from hailo_sdk_client import ClientRunner

import tensorflow as tf

# Building a simple Keras model
def build_small_example_net():
    inputs = tf.keras.Input(shape=(24, 24, 96), name="img")
    x = tf.keras.layers.Conv2D(24, 1, name='conv1')(inputs)
    x = tf.keras.layers.BatchNormalization(momentum=0.9, name='bn1')(x)
    outputs = tf.keras.layers.ReLU(max_value=6.0, name='relu1')(x)
    model = tf.keras.Model(inputs, outputs, name="small_example_net")
    return model


# Converting the Model to tflite
model = build_small_example_net()
model_name = 'small_example'
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.target_spec.supported_ops = [
    tf.lite.OpsSet.TFLITE_BUILTINS,  # enable TensorFlow Lite ops.
    tf.lite.OpsSet.SELECT_TF_OPS  # enable TensorFlow ops.
]
tflite_model = converter.convert()  # may cause warnings in jupyter notebook, don't worry.

tflite_model_path = 'small_example.tflite'
with tf.io.gfile.GFile(tflite_model_path, 'wb') as f:
    f.write(tflite_model)


chosen_hw_arch = 'hailo8'
# Parsing the model to Hailo format
runner = ClientRunner(hw_arch=chosen_hw_arch)
hn, npz = runner.translate_tf_model(tflite_model_path, model_name)

hailo_model_har_name = f'{model_name}_hailo_model.har'
runner.save_har(hailo_model_har_name)

# 保存的数据可视化数据图
from IPython.display import SVG
!hailo visualizer {hailo_model_har_name} --no-browser

# 验证流程采用代表数据组
import numpy as np
calib_dataset=np.random.randint(low=0, high=10, size=(50,24, 24, 96))
np.save('calib_set.npy', calib_dataset)
runner.optimize(calib_dataset)

runner.optimize后显示的内容

[info] Starting Model Optimization
[warning] Reducing optimization level to 1 (the accuracy won't be optimized and compression won't be used) because there's less data than the recommended amount (1024)
[info] Model received quantization params from the hn
[info] Starting Mixed Precision
[info] Mixed Precision is done (completion time is 00:00:00.31)
[info] create_layer_norm skipped
[info] Starting Stats Collector
[info] Using dataset with 50 entries for calibration
Calibration: 100%|██████████| 50/50 [00:05<00:00,  9.34entries/s]
[info] Stats Collector is done (completion time is 00:00:05.47)

[info] No shifts available for layer small_example/conv1/conv_op, using max shift instead. delta=0.10578701765651832
[info] Starting Bias Correction
[info] The algorithm Bias Correction will use up to 0.02 GB of storage space
[info] Using dataset with 50 entries for Bias Correction
Bias Correction: 100%|██████████| 1/1 [00:02<00:00,  2.59s/blocks, Layers=['small_example/conv1_output_0']]
[info] Bias Correction is done (completion time is 00:00:02.89)
[info] Adaround skipped
[info] Fine Tune skipped

[info] Starting Layer Noise Analysis
Full Quant Analysis: 100%|██████████| 2/2 [00:03<00:00,  1.51s/iterations]
[info] Layer Noise Analysis is done (completion time is 00:00:03.77)
[info] Output layers signal-to-noise ratio (SNR): measures the quantization noise (higher is better)
[info] 	small_example/output_layer1 SNR:	47.17 dB
[info] Model Optimization is done

转换为量化的HAR文件

# Save the result state to a Quantized HAR file
quantized_model_har_path = f'{model_name}_quantized_model.har'
runner.save_har(quantized_model_har_path)

转换为HEF文件

from hailo_sdk_client import ClientRunner
runner = ClientRunner(har=quantized_model_har_path)

hef = runner.compile()

file_name = f'{model_name}.hef'
with open(file_name, 'wb') as f:
    f.write(hef)

模型分析

har_path = f'{model_name}_compiled_model.har'
runner.save_har(har_path)
!hailo profiler {har_path}

模型输出的信息

[info] Saved HAR to: /home/featurize/small_example_compiled_model.har
[info] Current Time: 00:52:07, 04/11/24
[info] CPU: Architecture: x86_64, Model: Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz, Number Of Cores: 6, Utilization: 0.7%
[info] Memory: Total: 25GB, Available: 22GB
[info] System info: OS: Linux, Kernel: 5.4.0-91-generic
[info] Hailo DFC Version: 3.27.0
[info] HailoRT Version: Not Installed
[info] PCIe: No Hailo PCIe device was found
[info] Running `hailo profiler small_example_compiled_model.har`
[info] Running profile for small_example in state compiled_model
[info] 
Model Details
--------------------------------  ----------
Input Tensors Shapes              24x24x96
Operations per Input Tensor       0.00 GOPs
Operations per Input Tensor       0.00 GMACs
Pure Operations per Input Tensor  0.00 GOPs
Pure Operations per Input Tensor  0.00 GMACs
Model Parameters                  0.01 M
--------------------------------  ----------

Profiler Input Settings
-----------------  -----------------
Optimization Goal  Reach Highest FPS
Profiler Mode      Compiled
-----------------  -----------------

Performance Summary
----------------------  --------------------
Number of Devices       1
Number of Contexts      1
Throughput              57463.01 FPS
Latency                 0.02 ms
Operations per Second   152.52 GOP/s
MACs per Second         77.05 GMAC/s
Total Input Bandwidth   2.96 Gigabytes/sec
Total Output Bandwidth  757.57 Megabytes/sec
Context Switch Configs  N/A
----------------------  --------------------
[info] Saved Profiler HTML Report to: /home/featurize/small_example_compiled_model.html

输出文件如下所示
输出文件
后续在设备端运行的情况分析待续。。。
也可以参照之前的文章
Hailo-8算力卡初步测试分享
【Hailo-8算力卡推断测试】

<think>好的,我现在需要帮助用户解决关于在树莓派上使用Hailo模块的问题。用户提到他们参考了一篇关于模型训练与部署的博客,特别是涉及Hailo模块和YOLO模型的应用。首先,我需要确认Hailo模块的基本信息,比如它是什么,主要功能是什么,以及用户可能需要的步骤来配置和使用它。 用户的问题集中在使用教程、配置和示例上,所以回答需要结构清晰,分步骤说明。我应该先检查Hailo模块的官方文档,看看是否有针对树莓派的特定指南。通常,这类硬件模块需要安装驱动、配置环境,然后运行示例代码。同时,用户提到的博客可能已经提供了一些步骤,但可能需要更详细的说明。 接下来,我需要考虑可能的依赖项,比如操作系统版本、Python环境、必要的库等。例如,Hailo模块可能需要特定的Linux内核版本或Python包。此外,硬件连接部分也很重要,比如如何将Hailo模块连接到树莓派的GPIO引脚,是否需要额外的电源或适配器。 然后是关于模型部署的部分。用户提到了YOLO模型,可能需要将训练好的模型转换为Hailo支持的格式,比如Hailo的.hef文件。这里可能需要使用Hailo提供的工具链,如Hailo Model Zoo或Hailo Dataflow Compiler。需要详细说明转换步骤和如何在树莓派上加载和运行转换后的模型。 另外,用户可能会遇到常见问题,比如驱动安装失败、模型转换错误或运行时性能问题。我应该预见到这些情况,并在教程中提供故障排除的建议,例如检查日志文件、验证硬件连接、确保依赖项版本兼容等。 最后,生成的相关问题需要围绕用户可能的后续疑问,比如优化性能、与其他硬件的比较、应用案例等,这样可以帮助用户进一步探索Hailo模块的用途。 现在,我需要将这些思考整理成一个结构化的回答,确保每个步骤清晰,并引用用户提到的博客作为参考。同时,语言要简明,避免技术术语过于复杂,让用户能够逐步跟随操作。</think>### 树莓派Hailo模块使用教程与配置示例 #### 1. **环境准备** - **硬件要求**: - 树莓派5B+ (建议使用4B及以上版本) - Hailo AI加速模块(如Hailo-8- 兼容的摄像头模块(如Raspberry Pi Camera V2) - **系统要求**: - Raspberry Pi OS (64位版本) - Python 3.7+ - 安装依赖项: ```bash sudo apt-get install -y libhailort hailortcli ``` #### 2. **硬件连接** -Hailo模块通过PCIe接口或USB3.0适配器连接至树莓派[^1]。 - 验证硬件识别: ```bash lsusb | grep Hailo # 若使用USB接口 lspci | grep Hailo # 若使用PCIe接口 ``` #### 3. **模型转换与部署** - **步骤1**: 将YOLO模型转换为Hailo格式(.hef) 使用Hailo工具链(需在x86主机操作): ```bash hailo-convert yolov5s.onnx --output yolov5s.hef ``` - **步骤2**: 将.hef文件传输至树莓派 - **步骤3**: 编写推理代码示例(Python): ```python import hailort import cv2 # 初始化Hailo设备 device = hailort.VDevice() model = device.load_model("yolov5s.hef") # 图像预处理与推理 image = cv2.imread("test.jpg") input_data = preprocess(image) # 自定义预处理函数 outputs = model.infer(input_data) results = postprocess(outputs) # 自定义后处理函数 ``` #### 4. **性能优化技巧** - 启用多线程推理: ```python model.set_thread_count(4) # 根据CPU核心数调整 ``` - 使用量化技术减少模型大小 - 通过`hailortcli`监控资源占用: ```bash hailortcli monitor ``` #### 5. **验证与调试** - 运行官方示例测试硬件兼容性: ```bash git clone https://github.com/hailo-ai/tappas cd tappas && ./install.sh ``` - 查看日志文件定位错误: ```bash journalctl -u hailort.service ```
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值