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

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.0if 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 ospath = '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.hqwc.cn/news/234541.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

深入了解Java8新特性-日期时间API之ChronoUnit、ChronoField

阅读建议 嗨,伙计!刷到这篇文章咱们就是有缘人,在阅读这篇文章前我有一些建议: 本篇文章大概3000多字,预计阅读时间长需要5分钟。本篇文章的实战性、理论性较强,是一篇质量分数较高的技术干货文章&#x…

Python (十六) 错误和异常

程序员的公众号:源1024,获取更多资料,无加密无套路! 最近整理了一波电子书籍资料,包含《Effective Java中文版 第2版》《深入JAVA虚拟机》,《重构改善既有代码设计》,《MySQL高性能-第3版》&…

【李肯C语言小册.必读】为什么有这份专栏?解决什么问题?有哪些价值?是否值得订阅?

文末有惊喜...... 一、李肯的自我介绍 【昵称】架构师李肯 【坐标】深圳 【职业】主业中厂物联网架构师,副业技术自媒体 【个人标签】 专注于嵌入式物联网超10年的系统架构师 国产操作系统RT-Thread技术社区专家、2022年度优秀布道师 CSDN深圳城市开发者社区主…

软件测试简历怎么写?可以参考这份简历

个人简历 基本信息 姓名:名字 性别:男 年龄:25 学历:本科 联系电话&#xff1a…

滚珠丝杆在各种自动化设备中的作用

滚珠丝杆因其具有高精度、高刚度和长寿命等特性,成为许多设备中的重要组成部分,在许多行业中都有广泛的应用,接下来我们看看滚珠丝杆的具体应用有哪些? 1、打孔机:提供精确的导向,使打孔机的滑块能够沿固定…

Python基础语法之学习字符串快速格式化

Python基础语法之学习字符串快速格式化 一、代码二、效果 一、代码 # 通过f"{占位}"控制字符串快速格式化,不做精度控制 name "张三" age 13 money 12.5 text f"姓名是{name},年龄是{age},钱是{money}" print(text)二、效果 每一天都是一个…

阿里云服务器部署node和npm

目录 1.链接服务器2.找到node 下载地址3获取链接地址4下载到linux5.解压6.重命名 解压后的文件7.配置环境变量7.1复制当前的bin目录7.2vim /etc/profile7.3在按下ESC按键 8.重启环境变量9.输入node10.npm配置加速镜像 1.链接服务器 2.找到node 下载地址 https://nodejs.org/d…

【WP】Geek Challenge 2023 web 部分wp

EzHttp http协议基础题 unsign 简单反序列化题 n00b_Upload 很简单的文件上传&#xff0c;上传1.php&#xff0c;抓包&#xff0c;发现php内容被过滤了&#xff0c;改为<? eval($_POST[‘a’]);?>&#xff0c;上传成功&#xff0c;命令执行读取就好了 easy_php …

商家门店小程序怎么做?门店小程序的优势和好处

生活服务类商家在当前数字化时代&#xff0c;越来越认识到门店小程序的重要性。门店小程序不仅为商家提供了一个在线展示的窗口&#xff0c;更为其打造了一个与消费者直接互动的平台。有了门店小程序&#xff0c;商家可以更加便捷地管理商品信息、订单流程&#xff0c;同时还能…

基于YOLOv8的道路缺陷检测:自研模块 MSAM 注意力 PK CBAM注意力,实现暴力涨点

&#x1f4a1;&#x1f4a1;&#x1f4a1;本文自研创新改进&#xff1a;MSAM&#xff08;CBAM升级版&#xff09;&#xff1a;通道注意力具备多尺度性能&#xff0c;多分支深度卷积更好的提取多尺度特征&#xff0c;最后高效结合空间注意力 1&#xff09;作为注意力MSAM使用&am…

高等数学积分关系定理(格林公式、高斯公式、斯托克斯公式)的理解

1 格林公式、高斯公式、斯托克斯公式 1.1 格林公式&#xff08;Green formula&#xff09; 1.1.1 格林公式例题 1.2 高斯公式&#xff08;Gauss formula&#xff09; 1.2.1 高斯公式例题1 1.2.2 高斯公式例题2 1.3 斯托克斯公式&#xff08;Stokes formula&#xff09; 1.3.1 …

石油化工隐蔽设备AR可视化检修协助系统让新手也能轻松上岗

随着城市基础设施建设的不断推进&#xff0c;地下管线巡检工作的重要性日益凸显。传统的巡检方法已无法满足现代都市的高效运营需求。此时&#xff0c;地下管线AR智慧巡检远程协助系统应运而生&#xff0c;凭借其独特的特点与优势&#xff0c;为城市地下管线巡检带来了革命性的…