深度學習:21天實戰caffe學習資源-4-環境安裝


使用anaconda3環境下的python2.7, 機器macos mojave 10.14

 

1.安裝Xcode

首先現在app store中安裝Xcode:

不然會有” framework not found vecLib “的問題出現

 

2.相應包安裝

1.首先要安裝homebrew包管理工具,在終端運行下面命令:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

 

2.然后利用該管理工具安裝caffe依賴包

brew install -vd snappy leveldb gflags glog szip lmdb
brew install hdf5 opencv
brew install openblas

 

3.安裝boost

brew install boost@1.59 boost-python@1.59 brew link boost@1.59 --force brew link boost-python@1.59 --force

 

4. 安裝2.6.1版本的protobuf

一開始的是安裝3.5.1版本的protobuf,但是后面出現問題,所以還是安裝2.6.1版本

下面只是記錄一下

cd ~/Downloads 
wget https://github.com/protocolbuffers/protobuf/archive/v3.5.1.zip
unzip v3.5.1.zip
cd protobuf-3.5.1
./autogen.sh
./configure
make
make install

 運行./autogen.sh過程出錯:

+ autoreconf -f -i -Wall,no-obsolete
./autogen.sh: line 50: autoreconf: command not found

解決方案是安裝protobuf的依賴項:

(deeplearning2) userdeMacBook-Pro:protobuf-3.5.1 user$ brew install autoconf automake libtool

再重新運行./autogen.sh及之后命令

成功后查看版本:

(deeplearning2) userdeMacBook-Pro:protobuf-3.5.1 user$ protoc --version
libprotoc 3.5.1

 這里在下面caffe文件夾中make all之前忘記make clean了,可能是這個原因沒成功也不一定,但是2.6.0版本的的確成功了,直接用該版本即可

 

后面發現還是不行,就去下了2.6.1版本的:

/Library/Developer/CommandLineTools/usr/bin/make  check-TESTS
PASS: protobuf-test
PASS: protobuf-lazy-descriptor-test
PASS: protobuf-lite-test
PASS: google/protobuf/compiler/zip_output_unittest.sh
PASS: google/protobuf/io/gzip_stream_unittest.sh
============================================================================
Testsuite summary for Protocol Buffers 2.6.1
============================================================================
# TOTAL: 5
# PASS:  5
# SKIP:  0
# XFAIL: 0
# FAIL:  0
# XPASS: 0
# ERROR: 0
============================================================================

 版本查看:

(deeplearning2) userdeMacBook-Pro:~ user$  protoc --version
libprotoc 2.6.1

 

 

3.下載caffe源碼

(deeplearning2) userdeMacBook-Pro:~ user$ git clone https://github.com/bvlc/caffe.git
Cloning into 'caffe'...
remote: Enumerating objects: 65269, done.

下載好后:

(deeplearning2) userdeMacBook-Pro:~ user$ cd caffe
(deeplearning2) userdeMacBook-Pro:caffe user$ cp Makefile.config.example Makefile.config

 

4.更改Makefile.config文件

打開CPU_ONLY選項,保存

# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := 1

即去掉前面的#號,表示caffe編譯時僅支持CPU,不支持GPU

 

取消CPU_ONLY前面的注釋

取消USE_OPENCV前面的注釋

取消USE_HDF5前面的注釋,並且改成1

更改CUSTOM_CXX

注釋掉CUDA_DIR那一行

將BLAS改成open

設置blas的路徑,取消下面兩行的注釋

# uncomment to disable IO dependencies and corresponding data layers
USE_OPENCV := 0

# This code is taken from https://github.com/sh1r0/caffe-android-lib
USE_HDF5 := 1

