utuntu18.4 anaconda3下安装caffe
首先上安装成功的图
必须吐槽caffe的安装,太难了,protoc问题,opencv问题,c++11和gnu++11
1 安装anaconda(python3.6版本),cuda,cudnn等一堆问题
安装anaconda,cuda,cudnn参考我的一篇博文
2 安装正确的protobuf
找到自己电脑里的protobuf,查看版本
su #输入密码,以下操作都在root权限下进行
protc --verison
如果是2.6.1,那么进行第三步。如果不是,使用以下命令卸载。
sudo apt-get remove libprotobuf-dev
sudo apt-get autoremove libprotobuf-dev protobuf-compiler
which protoc #运行完“which protoc”会显示一个protoc的路径,如果没有显示则下面这条命令不必执行
sudo rm /usr/local/bin/protoc #删除上边显示的路径内容
在github protobuf上找到protobuf2.6.1版本,下载下来使用以下指令安装
cd protobuf
./autogen.sh
./configure
make
make check
sudo make install
sudo ldconfig
protoc --version
#输出protobuf版本信息则表示安装成功
3 安装caffe
3.1 安装依赖项
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 libatlas-base-dev
sudo apt-get install libhdf5-serial-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
3.2 下载caffe
在github上找到caffe,下载caffe
git clone https://github.com/BVLC/caffe.git
cd caffe
3.3 进入python文件夹
修改其中的内容,将python-dateutil,protobuf的版本限制条件删掉。然后pip安装依赖项
vim requirements.txt #修改其中的python-dateutil,protobuf
for req in $(cat requirements.txt); do pip3 install $req; done
3.4 安装caffe
退回到caffe文件夹, 复制Makefile.config模板, 再改一些内容
cp Makefile.config.example Makefile.config
这部分参考博文
不同之处是cpu_only不注释掉
## 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#(因为我们要配置cpu版本的,所以这里把引号去掉)
# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 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#(我们的opencv是3版本的,所以这里我们把引号去掉,之前opencv3已经安装并编译成功)
# 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
# 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 := 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#(我们使用的python3.6版本在anaconda3中,所以这里要取消注释,修改为你的根目录位置)
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python3.6m \#(这两个目录要看具体anaconda3中目录的名字,我做了对应修改)
$(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include
# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.6m#(这里我取消了注释,并且修改了内容,重新编译boost python3,后面讲)
# 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)/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/hdf5/serial
# 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 ?= @
LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda3/lib#(这里是由于会出现一个关于png的错误,我后面讲)
修改Makefile中的内容,在protoc前加入protoc的具体位置,可使用which protoc查看
# 在protoc --proto_path=$(PROTO_SRC_DIR) --cpp_out=$(PROTO_BUILD_DIR) $<
# 和protoc --proto_path=src --python_out=python $< 前都加上位置改为
/usr/local/bin/protoc --proto_path……
接下来使用命令编译
make all -j4
# 如果编译出现问题,使用make clean 然后百度问题如何解决,不断尝试直到make成功
make clean
make runtest -j4
成功的话就会出现感人的passed
3.5 安装python关联
只是这样的话只能在终端中操作,想要在python中import,需要添加环境变量
# 可能不需要这步操作
make pycaffe -j4
配置环境
gedit ~/.bashrc
# 添加你下载的caffe下的python文件夹的位置
PYTHONPATH=/path/to/caffe/python:$PYTHONPATH
source ~/.bashrc
测试import caffe
感人的通过
4 要点
1、可能出现各种问题,百度一下慢慢解决吧
2、tensorflow也要和protobuf,python匹配,另外小心别把python版本升到3.7,或者出现其他组件升级导致tensorflow又用不了了
5 用mnist测试一下caffe工作情况
cd /path/to/caffe
#下载MNIST数据库并解压缩
./data/mnist/get_mnist.sh
#将其转换成Lmdb数据库格式
./examples/mnist/create_mnist.sh
#训练网络
./examples/mnist/train_lenet.sh
# 问题集锦
- undefined reference to `TIFFReadDirectory@LIBTIFF_4.0
在Makefile里修改LIBRARIES
LIBRARIES += boost_thread stdc++ boost_regex - 编译问题
尽量使用同一个编译器,如我是用的是gcc g++4.8