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/
链接: https://github.com/protocolbuffers/protobuf
3.编译安装
cd protobuf-21.6
./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
链接: 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.测试
链接: https://github.com/ppogg/YOLOv5-Lite
其他类库安装
pip install onnx coremltools onnx-simplifier
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()
链接: https://github.com/ppogg/ncnn-android-v5lite/
ncnn-YYYYMMDD-android-vulkan下载完成后解压到Android项目的app/src/main/jni下,并修改相应的app/src/main/jni/CMakeLists.txt
链接: https://github.com/ppogg/ncnn-android-v5lite/
opencv-mobile-XYZ-android下载完成后解压到Android项目的app/src/main/jni下,并修改相应的app/src/main/jni/CMakeLists.txt
链接: https://github.com/ppogg/ncnn-android-v5lite/
到以下路径查看对应的ncnn-YYYYMMDD-android-vulkan和opencv-mobile-XYZ-android,下载对应的版本,最好跟Android项目下的一致。
此处为1111和2222,对应模型文件中最后的两个Permute
static const char *class_names[] = {
"piston", "BoomMultiWayValve"
};
【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/