关闭

Faster R-CNN CPU环境搭建(h 参考 成功)

标签: python深度学习
629人阅读 评论(0) 收藏 举报
分类:

操作系统:

bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/tools$ cat /etc/issue
Ubuntu 14.04.2 LTS \n \l

Python版本:

bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/tools$ python --version
Python 2.7.6

pip版本:

bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/tools$ pip --version
pip 1.5.4 from /usr/lib/python2.7/dist-packages (python 2.7)

环境变量情况:

bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/caffe-fast-rcnn$ echo $LD_LIBRARY_PATH

bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/caffe-fast-rcnn$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

~/.bashrc内容,可以看到所有和PATH以及LD_LIBRARY_PATH相关的内容都没有设置:

# ~/.bashrc: executed by bash(1) for non-login shells.
# added by Anaconda2 4.0.0 installer
#export PATH="/home/bigtop/anaconda2/bin:$PATH"
#export LD_LIBRARY_PATH="/home/bigtop/anaconda2/lib/":$LD_LIBRARY_PATH
#export LD_LIBRARY_PATH="/lib/x86_64-linux-gnu/":$LD_LIBRARY_PATH

 

1. 安装Caffe需要的依赖包:

sudo apt-get install build-essential  # basic requirement
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler #required by caffe

image

 

使用完上面的命令后,依赖已经安装完毕,但是由于Ubuntu 14.04版本的原因,导致opencv相关的环境不能够正常的work。所以,我重新编译了一个OpenCV,版本为3.1.0。

image

 

在解压后的目录中执行:

bigtop@bigtop-SdcOS-Hypervisor:~/tools/opencv-3.1.0$  cmake -DBUILD_TIFF=ON

然后执行make 和make install

2. 编译cafe-fast-rcnn

复制代码
bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/caffe-fast-rcnn$ ls
build       CMakeLists.txt   data        examples    LICENSE          Makefile.config~         models     scripts
caffe.cloc  CONTRIBUTING.md  distribute  include     Makefile         Makefile.config.example  python     src
cmake       CONTRIBUTORS.md  docs        INSTALL.md  Makefile.config  matlab                   README.md  tools
bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/caffe-fast-rcnn$ pwd
/home/bigtop/py-faster-rcnn/caffe-fast-rcnn
复制代码

修改这个目录下的Makefile.config(如果没有这个文件,就直接cp Makefile.config.example Makefile.config)

将CPU_ONLY := 1开关和WITH_PYTHON_LAYER开关打开:

image

image

然后在该目录下执行:make –j8 && make pycaffe

在此过程中,可能会出现各种和python相关的包缺失问题,这里记录下,以便查询:

A》将caffe-fast-rcnn/python目录下的requirements下的依赖都装一遍:

image

复制代码
bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/caffe-fast-rcnn/python$ cat requirements.txt 
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
复制代码

执行如下命令:

for req in $(cat requirements.txt); do pip install $req; done

这里有一个小技巧,因为pip这个工具对应的网络非常的烂:

Image

这个时候,可以将其改为国内的镜像网站,速度将提升几个数量级,方法如下:

新建~/.pip/pip.confg文件,内容如下:

[global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com

或者在安装一个软件包的时候使用-i选项:

Image

在我安装requirements.txt中涉及的依赖包的过程中,发现matplotlib始终没有安装成功,最后采用apt-get的方式进行了安装,如下:

sudo apt-get install python-matplotlib

B>opencv环境和caffe-fast-rcnn默认的Makefile配置有点小问题,cv::imread(cv:: String const&, int)找不到:

Image

解决方案:

Image

Image

 

在一切都正常的情况下,对caffe-fast-rcnn进行make和make pycaffe的结果如下:

Image

编译好caffe-fast-rcnn后,在py-faster-rcnn/lib中执行make命令:

复制代码
bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/lib$ make
python setup.py build_ext --inplace
running build_ext
skipping 'utils/bbox.c' Cython extension (up-to-date)
skipping 'nms/cpu_nms.c' Cython extension (up-to-date)
skipping 'pycocotools/_mask.c' Cython extension (up-to-date)
rm -rf build
bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/lib$
复制代码

 

3. 在安装配置好caffe-fast-rcnn后,修改py-faster-rcnn相关配置,让其模型可以在没有GPU的环境下运行:

A>将 ~/py-faster-rcnn/lib/fast_rcnn/config.py的如下内容:

image

B>将 ~/py-faster-rcnn/tools/test_net.py和 ~/py-faster-rcnn/tools/train_net.py的caffe.set_mode_gpu()修改为caffe.set_mode_cpu().

image

image

C>将~/py-faster-rcnn/lib/setup.py中,含有'nms.gpu_nms’的部分去掉,去掉后的内容如下:

复制代码
112 ext_modules = [
113     Extension(
114         "utils.cython_bbox",
115         ["utils/bbox.pyx"],
116         extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
117         include_dirs = [numpy_include]
118     ),
119     Extension(
120         "nms.cpu_nms",
121         ["nms/cpu_nms.pyx"],
122         extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
123         include_dirs = [numpy_include]
124     ),
125     Extension(
126         'pycocotools._mask',
127         sources=['pycocotools/maskApi.c', 'pycocotools/_mask.pyx'],
128         include_dirs = [numpy_include, 'pycocotools'],
129         extra_compile_args={
130             'gcc': ['-Wno-cpp', '-Wno-unused-function', '-std=c99']},
131     ),
132 ]
复制代码

D>做到上面三部后,还是不够的,还需要将:

../lib/fast_rcnn/nms_wrapper.py:9:#from nms.gpu_nms import gpu_nms

注释掉:

image

否则,会抛出如下的异常:

复制代码
Traceback (most recent call last):
File "./demo.py", line 18, in 
from fast_rcnn.test import im_detect
File ".../py-faster-rcnn-master/tools/../lib/fast_rcnn/test.py", line 17, in 
from fast_rcnn.nms_wrapper import nms
File ".../py-faster-rcnn-master/tools/../lib/fast_rcnn/nms_wrapper.py", line 11, in 
from nms.gpu_nms import gpu_nms
ImportError: No module named gpu_nms
复制代码

 

4. 运行demo.py

在环境一切就绪的情况下,将faster的模型下载下来:

bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/data/scripts$ ls
fetch_faster_rcnn_models.sh  fetch_imagenet_models.sh  fetch_selective_search_data.sh

运行其中的./fetch_faster_rcnn_models.sh脚本就可以下载下来了。

在/home/bigtop/py-faster-rcnn/tools目录下运行, python demo.py --cpu:

python demo.py --cpu --net zf  (运行ZF模型)

最后的结果如下:

 

1

2

 

image

 

image

image

 

注意:由于我是在没有图形界面终端上运行的,默认情况下demo.py会假设运行在有图形界面的环境中,需要修改demo.py的地方如下:

首先,在demo.py代码的最前面,注意一定是最前面,否则可能不成功,加入如下两行:

image

其次,在plt.draw()的地方加入savefig()语句,将结果保存成jpg文件形式:

image

 

5. 其他在安装过程中遇到的问题(比较杂,记录于此),如果上面的四个步骤进行的比较顺利的话,是不会遇到下面这些问题的:

5.1 No module named skimage.io:

Image

 

5.2 下面这个问题是因为缺少,easydict,使用 sudo pip install easydict可以解决:

复制代码
bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/tools$ python demo.py  --cpu
Traceback (most recent call last):
  File "demo.py", line 17, in <module>
    from fast_rcnn.config import cfg
  File "/home/bigtop/py-faster-rcnn/tools/../lib/fast_rcnn/config.py", line 23, in <module>
    from easydict import EasyDict as edict
ImportError: No module named easydict
bigtop@bigtop-SdcOS-Hypervisor:~/py-faster-rcnn/tools$ sudo pip install  easydict
Downloading/unpacking easydict
  Downloading easydict-1.6.zip
  Running setup.py (path:/tmp/pip_build_root/easydict/setup.py) egg_info for package easydict

Installing collected packages: easydict
  Running setup.py install for easydict

  Could not find .egg-info directory in install record for easydict
Successfully installed easydict
Cleaning up...

sudo pip install  easydict
复制代码

 

5.3 这个问题是因为scipy安装出现问题,将其删掉:rm -fr /tmp/pip_build_root/scipy/,然后重新安装可以解决:

复制代码
d --compile failed with error code 1 in /tmp/pip_build_root/scipy
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    load_entry_point('pip==1.5.4', 'console_scripts', 'pip')()
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 235, in main
    return command.main(cmd_args)
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 161, in main
    text = '\n'.join(complete_log)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 98: ordinal not in range(128)
复制代码

 

网上有很多搭建caffe的教程,都提到了用Anaconda,本来这个包是很好的,它可以解决很多python依赖的问题,可惜的是,它和我用的Ubuntu版本兼容性出现了问题,所以,我最终放弃了Anaconda,所有的python依赖都通过pip或者是apt-get进行了安装。

 

5.4 报下面这个错误,是因为caffe的环境都没有准备好,很有可能是没有执行make pycaffe:

复制代码
Traceback (most recent call last):
  File "detector.py", line 29, in <module>
    import caffe
  File "/python/caffe/__init__.py", line 1, in <module>
    from .pycaffe import Net
  File "/caffe/pycaffe.py", line 6, in <module>
    from ._caffe import CaffeNet
ImportError: No module named _caffe
复制代码

 

5.5 error: undefined reference to `TIFFIsTiled@LIBTIFF_4.0'

error: undefined reference to `TIFFIsTiled@LIBTIFF_4.0'

这个就是上文中提到的,使用ubuntu自带的opencv库会出现的问题,解决办法就是重新编译opencv。

 

6. 总结

整个过程还是颇费周折,因为caffe依赖的东西太多,环境搭建费事费力,最好的办法还是弄一个docker镜像,这样才能够从环境搭建的苦海中解脱,可惜的是我从daocloud上down下来的镜像是不能够运行在cpu上的。

 

 

参考文档:

1. http://caffe.berkeleyvision.org/installation.html  caffe官方文档

2. https://github.com/BVLC/caffe/issues/1276  cv::imread(cv::String const&, int)' collect2: error

3.https://github.com/BVLC/caffe/issues/263  can't import _caffe module

4.https://github.com/rbgirshick/py-faster-rcnn/issues/8 ImportError: No module named gpu_nms

5.http://www.cnblogs.com/empty16/p/4828476.html caffe环境搭建文章

6.https://github.com/BVLC/caffe/issues/50 make pycaffe error

7.http://blog.csdn.net/tangwei2014/article/details/45442275 Fast RCNN Ubuntu安装笔记

8.http://topmanopensource.iteye.com/blog/2004853 pip镜像加速

9. https://github.com/BVLC/caffe/issues/1276 ../lib/libcaffe.so: undefined reference tocv::imread

10. https://groups.google.com/forum/#!topic/caffe-users/wKYe45FKSqE  Installation error: undefined reference to `TIFFIsTiled@LIBTIFF_4.0'

11. https://groups.google.com/forum/#!topic/caffe-users/0PrZlro7QbU  undefined reference to `lzma_index_buffer_decode@XZ_5.0

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:139397次
    • 积分:2249
    • 等级:
    • 排名:第16792名
    • 原创:50篇
    • 转载:241篇
    • 译文:0篇
    • 评论:18条
    最新评论