Faster R-CNN CPU環境搭建


操作系統:

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:

Image

最后的結果如下:

 

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 to cv::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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM