esp32-s3部署yolox_nano进行目标检测

news/2024/7/10 0:52:27 标签: 人工智能, 目标检测, 嵌入式硬件

ESP32-S3部署yolox_nano进行目标检测

      • 一、生成模型部署项目
        • 01 环境
        • 02 配置TVM包
        • 03 模型量化
          • 3.1预处理
          • 3.2 量化
        • 04 生成项目
      • 二、烧录程序

手上的是ESP32-S3-WROOM-1 N8R8芯片,整个链路跑通了,但是识别速度太慢了,20秒一张图,所以暂时还没打算进一步优化程序。
在这里插入图片描述

一、生成模型部署项目

官方指导文件:使用TVM自动生成模型部署项目

先下载onnx模型:yolox_nano.onnx,将下载好的yolox_nano.onnx放置在esp-dl/tutorial/evm_example路径下。

01 环境
  • ESP-IDF 5.0
  • 虚拟机Ubuntu 20.04
  • python环境
    在这里插入图片描述
02 配置TVM包

按官方文档下载完包后,设置环境变量PYTHONPATH

sudo vim ~/.bashrc
# 在文件的最后添加以下行,其中path-to-esp-dl更换为你的文件路径
export PYTHONPATH='$PYTHONPATH:/path-to-esp-dl/tools/tvm/python'
03 模型量化
3.1预处理
~/esp-dl $ cd tutorial/tvm_example
~/esp-dl/tutorial/tvm_example $ python -m onnxruntime.quantization.preprocess --input yolox_nano.onnx --output yolox_nano_opt.onnx
3.2 量化
  • 生成校准数据
import numpy as np
import cv2
import os

# 图片路径
path = 'esp-dl/img/calib'

# 读取图片并将它们保存为numpy数组
images = []
for filename in os.listdir(path):
    img = cv2.imread(os.path.join(path, filename))
    img_resized = cv2.resize(img, (416, 416))
    img_array = np.transpose(img_resized, (2, 0, 1))
    img_array = img_array / 255.0
    if img_array is not None:
        images.append(img_array)
        print(filename)

# 将numpy数组保存为npy文件
np.save('esp-dl/tutorial/tvm_example/calib_416x416.npy', images)
  • 生成模型输入
import numpy as np
import cv2
import os

path = 'esp-dl/img/input.jpg'

img = cv2.imread(path)
img_resized = cv2.resize(img, (416, 416))
img_array = np.transpose(img_resized, (2, 0, 1))
img_array = img_array / 255.0
images = [img_array]

np.save('esp-dl/tutorial/tvm_example/input_416x416.npy', images)
  • 生成量化后的模型
~/esp-dl/tutorial/tvm_example $ python ../../tools/tvm/esp_quantize_onnx.py --input_model yolox_nano_opt.onnx --output_model yolox_nano_quant.onnx --calibrate_dataset calib_416x416.npy
Collecting tensor data and making histogram ...
Finding optimal threshold for each tensor using entropy algorithm ...
Number of tensors : 365
Number of histogram bins : 128 (The number may increase depends on the data it collects)
Number of quantized bins : 128
WARNING:root:Please use QuantFormat.QDQ for activation type QInt8 and weight type QInt8. Or it will lead to bad performance on x64.
04 生成项目
~/esp-dl/tutorial/tvm_example $ python ../../tools/tvm/export_onnx_model.py --model_path yolox_nano_quant.onnx --img_path input_416x416.npy --target_chip esp32s3 --out_path "." --template_path "../../tools/tvm/template_project_for_model/"
Model Information:
------------------
Input Name: images
Input Shape: (1, 3, 416, 416)
Input DType: float
Output Name: output
Output Shape: (1, 3549, 85)
Output DType: float
[17:21:47] /home/gansichen/Workspace/projects/local/framework/tvm/src/relay/transforms/convert_layout.cc:99: Warning: Desired layout(s) not specified for op: nn.max_pool2d
[17:21:47] /home/gansichen/Workspace/projects/local/framework/tvm/src/relay/transforms/convert_layout.cc:99: Warning: Desired layout(s) not specified for op: nn.max_pool2d
[17:21:47] /home/gansichen/Workspace/projects/local/framework/tvm/src/relay/transforms/convert_layout.cc:99: Warning: Desired layout(s) not specified for op: nn.max_pool2d
[17:21:47] /home/gansichen/Workspace/projects/local/framework/tvm/src/relay/transforms/convert_layout.cc:99: Warning: Desired layout(s) not specified for op: image.resize2d
[17:21:47] /home/gansichen/Workspace/projects/local/framework/tvm/src/relay/transforms/convert_layout.cc:99: Warning: Desired layout(s) not specified for op: image.resize2d
esp_dl_library_path: /home/zymidea/Desktop/esp32-cam/esp-dl
generated project in: ./new_project

二、烧录程序

烧录用的windows系统,将虚拟机中生成的new_project文件夹复制到PC端,打开ESP-IDF CMD

cd new_preject
idf.py set-target esp32s3
idf.py flash monitor

这是按照官方的教程进行烧录,但是模型太大会出现内存溢出esp32-template-project.elf section '.dram0.bss' will not fit in region 'dram0_0_seg' region 'dram0_0_seg' overflowed by 2141320 bytes

~/new_project $ idf.py size-components
...
Total sizes:                                                                               
Used static IRAM:   61042 bytes ( 301198 remain, 16.9% used)                                    
	.text size:   60015 bytes                                                                  
	.vectors size:    1027 bytes                                                         
Used stat D/IRAM: 2442376 bytes (-2096520 remain, 706.2% used) Overflow detected!              
	.data size:   11088 bytes                                                                  
	.bss  size: 2431288 bytes                                                             
