系统相关
首页 > 系统相关> > ubuntu18.04, cuda10 anaconda编译安装caffe

ubuntu18.04, cuda10 anaconda编译安装caffe

作者:互联网

弄了好几天,终于安装上了。全是亲身踩过的坑。

可能是我linux使用不熟练,配置过程中出现问题不知道怎么解。所以我要记录全面一点。

一、知识点-添加环境变量

在Linux里设置环境变量的方法

0查看环境变量

可以``export``查看所有的变量。可以通过``export | grep anaconda`` 筛选出你想要查看的关键词,这里grep后面就是你要抓取的关键词。

1 临时添加 


在末尾添加

export PATH=$PATH:/home/nnir712/software/protobuf/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/nnir712/software/protobuf/lib


直接赋值就会覆盖之前原来的变量

export PATH=:/home/nnir712/software/protobuf/bin

2 永久添加

 .bashrc

`vi ~/.bashrc `在文件里加入想要添加的环境变量,比如

PATH=$PATH:/home/nnir712/software/protobuf/bin

这种修改需要重启terminal窗口才能生效

profile 

`vi /etc/profile ` 同样在文件里加入想要添加的环境变量,比如

PATH=$PATH:/home/nnir712/software/protobuf/bin

这种修改需要重启电脑才能生效

 

二、anaconda环境的 caffe安装

三个比较靠谱的参考

caffe安装
[Ubuntu16.04 Caffe 安装步骤记录(超详尽)]
[Caffe学习(二)—下载、编译和安装Caffe(源码安装方式)]
最后我是参考的[caffe安装]()成功的
**所有的安装,也不一定是这固定顺序。**
不同的conda版本也会有不同的性格。我发现,我第一次安装成功了,第二次再这样操作就不行了。


 1、安装依赖


### 先装skimage。numpy 