# Uncomment if you're using OpenCV 3
OPENCV_VERSION := 3
# CUDA directory contains bin/ and lib/ directories that we need.
#CUDA_DIR := /usr/local/cuda
BLAS := open # Homebrew puts openblas in a directory that is not on the standard search path BLAS_INCLUDE := $(shell brew --prefix openblas)/include BLAS_LIB := $(shell brew --prefix openblas)/lib
# Homebrew puts openblas in a directory that is not on the standard search path
BLAS_INCLUDE := $(shell brew --prefix openblas)/include
BLAS_LIB := $(shell brew --prefix openblas)/lib

 

下面就是設置路徑,使用的是anaconda3,這個根據你自己的路徑設置

注釋掉:

# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
# PYTHON_INCLUDE := /usr/include/python2.7 \
#        /usr/lib/python2.7/dist-packages/numpy/core/include

更改為:

# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := /anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/envs/deeplearning2/include \
        $(ANACONDA_HOME)/envs/deeplearning2/include/python2.7 \
        $(ANACONDA_HOME)/envs/deeplearning2/lib/python2.7/site-packages/numpy/core/include

下面這個也更改為:

# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib PYTHON_LIB := $(ANACONDA_HOME)/envs/deeplearning2/lib

 然后添加一些include和lib路徑:

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/opt/boost@1.59/include /usr/local/opt/hdf5/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/opt/boost-python@1.59/lib /usr/local/opt/hdf5/lib

最終版本為:

## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!

# cuDNN acceleration switch (uncomment to build with cuDNN).
# USE_CUDNN := 1

# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := 1

# uncomment to disable IO dependencies and corresponding data layers
USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0
# This code is taken from https://github.com/sh1r0/caffe-android-lib
USE_HDF5 := 1

# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
#    You should not set this flag if you will be reading LMDBs with any
#    possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1

# Uncomment if you're using OpenCV 3
# OPENCV_VERSION := 3

# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++
CUSTOM_CXX := clang++ -std=c++11

# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
# On Ubuntu 14.04, if cuda tools are installed via
# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr

# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
        -gencode arch=compute_20,code=sm_21 \
        -gencode arch=compute_30,code=sm_30 \
        -gencode arch=compute_35,code=sm_35 \
        -gencode arch=compute_50,code=sm_50 \
        -gencode arch=compute_52,code=sm_52 \
        -gencode arch=compute_60,code=sm_60 \
        -gencode arch=compute_61,code=sm_61 \
        -gencode arch=compute_61,code=compute_61

# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := open
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
# BLAS_INCLUDE := /path/to/your/blas
# BLAS_LIB := /path/to/your/blas

# Homebrew puts openblas in a directory that is not on the standard search path
BLAS_INCLUDE := $(shell brew --prefix openblas)/include
BLAS_LIB := $(shell brew --prefix openblas)/lib

# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
# MATLAB_DIR := /usr/local
# MATLAB_DIR := /Applications/MATLAB_R2012b.app

# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
# PYTHON_INCLUDE := /usr/include/python2.7 \
#         /usr/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := /anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/envs/deeplearning2/include \
        $(ANACONDA_HOME)/envs/deeplearning2/include/python2.7 \
        $(ANACONDA_HOME)/envs/deeplearning2/lib/python2.7/site-packages/numpy/core/include

# Uncomment to use Python 3 (default is Python 2)
# PYTHON_LIBRARIES := boost_python3 python3.5m
# PYTHON_INCLUDE := /usr/include/python3.5m \
#                 /usr/lib/python3.5/dist-packages/numpy/core/include

# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/envs/deeplearning2/lib

# Homebrew installs numpy in a non standard path (keg only)
# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
# PYTHON_LIB += $(shell brew --prefix numpy)/lib

# Uncomment to support layers written in Python (will link against Python libs)
WITH_PYTHON_LAYER := 1

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/opt/boost@1.59/include /usr/local/opt/hdf5/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/opt/boost-python@1.59/lib /usr/local/opt/hdf5/lib

# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
# INCLUDE_DIRS += $(shell brew --prefix)/include
# LIBRARY_DIRS += $(shell brew --prefix)/lib

# NCCL acceleration switch (uncomment to build with NCCL)
# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1

# Uncomment to use `pkg-config` to specify OpenCV library paths.
# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
# USE_PKG_CONFIG := 1

# N.B. both build and distribute dirs are cleared on `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute

# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
# DEBUG := 1

# The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := 0

# enable pretty build (comment to see full commands)
Q ?= @
View Code

 

如果你在運行make test命令出下面類似的錯誤:

In file included from /usr/local/include/leveldb/iterator.h:20:
/usr/local/include/leveldb/status.h:27:11: error: expected ';' at end of declaration list
  Status() noexcept : state_(nullptr) {}
          ^
          ;
/usr/local/include/leveldb/status.h:33:16: warning: rvalue references are a C++11 extension [-Wc++11-extensions]
  Status(Status&& rhs) noexcept : state_(rhs.state_) { rhs.state_ = nullptr; }
               ^
/usr/local/include/leveldb/status.h:33:23: error: expected ';' at end of declaration list
  Status(Status&& rhs) noexcept : state_(rhs.state_) { rhs.state_ = nullptr; }
                      ^
                      ;
/usr/local/include/leveldb/status.h:103:16: error: 'Status' is missing exception specification 'throw()'
inline Status::Status(const Status& rhs) {
               ^
                                         throw()
/usr/local/include/leveldb/status.h:24:22: note: previous declaration is here
class LEVELDB_EXPORT Status {
                     ^
/usr/local/include/leveldb/status.h:115:40: warning: rvalue references are a C++11 extension [-Wc++11-extensions]
inline Status& Status::operator=(Status&& rhs) noexcept {
                                       ^
/usr/local/include/leveldb/status.h:115:48: error: expected function body after function declarator
inline Status& Status::operator=(Status&& rhs) noexcept {
                                               ^
In file included from src/caffe/util/db.cpp:2:

之前出這樣的內容以為是leveldb版本的問題,但是后面發現設置該為:

# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++
CUSTOM_CXX := clang++ -std=c++11

需要添加-std=c++11才能正常編譯,就沒有上面的問題了,跟着上面的配置走應該就不會有問題

 

想要讀取python寫的layer,就要取消下面行的注釋:

# Uncomment to support layers written in Python (will link against Python libs) WITH_PYTHON_LAYER := 1

 

5.make編譯:

make all
make test
make runtest
make pycaffe

 ⚠️注意:過程中可能會出現很多warning,只要不出現error,那都可以忽略

 

下面的問題基本上都使用更改配置的方法解決了,所以如果你跟着我上面的配置走,下面的一些問題你應該不會遇見

下面只是記錄一下

 

⚠️運行make命令時,出了任何錯,更改過后都要記得make clean一次,然后再從頭make all進行編譯

make all問題:

bool    OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue,
        ^
src/caffe/net.cpp:774:7: error: use of undeclared identifier 'H5Fis_hdf5'
  if (H5Fis_hdf5(trained_filename.c_str())) {
      ^
11 warnings and 1 error generated.
make: *** [.build_release/src/caffe/net.o] Error 1

解決,在配置中添加boost路徑:

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/local/opt/boost@1.59/include /usr/local/opt/hdf5/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/local/opt/boost-python@1.59/lib /usr/local/opt/hdf5/lib

 

make runtest問題:

(deeplearning2) userdeMacBook-Pro:caffe user$ make runtest
.build_release/tools/caffe
dyld: Library not loaded: /usr/local/opt/gcc/lib/gcc/8/libgfortran.5.dylib
  Referenced from: /usr/local/opt/openblas/lib/libopenblasp-r0.3.5.dylib
  Reason: image not found
make: *** [runtest] Abort trap: 6

原因:

因為我本地安裝的gcc是9版本,只有/usr/local/opt/gcc/lib/gcc/9,沒有8的路徑

解決辦法就是安裝了個8的版本:

 

(deeplearning2) userdeMacBook-Pro:caffe user$ brew install gcc@8
==> Downloading https://homebrew.bintray.com/bottles/gcc@8-8.3.0.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/b1/b1e150c72b4c3b7f3493371d71cdb668f691bfee2e998e5b0bf570eed28254d6?__gda__=exp=1565346562~hmac=81794cd252808740593fa500900f13dbd
######################################################################## 100.0%
==> Pouring gcc@8-8.3.0.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/gcc@8/8.3.0: 1,413 files, 288.5MB

 

然后到/usr/local/Cellar/gcc@8/8.3.0/lib/gcc/8將該文件夾復制一份到/usr/local/opt/gcc/lib/gcc/位置下

 

然后在make runtest,又出錯:

Bye...
*** Aborted at 1565349441 (unix time) try "date -d @1565349441" if you are using GNU date ***
PC: @                0x0 (unknown)
*** SIGABRT (@0x7fff62e09b86) received by PID 73394 (TID 0x1142795c0) stack trace: ***
    @     0x7fff62eb4b3d _sigtramp
make: *** [runtest] Abort trap: 6

類似這樣的錯誤,將hdf5移除:

conda remove hdf5

再運行make runtest即可:

...
[ RUN ] NetTest/1.TestUnsharedWeightsDiffNet [ OK ] NetTest/1.TestUnsharedWeightsDiffNet (0 ms) [ RUN ] NetTest/1.TestSkipPropagateDown [ OK ] NetTest/1.TestSkipPropagateDown (1 ms) [ RUN ] NetTest/1.TestSharedWeightsUpdate [ OK ] NetTest/1.TestSharedWeightsUpdate (1 ms) [ RUN ] NetTest/1.TestAllInOneNetVal [ OK ] NetTest/1.TestAllInOneNetVal (1 ms) [ RUN ] NetTest/1.TestParamPropagateDown [ OK ] NetTest/1.TestParamPropagateDown (1 ms) [ RUN ] NetTest/1.TestGetLayerByName [ OK ] NetTest/1.TestGetLayerByName (1 ms) [ RUN ] NetTest/1.TestHasBlob [ OK ] NetTest/1.TestHasBlob (0 ms) [----------] 26 tests from NetTest/1 (25 ms total) [----------] Global test environment tear-down [==========] 1058 tests from 143 test cases ran. (36250 ms total) [ PASSED ] 1058 tests.

 

如果遇見問題:

make: .build_release/tools/caffe: No such file or directory

那是因為make clean后沒有從make all重新運行

 

這個時候如果就嘗試import caffe還是不能成功的,還要make pycaffe

make runtest成功后,運行make pycaffe出現問題:

userdeMacBook-Pro:caffe user$ make pycaffe
make: Nothing to be done for `pycaffe'.

這是因為我之前運行過該命令一次,所以要運行一遍make clean,再從頭make all再來一遍即可

make pycaffe返回結尾為:

...
touch python/caffe/proto/__init__.py 
PROTOC (python) src/caffe/proto/caffe.proto

這就表示編譯成功了

 

6.測試

這時候測試python能不能導入,發現還是沒成功,還需要將路徑寫入:

sudo vim ~/.bash_profile

在文件末尾寫入:

export PYTHONPATH=~/caffe/python:$PYTHONPATH

根據你自己的caffe安裝路徑來

然后再打開新的窗口查看即成功了:

(base) userdeMacBook-Pro:~ user$ conda activate deeplearning2
(deeplearning2) userdeMacBook-Pro:~ user$ python -c 'import caffe'
(deeplearning2) userdeMacBook-Pro:~ user$ 

 

中間還做過操作,但我覺得應該是沒用的:

source /etc/profile

如果你上面更改路徑后測試還是沒成功,試試再運行該命令

 

到這里終於安裝成功了,不容易

 

7.使用

在這里我要使用caffe實現的一件事就是將caffe model模型轉為pytorch格式的模型,可見:

DEX-6-caffe模型轉成pytorch模型辦法(其中的4.下載對應的caffemodel模型並轉換)

 


免責聲明!

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



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