改进的yolov5目标检测-yolov5替换骨干网络-yolo剪枝(TensorRT及NCNN部署)-1
https://developer.aliyun.com/article/1446504?spm=a2c6h.13148508.setting.30.68a34f0e3ZrSNI
C、MobileNetv3Small-YOLOv5
(1)原文结构,部分使用Hard-Swish激活函数以及SE模块
(2)Neck与head部分嫁接YOLOv5l原结构
(3)中间通道人为设定(expand)
D、EfficientNetLite0-YOLOv5
(1)使用Lite0结构,且不使用SE模块
(2)针对dropout_connect_rate,手动赋值(随着idx_stage变大而变大)
(3)中间通道一律*6(expand)
E、PP-LCNet-YOLOv5
(1)使用PP-LCNet-1x结构,在网络末端使用SE以及5*5卷积核
(2)SeBlock压缩维度为原1/16
(3)中间通道不变
Pruning for YOLOs
Model | mAP | mAP@50 | Parameters(M) | GFLOPs | FPS@CPU |
YOLOv5s | 18.4 | 34 | 7.05 | 15.9 | |
YOLOv5n | 13 | 26.2 | 1.78 | 4.2 | |
YOLOv5s-EagleEye@0.6 | 14.3 | 27.9 | 4.59 | 9.6 |
1、Prune Strategy
(1)基于YOLOv5块状结构设计,对Conv、C3、SPP(F)模块进行剪枝,具体来说有以下:
- Conv模块的输出通道数
- C3模块中cv2块和cv3块的输出通道数
- C3模块中若干个bottleneck中的cv1块的输出通道数
(2)八倍通道剪枝(outchannel = 8*n)
(3)ShortCut、concat皆合并剪枝
2、Prune Tools
(1)EagleEye
EagleEye: Fast Sub-net Evaluation for Efficient Neural Network Pruning
基于搜索的通道剪枝方法,核心思想是随机搜索到大量符合目标约束的子网,然后快速更新校准BN层的均值与方差参数,并在验证集上测试校准后全部子网的精度。精度最高的子网拥有最好的架构,经微调恢复后能达到较高的精度。
Usage
- 正常训练模型
python train.py --data data/VisDrone.yaml --imgsz 640 --weights yolov5s.pt --cfg models/prunModels/yolov5s-pruning.yaml --device 0
(注意训练其他模型,参考/prunModels/yolov5s-pruning.yaml进行修改,目前已支持v6架构)
- 搜索最优子网
python pruneEagleEye.py --weights path_to_trained_yolov5_model --cfg models/prunModels/yolov5s-pruning.yaml --data data/VisDrone.yaml --path path_to_pruned_yolov5_yaml --max_iter maximum number of arch search --remain_ratio the whole FLOPs remain ratio --delta 0.02
- 微调恢复精度
python train.py --data data/VisDrone.yaml --imgsz 640 --weights path_to_Eaglepruned_yolov5_model --cfg path_to_pruned_yolov5_yaml --device 0
(2)Network Slimming
Learning Efficient Convolutional Networks through Network Slimming
Usage
- 模型BatchNorm Layer \gamma 稀疏化训练
python train.py --data data/VisDrone.yaml --imgsz 640 --weights yolov5s.pt --cfg models/prunModels/yolov5s-pruning.yaml --device 0 --sparse
(注意训练其他模型,参考/prunModels/yolov5s-pruning.yaml进行修改,目前已支持v6架构)
- BatchNorm Layer剪枝
python pruneSlim.py --weights path_to_sparsed_yolov5_model --cfg models/prunModels/yolov5s-pruning.yaml --data data/VisDrone.yaml --path path_to_pruned_yolov5_yaml --global_percent 0.6 --device 3
- 微调恢复精度
python train.py --data data/VisDrone.yaml --imgsz 640 --weights path_to_Slimpruned_yolov5_model --cfg path_to_pruned_yolov5_yaml --device 0
Quantize Aware Training for YOLOs
MQBench是实际硬件部署下评估量化算法的框架,进行各种适合于硬件部署的量化训练(QAT)
Requirements
- PyTorch == 1.8.1
Install MQBench Lib
由于MQBench目前还在不断更新,选择0.0.2稳定版本作为本仓库的量化库。
git clone https://github.com/ZLkanyo009/MQBench.git cd MQBench python setup.py build python setup.py install
Usage
训练脚本实例:
python train.py --data VisDrone.yaml --weights yolov5n.pt --cfg models/yolov5n.yaml --epochs 300 --batch-size 8 --img 608 --nosave --device 0,1 --sync-bn --quantize --BackendType NNIE
Deploy
目前已支持TensorRT及NCNN部署,详见deploy
To do
- Multibackbone: MobileNetV3-small
- Multibackbone: ShuffleNetV2
- Multibackbone: GhostNet
- Multibackbone: EfficientNet-Lite0
- Multibackbone: PP-LCNet
- Multibackbone: TPH-YOLOv5
- Module: SwinTrans(C3STR)
- Module: Deformable Convolution
- Pruner: Network Slimming
- Pruner: EagleEye
- Pruner: OneShot (L1, L2, FPGM), ADMM, NetAdapt, Gradual, End2End
- Quantization: MQBench
- Knowledge Distillation