与他们不同的是,我先安装numpy。因为我发现caffe需要用到skimage。而这里面有存在numpy与skimage版本不兼容的问题[numpy/issues](https://github.com/numpy/numpy/issues/12744)。最后发现1.15.4的numpy可以支持skimage
`

conda install numpy==1.15.4
conda install -c conda-forge scikit-image
激活python试试能不能用:from skimage import io,transform

只要没报错就成功了`

这里也不一定先安装numpy。因为我第二次尝试时发现装了numpy==1.15.4,skimage装不上了。貌似是conda更新了。
直接conda install -c conda-forge scikit-image就可以了,conda自动帮我安装了numpy1.11.3版本的。


### 依赖

conda install boost hdf5 snappy leveldb lmdb gflags glog

## 2、protobuf

这个protobuf很乱。也许你的环境里就有。
你可以先通过``protoc --version``查询一下,如果能报出版本号,就说明安装了。但是也要注意,可能需要你卸载重新安装。
它可以通过3种方式安装。但是还是推荐使用②conda安装,因为我这样装的,能用。

①apt安装

这种方式会把protoc安装到``/usr/bin``里面,可以通过``ls /usr/bin | grep protoc``查看这里有没有。``/usr/bin``一般都会添加在PATH的环境变量里,所以只要你用apt安装了protobuf,所有的环境都会找到,包括anaconda的沙箱中,因此你用anaconda再安装一遍就会有两个位置安装,就很乱。
如果不行的话就先卸载,再安装。

sudo apt-get remove libprotobuf-dev
sudo apt-get remove  protobuf-compiler 

安装:
 

sudo apt-get install libprotobuf-dev protobuf-compiler

②conda安装

这里也是参考[caffe安装](

``conda install protobuf==3.5.1``
添加环境变量:
``echo 'export PATH=/home/nnir712/anaconda3/pkgs/libprotobuf-3.5.2-h6f1eeef_0/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/home/nnir712/anaconda3/pkgs/libprotobuf-3.5.2-h6f1eeef_0/lib:$LD_LIBRARY_PATH' >> ~/.bashrc``
重新激活:
``source ~/.bashrc``
再编译安装protocbuf-python
[下载protobuf](https://github.com/protocolbuffers/protobuf/releases/tag/v3.5.1)
tar -xzvf protobuf-python-3.5.1.tar.gz
cd protobuf-3.5.1
cd python
python setup.py build
python setup.py test
python setup.py install

③编译安装


参考[源码编译安装](https://blog.csdn.net/wenwenxiong/article/details/53644845)

 

3、修改Makefile.config


完全参考[caffe安装](https://zoesxw.github.io/2018/07/14/caffe%E5%AE%89%E8%A3%85/)的经验
主要是注意python的路径设置ANACONDA_HOME、PYTHON_INCLUDE、PYTHON_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 := 0

# 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++

# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda-10.0
# 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_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 := atlas
# 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 := $(HOME)/anaconda3
#PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
#		 $(ANACONDA_HOME)/include/python3.6m \
#		 $(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include
PYTHON_INCLUDE := $(ANACONDA_HOME)/envs/skimg/include \
		 $(ANACONDA_HOME)/envs/skimg/include/python3.6m \
		 $(ANACONDA_HOME)/envs/skimg/lib/python3.6/site-packages/numpy/core/include


# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python36 python3.6m
# 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/skimg/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/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial /home/nnir712/anaconda3/envs/skimg/lib/
#/home/nnir712/software/protobufin/include/

# 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 ?= @

 

4、编译

 

make clean  # 如果之前编译过的话,清除掉
make all -j8  # j8是使用8个线程,看机子上有几个核
make test -j8
make runtest -j8
make pycaffe -j8
添加环境变量:
`export PYTHONPATH=$PYTHONPATH:/home/nnir712/software/caffe/python/
export CAFFE_ROOT="/home/nnir712/software/caffe/"`

成功!可以测试import caffe是否成功
顺便再安装上opencv:
`pip install opencv-python==3.4.1.15 -i https://pypi.douban.com/simple/
pip install opencv-contrib-python==3.4.1.15 -i https://pypi.douban.com/simple/`
## 问题1:protobuf的问题

fatal error: google/protobuf/port_def.inc: No such file or directory

`sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler 
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler libatlas-base-dev
sudo apt-get install python-dev python-pip gfortran`
以上步骤有可能会提示你安装过最近版本的。这种情况不会再安装,用protoc --version查看版本号。看看能否找到protobuf。如果不行的话就先卸载,再安装。
`sudo apt-get remove libprotobuf-dev
sudo apt-get remove  protobuf-compiler `
安装:
`sudo apt-get install libprotobuf-dev protobuf-compiler`

## 问题2:python路径
/usr/include/boost/python/detail/wrap_python.hpp:50:11: fatal error: pyconfig.h: 没有那个文件或目录


PYTHON_INCLUDE := $(ANACONDA_HOME)/envs/caffepy36/include \
                 $(ANACONDA_HOME)/envs/caffepy36/include/python3.6m \
                 $(ANACONDA_HOME)/envs/caffepy36/lib/python3.6/site-packages/numpy/core/include

发现没有numpy

sudo apt-get remove  libboost-all-dev****

/usr/bin/ld: 找不到 -lboost_python36 
![9cc1e421353ae63e64e92365547b058f.png](en-resource://database/819:1)


export PYTHONPATH=$PYTHONPATH:/home/nnir712/software/caffe/python/
export CAFFE_ROOT="/home/nnir712/software/caffe/"

ImportError: libboost_system.so.1.67.0: cannot open shared object file: No such file or directory
[配置了libboost_system.so.1.67.0 但还是显示找不到](https://blog.csdn.net/qq_33144323/article/details/81951308)
`sudo vi /etc/ld.so.conf
添加:/home/nnir712/anaconda3/envs/caffepy36/lib/
sudo ldconfig`

 

 

待续

标签:protobuf,lib,安装,dev,cuda10,caffe,anaconda,include,numpy
来源: https://blog.csdn.net/firing00/article/details/96732745