研究Fast rcnn代码
2016-09-29 17:06
357 查看
原文:http://arxiv.org/pdf/1504.08083.pdf
代码:https://github.com/rbgirshick/fast-rcnn
1.准备工作
1.1 软件准备
首先,需要安装Caffe和pycaffe。
caffe原作者网页:http://caffe.berkeleyvision.org/installation.html
欧新宇师兄的caffe安装说明:http://ouxinyu.github.io/Blogs/20140723001.html
注意:必须在 Makefile.config配置文件中打开Python层支持。
# In your Makefile.config, make sure to have this line uncommented
WITH_PYTHON_LAYER := 1
其次,可能需要Python安装包:cython,python-OpenCV,easydict
先装一个python包管理器pip:
sudo apt-get install python-pip
再装那三个包:
sudo
pip install cython
#sudo
pip install python-opencv
sudo pip install easydict
再次,可能需要MATLAB,主要用于对PASCAL
voc数据集的评估。
1.2 硬件准备
对于训练较小的网络(CaffeNet,
VGG_CNN_M_1024),至少需要3G内存的GPU(如:Titan,K20,K40...)
对于训练VGG16,至少需要一个K40(约11G内存),这里我们就不考虑了。
2.安装(用于demo)
2.1 从github上clone到Fast
RCNN的仓库。最好就直接这么clone,不要自己去下载,不然还满麻烦的。
# Make sure to clone with --recursive
Git clone
--recursive https://github.com/rbgirshick/fast-rcnn.git
2.2 生成Cython模块(下面的$FRCN_ROOT都是指fast-rcnn的解压位置)
cd $FRCN_ROOT/lib
make
2.3 生成Caffe和pycaffe
cd $FRCN_ROOT/caffe-fast-rcnn
# Now follow the Caffe installation instructions here:
# http://caffe.berkeleyvision.org/installation.html
# If you're experienced with Caffe and have all of the requirements installed
# and your Makefile.config in place, then simply do:
make -j8 && make pycaffe
2.4 下载Fast RCNN检测器
cd $FRCN_ROOT
./data/scripts/fetch_fast_rcnn_models.sh
3.运行demo
3.1 Python版
cd $FRCN_ROOT
./tools/demo.py
可能我安装了cudnn,所以即使2G内存的GPU也是可以的。
如果用CPU模式,就是
cd $FRCN_ROOT
./tools/demo.py --cpu
显然是慢很多的。效果图如下所示:
demo中是用VGG16网络,在PASCAL
VOC2007上训练的模型来执行检测的,这个模型比较大,如果把caffe弄崩溃了,可以换一个小一点的网络,其实还更快一点,如
./tools/demo.py --net caffenet
或者
./tools/demo.py --net vgg_cnn_m_1024
或者就用CPU模式好了。
3.2 MATLAB版(暂时没找到编译好的caffe,现在先不管)
在matlab文件夹下打开matlab,下面是我的matlab的安装地址。
cd $FRCN_ROOT/matlab
/usr/local/MATLAB/R2014a/bin/matlab #
wait for matlab to start...
把$FRCN_ROOT/caffe-fast-rcnn/matlab下的caffe文件夹拷贝到$FRCN_ROOT/matlab中,为防止内存不够,我们还是以CaffeNet为例,把fast-rcnn-demo.m中的所有VGG16改为CaffeNet。在matlab命令行下输入命令:
>> fast_rcnn_demo
3.3 一些获取object
proposal的算法代码
Selective Search: original
matlab code, python wrapper
EdgeBoxes: matlab
code
GOP and LPO: python
code
MCG: matlab
code
RIGOR: matlab
code
4.准备数据集
4.1 首先要下载训练集、验证集、测试集,例子是VOC2007。资源在墙外,将给出百度云盘中的地址。
wget http://pascallin.ecs.soton.ac.uk/challenges/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
wget http://pascallin.ecs.soton.ac.uk/challenges/VOC/voc2007/VOCtest_06-Nov-2007.tar
wget http://pascallin.ecs.soton.ac.uk/challenges/VOC/voc2007/VOCdevkit_08-Jun-2007.tar
4.2 提取所有压缩包到同一个下面称为$VOCdevkit的文件夹下。
tar xvf VOCtrainval_06-Nov-2007.tar
tar xvf VOCtest_06-Nov-2007.tar
tar xvf VOCdevkit_08-Jun-2007.tar
要有这些基本的目录:
$VOCdevkit/ #
development kit
$VOCdevkit/VOCcode/ #
VOC utility code
$VOCdevkit/VOC2007 #
image sets, annotations, etc.
4.3 创建对VOC2007数据集的symlink,也就是链接FRCN_ROOT和VOC2007的目录。
cd $FRCN_ROOT/data
ln -s $VOCdevkit VOCdevkit2007
这个方法非常好,因为别的工程里面也可能用到这个数据集,这样就不用多次拷贝了,节省了很多存储空间,windows下面就没有。
4.4 可以再用同样的办法得到VOC2010和2012的数据集,如果有需要的话。
4.5 下载预先用selective
search计算好的object proposal。
cd $FRCN_ROOT
./data/scripts/fetch_selective_search_data.sh
会下载到$FRCN_ROOT/data下,解压后是一个名为selective_search_data的文件夹。
4.6 下载预先训练好的ImageNet模型。
cd $FRCN_ROOT
./data/scripts/fetch_imagenet_models.sh
下载到三个模型,分别是CaffeNet
(model S), VGG_CNN_M_1024 (model M), and VGG16 (model L),会下载到$FRCN_ROOT/data下,解压后是一个名为imagenet_models的文件夹。
5.模型的训练与测试
5.1 训练模型
训练Fast
R-CNN检测器,以在VOC2007上训练一个CaffeNet 的网络为例。
./tools/train_net.py --gpu 0 --solver models/CaffeNet/solver.prototxt --weights data/imagenet_models/CaffeNet.v2.caffemodel
这里我出现了EnvironmentError:
MATLAB command 'matlab' not found. Please add 'matlab' to your PATH.这种错误,说明没把matlab的路径添加到环境变量中,下面的语句设置环境变量:
export PATH=$PATH:"/usr/local/MATLAB/R2014a/bin"
又提示说ImportError: No module named yaml,那就下载安装一个:
sudo apt-get install python-yaml
再次运行代码就可以了。如果显示内存不够,可以用nvidia-smi随时查看内存使用情况。每10000次迭代会生成一个model,结果存放在output文件夹中。
训练VGG_CNN_M_1024网络时,会提示说内存不够,就把$FRCN_ROOT/lib/fast_rcnn下的config.py中每个minibatch所用的图片由2改为1,如果还不行,说明GPU内存太小,只能换GPU了。
./tools/train_net.py --gpu 0 --solver models/VGG_CNN_M_1024/solver.prototxt --weights data/imagenet_models/VGG_CNN_M_1024.v2.caffemodel
训练 VGG16网络,据作者说,即使把每个minibatch所用的图片由2改为1,也需要将近5G的GPU内存,3G以上内存的可以尝试一下,cudnn可能在一定程度上起到了优化作用。
5.2 测试模型
在自己的模型还没有训练好,或者训练得不够好的时候,可以试试作者提供的模型:
./tools/test_net.py --gpu 0 --def models/CaffeNet/test.prototxt --net
data/fast_rcnn_models/caffenet_fast_rcnn_iter_40000.caffemodel
在测试的时候一直报下面这样的错,困扰了很久,找到原因后觉得自己蠢哭了。
把VOCevaldet中相应文件名输出来,发现问题出在VOCinit上,我们现在是在测试,把
第一句注释掉,第二句取消注释。
下面再测试自己的模型:
./tools/test_net.py --gpu 0 --def models/CaffeNet/test.prototxt --net
output/default/voc_2007_trainval/caffenet_fast_rcnn_iter_40000.caffemodel
测试的结果也在output文件夹中。
5.3 用全连接层压缩的SVD来压缩FRCNN模型
./tools/compress_net.py --def models/CaffeNet/test.prototxt --def-svd models/CaffeNet/compressed/test.prototxt
--net output/default/voc_2007_trainval/caffenet_fast_rcnn_iter_40000.caffemodel
压缩后的模型和压缩前的模型是放在一起的,只是名字不一样,在output下的相应文件夹下。再测试这个压缩后的模型:
./tools/test_net.py --gpu 0 --def models/CaffeNet/compressed/test.prototxt --net output/default/voc_2007_trainval/vcaffenet_fast_rcnn_iter_40000_svd_fc6_1024_fc7_256.caffemodel
好像是快了一些吧,反正这也不是重点。
附录
1.$FRCN_ROOT/experiments/scripts下的这些脚本可以再现作者论文中的实验,有兴趣的话可以试一下。
2.日志文件下载地址:Experiment
logs
3.PASCAL VOC的一些检测结果
voc_2007_test_results_fast_rcnn_caffenet_trained_on_2007_trainval.tgzvoc_2007_test_results_fast_rcnn_vgg16_trained_on_2007_trainval.tgzvoc_2007_test_results_fast_rcnn_vgg_cnn_m_1024_trained_on_2007_trainval.tgz voc_2012_test_results_fast_rcnn_vgg16_trained_on_2007_trainvaltest_2012_trainval.tgzvoc_2012_test_results_fast_rcnn_vgg16_trained_on_2012_trainval.tgz
4.Fast
R-CNN VGG16 modeltrained on VOC07 train,val,test union with VOC12 train,val
最后,为什么自己用selective
search生成的bounding boxes就再难再现demo里面那么好的效果,而作者目前也没给出回应。
感谢欧新宇师兄的热心指导。
代码:https://github.com/rbgirshick/fast-rcnn
1.准备工作
1.1 软件准备
首先,需要安装Caffe和pycaffe。
caffe原作者网页:http://caffe.berkeleyvision.org/installation.html
欧新宇师兄的caffe安装说明:http://ouxinyu.github.io/Blogs/20140723001.html
注意:必须在 Makefile.config配置文件中打开Python层支持。
# In your Makefile.config, make sure to have this line uncommented
WITH_PYTHON_LAYER := 1
其次,可能需要Python安装包:cython,python-OpenCV,easydict
先装一个python包管理器pip:
sudo apt-get install python-pip
再装那三个包:
sudo
pip install cython
#sudo
pip install python-opencv
sudo pip install easydict
再次,可能需要MATLAB,主要用于对PASCAL
voc数据集的评估。
1.2 硬件准备
对于训练较小的网络(CaffeNet,
VGG_CNN_M_1024),至少需要3G内存的GPU(如:Titan,K20,K40...)
对于训练VGG16,至少需要一个K40(约11G内存),这里我们就不考虑了。
2.安装(用于demo)
2.1 从github上clone到Fast
RCNN的仓库。最好就直接这么clone,不要自己去下载,不然还满麻烦的。
# Make sure to clone with --recursive
Git clone
--recursive https://github.com/rbgirshick/fast-rcnn.git
2.2 生成Cython模块(下面的$FRCN_ROOT都是指fast-rcnn的解压位置)
cd $FRCN_ROOT/lib
make
2.3 生成Caffe和pycaffe
cd $FRCN_ROOT/caffe-fast-rcnn
# Now follow the Caffe installation instructions here:
# http://caffe.berkeleyvision.org/installation.html
# If you're experienced with Caffe and have all of the requirements installed
# and your Makefile.config in place, then simply do:
make -j8 && make pycaffe
2.4 下载Fast RCNN检测器
cd $FRCN_ROOT
./data/scripts/fetch_fast_rcnn_models.sh
3.运行demo
3.1 Python版
cd $FRCN_ROOT
./tools/demo.py
可能我安装了cudnn,所以即使2G内存的GPU也是可以的。
如果用CPU模式,就是
cd $FRCN_ROOT
./tools/demo.py --cpu
显然是慢很多的。效果图如下所示:
demo中是用VGG16网络,在PASCAL
VOC2007上训练的模型来执行检测的,这个模型比较大,如果把caffe弄崩溃了,可以换一个小一点的网络,其实还更快一点,如
./tools/demo.py --net caffenet
或者
./tools/demo.py --net vgg_cnn_m_1024
或者就用CPU模式好了。
3.2 MATLAB版(暂时没找到编译好的caffe,现在先不管)
在matlab文件夹下打开matlab,下面是我的matlab的安装地址。
cd $FRCN_ROOT/matlab
/usr/local/MATLAB/R2014a/bin/matlab #
wait for matlab to start...
把$FRCN_ROOT/caffe-fast-rcnn/matlab下的caffe文件夹拷贝到$FRCN_ROOT/matlab中,为防止内存不够,我们还是以CaffeNet为例,把fast-rcnn-demo.m中的所有VGG16改为CaffeNet。在matlab命令行下输入命令:
>> fast_rcnn_demo
3.3 一些获取object
proposal的算法代码
Selective Search: original
matlab code, python wrapper
EdgeBoxes: matlab
code
GOP and LPO: python
code
MCG: matlab
code
RIGOR: matlab
code
4.准备数据集
4.1 首先要下载训练集、验证集、测试集,例子是VOC2007。资源在墙外,将给出百度云盘中的地址。
wget http://pascallin.ecs.soton.ac.uk/challenges/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
wget http://pascallin.ecs.soton.ac.uk/challenges/VOC/voc2007/VOCtest_06-Nov-2007.tar
wget http://pascallin.ecs.soton.ac.uk/challenges/VOC/voc2007/VOCdevkit_08-Jun-2007.tar
4.2 提取所有压缩包到同一个下面称为$VOCdevkit的文件夹下。
tar xvf VOCtrainval_06-Nov-2007.tar
tar xvf VOCtest_06-Nov-2007.tar
tar xvf VOCdevkit_08-Jun-2007.tar
要有这些基本的目录:
$VOCdevkit/ #
development kit
$VOCdevkit/VOCcode/ #
VOC utility code
$VOCdevkit/VOC2007 #
image sets, annotations, etc.
4.3 创建对VOC2007数据集的symlink,也就是链接FRCN_ROOT和VOC2007的目录。
cd $FRCN_ROOT/data
ln -s $VOCdevkit VOCdevkit2007
这个方法非常好,因为别的工程里面也可能用到这个数据集,这样就不用多次拷贝了,节省了很多存储空间,windows下面就没有。
4.4 可以再用同样的办法得到VOC2010和2012的数据集,如果有需要的话。
4.5 下载预先用selective
search计算好的object proposal。
cd $FRCN_ROOT
./data/scripts/fetch_selective_search_data.sh
会下载到$FRCN_ROOT/data下,解压后是一个名为selective_search_data的文件夹。
4.6 下载预先训练好的ImageNet模型。
cd $FRCN_ROOT
./data/scripts/fetch_imagenet_models.sh
下载到三个模型,分别是CaffeNet
(model S), VGG_CNN_M_1024 (model M), and VGG16 (model L),会下载到$FRCN_ROOT/data下,解压后是一个名为imagenet_models的文件夹。
5.模型的训练与测试
5.1 训练模型
训练Fast
R-CNN检测器,以在VOC2007上训练一个CaffeNet 的网络为例。
./tools/train_net.py --gpu 0 --solver models/CaffeNet/solver.prototxt --weights data/imagenet_models/CaffeNet.v2.caffemodel
这里我出现了EnvironmentError:
MATLAB command 'matlab' not found. Please add 'matlab' to your PATH.这种错误,说明没把matlab的路径添加到环境变量中,下面的语句设置环境变量:
export PATH=$PATH:"/usr/local/MATLAB/R2014a/bin"
又提示说ImportError: No module named yaml,那就下载安装一个:
sudo apt-get install python-yaml
再次运行代码就可以了。如果显示内存不够,可以用nvidia-smi随时查看内存使用情况。每10000次迭代会生成一个model,结果存放在output文件夹中。
训练VGG_CNN_M_1024网络时,会提示说内存不够,就把$FRCN_ROOT/lib/fast_rcnn下的config.py中每个minibatch所用的图片由2改为1,如果还不行,说明GPU内存太小,只能换GPU了。
./tools/train_net.py --gpu 0 --solver models/VGG_CNN_M_1024/solver.prototxt --weights data/imagenet_models/VGG_CNN_M_1024.v2.caffemodel
训练 VGG16网络,据作者说,即使把每个minibatch所用的图片由2改为1,也需要将近5G的GPU内存,3G以上内存的可以尝试一下,cudnn可能在一定程度上起到了优化作用。
5.2 测试模型
在自己的模型还没有训练好,或者训练得不够好的时候,可以试试作者提供的模型:
./tools/test_net.py --gpu 0 --def models/CaffeNet/test.prototxt --net
data/fast_rcnn_models/caffenet_fast_rcnn_iter_40000.caffemodel
在测试的时候一直报下面这样的错,困扰了很久,找到原因后觉得自己蠢哭了。
把VOCevaldet中相应文件名输出来,发现问题出在VOCinit上,我们现在是在测试,把
第一句注释掉,第二句取消注释。
下面再测试自己的模型:
./tools/test_net.py --gpu 0 --def models/CaffeNet/test.prototxt --net
output/default/voc_2007_trainval/caffenet_fast_rcnn_iter_40000.caffemodel
测试的结果也在output文件夹中。
5.3 用全连接层压缩的SVD来压缩FRCNN模型
./tools/compress_net.py --def models/CaffeNet/test.prototxt --def-svd models/CaffeNet/compressed/test.prototxt
--net output/default/voc_2007_trainval/caffenet_fast_rcnn_iter_40000.caffemodel
压缩后的模型和压缩前的模型是放在一起的,只是名字不一样,在output下的相应文件夹下。再测试这个压缩后的模型:
./tools/test_net.py --gpu 0 --def models/CaffeNet/compressed/test.prototxt --net output/default/voc_2007_trainval/vcaffenet_fast_rcnn_iter_40000_svd_fc6_1024_fc7_256.caffemodel
好像是快了一些吧,反正这也不是重点。
附录
1.$FRCN_ROOT/experiments/scripts下的这些脚本可以再现作者论文中的实验,有兴趣的话可以试一下。
2.日志文件下载地址:Experiment
logs
3.PASCAL VOC的一些检测结果
voc_2007_test_results_fast_rcnn_caffenet_trained_on_2007_trainval.tgzvoc_2007_test_results_fast_rcnn_vgg16_trained_on_2007_trainval.tgzvoc_2007_test_results_fast_rcnn_vgg_cnn_m_1024_trained_on_2007_trainval.tgz voc_2012_test_results_fast_rcnn_vgg16_trained_on_2007_trainvaltest_2012_trainval.tgzvoc_2012_test_results_fast_rcnn_vgg16_trained_on_2012_trainval.tgz
4.Fast
R-CNN VGG16 modeltrained on VOC07 train,val,test union with VOC12 train,val
最后,为什么自己用selective
search生成的bounding boxes就再难再现demo里面那么好的效果,而作者目前也没给出回应。
感谢欧新宇师兄的热心指导。
相关文章推荐
- LeetCode 258. Add Digits 题解(C++)
- C++如何读取任意数目的整数
- C#IO流总结
- Java abstract class 和 interface 的区别
- C语言中预处理器的相关知识:
- django模型中的抽象类(abstract)
- 对JAVA集合进行遍历删除时务必要用迭代器
- 聊聊并发(三)——JAVA线程池的分析和使用
- 第30个python程序:else和if
- Java数据类型转换(自动转换和强制转换)
- java中反射的三种方法
- 《汇编语言》学习(四)汇编程序
- Java API 1.8源码--【集合类 collection】
- Spring-MVC第一弹简单项目搭建
- python中easy_install 卸载已经安装的包
- Java API 1.8源码--【集合类 collection】
- 使用java语言实现各种排序算法
- C#使用MemoryStream类读写内存
- Spring Boot中使用Swagger2构建强大的RESTful API文档
- 使用springfox整合SpringMVC和Swagger