1. 程式人生 > >Caffe 環境搭建中應注意的問題

Caffe 環境搭建中應注意的問題

TensorFlow對應的是TheanoTorch; 
Caffe專精於影象處理,Caffe方便,更快入門上手; 
在通用的DL task上,Caffe不如Theano。 
CNN(卷積神經網路)、RNN(迴圈神經網路)、DNN(深度神經網路)

開發環境搭建:

一、沒有GPU

[email protected]-virtual-machine:~$ lspci | grep -i nvidia 
[email protected]-virtual-machine:~$ 
  • 1
  • 2

二、ubuntu版本

[email protected]:~$ uname -m && cat /etc/*release
x86_64
DISTRIB_ID
=Ubuntu DISTRIB_RELEASE=15.10 DISTRIB_CODENAME=wily DISTRIB_DESCRIPTION="Ubuntu 15.10" NAME="Ubuntu" VERSION="15.10 (Wily Werewolf)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 15.10" VERSION_ID="15.10" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
[email protected]
:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

三、gcc

[email protected]virtual-machine:~$ gcc --version  
gcc.real (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [email protected]virtual-machine:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

四、安裝依賴庫

[email protected]-virtual-machine:~$ sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
[sudo] password for learning: 
Reading package lists... Done
Building dependency tree      
  • 1
  • 2
  • 3
  • 4

這裡寫圖片描述

sudo apt-get install --no-install-recommends libboost-all-dev
  • 1

這裡寫圖片描述

sudo apt-get install libatlas-base-dev
  • 1

這裡寫圖片描述

五、安裝python

六、安裝Opencv

七、安裝依賴庫 
[email protected]:~$ sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev 
[sudo] password for learning: 
Reading package lists… Done

這裡寫圖片描述

八、下載Caffe

[email protected]:~$ git clone git://github.com/BVLC/caffe.git
Cloning into 'caffe'...
remote: Counting objects: 34637, done.
Receiving objects: 100% (34637/34637), 47.81 MiB | 81.00 KiB/s, done.
remote: Total 34637 (delta 0), reused 0 (delta 0), pack-reused 34636
Resolving deltas: 100% (23287/23287), done.
Checking connectivity... done.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

這裡寫圖片描述

九、修改

Makefile 修改: 
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

這一塊程式碼不需要修改 
這裡寫圖片描述

修改處: 
caffe/examples/cpp_classification/classification.cpp檔案

這裡寫圖片描述

十、編譯

[email protected]:~/caffe$ cp Makefile.config.example Makefile.config

[email protected]:~/caffe$ ls
caffe.cloc       data      INSTALL.md               matlab     src
cmake            docker    LICENSE                  models     tools
CMakeLists.txt   docs      Makefile                 python
CONTRIBUTING.md  examples  Makefile.config          README.md
CONTRIBUTORS.md  include   Makefile.config.example  scripts
[email protected]:~/caffe$ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

[email protected]:~/caffe$ gedit Makefile.config

這裡寫圖片描述

[email protected]:~/caffe$ make all 
出現問題:

[email protected]:~/caffe$ make all
PROTOC src/caffe/proto/caffe.proto
CXX .build_release/src/caffe/proto/caffe.pb.cc
CXX src/caffe/data_transformer.cpp
CXX src/caffe/common.cpp
CXX src/caffe/internal_thread.cpp
CXX src/caffe/blob.cpp
CXX src/caffe/data_reader.cpp
CXX src/caffe/parallel.cpp
CXX src/caffe/util/hdf5.cpp
In file included from src/caffe/util/hdf5.cpp:1:0:
./include/caffe/util/hdf5.hpp:6:18: fatal error: hdf5.h: No such file or directory
compilation terminated.
Makefile:572: recipe for target '.build_release/src/caffe/util/hdf5.o' failed
make: *** [.build_release/src/caffe/util/hdf5.o] Error 1
[email protected]:~/caffe$ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

解決: 
Makefile.config 
INCLUDE_DIRS 
/usr/include/hdf5/serial/ 
Makefile 
LIBRARIES 
hdf5_hl and hdf5 改為 hdf5_serial_hl ,hdf5_serial

這裡寫圖片描述

這裡寫圖片描述

出現問題:

LD -o .build_release/lib/libcaffe.so.1.0.0-rc3
CXX tools/finetune_net.cpp
CXX/LD -o .build_release/tools/finetune_net.bin
CXX tools/net_speed_benchmark.cpp
CXX/LD -o .build_release/tools/net_speed_benchmark.bin
CXX tools/compute_image_mean.cpp
CXX/LD -o .build_release/tools/compute_image_mean.bin
.build_release/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)'
collect2: error: ld returned 1 exit status
Makefile:616: recipe for target '.build_release/tools/compute_image_mean.bin' failed
make: *** [.build_release/tools/compute_image_mean.bin] Error 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

解決方法: 
Makefile 
修改: 
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

編譯成功:

這裡寫圖片描述

make test

這裡寫圖片描述

make runtest

這裡寫圖片描述

[----------] 2 tests from BatchReindexLayerTest/0, where TypeParam = caffe::CPUDevice<float>
[ RUN      ] BatchReindexLayerTest/0.TestForward
[       OK ] BatchReindexLayerTest/0.TestForward (0 ms)
[ RUN      ] BatchReindexLayerTest/0.TestGradient
[       OK ] BatchReindexLayerTest/0.TestGradient (373 ms)
[----------] 2 tests from BatchReindexLayerTest/0 (374 ms total)

[----------] Global test environment tear-down
[==========] 1058 tests from 146 test cases ran. (134225 ms total)
[  PASSED  ] 1058 tests.
[email protected]virtual-machine:~/caffe$ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

十一、配置pycaffe

sudo apt-get install python-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflags Cython ipython

sudo apt-get install protobuf-c-compiler protobuf-compiler

[email protected]:~/caffe$ make pycaffe

learning@learning-virtual-machine:~/caffe$ 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
learning@learning-virtual-machine:~/caffe$ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

sudo gedit /etc/profile 
末尾新增: export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH 
用完整路徑 
source /etc/profile

[email protected]:~/caffe$ python 
Python 2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010] on linux2 
Type “help”, “copyright”, “credits” or “license” for more information. 
.>>>

出現問題:

.>>> import caffe
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ImportError: No module named caffe
  • 1
  • 2
  • 3
  • 4

解決方法: 
sudo gedit /etc/profile 
export PYTHONPATH=$PYTHONPATH:/home/learning/caffe/python 
source /etc/profile

這裡寫圖片描述

補充:baidu解釋 
Python(英國發音:/ˈpaɪθən/ 美國發音:/ˈpaɪθɑːn/), 是一種面向物件、解釋型計算機程式設計語言,由Guido van Rossum於1989年發明,第一個公開發行版發行於1991年。 
Python是純粹的自由軟體, 原始碼和直譯器CPython遵循 GPL(GNU General Public License)協議[1] 。 
Python語法簡潔清晰,特色之一是強制用空白符(white space)作為語句縮排。 
Python具有豐富和強大的庫。它常被暱稱為膠水語言,能夠把用其他語言製作的各種模組(尤其是C/C++)很輕鬆地聯結在一起。常見的一種應用情形是,使用Python快速生成程式的原型(有時甚至是程式的最終介面),然後對其中[2] 有特別要求的部分,用更合適的語言改寫,比如3D遊戲中的圖形渲染模組,效能要求特別高,就可以用C/C++重寫,而後封裝為Python可以呼叫的擴充套件類庫。需要注意的是在您使用擴充套件類庫時可能需要考慮平臺問題,某些可能不提供跨平臺的實現。

相關推薦

no