Skip to content

LISTENAI/QR_Code_Detection_lnn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

二维码检测 Demo

本工程用于二维码检测模型的浮点训练、面向平台的约束训练、量化训练、ONNX 导出、Thinker 打包和仿真验证。工程基于开源 YOLOv1 二维码检测示例修改,并针对 ListenAI VENUS、ARCS、VENUSA 平台做了适配。

支持平台:arcsvenusvenusa

原始工程参考:https://github.com/abeardear/pytorch-YOLO-v1/tree/master

对原工程做了适配:arcsvenus 使用平均池化,venusa 使用最大池化。

开发环境准备

根据机器配置,参考 linger/thinker 官方教程配置环境,也可以直接使用对应 Docker 镜像。

示例环境仅供参考:Python 3.10、CUDA 11.8+、PyTorch、torchvision、linger、thinker 工具链。

LNN 工具链环境创建

conda create -n test_env python=3.10
conda activate test_env

数据集准备

二维码数据集下载链接:

百度网盘: https://pan.baidu.com/s/1d66HKN-8773D2FmvyhMwFg
密码: 37rr

默认路径:

  • dataset/QRimages/:训练和测试图片目录
  • dataset/train_labels-QRcode.txt:训练标注文件
  • dataset/test_labels-QRcode.txt:测试标注文件

训练和模型转换

浮点模型训练

cd thinker/demo/QRcode_detection
# arcs
python train.py --type float --platform arcs --epochs 80 --out_path ./weights
# venus
python train.py --type float --platform venus --epochs 80 --out_path ./weights
#venusa
python train.py --type float --platform venusa --epochs 80 --out_path ./weights

约束训练

# arcs
python train.py --type clamp --platform arcs --epochs 20 --checkpoint ./weights/ori_weights/yolo-float-80-avgpool.pth --out_path ./weights
#venus
python train.py --type clamp --platform venus --epochs 20 --checkpoint ./weights/ori_weights/yolo-float-80-avgpool.pth --out_path ./weights
#venusa
python train.py --type clamp --platform venusa --epochs 20 --checkpoint ./weights/ori_weights/yolo-float-80-maxpool.pth --out_path ./weights

量化训练和 ONNX 导出

#arcs
python train.py --type quant --platform arcs --epochs 20 --checkpoint ./weights/cweights/yolo-clamp-20-from-float80-arcs-avgpool.pth --out_path ./weights
#venus
python train.py --type quant --platform venus --epochs 20 --checkpoint ./weights/cweights/yolo-clamp-20-from-float80-venus-avgpool.pth --out_path ./weights
#venusa
python train.py --type quant --platform venusa --epochs 20 --checkpoint ./weights/cweights/yolo-clamp-20-from-float80-venusa-maxpool.pth --out_path ./weights

仿真推理

图优化和资源打包

cd thinker/demo/QRcode_detection
#arcs
tpacker -g ./weights/onnx_weights/yolo-quant-20-from-clamp20-avgpool-arcs.onnx -o ./weights/bin_pkg/model_arcs_40_5_5.bin --threshold1 32768 --threshold2 65536 --threshold4 32768 --dma_prefetch False
#venus
tpacker -g ./weights/onnx_weights/yolo-quant-20-from-clamp20-avgpool-venus.onnx -o ./weights/bin_pkg/model_venus_40_5_5.bin --threshold1 32768 --threshold2 65536 --threshold4 32768 --dma_prefetch False
#venusa
tpacker -g ./weights/onnx_weights/yolo-quant-5-from-clamp5-maxpool-venusa.onnx -o ./weights/bin_pkg/model_venusa_40_5_5.bin --threshold1 65536 --threshold2 65536 --threshold4 262100 --dma_prefetch=false

仿真平台执行

thinker/CMakeLists.txt 中打开目标平台开关,例如 venus:

-DTHINKER_USE_VENUS=ON
-DTHINKER_USE_ARCS=OFF
-DTHINKER_USE_VENUSA=OFF

编译动态库:

cd thinker
sh ./scripts/x86_linux.sh

执行仿真:

#arcs
./bin/test_thinker ./demo/QRcode_detection/weights/bin_pkg/model_arcs_40_5_5.bin ./demo/QRcode_detection/test/0000964.bin ./demo/QRcode_detection/test/output_arcs_40_5_5.bin
#venus
./bin/test_thinker ./demo/QRcode_detection/weights/bin_pkg/model_venus_40_5_5.bin ./demo/QRcode_detection/test/0000964.bin ./demo/QRcode_detection/test/output_venus_40_5_5.bin
#venusa
./bin/test_thinker ./demo/QRcode_detection/weights/bin_pkg/model_venusa_40_5_5.bin ./demo/QRcode_detection/test/0000964.bin ./demo/QRcode_detection/test/output_venusa_40_5_5.bin

训练-推理一致性验证

cd thinker
# arcs
tvalidator -g ./demo/QRcode_detection/weights/onnx_weights/yolo-quant-20-from-clamp20-avgpool-arcs.onnx -r ./demo/QRcode_detection/weights/bin_pkg/model_arcs_40_5_5.bin
# venus
tvalidator -g ./demo/QRcode_detection/weights/onnx_weights/yolo-quant-20-from-clamp20-avgpool-venus.onnx -r ./demo/QRcode_detection/weights/bin_pkg/model_venus_40_5_5.bin
# venusa
tvalidator -g ./demo/QRcode_detection/weights/onnx_weights/yolo-quant-5-from-clamp5-maxpool-venusa.onnx -r ./demo/QRcode_detection/weights/bin_pkg/model_venusa_40_5_5.bin

芯片部署

固件集成待补充。

目录结构

  • modules/:模型结构定义
  • criterion/:YOLO 损失函数
  • prepare_data/:数据集读取逻辑
  • configs/:不同平台的 linger 配置
  • train.py:浮点、约束、量化训练入口;量化训练后自动导出 ONNX
  • weights/:训练权重、ONNX、Thinker 打包结果
  • test/:仿真输入输出样例
  • docs/:示例图片和结果
  • README.md:中文说明
  • README_EN.md:英文说明

About

QR Code Detection Algorithm Demo: LNN-based Engineering Deployment on ListenAI Toolchain

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages