caffe 無GPU 環境搭建

weixin_33896726發表於2016-05-03
root@k-Lenovo:/home/k# sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
root@k-Lenovo:/home/k# sudo apt-get install --no-install-recommends libboost-all-dev

 

root@k-Lenovo:~# sudo apt-get install linux-headers-$(uname -r)

 

BLAS

root@k-Lenovo:/home/k# sudo apt-get install libatlas-base-dev

 其他依賴項

root@k-Lenovo:/home/k# sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev 

 OpenCV

root@k-Lenovo:/home/k# sudo apt-get install build-essential

 

root@k-Lenovo:/home/k# sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev 

 

root@k-Lenovo:/home/k# sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

 

1
2
3
4
git clone https://github.com/Itseez/opencv.git  
cd ~/opencv  
mkdir build  
cd build

 

http://pan.baidu.com/s/1c1PF9w0   #opencv下載

 

root@k-Lenovo:~/opencv/build# cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local  ..

 

root@k-Lenovo:~/opencv/build# ls
3rdparty               CPackSourceConfig.cmake  Makefile
apps                   CTestTestfile.cmake      modules
bin                    custom_hal.hpp           opencv2
CMakeCache.txt         cvconfig.h               OpenCVConfig.cmake
CMakeFiles             data                     OpenCVConfig-version.cmake
cmake_install.cmake    doc                      OpenCVModules.cmake
cmake_uninstall.cmake  include                  test-reports
CMakeVars.txt          junk                     unix-install
CPackConfig.cmake      lib                      version_string.tmp

 

root@k-Lenovo:~/opencv/build# make -j4

 

root@k-Lenovo:~/opencv/build#  sudo make install

 Caffe

root@k-Lenovo:~/caffe# ls
caffe.cloc       data      INSTALL.md               models     tools
cmake            docker    LICENSE                  python
CMakeLists.txt   docs      Makefile                 README.md
CONTRIBUTING.md  examples  Makefile.config.example  scripts
CONTRIBUTORS.md  include   matlab                   src
root@k-Lenovo:~/caffe# pwd
/root/caffe
root@k-Lenovo:~/caffe# cp Makefile.config.example Makefile.config

 

root@k-Lenovo:~/caffe# vim Makefile

 

endif
ifeq ($(USE_OPENCV), 1)
        LIBRARIES += opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs #增加opencv_imgcodecs 

        ifeq ($(OPENCV_VERSION), 3)
                LIBRARIES += opencv_imgcodecs
        endif

 

root@k-Lenovo:~/caffe# make all

 

root@k-Lenovo:~/caffe# make all
CXX src/caffe/common.cpp
In file included from ./include/caffe/common.hpp:19:0,
                 from src/caffe/common.cpp:7:
./include/caffe/util/device_alternate.hpp:34:23: fatal error: cublas_v2.h: No such file or directory
 #include <cublas_v2.h>
                       ^
compilation terminated.
make: *** [.build_release/src/caffe/common.o] Error 1

 

 

root@k-Lenovo:~/caffe# vi Makefile.config
# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := 1  #關鍵

 

root@k-Lenovo:~/caffe# make all

 

root@k-Lenovo:~/caffe# make test

 

root@k-Lenovo:~/caffe# make runtest

 

root@k-Lenovo:~/caffe# make runtest
.build_release/tools/caffe
.build_release/tools/caffe: error while loading shared libraries: libopencv_core.so.3.1: cannot open shared object file: No such file or directory
make: *** [runtest] Error 127

 

root@k-Lenovo:~/caffe# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

 

root@k-Lenovo:~/caffe# make runtest
... [----------] 1 test from SolverTest/1, where TypeParam = caffe::CPUDevice<double> [ RUN ] SolverTest/1.TestInitTrainTestNets [ OK ] SolverTest/1.TestInitTrainTestNets (1 ms) [----------] 1 test from SolverTest/1 (1 ms total) [----------] 2 tests from EuclideanLossLayerTest/1, where TypeParam = caffe::CPUDevice<double> [ RUN ] EuclideanLossLayerTest/1.TestGradient [ OK ] EuclideanLossLayerTest/1.TestGradient (0 ms) [ RUN ] EuclideanLossLayerTest/1.TestForward [ OK ] EuclideanLossLayerTest/1.TestForward (0 ms) [----------] 2 tests from EuclideanLossLayerTest/1 (0 ms total) [----------] Global test environment tear-down [==========] 1054 tests from 146 test cases ran. (37419 ms total) [ PASSED ] 1054 tests.

 安裝python和pip

