• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1## 2.4 Caffe环境搭建
2
3### 注意:如果您的Ubuntu是20.04版本的
4
5* 请参考《[如何在Ubuntu20.02 上面搭建caffe环境](https://gitee.com/wgm2022/wu_guiming.gitee.io/blob/master/01%20%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA%E7%9B%B8%E5%85%B3/%E5%A6%82%E4%BD%95%E5%9C%A8Ubuntu20.02%20%E4%B8%8A%E9%9D%A2%E6%90%AD%E5%BB%BAcaffe%E7%8E%AF%E5%A2%83.md)6
7### 如果您的Ubuntu是18.04版本的,请继续往下操作
8
9* 1、在Ubuntu系统中,分步执行下面的命令,对Ubuntu里面的软件进行更新。
10
11```sh
12sudo apt-get update
13sudo apt-get upgrade
14```
15
16* 2、分步执行下面的命令,安装所需的依赖软件。
17
18```sh
19sudo apt-get install -y libopencv-dev -y
20sudo apt-get install -y build-essential cmake git pkg-config -y
21sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler -y
22sudo apt-get install -y liblapack-dev -y
23sudo apt-get install -y libatlas-base-dev -y
24sudo apt-get install -y --no-install-recommends libboost-all-dev -y
25sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev -y
26sudo apt-get install -y python-numpy python-scipy -y
27sudo apt-get install -y python3-pip -y
28sudo apt-get install -y python3-numpy python3-scipy -y
29```
30
31* 3、执行下面的命令,下载caffe开源软件
32
33```sh
34git clone https://github.com/BVLC/caffe.git
35```
36
37* 4、进入caffe/python/目录下,执行下面的命令,下载依赖的软件
38
39```sh
40cd caffe/python
41for req in $(cat requirements.txt); do pip3 install $req; done
42```
43
44* 5、进入caffe目录下,执行下面的命令,将 Makefile.config.example 文件复制一份并更名为 Makefile.config
45
46```sh
47cp Makefile.config.example Makefile.config
48```
49
50* 6、接下来是修改Makefile.config里面的配置,使用vim工具打开Makefile.config51
52```sh
53vim Makefile.config
54```
55
56* ① 将CPU_ONLY前面的注释去掉。
57
58```sh
59# 将
60# CPU_ONLY := 1
61# 改为
62CPU_ONLY := 1
63```
64
65* ② 将OPENCV_VERSION前面的注释去掉
66
67```sh
68# 将
69# OPENCV_VERSION := 3
70# 改为
71OPENCV_VERSION := 3
72```
73
74* ③ 因为我们Ubuntu的环境是python3.6,所以请把PYTHON_INCLUDE = python2.7这个配置前面加上注释,且把PYTHON_INCLUDE=python3.5的注释打开,把所有的3.5都改成3.6,具体修改如下:
75* 注意:如果您的python3是其他的版本,就把python3.5改成您Ubuntu对应的python3版本即可
76
77![](./figures/hispark_taurus_ai_sample/028%E4%BF%AE%E6%94%B9python%E7%89%88%E6%9C%AC%E5%A5%BD.png)
78
79* ④ 将WITH_PYTHON_LAYER := 1前面的注释去掉
80
81```sh
82# 将
83# WITH_PYTHON_LAYER := 1
84# 改为
85WITH_PYTHON_LAYER := 1
86```
87
88* ⑤ 修改INCLUDE_DIRS和LIBRARY_DIRS
89
90```sh
91# 将
92INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
93LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
94改为
95INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
96LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
97```
98
99修改后的文件如下所示:
100
101```python
102## Refer to http://caffe.berkeleyvision.org/installation.html
103# Contributions simplifying and improving our build system are welcome!
104
105# cuDNN acceleration switch (uncomment to build with cuDNN).
106# USE_CUDNN := 1
107
108# CPU-only switch (uncomment to build without GPU support).
109CPU_ONLY := 1
110
111# uncomment to disable IO dependencies and corresponding data layers
112# USE_OPENCV := 0
113# USE_LEVELDB := 0
114# USE_LMDB := 0
115# USE_HDF5 := 0
116
117# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
118#	You should not set this flag if you will be reading LMDBs with any
119#	possibility of simultaneous read and write
120# ALLOW_LMDB_NOLOCK := 1
121
122# Uncomment if you're using OpenCV 3
123OPENCV_VERSION := 3
124
125# To customize your choice of compiler, uncomment and set the following.
126# N.B. the default for Linux is g++ and the default for OSX is clang++
127# CUSTOM_CXX := g++
128
129# CUDA directory contains bin/ and lib/ directories that we need.
130CUDA_DIR := /usr/local/cuda
131# On Ubuntu 14.04, if cuda tools are installed via
132# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
133# CUDA_DIR := /usr
134
135# CUDA architecture setting: going with all of them.
136# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
137# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
138# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
139CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
140		-gencode arch=compute_20,code=sm_21 \
141		-gencode arch=compute_30,code=sm_30 \
142		-gencode arch=compute_35,code=sm_35 \
143		-gencode arch=compute_50,code=sm_50 \
144		-gencode arch=compute_52,code=sm_52 \
145		-gencode arch=compute_60,code=sm_60 \
146		-gencode arch=compute_61,code=sm_61 \
147		-gencode arch=compute_61,code=compute_61
148
149# BLAS choice:
150# atlas for ATLAS (default)
151# mkl for MKL
152# open for OpenBlas
153BLAS := atlas
154# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
155# Leave commented to accept the defaults for your choice of BLAS
156# (which should work)!
157# BLAS_INCLUDE := /path/to/your/blas
158# BLAS_LIB := /path/to/your/blas
159
160# Homebrew puts openblas in a directory that is not on the standard search path
161# BLAS_INCLUDE := $(shell brew --prefix openblas)/include
162# BLAS_LIB := $(shell brew --prefix openblas)/lib
163
164# This is required only if you will compile the matlab interface.
165# MATLAB directory should contain the mex binary in /bin.
166# MATLAB_DIR := /usr/local
167# MATLAB_DIR := /Applications/MATLAB_R2012b.app
168
169# NOTE: this is required only if you will compile the python interface.
170# We need to be able to find Python.h and numpy/arrayobject.h.
171# PYTHON_INCLUDE := /usr/include/python2.7 \
172#		/usr/lib/python2.7/dist-packages/numpy/core/include
173# Anaconda Python distribution is quite popular. Include path:
174# Verify anaconda location, sometimes it's in root.
175# ANACONDA_HOME := $(HOME)/anaconda
176# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
177		# $(ANACONDA_HOME)/include/python2.7 \
178		# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
179
180# Uncomment to use Python 3 (default is Python 2)
181PYTHON_LIBRARIES := boost_python3 python3.6m
182PYTHON_INCLUDE := /usr/include/python3.6m \
183                 /usr/lib/python3.6/dist-packages/numpy/core/include
184
185# We need to be able to find libpythonX.X.so or .dylib.
186PYTHON_LIB := /usr/lib
187# PYTHON_LIB := $(ANACONDA_HOME)/lib
188
189# Homebrew installs numpy in a non standard path (keg only)
190# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
191# PYTHON_LIB += $(shell brew --prefix numpy)/lib
192
193# Uncomment to support layers written in Python (will link against Python libs)
194WITH_PYTHON_LAYER := 1
195
196# Whatever else you find you need goes here.
197INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
198LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
199
200# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
201# INCLUDE_DIRS += $(shell brew --prefix)/include
202# LIBRARY_DIRS += $(shell brew --prefix)/lib
203
204# NCCL acceleration switch (uncomment to build with NCCL)
205# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
206# USE_NCCL := 1
207
208# Uncomment to use `pkg-config` to specify OpenCV library paths.
209# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
210# USE_PKG_CONFIG := 1
211
212# N.B. both build and distribute dirs are cleared on `make clean`
213BUILD_DIR := build
214DISTRIBUTE_DIR := distribute
215
216# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
217# DEBUG := 1
218
219# The ID of the GPU that 'make runtest' will use to run unit tests.
220TEST_GPUID := 0
221
222# enable pretty build (comment to see full commands)
223Q ?= @
224```
225
226* 7、修改Makefile文件里面的一些配置,使用vim 打开Makefile,进行修改。
227
228```
229vim Makefile
230```
231
232* ① 修改DYNAMIC_VERSION_REVISION的值
233
234```sh
235# 将
236DYNAMIC_VERSION_REVISION  := 0
237# 改为
238DYNAMIC_VERSION_REVISION  := 0-rc3
239```
240
241* ② 修改LIBRARIES的值
242
243```sh
244# 将
245LIBRARIES += glog gflags protobuf boost_system boost_filesystem m
246# 改为
247LIBRARIES += glog gflags protobuf boost_system boost_filesystem boost_regex m hdf5_hl hdf5
248```
249
250![](./figures/hispark_taurus_ai_sample/029%E4%BF%AE%E6%94%B9libraries.png)
251
252```sh
253# 将
254LIBRARIES += opencv_imgcodecs
255# 改为
256LIBRARIES += opencv_imgcodecs opencv_videoio
257```
258
259![](./figures/hispark_taurus_ai_sample/030%E4%BF%AE%E6%94%B9libraries2.png)
260
261* ③ 将# NCCL acceleration configuration下面的四行注释掉
262
263```sh
264# 将
265# NCCL acceleration configuration
266ifeq ($(USE_NCCL), 1)
267
268    LIBRARIES += nccl
269    COMMON_FLAGS += -DUSE_NCCL
270endif
271# 改为
272# NCCL acceleration configuration
273# ifeq ($(USE_NCCL), 1)
274#   LIBRARIES += nccl
275#   COMMON_FLAGS += -DUSE_NCCL
276# endif
277```
278
279![](./figures/hispark_taurus_ai_sample/031%E6%B3%A8%E9%87%8ANCCL.png)
280
281* 8、在caffe目录下,分步执行下面的命令,来编译caffe。
282
283```sh
284make -j4
285make pycaffe
286```
287
288* 9、执行下面的命令,将caffe的python路径设置为环境变量,并更新环境变量。
289
290​	执行下面的命令,打开.bashrc
291
292```sh
293sudo vim ~/.bashrc
294```
295
296把下面的命令,添加到.bashrc文件的末尾,主要/home/hispark/code/caffe/python是我自己Ubuntu的caffe/python路径,这里请填写为您自己Ubuntu中的caffe/python路径。
297
298```shell
299在文件的末尾加上下面的语句
300export PYTHONPATH=/home/hispark/code/caffe/python:$PYTHONPATH
301```
302
303
304
305![](./figures/hispark_taurus_ai_sample/032python%E7%9A%84%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F.png)
306
307​	在执行下面的命令,更新环境变量
308
309```sh
310source ~/.bashrc
311```
312
313* 10、测试caffe环境是否OK,在Ubuntu的任意目录下,执行 python3,当出现”>>>”的提示符后,再输入import caffe,如果没有任何报错信息,说明caffe环境已经搭建成功了。
314
315```sh
316python3
317import caffe
318```
319
320