跑通YOLOv5-Lite
环境搭建
系统相关依赖安装
sudo apt install build-essential git cmake libprotobuf-dev protobuf-compiler libvulkan-dev vulkan-utils libopencv-dev
依赖安装报错
解决方案:
更新清华镜像源
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
执行更新列表
sudo apt-get update
参考:https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/
protobuf安装
链接: https://github.com/protocolbuffers/protobuf
1.protobuf下载
3.编译安装
cd protobuf-21.6
./autogen.sh
运行./autogen.sh报错
解决方案
参考https://blog.csdn.net/le_murmure/article/details/105947515
sudo apt-get install autoconf automake libtool
./configure
make -j8
sudo make install
sudo ldconfig
3.测试
protoc --version
NCNN安装
链接: https://github.com/Tencent/ncnn
1.NCNN下载
ncnn需要下载源码ncnn-20220729.zip,如果直接在页面下载master版本,编译会报错 ‘_mm512_reduce_add_ps’ was not declared in this scope
3.编译安装
cd ncnn-20220729/
mkdir build && cd build && cmake .. && make -j8
3.测试
YOLOv5-Lite
链接: https://github.com/ppogg/YOLOv5-Lite
Pytorch及其他类库安装![Pytorch](https://img-blog.csdnimg.cn/a92a7aba50744760be4ed190ecb6796e.png)
其他类库安装
pip install onnx coremltools onnx-simplifier
模型下载![模型下载](https://img-blog.csdnimg.cn/8c44f3849fea4001a131becae4967148.png)
v5lite-e.pt官方预训练模型推理测试
python detect.py --source ./sample/horse.jpg
模型训练
- 数据标注
labelimg 采用yolo标注后整理为以下格式(val,test文件夹可以不需要)
- 数据配置
cd YOLOv5-Lite-1.4/data/
cp coco128.yaml device.yaml
(由于我没有将数据集全部拿去训练,没有切分test,val数据集,所以test,val都直接采用train的数据集)
- 模型配置
cd YOLOv5-Lite-1.4/models
cp v5Lite-e.yaml v5Lite-e-device.yaml
- 执行训练
python train.py --data ./data/device.yaml --cfg ./models/v5Lite-e-device.yaml --weights ./weights/v5lite-e.pt --batch-size 128 --img-size 320,320
训练报错:RuntimeError: result type Float can‘t be cast to the desired output type long int
参考:https://blog.csdn.net/Thebest_jack/article/details/125649451
修改【utils】中的【loss.py】里面的两处内容
-
打开你的【utils】文件下的【loss.py】
-
按【Ctrl】+【F】打开搜索功能,输入【for i in range(self.nl)】找到下面的一行内容:
anchors = self.anchors[i]替换为 anchors, shape = self.anchors[i], p[i].shape -
按【Ctrl】+【F】打开搜索功能,输入【indices.append】找到下面的一行内容:
indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices 替换为
indices.append((b, a, gj.clamp_(0, shape[2] - 1), gi.clamp_(0, shape[3] - 1))) # image, anchor, grid
- 模型推理验证工作
python detect.py --source ./train/images/BoomMultiWayValve_LinearContrast.jpg --weights ./runs/train/exp/weights/best.pt
模型转换
将训练后的模型改名为yolov5-device-lite-e.pt 并放置到./weights/目录下
pt导出为onnx
python export.py --weights weights/yolov5-device-lite-e.pt --img 320 --batch 1
简化onnx
python -m onnxsim weights/yolov5-device-lite-e.onnx weights/yolov5-device-lite-e-sim.onnx
onnx转化为ncnn模型
cd /home/hw/wenzw/yolov5_android/ncnn/build/tools/onnx
./onnx2ncnn /home/hw/wenzw/yolov5_android/YOLOv5-Lite-1.4/weights/yolov5-device-lite-e-sim.onnx /home/hw/wenzw/yolov5_android/YOLOv5-Lite-1.4/weights/yolov5-e-lite.param /home/hw/wenzw/yolov5_android/YOLOv5-Lite-1.4/weights/yolov5-e-lite.bin
优化ncnn模型
cd /home/hw/wenzw/yolov5_android/ncnn/build/tools
./ncnnoptimize /home/hw/wenzw/yolov5_android/YOLOv5-Lite-1.4/weights/yolov5-e-lite.param /home/hw/wenzw/yolov5_android/YOLOv5-Lite-1.4/weights/yolov5-e-lite.bin /home/hw/wenzw/yolov5_android/YOLOv5-Lite-1.4/weights/yolov5-e-lite-opt.param /home/hw/wenzw/yolov5_android/YOLOv5-Lite-1.4/weights/yolov5-e-lite-opt.bin 65536
param修改
将倒数两个Permute所对应的值更改为1111,2222()
Android端部署
ncnn-YYYYMMDD-android-vulkan
链接: https://github.com/ppogg/ncnn-android-v5lite/
ncnn-YYYYMMDD-android-vulkan下载完成后解压到Android项目的app/src/main/jni下,并修改相应的app/src/main/jni/CMakeLists.txt
opencv-mobile-XYZ-android
链接: https://github.com/ppogg/ncnn-android-v5lite/
opencv-mobile-XYZ-android下载完成后解压到Android项目的app/src/main/jni下,并修改相应的app/src/main/jni/CMakeLists.txt
ncnn-android-v5lite
链接: https://github.com/ppogg/ncnn-android-v5lite/
- 修改CMakeLists.txt
到以下路径查看对应的ncnn-YYYYMMDD-android-vulkan和opencv-mobile-XYZ-android,下载对应的版本,最好跟Android项目下的一致。
- 模型导入到Android项目
将原先的e.param和e.bin更改名称为e1.param和e.bin
将转换后的ncnn模型复制到app\src\main\assets下
此处为1111和2222,对应模型文件中最后的两个Permute
- classes类别名称更改
注意事项
- Android项目中包含了c,e,i8s,s四种模型,而本文只转换了e模型,且识别类别为2。故使用其他模型识别的时候会因为对应的类别缺少导致报错。
static const char *class_names[] = {
"piston", "BoomMultiWayValve"
};
- 安装app失败
项目添加gradle.properties文件,并写入android.injected.testOnly=false
参考
【1】https://github.com/ppogg/YOLOv5-Lite
【2】https://github.com/cmdbug/YOLOv5_NCNN
【3】https://blog.csdn.net/weixin_45829462/article/details/119787840?spm=1001.2014.3001.5502
【4】https://blog.csdn.net/weixin_46502301/article/details/118395689
【5】https://github.com/ppogg/ncnn-android-v5lite/