root@k-Lenovo:~/caffe# python --version
Python 2.7.6

 pip是專門用於安裝python各種依賴庫的,所以我們這裡安裝一下pip1.5.6  http://pan.baidu.com/s/1cfsHv4

root@k-Lenovo:~# wget  https://pypi.python.org/packages/source/p/pip/pip-1.5.6.tar.gz
--2016-05-03 14:40:36--  https://pypi.python.org/packages/source/p/pip/pip-1.5.6.tar.gz
Resolving pypi.python.org (pypi.python.org)... 103.245.224.223
Connecting to pypi.python.org (pypi.python.org)|103.245.224.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 938120 (916K) [application/octet-stream]
Saving to: ‘pip-1.5.6.tar.gz’

100%[===================================================>] 938,120      294KB/s   in 3.1s   

2016-05-03 14:40:40 (294 KB/s) - ‘pip-1.5.6.tar.gz’ saved [938120/938120]

 

root@k-Lenovo:~# cd pip-1.5.6
root@k-Lenovo:~/pip-1.5.6# ls
AUTHORS.txt  docs         MANIFEST.in  pip.egg-info  PROJECT.txt  setup.cfg
CHANGES.txt  LICENSE.txt  pip          PKG-INFO      README.rst   setup.py

 

root@k-Lenovo:~/pip-1.5.6# sudo python setup.py install
Traceback (most recent call last):
  File "setup.py", line 6, in <module>
    from setuptools import setup, find_packages
ImportError: No module named setuptools

 有些電腦可能會提示 no moudle name setuptools 的錯誤,這是沒有安裝setuptools的原因。那就需要先安裝一下setuptools, 到https://pypi.python.org/packages/source/s/setuptools/setuptools-19.2.tar.gz 下載安裝包setuptools-19.2.tar.gz,然後解壓執行

http://pan.baidu.com/s/1dFx31Ax

root@k-Lenovo:~# wget https://pypi.python.org/packages/source/s/setuptools/setuptools-19.2.tar.gz
--2016-05-03 14:44:07--  https://pypi.python.org/packages/source/s/setuptools/setuptools-19.2.tar.gz
Resolving pypi.python.org (pypi.python.org)... 103.245.224.223
Connecting to pypi.python.org (pypi.python.org)|103.245.224.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 641777 (627K) [application/octet-stream]
Saving to: ‘setuptools-19.2.tar.gz’

100%[===================================================>] 641,777      287KB/s   in 2.2s   

2016-05-03 14:44:11 (287 KB/s) - ‘setuptools-19.2.tar.gz’ saved [641777/641777]

 

root@k-Lenovo:~# ls
caffe      opencv      pip-1.5.6         setuptools-19.2
caffe.tar  opencv.tar  pip-1.5.6.tar.gz  setuptools-19.2.tar.gz
root@k-Lenovo:~# cd setuptools-19.2
root@k-Lenovo:~/setuptools-19.2# ls
bootstrap.py     ez_setup.py  msvc-build-launcher.cmd  release.py  setuptools.egg-info
CHANGES.txt      launcher.c   PKG-INFO                 scripts     tests
conftest.py      Makefile     pkg_resources            setup.cfg   tox.ini
docs             MANIFEST.in  pytest.ini               setup.py
easy_install.py  _markerlib   README.txt               setuptools

 

root@k-Lenovo:~/setuptools-19.2# sudo python setup.py install

 

root@k-Lenovo:~# cd pip-1.5.6
root@k-Lenovo:~/pip-1.5.6# sudo python setup.py install

 

安裝pyhon介面依賴庫

