101 lines
3.4 KiB
Markdown
101 lines
3.4 KiB
Markdown
# HLS TinyCNN 部署说明
|
||
|
||
本目录实现了一条不依赖 FINN/Linux 的 PYNQ-Z2 HLS 演示路线:
|
||
|
||
```text
|
||
训练 TinyCNN -> 导出 HLS 权重头文件 -> Vitis HLS 打包 IP -> Vivado 生成 bit/hwh -> PYNQ DMA 推理
|
||
```
|
||
|
||
## 当前状态
|
||
|
||
已在 Windows 工具链上跑通:
|
||
|
||
- Vitis HLS:`F:\ZYNQ_\Vitis_HLS\2020.2\bin\vitis_hls.bat`
|
||
- Vivado:`F:\ZYNQ_\Vivado\2020.2\bin\vivado.bat`
|
||
- HLS IP:`hls_cnn/build/tiny_cnn_hls/solution1/impl/ip/component.xml`
|
||
- IP 压缩包:`hls_cnn/ip_repo/tiny_cnn_accel.zip`
|
||
- PYNQ overlay:`hls_cnn/output/tiny_cnn.bit`、`hls_cnn/output/tiny_cnn.hwh`
|
||
|
||
注意:工程路径包含中文,Xilinx 2020.2 对非 ASCII 路径不稳定。`run_hls.bat` 和 `run_vivado.bat` 会自动复制到 `C:\hls_tiny_cnn_work\hls_cnn` 构建,再把产物复制回本目录。
|
||
|
||
## 模型与接口
|
||
|
||
TinyCNN 是一个用于上板演示的小网络,不是完整 MobileNetV2:
|
||
|
||
- 输入:`64x64` 灰度图,PYNQ 端发送 `uint32[4096]`,低 8 bit 为像素值。
|
||
- 网络:`Conv(1->8)+ReLU+MaxPool`,`Conv(8->16)+ReLU+MaxPool`,`Conv(16->32)+ReLU+MaxPool`,`GlobalAvgPool`,`FC(32->10)`。
|
||
- 输出:`int32[10]` logits,按 `1024` 倍定点缩放,PYNQ 端直接 `argmax`。
|
||
- HLS 控制:`s_axilite port=return`,PYNQ 脚本会写 `ap_start`。
|
||
|
||
## 训练与导出
|
||
|
||
```bat
|
||
cd pc_training
|
||
python train_tiny_cnn.py --data_dir ..\data\processed --input_size 64 --epochs 150 --batch_size 32 --num_workers 0 --save_path ..\data\models\tiny_cnn.pth
|
||
cd ..
|
||
python hls_cnn\export_weights.py --model data\models\tiny_cnn.pth --test_image data\processed\val\class_0\10.jpg
|
||
```
|
||
|
||
本机实测:当前 TinyCNN 训练到 150 epochs 后最佳验证准确率只有 `13.00%`,未达到原计划的 `70%`。因此当前 HLS overlay 适合作为“DMA + HLS CNN + PYNQ 上板”的硬件闭环演示,不适合作为最终识别精度结果。
|
||
|
||
## 构建 HLS IP
|
||
|
||
```bat
|
||
hls_cnn\run_hls.bat
|
||
```
|
||
|
||
脚本会自动处理 Vivado/Vitis HLS 2020.2 在 2026 年日期下生成过大 `core_revision` 导致 IP packager 报错的问题。
|
||
|
||
当前 HLS 估算结果:
|
||
|
||
- 时钟:100 MHz,估算周期 `7.245 ns`
|
||
- 延迟:`2,779,684` cycles,约 `27.797 ms`
|
||
- 资源估算:BRAM `77/280`,DSP `12/220`,FF `3021/106400`,LUT `5850/53200`
|
||
|
||
## 生成 bitstream
|
||
|
||
```bat
|
||
hls_cnn\run_vivado.bat
|
||
```
|
||
|
||
生成:
|
||
|
||
```text
|
||
hls_cnn/output/tiny_cnn.bit
|
||
hls_cnn/output/tiny_cnn.hwh
|
||
```
|
||
|
||
Vivado Block Design 包含:
|
||
|
||
- `processing_system7_0`
|
||
- `axi_dma_0`
|
||
- `tiny_cnn_accel_0`
|
||
- AXI-Lite interconnect
|
||
- AXI memory interconnect
|
||
|
||
## PYNQ 测试
|
||
|
||
把产物和测试脚本传到 PYNQ-Z2:
|
||
|
||
```bash
|
||
scp hls_cnn/output/tiny_cnn.bit xilinx@192.168.2.99:~/
|
||
scp hls_cnn/output/tiny_cnn.hwh xilinx@192.168.2.99:~/
|
||
scp fpga_deployment/tiny_cnn_inference.py xilinx@192.168.2.99:~/
|
||
scp -r data/processed/val xilinx@192.168.2.99:~/test_images
|
||
```
|
||
|
||
在 PYNQ 上运行:
|
||
|
||
```bash
|
||
python3 tiny_cnn_inference.py --bitstream tiny_cnn.bit --image test_images/class_0/10.jpg
|
||
python3 tiny_cnn_inference.py --bitstream tiny_cnn.bit --test_dir test_images --runs 100
|
||
```
|
||
|
||
## 后续精度改进方向
|
||
|
||
当前硬件链路已经打通,下一步应优先改模型和数据,而不是继续调 Vivado:
|
||
|
||
- 保持 HLS 接口不变,尝试更强但仍可综合的小模型。
|
||
- 改为二值/阈值连通域、峰值检测、传统图像特征加分类器等更贴近“光瓣数量”的算法。
|
||
- 如果论文更看重准确率,保留 MobileNetV2/ONNX/PC 端结果作为主精度结果,HLS TinyCNN 作为 FPGA 加速演示。
|