本文主要是介绍yolov2 推理测试 - 模型转换❤️darknet 转 ncnn❤️【yolov2之darknet】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本博文基本按照 YOLOv2 darknet 官方教程,对预训练模型 展开测试,简单记录
YOLOv2 模型转 ncnn 部分遇到报错,本博文未做处理;对这部分有兴趣可参考
yolov3(darknet )训练 - 测试 - 模型转换❤️darknet 转 ncnn 之C++运行推理❤️【yolov3 实战一览】
文章目录
- 🥇 基础信息
- 📕 运行过程如下【darknet 模型测试】
- 下载仓库代码
- make 【基础环境编译】
- 下载模型
- run the detector【测试 dog.jpg】
- run the detector【测试二 horses.jpg 】
- yolov2-tiny-voc.weights 测试
- 📘 darknet 版本 yolo2 转 ncnn【darknet2ncnn】
- 📙 pytorch 版本 yolo2 转 ncnn
🥇 基础信息
- yolov2【yolo-9000】 来自 2016 年,至今已有 pytorch、keras、darknet 等多个框架的训练版本
- 本博文围绕 官方 darknet 模型进行测试 和 ncnn 尝试转换
依照如下 darknet/yolov2 官网 依次运行即可
https://pjreddie.com/darknet/yolov2/
📕 运行过程如下【darknet 模型测试】
下载仓库代码
git clone https://github.com/pjreddie/darknet
cd darknet
make 【基础环境编译】
make
# 输出如下:
mkdir -p obj
mkdir -p backup
mkdir -p results
gcc -Iinclude/ -Isrc/ -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -c ./src/gemm.c -o obj/gemm.o...
下载模型
wget 命令下载 或者 手动到浏览器下载
wget https://pjreddie.com/media/files/yolov2.weights--2021-10-09 14:52:58-- https://pjreddie.com/media/files/yolov2.weights
Resolving pjreddie.com (pjreddie.com)... 128.208.4.108
Connecting to pjreddie.com (pjreddie.com)|128.208.4.108|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 203934260 (194M) [application/octet-stream]
Saving to: ‘yolov2.weights’yolov2.weights 100%[================================================================================================================================================>] 194.49M 161KB/s in 8m 48s 2021-10-09 15:01:46 (377 KB/s) - ‘yolov2.weights’ saved [203934260/203934260]# 或者
wget https://pjreddie.com/media/files/yolov2-tiny-voc.weights
run the detector【测试 dog.jpg】
./darknet detect cfg/yolov2.cfg yolov2.weights data/dog.jpg# 输出如下
layer filters size input output0 conv 32 3 x 3 / 1 608 x 608 x 3 -> 608 x 608 x 32 0.639 BFLOPs1 max 2 x 2 / 2 608 x 608 x 32 -> 304 x 304 x 322 conv 64 3 x 3 / 1 304 x 304 x 32 -> 304 x 304 x 64 3.407 BFLOPs3 max 2 x 2 / 2 304 x 304 x 64 -> 152 x 152 x 644 conv 128 3 x 3 / 1 152 x 152 x 64 -> 152 x 152 x 128 3.407 BFLOPs5 conv 64 1 x 1 / 1 152 x 152 x 128 -> 152 x 152 x 64 0.379 BFLOPs6 conv 128 3 x 3 / 1 152 x 152 x 64 -> 152 x 152 x 128 3.407 BFLOPs7 max 2 x 2 / 2 152 x 152 x 128 -> 76 x 76 x 1288 conv 256 3 x 3 / 1 76 x 76 x 128 -> 76 x 76 x 256 3.407 BFLOPs9 conv 128 1 x 1 / 1 76 x 76 x 256 -> 76 x 76 x 128 0.379 BFLOPs10 conv 256 3 x 3 / 1 76 x 76 x 128 -> 76 x 76 x 256 3.407 BFLOPs11 max 2 x 2 / 2 76 x 76 x 256 -> 38 x 38 x 25612 conv 512 3 x 3 / 1 38 x 38 x 256 -> 38 x 38 x 512 3.407 BFLOPs13 conv 256 1 x 1 / 1 38 x 38 x 512 -> 38 x 38 x 256 0.379 BFLOPs14 conv 512 3 x 3 / 1 38 x 38 x 256 -> 38 x 38 x 512 3.407 BFLOPs15 conv 256 1 x 1 / 1 38 x 38 x 512 -> 38 x 38 x 256 0.379 BFLOPs16 conv 512 3 x 3 / 1 38 x 38 x 256 -> 38 x 38 x 512 3.407 BFLOPs17 max 2 x 2 / 2 38 x 38 x 512 -> 19 x 19 x 51218 conv 1024 3 x 3 / 1 19 x 19 x 512 -> 19 x 19 x1024 3.407 BFLOPs19 conv 512 1 x 1 / 1 19 x 19 x1024 -> 19 x 19 x 512 0.379 BFLOPs20 conv 1024 3 x 3 / 1 19 x 19 x 512 -> 19 x 19 x1024 3.407 BFLOPs21 conv 512 1 x 1 / 1 19 x 19 x1024 -> 19 x 19 x 512 0.379 BFLOPs22 conv 1024 3 x 3 / 1 19 x 19 x 512 -> 19 x 19 x1024 3.407 BFLOPs23 conv 1024 3 x 3 / 1 19 x 19 x1024 -> 19 x 19 x1024 6.814 BFLOPs24 conv 1024 3 x 3 / 1 19 x 19 x1024 -> 19 x 19 x1024 6.814 BFLOPs25 route 1626 conv 64 1 x 1 / 1 38 x 38 x 512 -> 38 x 38 x 64 0.095 BFLOPs27 reorg / 2 38 x 38 x 64 -> 19 x 19 x 25628 route 27 2429 conv 1024 3 x 3 / 1 19 x 19 x1280 -> 19 x 19 x1024 8.517 BFLOPs30 conv 425 1 x 1 / 1 19 x 19 x1024 -> 19 x 19 x 425 0.314 BFLOPs31 detection
mask_scale: Using default '1.000000'
Loading weights from yolov2.weights...Done!
data/dog.jpg: Predicted in 10.937355 seconds.
dog: 82%
truck: 64%
bicycle: 85%
run the detector【测试二 horses.jpg 】
./darknet detect cfg/yolov2.cfg yolov2.weights data/horses.jpg # 输出如下mask_scale: Using default '1.000000'
Loading weights from yolov2.weights...Done!
data/horses.jpg: Predicted in 11.372653 seconds.
horse: 91%
horse: 84%
horse: 62%
yolov2-tiny-voc.weights 测试
./darknet detector test cfg/voc.data cfg/yolov2-tiny-voc.cfg yolov2-tiny-voc.weights data/dog.jpg# 输出如下layer filters size input output0 conv 16 3 x 3 / 1 416 x 416 x 3 -> 416 x 416 x 16 0.150 BFLOPs1 max 2 x 2 / 2 416 x 416 x 16 -> 208 x 208 x 162 conv 32 3 x 3 / 1 208 x 208 x 16 -> 208 x 208 x 32 0.399 BFLOPs3 max 2 x 2 / 2 208 x 208 x 32 -> 104 x 104 x 324 conv 64 3 x 3 / 1 104 x 104 x 32 -> 104 x 104 x 64 0.399 BFLOPs5 max 2 x 2 / 2 104 x 104 x 64 -> 52 x 52 x 646 conv 128 3 x 3 / 1 52 x 52 x 64 -> 52 x 52 x 128 0.399 BFLOPs7 max 2 x 2 / 2 52 x 52 x 128 -> 26 x 26 x 1288 conv 256 3 x 3 / 1 26 x 26 x 128 -> 26 x 26 x 256 0.399 BFLOPs9 max 2 x 2 / 2 26 x 26 x 256 -> 13 x 13 x 25610 conv 512 3 x 3 / 1 13 x 13 x 256 -> 13 x 13 x 512 0.399 BFLOPs11 max 2 x 2 / 1 13 x 13 x 512 -> 13 x 13 x 51212 conv 1024 3 x 3 / 1 13 x 13 x 512 -> 13 x 13 x1024 1.595 BFLOPs13 conv 1024 3 x 3 / 1 13 x 13 x1024 -> 13 x 13 x1024 3.190 BFLOPs14 conv 125 1 x 1 / 1 13 x 13 x1024 -> 13 x 13 x 125 0.043 BFLOPs15 detectionmask_scale: Using default '1.000000'
Loading weights from yolov2-tiny-voc.weights...Done!
data/dog.jpg: Predicted in 1.149598 seconds.
dog: 78%
car: 55%
car: 50%
预测图像效果如下
📘 darknet 版本 yolo2 转 ncnn【darknet2ncnn】
darknet2ncnn
./darknet2ncnn yolov2.cfg yolov2.weights yolov2-darknet.param yolov2-darknet.bin 1# 输出如下Loading cfg...
Loading weights...
Converting model...
77 layers, 78 blobs generated.
NOTE: The input of darknet uses: mean_vals=0 and norm_vals=1/255.f.
NOTE: Remember to use ncnnoptimize for better performance.# 或者./darknet2ncnn yolov2-tiny-voc.cfg yolov2-tiny-voc.weights yolov2-darknet.param yolov2-darknet.bin 1Loading cfg...
Loading weights...
Converting model...
33 layers, 33 blobs generated.
NOTE: The input of darknet uses: mean_vals=0 and norm_vals=1/255.f.
NOTE: Remember to use ncnnoptimize for better performance.
ncnnoptimize 优化报错如下 【yolov2.weights 和 yolov2-tiny-voc.weights 转 ncnn 遇到一样的问题】
./ncnnoptimize darknet/yolov2-darknet.param darknet/yolov2-darknet.bin yolov2-darknet-opt.param yolov2-darknet-opt.bin 0# 运行报错,输出如下ParamDict parse value failed
ParamDict load_param 69 26_211_bn_leaky failed
parse top_count failed
load_model error at layer 69, parameter file has inconsistent content.
Segmentation fault (core dumped)
原因分析如下,【暂无法处理】
📙 pytorch 版本 yolo2 转 ncnn
ncnn 官方支持的 yolo2 模型转换貌似是 pytorch 版本
https://github.com/longcw/yolo2-pytorch
中间遇到 pytorch 版本过低的问题,因此没有继续下去
- 一个报错的参考解决方法链接:ImportError: torch.utils.ffi is deprecated
遇到未知报错的我
这篇关于yolov2 推理测试 - 模型转换❤️darknet 转 ncnn❤️【yolov2之darknet】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!