YOLOv3转换为caffe模型

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nodototao/article/details/85711703

yolov3里面有些层,比如:shortcut, route, upsample, yolo等这些层是caffe不支持的,但在caffe中可以用eltwise替换shortcut,用concat替换route,但是yolo层只能自己实现写了, upsample可以自己在caffe里添加该层的实现。

1 caffe中添加upsample层的实现

感谢chen大神提供的代码。
添加upsample这一层的代码:upsample层代码在这里

upsample_layer.hpp放入include/caffe/layers下面
upsample_layer.cpp与upsample_layer.cu放在src/caffe/layers下面

修改相应的caffe.proto文件, src/caffe/proto/caffe.proto中的LayerParameter的最后一行加入:

	message LayerParameter{
		......
		optional UpsampleParameter upsample_param = 149;
	}

注意149为新层的ID号,该ID号根据个人的caffe.proto文件指定
然后再caffe.proto中添加upsample层的参数

	message UpsampleParameter{
		optional int32 scale = 1 [default = 1];
	}

注意:

make clean后, 重新编译。后面会说明,没有make clean编译后出现的问题。

2 模型转换

可以借助一个模型转换的工具:https://github.com/marvis/pytorch-caffe-darknet-convert;
(需要安装pytorch)
1) pytorch安装(ubuntu14.04, cuda8.0, python2.7)
在安装过程中,由于pip版本为1.5.4(pip --version),

报错:*** is not a supported wheel on this platform

需要先对版本进行升级才行,升级步骤如下:

  1. sudo apt-get remove python-pip
  2. https://pypi.python.org/pypi/pip#downloads, 下载pip的源码
    18.1版本的pip,下载Source
  3. 解压源码
  4. 执行 sudo python setup.py install

下载pytorch的whl文件(torch-0.4.1-cp27-cp27mu-linux_x86_64.whl),注意python版本

pip install torch-0.4.1-cp27-cp27mu-linux_x86_64.whl

2) 模型转换工具说明

上述github上介绍的是基于yolol与yolov2的
(1)这两个weights的存储方式与yolov3的存储方式有点不同
(2)yolov3上有upsample层在之前的版本上没有
基于yolov3的darknet2caffe.py的文件,参考 https://github.com/ChenYingpeng/caffe-yolov3 中的yolov3_darknet2caffe.py文件

3)模型转换工具使用中出现的问题及解决办法
(1)yolov3_darknet2caffe.py 代码缩进问题: ”TabError: inconsistent use of tabs and spaces in indentation"
在这里插入图片描述
解决方法:自行更改出错行的缩进问题

(2)Error in ‘python’: free(): invalid pointer: 0x000000000d2de78
在这里插入图片描述
解决方法:

缺少libtcmalloc-minimal4库,
首先安装 sudo apt-get install libtcmalloc-minimal4
sudo gedit ~/.bashrc
在文件末尾添加如下代码: export LD_PRELOAD="/usr/lib/libtcmalloc_minimal.so.4"
最后重新载入环境变量即可: sudo source ~/.bashrc

(3)OverflowError: long int too large to convert to int

在这里插入图片描述
解决方法

这里是引用
由于在添加upsample层后,直接make 和 make pycaffe,所以在这里转换模型时出现这个问题。
通过查看相关内容的错误信息,最后解决方法是:
在caffe工程中,首先make clean,
然后,重新编译caffe
make all -j32
make pycaffe

3 caffe_yolov3实现(yolo层),出现的问题及解决方法

参看chen大神的github地址: https://github.com/ChenYingpeng/caffe-yolov3

上述模型转换中的替换darknet2caffe.py的yolov3_darnet2caffe.py就在该工程的model_convert文件夹中

具体实现,参看源码实现,下面记录下使用过程中,针对自己环境出现的一些问题和解决方法

1) cmke …时,出现 error: identifier “nullptr” is undefined, expected a ";"等错误,如下图
在这里插入图片描述
解决方法

这个应该C++11版本的问题,修改CMakeLists.txt文件
在这里插入图片描述
12-16行为新添加的内容, 12-15行可以选择添加, 添加-std=c++11解决问题

2)/usr/lib/x86_64-linux-gnu/libGLEW.so.1.13
在这里插入图片描述解决方法:

在这里插入图片描述
查看相关文件下的内容:/usr/lib/x86_64-linux-gnu/libGLEW.so.1.10,改为libGLEW.so.1.10

3)build目录执行编译生成的可执行文件./x86_64/bin/detectnet时,出现问题
由于编译时出现的警告:opencv3.4和opencv3.2的版本冲突问题
在这里插入图片描述
解决方法

在这里插入图片描述
原来的 89 和 91行,自己查找系统的Opencv版本为3.4
添加92-96行,手动添加需要的opencv动态库

检测结果
在这里插入图片描述

参考博客
1)基于caffe框架复现yolov3目标检测
https://blog.csdn.net/Chen_yingpeng/article/details/80692018
github地址:https://github.com/ChenYingpeng/caffe-yolov3
2)Yolov3转化Caffe框架详解
https://blog.csdn.net/watermelon1123/article/details/82083522
githhub地址:https://github.com/marvis/pytorch-caffe-darknet-convert

猜你喜欢

转载自blog.csdn.net/nodototao/article/details/85711703
今日推荐