Used Flash size : 3729295 bytes                                                                
	.text     :  473467 bytes                                                                  
	.rodata   : 3255572 bytes                                                             
Total image size: 3801425 bytes (.bin may be padded larger) 

在这里插入图片描述

找到new_project/build/project_description.jsonlibtvm_model.a静态文件的源代码。
在这里插入图片描述

官方指导片外RAM

需要调整的是将模型的权重文件保存到flash并将模型的输出存放在PSRAM,操作如下

// 打开/new_project/components/tvm_model/model/codegen/host/src/default_lib0.c

// 代码最前面
// 增加一个头文件
#include "E:/Espressif/frameworks/esp-idf-v5.0.4/components/esp_common/include/esp_attr.h"

// static struct global_const_workspace 将static改为const
const struct global_const_workspace
    
// 代码最后面
// __attribute__((section(".bss.noinit.tvm"), aligned(16))) 将这句话注释掉
static EXT_RAM_BSS_ATTR uint8_t global_workspace[2422784]; // 增加宏EXT_RAM_BSS_ATTR
// 打开/new_project/main/output_data.h
const static _SECTION_ATTR_IMPL(".ext_ram.bss", __COUNTER__) __attribute__((aligned(16))) float output_data[42588] // 指定该数组存放到外部RAM的.ext_ram.bss段
~/new_project $ idf.py menuconfig

在这里插入图片描述
在这里插入图片描述
修改完毕S键保存,Esc键退出。

修改/new_project/partitions.csv分区表中的factory的大小,原本的3000多K存储模型权重不够,将其增大点,三个区的Offset都清空,生成过程它会自动匹配。

在这里插入图片描述

所有的修改完毕后再重新再看一下各个RAM的使用情况

~/new_project $ idf.py size-components
...
Used static IRAM:   61042 bytes ( 301198 remain, 16.9% used)
	.text size:   60015 bytes
	.vectors size:    1027 bytes
Used stat D/IRAM:   19592 bytes ( 326264 remain, 5.7% used) 
	.data size:   11088 bytes
	.bss  size:    8504 bytes 
Used Flash size : 3729203 bytes                                                                
	.text     :  473455 bytes                                                                  
	.rodata   : 3255492 bytes                                                             
Total image size: 3801333 bytes (.bin may be padded larger) 
...

在这里插入图片描述

最后重新烧录就能运行成功了。

~/new_project $ idf.py flash monitor

在这里插入图片描述


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

相关文章

Jenkins部署配置及Jenkinsfile流水线任务示例【无图版】

一、下载 JDK环境下载:https://repo.huaweicloud.com/java/jdk/11.0.113/jdk-11.0.1_linux-x64_bin.tar.gz Jenkins安装包下载:https://github.com/jenkinsci/jenkins/releases/tag/jenkins-2.375.3 二、Jdk/Java环境配置 解压 tar -zxvf jdk-11.0.…

传统家装“死气沉沉”?VR智慧家装提供VR可视化方案

传统家装市场虽然处于成熟期,但是对于装修小白的户主来说,难以解决的痛点依旧还有很多。很多家装公司所谓的设计师,不一定全都具备设计知识,也不懂得从客户的需求出发,多重因素导致家装行业“死气沉沉”。 为了打破装修…

Elasticsearch:使用 ILM 示例运行降采样 (downsampling)

如果你对降采样还不是很熟的话,请阅读之前的文章 “Elasticsearch:对时间序列数据流进行降采样(downsampling)”。这是一个简化的示例,可让你快速了解降采样如何作为 ILM 策略的一部分来减少一组采样指标的存储大小。 该示例使用典…

2023_Spark_实验二十三:Kafka的安装与基本操作

Kafka的安装与基本操作 一、前提工作 二、Kafka安装 三、Kafka基本操作 一、前提工作 必须安装了zookeeper 单机可参考:zookeeper单机安装与配置 集群可参考:zookeeper的集群安装 二、Kafka安装 上传kafka_2.11-2.4.1.tgz到/tools目录下 解压安装到…

网络和Linux网络_7(传输层)UDP和TCP协议(端口号+确认应答+超时重传+三次握手四次挥手)

目录 1. 重看端口号 1.1 端口号的概念 1.2 端口号的划分 2. 重看UDP协议 2.1 UDP协议格式 2.2 UDP的特点 3. 重看TCP协议 3.1 TCP协议格式 3.2 TCP的解包分用 3.3 TCP的可靠性及机制 3.3.1 确认应答ACK机制 3.3.2 超时重传机制 3.3.3 连接管理机制(三次…

中台战略思想与架构总结

中台战略思想与架构总结 在2015年年中,马云带领阿里高管,拜访了游戏公司Supercell,以《部落战争》《海岛奇兵》《卡通农场》等游戏知名。 Supercell是一家典型的以小团队模式进行游戏开发的公司,一般来说两个员工,或…

云计算生成式 -给你不一样的音乐推荐新体验

目录 摘要: 正文: 一、亚马逊云与生成式 AI 结合的展望/总结 二、我用亚马逊云科技生成式 AI 产品打造了什么,解决了什么问题 三、未来云端技术发展趋势的见解 四、云端技术未来需要解决的问题 1、如何保护数据安全和隐私? …

Linux:docker的网络通信(7)

1.端口映射 端口映射---端口映射机制将容器内的服务提供给外部网络访问 启动容器时,不指定对应的端口,在容器外无法通过网络访问容器内的服务 可随机或指定映射端口范围 -P ---------大写P,开启随机端口 -p 宿主机端口:容器端口…