Faster R-CNN在Linux环境下面的训练和检测相信很多感兴趣的人都可以根据下面
获得python版本的环境搭建指导。
但是Faster R-CNN工作在Windows却很少看到有人能够成功搭建训练和检测环境。
本文,主要介绍使用Linux环境下自定义训练模型在Windows环境下Faster R-CNN的python版本的目标检测移植。
采用pascal_voc格式制作自己的数据集,模型使用中等网络模型,使用的端到端的训练方法,测试的使用CPU进行测试。
Faster R-CNN移植需要分两部分:
1、Faster R-CNN Caffe移植:
第一步:安装caffe的python依赖环境,可以依据faster rcnn caffe目录下面python的requirements.txt文件下载对应的python模块:
Cython>=0.19.2
numpy>=1.7.1
scipy>=0.13.2
scikit-image>=0.9.3
matplotlib>=1.3.1
ipython>=3.0.0
h5py>=2.2.0
leveldb>=0.191
networkx>=1.8.1
nose>=1.3.0
pandas>=0.12.0
python-dateutil>=1.4,<2
protobuf>=2.5.0
python-gflags>=2.0
pyyaml>=3.10
Pillow>=2.3.0
six>=1.1.0
也可以安装Anaconda 来更快速获得相应的科学计算工具库。由于该网站下载速度极慢,我采用 了python2.7的版本安装。
一般通过pip和wheel两种方式即可完成所有包的安装;
第二步:移植faster rcnn caffe,默认从https://github.com/rbgirshick/caffe-fast-rcnn/tree/faster-rcnn
下面的faster rcnn caffe本身不支持Windows的,需要基于该源码进行移植。
可以根据官方caffe-window版本进行对比移植。请使用VS2013工程进行编译。
官方caffe-window版本:https://github.com/BVLC/caffe/tree/windows
基本上移植难度很小。
第三步:生成pycaffe:
编译libcaffe,pycaffe。
检查Build\x64\Release\pycaffe\caffe目录下是否拷贝了pycaffe依赖库。若没有,则需要采用下面bat脚本生成:
进入当前caffe-windows\windows\scripts\\目录下执行下面命令:
ProtoCompile.cmd D:\\Faster_RCNN\\caffe-windows\\windows\\ D:\\Faster_RCNN\\caffe-windows\\windows\\scripts\\
PythonPreBuild D:\\Faster_RCNN\\caffe-windows\\windows\\ D:\\Faster_RCNN\\caffe-windows\\windows\\scripts\\
PythonPostBuild D:\\Faster_RCNN\\caffe-windows\\windows\\ D:\\Faster_RCNN\\caffe-windows\\Build\\x64\\Release\\
2、Faster R-CNN目标检测移植:
第一步:
#from nms.gpu_nms import gpu_nms
def nms(dets, thresh, force_cpu=True):
ext_modules = [
# unix _compile: obj, src, ext, cc_args, extra_postargs, pp_opts
Extension(
"utils.cython_bbox",
sources=["utils\\bbox.pyx"],
extra_compile_args={'gcc': []},
include_dirs = [numpy_include]
),
Extension(
"nms.cpu_nms",
sources=["nms\\cpu_nms.pyx"],
extra_compile_args={'gcc': []},
include_dirs = [numpy_include],
)
]
def compile(sources, output_dir=None, macros=None, include_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, depends=None):
postfix=os.path.splitext(sources[0])[1]
if postfix == '.cu':
# use the cuda for .cu files
#self.set_executable('compiler_so', CUDA['nvcc'])
# use only a subset of the extra_postargs, which are 1-1 translated
# from the extra_compile_args in the Extension class
postargs = extra_postargs['nvcc']
else:
postargs = extra_postargs['gcc']
return super(sources, output_dir, macros, include_dirs, debug, extra_preargs, postargs, depends)
cdef np.ndarray[np.int64_t, ndim=1] order = scores.argsort()[::-1]
第二步:修改demo
CLASSES = ('__background__',
'aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair',
'cow', 'diningtable', 'dog', 'horse',
'motorbike', 'person', 'pottedplant',
'sheep', 'sofa', 'train', 'tvmonitor')
CLASSES = ('__background__',
'flags')
NETS = {'vgg16': ('VGG16',
'vgg16_fast_rcnn_iter_40000.caffemodel'),
'vgg_cnn_m_1024': ('VGG_CNN_M_1024',
'vgg_cnn_m_1024_faster_rcnn_iter_40000.caffemodel'),
'zf': ('ZF',
'ZF_faster_rcnn_final.caffemodel')}
第三步:修改测试模型
In the train.prototxt and test.prototxt files that you're using,
you'll need to change num_output from 21 to 3 in the cls_score layer and from 84 to 12 in the bbox_pred layer.
You'll also need to change num_classes from 21 to 3 in the Python layer that provides data to the net (the very first layer).
在使用训练的模型测试的时候没有任何输出时就需要考虑Linux环境和Windows环境的差别了,此处我遇到过这种情况之一是由于模型加载不完整导致的该问题,具体修改可以参考如下:
http://blog.csdn.net/seasermy/article/details/51509571
修改内容:在io.cpp中增加windows的支持:
#ifdef _MSC_VER
int fd = open(filename, O_RDONLY|O_BINARY);
#else
int fd = open(filename, O_RDONLY);
#endif