caffe 无GPU 环境搭建

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

 

posted @ 2016-05-03 15:30  bass  阅读(6517)  评论(0编辑  收藏  举报