你的位置:首页 > 操作系统

[操作系统]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

 

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

 

root@k-Lenovo:~/opencv/build# ls3rdparty        CPackSourceConfig.cmake Makefileapps          CTestTestfile.cmake   modulesbin          custom_hal.hpp      opencv2CMakeCache.txt     cvconfig.h        OpenCVConfig.cmakeCMakeFiles       data           OpenCVConfig-version.cmakecmake_install.cmake  doc           OpenCVModules.cmakecmake_uninstall.cmake include         test-reportsCMakeVars.txt     junk           unix-installCPackConfig.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# lscaffe.cloc    data   INSTALL.md        models   toolscmake      docker  LICENSE         pythonCMakeLists.txt  docs   Makefile         README.mdCONTRIBUTING.md examples Makefile.config.example scriptsCONTRIBUTORS.md include  matlab          srcroot@k-Lenovo:~/caffe# pwd/root/cafferoot@k-Lenovo:~/caffe# cp Makefile.config.example Makefile

 

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

 

endififeq ($(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 allCXX src/caffe/common.cppIn 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 directorymake: *** [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 --versionPython 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.gzResolving pypi.python.org (pypi.python.org)... 103.245.224.223Connecting to pypi.python.org (pypi.python.org)|103.245.224.223|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 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.6root@k-Lenovo:~/pip-1.5.6# lsAUTHORS.txt docs     MANIFEST.in pip.egg-info PROJECT.txt setup.cfgCHANGES.txt LICENSE.txt pip     PKG-INFO   README.rst  setup.py

 

root@k-Lenovo:~/pip-1.5.6# sudo python setup.py installTraceback (most recent call last): File "setup.py", line 6, in <module>  from setuptools import setup, find_packagesImportError: 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.gzResolving pypi.python.org (pypi.python.org)... 103.245.224.223Connecting to pypi.python.org (pypi.python.org)|103.245.224.223|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 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:~# lscaffe   opencv   pip-1.5.6     setuptools-19.2caffe.tar opencv.tar pip-1.5.6.tar.gz setuptools-19.2.tar.gzroot@k-Lenovo:~# cd setuptools-19.2root@k-Lenovo:~/setuptools-19.2# lsbootstrap.py   ez_setup.py msvc-build-launcher.cmd release.py setuptools.egg-infoCHANGES.txt   launcher.c  PKG-INFO         scripts   testsconftest.py   Makefile   pkg_resources      setup.cfg  tox.inidocs       MANIFEST.in pytest.ini        setup.pyeasy_install.py _markerlib  README.txt        setuptools

 

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

 

root@k-Lenovo:~# cd pip-1.5.6root@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 ~/cafferoot@k-Lenovo:~/caffe# sudo apt-get install gfortran

 

root@k-Lenovo:~/caffe# cd python/root@k-Lenovo:~/caffe/python# lscaffe classify.py CMakeLists.txt detect.py draw_net.py requirements.txtroot@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# lsbuild      CONTRIBUTORS.md examples  Makefile.config     README.mdcaffe.cloc    data       include   Makefile.config.example scriptscmake      distribute    INSTALL.md matlab          srcCMakeLists.txt  docker      LICENSE   models          toolsCONTRIBUTING.md docs       Makefile  pythonroot@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/cafferoot@k-Lenovo:~/caffe/python/caffe# cd ..root@k-Lenovo:~/caffe/python# lscaffe classify.py CMakeLists.txt detect.py draw_net.py requirements.txtroot@k-Lenovo:~/caffe/python# pythonPython 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import caffecaffe/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.pyclassifier.py detector.py   imagenet   io.py     net_spec.pyc pycaffe.pyc

 安装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