在caffe根目錄的python資料夾下,有一個requirements.txt的清單檔案,上面列出了需要的依賴庫,按照這個清單安裝就可以了。

在安裝scipy庫的時候,需要fortran編譯器(gfortran),如果沒有這個編譯器就會報錯,因此,我們可以先安裝一下。

首先回到caffe的根目錄,然後執行安裝程式碼:

 

root@k-Lenovo:~/pip-1.5.6# cd ~/caffe
root@k-Lenovo:~/caffe# sudo apt-get install gfortran

 

root@k-Lenovo:~/caffe# cd python/
root@k-Lenovo:~/caffe/python# ls
caffe  classify.py  CMakeLists.txt  detect.py  draw_net.py  requirements.txt
root@k-Lenovo:~/caffe/python# for req in $(cat requirements.txt); do sudo pip install $req; done

 

root@k-Lenovo:~/caffe/python# cd ..
root@k-Lenovo:~/caffe# ls
build            CONTRIBUTORS.md  examples    Makefile.config          README.md
caffe.cloc       data             include     Makefile.config.example  scripts
cmake            distribute       INSTALL.md  matlab                   src
CMakeLists.txt   docker           LICENSE     models                   tools
CONTRIBUTING.md  docs             Makefile    python
root@k-Lenovo:~/caffe# sudo pip install -r python/requirements.txt

 

編譯python介面

首先,將caffe根目錄下的python資料夾加入到環境變數

開啟配置檔案bashrc

 

root@k-Lenovo:~/caffe# sudo vi ~/.bashrc

 

export PYTHONPATH=/root/caffe/python:$PYTHONPATH  #追加到最後

 

root@k-Lenovo:~/caffe/python# sudo ldconfig

 

root@k-Lenovo:~/caffe# sudo make pycaffe
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
touch python/caffe/proto/__init__.py
PROTOC (python) src/caffe/proto/caffe.proto

 檢視 python/caffe/ 目錄下,除了原先的一堆py字尾檔案,現在多出了一堆pyc字尾檔案

/root/caffe/python/caffe
root@k-Lenovo:~/caffe/python/caffe# cd ..
root@k-Lenovo:~/caffe/python# ls
caffe  classify.py  CMakeLists.txt  detect.py  draw_net.py  requirements.txt
root@k-Lenovo:~/caffe/python# python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
caffe/pycaffe.py:13: RuntimeWarning: to-Python converter for boost::shared_ptr<caffe::Net<float> > already registered; second conversion method ignored.
  from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
caffe/pycaffe.py:13: RuntimeWarning: to-Python converter for boost::shared_ptr<caffe::Blob<float> > already registered; second conversion method ignored.
  from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
caffe/pycaffe.py:13: RuntimeWarning: to-Python converter for boost::shared_ptr<caffe::Solver<float> > already registered; second conversion method ignored.
  from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
/usr/local/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
>>> 

 

root@k-Lenovo:~/caffe/python# cd caffe/
root@k-Lenovo:~/caffe/python/caffe# ls
_caffe.cpp     classifier.pyc  detector.pyc  __init__.py   io.pyc        proto        test
_caffe.so      coord_map.py    draw.py       __init__.pyc  net_spec.py   pycaffe.py
classifier.py  detector.py     imagenet      io.py         net_spec.pyc  pycaffe.pyc
安裝好caffe後,將caffe/python的路徑新增到使用者環境變數~/.bashrc中:
export PYTHONPATH=/root/caffe/python:$PYTHONPATH #這個是之前寫的

或者執行:

root@k-Lenovo:~/caffe# export PYTHONPATH=/root/caffe/python:$PYTHONPATH

 

 安裝jupyter

學會了python還不行,還得學習一下ipython,後者更加方便快捷,更有自動補全功能。而ipython notebook是ipython的最好展現方式。最新的版本改名為jupyter notebook,我們先來安裝一下。

 

root@k-Lenovo:~/caffe/python# sudo pip install jupyter

 安裝成功後,執行notebook

root@k-Lenovo:~/caffe/python# jupyter notebook

 

相關文章