验证ncnn yolov5 demo

238 阅读1分钟

修改yolov5.cpp

/ncnn/examples/yolov5.cpp 选择YOLOV5模型版本 有YOLOV5_V60、YOLOV5_V62两种选择 选择YOLOV5_V60

//#define YOLOV5_V60 1 //YOLOv5 v6.0
#define YOLOV5_V62 1 //YOLOv5 v6.2 export  onnx model method https://github.com/shaoshengsong/yolov5_62_export_ncnn

👇

#define YOLOV5_V60 1 //YOLOv5 v6.0
//#define YOLOV5_V62 1 //YOLOv5 v6.2 export  onnx model method https://github.com/shaoshengsong/yolov5_62_export_ncnn

导入模型结构文件(param)与权重文件(bin)

// original pretrained model from https://github.com/ultralytics/yolov5
// the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
#if YOLOV5_V62
if (yolov5.load_param("yolov5s_6.2.param"))
        exit(-1);
if (yolov5.load_model("yolov5s_6.2.bin"))
        exit(-1);
#elif YOLOV5_V60
if (yolov5.load_param("yolov5s_6.0.param"))
        exit(-1);
if (yolov5.load_model("yolov5s_6.0.bin"))
        exit(-1);
#else
yolov5.register_custom_layer("YoloV5Focus", YoloV5Focus_layer_creator);

if (yolov5.load_param("yolov5s.param"))
        exit(-1);
if (yolov5.load_model("yolov5s.bin"))
        exit(-1);
#endif
// original pretrained model from https://github.com/ultralytics/yolov5
// the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models

分别为原始模型和导出模型地址

编译

##### linux host system with gcc/g++
sudo su
mkdir -p build-host-gcc-linux
pushd build-host-gcc-linux
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/host.gcc.toolchain.cmake -DNCNN_BUILD_EXAMPLES=ON ..
make -j4
make install
popd

测试

cd ../examples
../build-host-gcc-linux/examples/yolov5 ../images/bus.jpg
root@xtark-vmpc:/home/xtark/ProjectZ/NCNN/ncnn/examples# ../build-host-gcc-linux/examples/yolov5 ../images/bus.jpg
0 = 0.85792 at 223.40 404.26 121.91 x 463.35
0 = 0.83182 at 680.67 383.63 128.33 x 499.23
0 = 0.80850 at 50.00 399.57 193.56 x 505.42
5 = 0.74772 at 21.70 214.60 781.77 x 564.65
0 = 0.39262 at 0.60 550.42 78.10 x 342.38

image.png