401 lines
21 KiB
Markdown
401 lines
21 KiB
Markdown
# ResNetSE
|
||
 <br> <br><br><br><a href="../../index.md#audio" target="_blank"><img alt="模型清单" src="https://img.shields.io/badge/audio-模型清单-cornflowerblue?logo=quicklook"></a><br>
|
||
|
||
# 下载
|
||
|
||
✨ 一键下载开发流程中所需的各种文件,包括编译使用的量化校准集、运行时工程的依赖库,以及输入输出文件。
|
||
|
||
💡 推荐使用linux版下载脚本,其wget包含断网自动重连功能,不会出现下载文件遗漏情况。
|
||
|
||
## windows
|
||
📌 第一次使用,请在C盘根目录下新建`icraft_auth.txt`,保存下载站账号密码,以换行符分隔
|
||
|
||
需要事先下载windows版本wget:
|
||
|
||
(若点击以下链接后未直接下载,请选择 ***1.20.3*** 版本下的对应系统链接进行下载)
|
||
|
||
[x86系统wget下载](https://eternallybored.org/misc/wget/1.20.3/32/wget.exe) [x64系统wget下载](https://eternallybored.org/misc/wget/1.20.3/64/wget.exe)
|
||
|
||
使用时需要将wget.exe的路径作为命令行参数传入,注意不是exe的父文件夹目录,而是包含wget.exe的完整绝对路径:
|
||
|
||
不下载Deps:`./download.ps1 "PATH_TO_WGET_EXE"`
|
||
|
||
如果您是第一次使用我们的模型库,请下载包括工程依赖库的所有文件:`./download.ps1 "PATH_TO_WGET_EXE" -d`
|
||
|
||
💡 下载过程中可能因网络问题出现中断情况,需 **自行重新运行** 下载脚本。
|
||
|
||
## linux
|
||
|
||
📌 第一次使用,请在/usr根目录下新建`icraft_auth.txt`,保存下载站账号密码,以换行符分隔
|
||
|
||
为确保文件格式正确,请在运行脚本前安装格式转换工具`dos2unix`,并执行格式转换命令:
|
||
```shell
|
||
sudo apt-get install dos2unix
|
||
dos2unix /usr/icraft_auth.txt
|
||
dos2unix ./download.sh
|
||
```
|
||
|
||
如果您是第一次使用我们的模型库,请下载包括工程依赖库的所有文件:`./download.sh -d`
|
||
|
||
如果之前已经在使用别的模型时下载过Deps依赖库,可以直接将其中的thirdparty部分复制到路径`3_deploy/Deps`,只需下载量化校准集和输入输出文件即可:`./download.sh`
|
||
|
||
|
||
🌟 Tips:
|
||
|
||
- 若想要直接获取原始weights和导出保存的模型,可分别前往 [weights](https://download.fdwxhb.com/data/04_FMSH-100AI/100AI/04_modelzoo/modelzoo_pub/weights/) 和 [fmodels](https://download.fdwxhb.com/data/04_FMSH-100AI/100AI/04_modelzoo/modelzoo_pub/compile/fmodels/) 网页上根据框架及模型名寻找并下载。
|
||
|
||
|
||
# 0. 文件结构说明
|
||
|
||
AI部署模型需要以下几部分文件
|
||
|
||
- 0_ResNetSE >模型原始工程,需要自行下载
|
||
- weights >存放原始权重,需要自行下载
|
||
- 1_scripts >若干脚本,用于保存Icraft编译器需要的模型、编译后仿真等功能
|
||
- 2_compile >Icraft编译器编译模型时所需要的文件
|
||
- 3_deploy >将Icraft编译器编译出的模型部署到硬件时需要的c++工程
|
||
|
||
# 1. python工程准备
|
||
|
||
## 1. **模型来源:**
|
||
|
||
- code:https://github.com/yeyupiaoling/AudioClassification-Pytorch.git
|
||
- branch:release/0.4.7
|
||
- commit_id:01310a5
|
||
- weights:https://download.fdwxhb.com/data/04_FMSH-100AI/100AI/04_modelzoo/modelzoo_pub/weights/pytorch/ResNetSE/ResNetSE_Fbank/best_model/model.pth
|
||
|
||
## 2. **保存模型**
|
||
|
||
**目的:将模型保存成可以被Icraft编译器编译的形态**
|
||
|
||
1)根据模型来源中的地址:https://download.fdwxhb.com/data/04_FMSH-100AI/100AI/04_modelzoo/modelzoo_pub/weights/pytorch/ResNetSE/ResNetSE_Fbank/best_model/model.pth ,下载原始weights,存放于 `/weights`文件夹中
|
||
|
||
<div style="background-color: #FFFFCC; color: #000000; padding: 10px; border-left: 5px solid #FFA500;">
|
||
注意:
|
||
|
||
* 有时开源的weights url可能会变更。如果上述weights url失效,请根据原工程相应的branch以及commit版本寻找正确的下载链接
|
||
* 若上述weights url永久失效,请联系本模型库相关人员获取权限下载
|
||
</div>
|
||
|
||
2)根据模型来源中的地址,下载指定commit id版本的源代码,文件夹名称要设置为:0_ResNetSE
|
||
|
||
```shell
|
||
# 在此模型根目录
|
||
mkdir 0_ResNetSE
|
||
git clone -b release/0.4.7 https://github.com/yeyupiaoling/AudioClassification-Pytorch.git 0_ResNetSE
|
||
cd 0_ResNetSE
|
||
git checkout 01310a5
|
||
```
|
||
|
||
3)进入1_scripts执行保存模型脚本
|
||
|
||
```shell
|
||
# 在此模型根目录
|
||
cd 1_scripts
|
||
python 1_save.py
|
||
```
|
||
|
||
**1_scripts提供脚本说明:**
|
||
|
||
- **环境要求:**Icraft编译器对**导出框架模型时**使用的**框架版本**有要求。即以下脚本中所有导出模型的脚本 `1_save.py `,必须在要求的框架版本下执行,其他脚本不限制。要求的版本:
|
||
|
||
- **pytorch**:支持pytorch1.9.0、pytorch2.0.1两个版本的原生网络模型文件(.pt格式),以及pytorch框架保存为onnx(opset=17)格式的模型文件(.onnx格式)
|
||
- **paddle**:仅支持PaddlePaddle框架保存为onnx(opset=11)格式的模型文件(.onnx格式),不支持框架原生网络模型文件
|
||
- **darknet**:支持Darknet框架原生网络模型[GitHub - pjreddie/darknet: Convolutional Neural Networks](https://github.com/pjreddie/darknet)
|
||
- 0_infer.py >可以推理一段音频并得到最终结果,模型原始权重会从 `/weights `中寻找,需要您预先下载
|
||
- 1_save.py >保存模型,保存好的用于Icraft编译器的模型,会存放在 `/2_compile/fmodel`
|
||
|
||
- 2_save_infer.py >用修改后保存的模型做前向推理,验证保存的模型与原模型是否一致
|
||
|
||
# 2.使用Icraft编译器编译模型
|
||
|
||
目的: 使用[Icraft编译器](https://gitee.com/mxh-spiger/icraft-introduction.git)将上一步保存好的**框架模型**转化为**硬件可部署模型**
|
||
|
||
- **1)相关命名说明:**
|
||
|
||
1)**fmodel**:frame model >用于Icraft编译器的框架模型
|
||
|
||
2)**imodel**:icraft model >用Icraft编译器编译出的模型
|
||
|
||
3)**qtset**:Quantitative Calibration Set >Icraft编译器所需的量化校准集
|
||
- **2)确认已安装正确的icraft版本**
|
||
|
||
检查方法:打开cmd运行:`icraft --version`
|
||
|
||
若已正常安装则会显示当前icraft版本,例如:
|
||
|
||
```
|
||
Icraft 版本:
|
||
* 3.7.1
|
||
|
||
CLI 版本:
|
||
3.7.0.0-a90988f(2412231401)
|
||
```
|
||
- 3)**执行编译:**
|
||
|
||
**在 `/2_compile`目录下执行编译:**
|
||
|
||
```shell
|
||
icraft compile config/ResNetSE_8.toml
|
||
```
|
||
|
||
如果过程顺利,将得到 icraft model(以 `.json` (graph)`.raw`(param)的格式保存)
|
||
|
||
其中包括编译各阶段产生的中间结果模型和最终用于片上部署的BY模型,直接被保存到: 3_deploy/modelzoo/ResNetSE/imodel
|
||
|
||
# 3. 仿真
|
||
|
||
通过配置3_deploy/modelzoo/ResNetSE/cfg/中yaml文件的sim字段为True实现模型仿真。
|
||
|
||
```json
|
||
imodel:
|
||
...
|
||
sim: true
|
||
...
|
||
```
|
||
|
||
# 4. 部署模型
|
||
|
||
## 部署环境检查
|
||
|
||
* 以root账户登录片上系统terminal(ssh或串口皆可),模型库默认的模型存放路径为以下目录,如果没有请预先创建:
|
||
|
||
```
|
||
/home/fmsh/ModelZoo/
|
||
```
|
||
|
||
* 检查板上环境是否正确:
|
||
1. 查看环境变量,指令:
|
||
`icraft --version`
|
||
|
||
看打印信息是否如下:
|
||
|
||
```shell
|
||
Icraft 版本:
|
||
* v3.7.1
|
||
|
||
CLI 版本:
|
||
3.7.0.0-a90988f(2412231401)
|
||
```
|
||
|
||
2. 若是,在任意目录下输入`icraft-serve`即可打开server
|
||
|
||
3. 检查icraft和customop安装包版本是否为`arm64`
|
||
|
||
```shell
|
||
# 检查icraft安装包版本
|
||
dpkg -l | grep icraft
|
||
# 检查customop安装包版本
|
||
dpkg -l | grep customop
|
||
```
|
||
|
||
如果依次显示如下信息,则安装版本正确:
|
||
|
||
```shell
|
||
ii icraft 3.7.1 arm64 This is Icraft for arm64
|
||
ii customop 3.7.1 arm64 This is Icraft CustomOp for arm64
|
||
```
|
||
|
||
4. 如果环境配置有误,请参考[Part 1_1 2.3.1 片上系统环境 编译环境准备](https://gitee.com/mxh-spiger/tutorial-runtime/blob/tt3.7.1/docs/Part%201_1%20quick-start.md#1%E7%BC%96%E8%AF%91%E7%8E%AF%E5%A2%83%E5%87%86%E5%A4%87-2)进行部署环境配置。
|
||
|
||
5. 根据此模型使用的硬算子,选择合适的位流,并在板上安装,所用硬算子及可选位流版本可参见本说明文档起始处的状态徽章,位流下载及安装说明请参考[1/4) 其他下载资源](https://gitee.com/mxh-spiger/icraft-introduction/tree/icraft_v3.7.1/#4%E5%85%B6%E4%BB%96%E4%B8%8B%E8%BD%BD%E8%B5%84%E6%BA%90)。
|
||
|
||
## python runtime:
|
||
|
||
|
||
目的:在AI硬件上执行模型前向推理
|
||
|
||
1. **python运行虚拟环境要求与准备**
|
||
|
||
- python版本:3.8(否则无法使用icraft的python API)
|
||
|
||
- 确保已安装icraft的python安装包
|
||
|
||
- socket模式使用:`pip install icraft-3.x.x-cp38-none-win_amd64.whl`
|
||
- axi模式使用:`pip install icraft-3.x.x-cp38-none-manylinux2014_aarch64.whl`
|
||
|
||
- 在选定的python运行虚拟环境中安装python运行时所需要的依赖包
|
||
|
||
```
|
||
cd 3_deploy/modelzoo/ResNetSE
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
- 安装一些便于开发pyrt的依赖包
|
||
|
||
```
|
||
cd 3_deploy/Deps/modelzoo
|
||
pip install -e .
|
||
```
|
||
|
||
<div style="background-color: #FFFFCC; color: #000000; padding: 10px; border-left: 5px solid #FFA500;">
|
||
由于python运行时代码在引入依赖包时候已经通过sys.path.append(R"../../../Deps/modelzoo")将一些便于开发pyrt的依赖包加入到了系统路径下,因此可以不用执行该步骤即可直接运行2.执行程序。这里提供额外的[pip install -e .]安装方式是为了在python运行时代码时候方便进行pyrtutils的依赖跳转。
|
||
|
||
2. **执行程序**
|
||
|
||
```
|
||
python ./ResNetSE_psin.py ../cfg/ResNetSE.yaml
|
||
```
|
||
|
||
在io/output中查看结果
|
||
|
||
|
||
|
||
# 5. 精度测试
|
||
|
||
<div style="background-color: #FFFFCC; color: #000000; padding: 10px; border-left: 5px solid #FFA500;">
|
||
由于精度测试需要遍历一个数据集中的所有音频,因此需要使用上位机作为主控操作系统(demo中是按windows作为上位机操作系统来做的),使用网口连接板子,运行时输入数据会通过上位机经由网口传到片上进行推理。
|
||
</div>
|
||
|
||
## 1. 环境准备
|
||
|
||
若已准备好环境请跳过此部分
|
||
|
||
**1. 网口调试环境准备**(如果已经准备好网口调试环境则看下一条)
|
||
|
||
1. 安装ssh
|
||
2. 查看或配置板子ip
|
||
|
||
- 使用串口连接板子
|
||
- `vim /etc/rc.local`
|
||
- 查看或设置ip
|
||
|
||
```
|
||
#!/bin/bash
|
||
ifconfig eth0 192.168.125.171 netmask 255.255.255.0
|
||
systemctl start sshd
|
||
```
|
||
3. 修改本地网络适配器配置
|
||
|
||
参考配置
|
||
|
||
- ipv4地址:`192.168.125.2`
|
||
- 子网掩码:`255.255.255.0`
|
||
- 默认网关:`192.168.125.1`
|
||
- 连接速度与双工:100mbps全双工
|
||
4. 使用网口或串口进入板上系统打开server
|
||
|
||
上位机:
|
||
|
||
```cmd
|
||
ssh root@192.168.125.171
|
||
```
|
||
|
||
板上:
|
||
确保位于root账户下,执行:
|
||
|
||
```shell
|
||
icraft-serve
|
||
```
|
||
|
||
* 请确保在root账户下执行上述命令
|
||
* 设备成功打开示意图
|
||
|
||
```
|
||
root@U:~# icraft-serve
|
||
[02/22/24 02:02:00.388] [I] Using port : 9981
|
||
[02/22/24 02:02:00.388] [I] synchronous mode
|
||
[02/22/24 02:02:00.388] [I] [irpc::port::tcp::_waitNewConn] wait for new connection
|
||
```
|
||
|
||
如果能正确运行则可以继续下一步。
|
||
|
||
**2.编译环境准备**
|
||
|
||
请参考[2.1Windows编译环境准备](https://gitee.com/mxh-spiger/tutorial-runtime/tree/rt3.0.0)进行精度测试环境配置。
|
||
|
||
- os: windows
|
||
- [cmake3.28](https://github.com/Kitware/CMake/releases/download/v3.28.1/cmake-3.28.1-windows-x86_64.msi)
|
||
- [visualstudio2022](https://visualstudio.microsoft.com/zh-hans/thank-you-downloading-visual-studio/?sku=Community&channel=Release&version=VS2022&source=VSLandingPage&passive=false&cid=2030)
|
||
|
||
## 2. 测试说明
|
||
|
||
1. **如果使用了detpost硬算子**(本模型未使用,请忽略此条)
|
||
|
||
- 1)修改customop toml
|
||
|
||
customop toml 即 config\customop\xxmodel.toml
|
||
|
||
需要将该文件中的 `thr_f`配置成0.001
|
||
|
||
```toml
|
||
[ImageMake]
|
||
no_imkpad = 0
|
||
|
||
[DetPost]
|
||
thr_f = 0.001 # 阈值
|
||
cmp_en = 1 # 是否做阈值比较
|
||
|
||
groups = 3
|
||
anchor_num = 3
|
||
position = 5
|
||
|
||
```
|
||
- 2)重新进行模型编译
|
||
|
||
```shell
|
||
icraft compile config/ResNetSE_8.toml
|
||
```
|
||
|
||
由于测精度程序由上位机控制,生成的imodel不用再复制到板子中
|
||
2. **数据集准备**
|
||
|
||
* 测试数据集下载:[UrbanSound8K](https://aistudio.baidu.com/datasetdetail/36625)
|
||
* 模型测试使用UrbanSound8K数据集的test部分,下载测试数据集请存放于 `\3_deploy\modelzoo\ResNetSE\pyrt\dataset\UrbanSound8K`
|
||
* `UrbanSound8K`数据集结构为:
|
||
```
|
||
├── UrbanSound8K
|
||
│ ├── FREESOUNDCREDITS.txt
|
||
│ ├── UrbanSound8K_README.txt
|
||
│ ├── audio
|
||
│ │ ├── fold1
|
||
│ │ ├── fold10
|
||
│ │ ├── fold2
|
||
│ │ ├── fold3
|
||
│ │ ├── fold4
|
||
│ │ ├── fold5
|
||
│ │ ├── fold6
|
||
│ │ ├── fold7
|
||
│ │ ├── fold8
|
||
│ │ └── fold9
|
||
│ └── metadata
|
||
│ └── UrbanSound8K.csv
|
||
├── label_list.txt
|
||
├── test_list.txt
|
||
└── train_list.txt
|
||
```
|
||
|
||
3. **修改 运行时的yaml配置文件**:
|
||
|
||
运行时需要配置一些模型路径,输入数据,后处理参数等,其配置文件在 `3_deploy/modelzoo/ResNetSE/cfg/`
|
||
|
||
测试时使用:`ResNetSE_test.yaml`
|
||
|
||
需要修改:
|
||
|
||
* imodel中参数:根据所使用板子ip设置ip
|
||
* dataset中参数:更改自己的dataset精度测试数据集路径
|
||
|
||
|
||
4. **编译、执行运行时程序**
|
||
|
||
python运行时(不需要编译):
|
||
|
||
```
|
||
cd 3_deploy/modelzoo/ResNetSE/pyrt
|
||
python ./ResNetSE_psin.py ../cfg/ResNetSE_test.yaml
|
||
```
|
||
|
||
程序结束自动计算出精度指标
|
||
|
||
|
||
|
||
# 6. 模型性能记录
|
||
|
||
| ResNetSE | input shape | hard time | qt_strategy | 精度(top-1) |
|
||
| -------------- | --------------- | -------------- | --------------- | ---------------- |
|
||
| float | [1, 398, 80] | - | - | 96.540 |
|
||
| 8bit | [1, 398, 80] | 16.968 ms | [kld-pc] | 95.881 |
|
||
| 16bit | [1, 398, 80] | 34.900ms | [kld-pc] | 96.339 |
|
||
|