安装caffe
- 下载caffe:执行命令: sudo git clone https://github.com/BVLC/caffe.git
-
安装依赖项:
sudo apt-get install libatlas-base-dev
sudo apt-get install libprotobuf-dev
sudo apt-get install libleveldb-dev
sudo apt-get install libsnappy-dev
sudo apt-get install libopencv-dev
sudo apt-get install libboost-all-dev
sudo apt-get install libhdf5-serial-dev
sudo apt-get install libgflags-dev
sudo apt-get install libgoogle-glog-dev
sudo apt-get install liblmdb-dev
sudo apt-get install protobuf-compiler
-
编译caffe
cd ~/caffe
sudo cp Makefile.config.example Makefile.config
make all
-
配置运行环境
sudo vi /etc/ld.so.conf.d/caffe.conf
添加内容:
/usr/local/cuda/lib64
- 更新配置
sudo ldconfig - caffe测试,执行以下命令:
cd ~/caffe
sudo sh data/mnist/get_mnist.sh
sudo sh examples/mnist/create_mnist.sh
最后测试:
sudo sh examples/mnist/train_lenet.sh
运行结果如下:
若出现
NVCC src/caffe/util/math_functions.cu
nvcc fatal : Unsupported gpu architecture 'compute_60'
make: *** [.build_release/cuda/src/caffe/util/math_functions.o] 错误 1
make: *** 正在等待未完成的任务....
这个错误,
解决办法:由于安装的cuda版本是7.5,当前下载的caffe版本比较新,需要修改里面的makefile文件,屏蔽下面的代码,cuda<8.0,谷歌具体参考网址
#-gencode arch=compute_60,code=sm_60 \
#-gencode arch=compute_61,code=sm_61 \
#-gencode arch=compute_61,code=compute_61
其他依赖项
我们查看caffe目录下 Makefile.config 内容如下:
## 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
# 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
# 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 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_50,code=compute_50
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := mkl
# 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)/anaconda
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
# $(ANACONDA_HOME)/include/python2.7 \
# $(ANACONDA_HOME)/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)/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
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/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
# 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
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 ?= @
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
可以看到诸如
# cuDNN acceleration switch (uncomment to build with cuDNN).
# USE_CUDNN := 1
# Uncomment if you're using OpenCV 3
# OPENCV_VERSION := 3
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
都是使用默认的设置,我们可以安装其他依赖项提高caffe运行效率
opencv3.0安装
-
github上有人写好完整的运行脚本自动下载opencv,编译,安装,配置等
-
Caffe + Ubuntu 15.04 + CUDA 7.5 新手安装配置指南 作者 Xin-Yu Ou(欧新宇) 可以到他的网盘中下载
PS:为了方便大家使用,我提供一个百度云盘,用于分享部分安装过程中需要用到的软件包和链接地址(所有软件包仅供学术交流使用,请大家尽量去官网下载。)。百度云盘链接:http://pan.baidu.com/s/1qX1uFHa 密码:wysa
-
在Install-OpenCV-master文件夹中包含安装各个版本opencv脚本
-
切换到目录执行:
sudo sh Ubuntu/dependencies.sh
安装依赖项
-
执行opencv3.0安装脚本
sudo sh Ubuntu/3.0/opencv3_0_0.sh
等待安装完成即可
-
修改Makefile.config
# Uncomment if you're using OpenCV 3
OPENCV_VERSION := 3
-
(可选)opencv3.1已经发布,如果要安装最新的opencv3.1,我们可以先执行
sudo sh get_latest_version_download_file.sh
获取最新的地址,然后更新opencv3_0_0.sh中的下载地址,同时需要修正文件名等
arch=$(uname -m)
if [ "$arch" == "i686" -o "$arch" == "i386" -o "$arch" == "i486" -o "$arch" == "i586" ]; then
flag=1
else
flag=0
fi
echo "Installing OpenCV 3.0.0"
mkdir OpenCV
cd OpenCV
echo "Removing any pre-installed ffmpeg and x264"
sudo apt-get -y remove ffmpeg x264 libx264-dev
echo "Installing Dependenices"
sudo apt-get -y install libopencv-dev
sudo apt-get -y install build-essential checkinstall cmake pkg-config yasm
sudo apt-get -y install libtiff4-dev libjpeg-dev libjasper-dev
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev
sudo apt-get -y install python-dev python-numpy
sudo apt-get -y install libtbb-dev
sudo apt-get -y install libqt4-dev libgtk2.0-dev
sudo apt-get -y install libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev
sudo apt-get -y install x264 v4l-utils ffmpeg
sudo apt-get -y install libgtk2.0-dev
echo "Downloading OpenCV 3.0.0"
wget -O opencv-3.0.0.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/3.0.0/opencv-3.0.0.zip/download
echo "Installing OpenCV 3.0.0"
unzip opencv-3.0.0.zip
cd opencv-3.0.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
make -j8
sudo make install
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
echo "OpenCV 3.0.0 ready to be used"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
安装opencv3遇到的问题
- 在执行
sudo sh Ubuntu/3.0/opencv3_0_0.sh
出现有个地方一直卡住了,显示在下载一个文件: ippicv_linux_20141027.tgz
因为墙的原因,这个文件无法下载下来 -
[其他文档] ippicv_linux_20141027.tgz 处下载文件 ippicv_linux_20141027.tgz
-
下载后拷贝到opencv/3rdparty/ippicv/downloads/linux-8b449a536a2157bcad08a2b9f266828b/ 目录下即
- http://stackoverflow.com/questions/25726768/opencv-3-0-trouble-with-installation
——————————————————————————
cv2
ImportError: No module named cv2
Solution
python2:
conda install -c menpo opencv (安装opencv2.4.11, conda用的opencv3.1没有编译gtk,所以imshow等函数会出问题)
python3
opencv3.2安装完成之后,在/usr/下搜索cv2.so文件,复制到虚拟环境中:
eg:/home/xx/anaconda3/envs/tf3/lib/python3.6/site-packages/cv2.so 即可
安装BLAS——选择MKL
- 首先下载 MKL(Intel(R) Parallel Studio XE Cluster Edition for Linux 2016)
网址: https://software.intel.com/en-us/intel-education-offerings
Caffe + Ubuntu 15.04 + CUDA 7.5 新手安装配置指南 作者 Xin-Yu Ou(欧新宇) 可以到他的网盘中下载, 需要自己申请序列号 -
下载完成后: parallel_studio_xe_2016.tgz
-
执行以下命令:
$ tar zxvf parallel_studio_xe_2016.tar.gz
$ chmod a+x parallel_studio_xe_2016 -R
$ sh install_GUI.sh
-
环境配置:
$ sudo gedit /etc/ld.so.conf.d/intel_mkl.conf
然后添加以下内容
/opt/intel/lib/intel64
/opt/intel/mkl/lib/intel64
配置生效: sudo ldconfig -v
安装MKL完成
-
修改Makefile.config
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := mkl
cuDNN安装
-
cudnn下载
下载地址:https://developer.nvidia.com/cudnn
或者到网盘: http://pan.baidu.com/s/1bnOKBO 下载
下载相应文件cudnn-7.0-linux-x64-v4.0-rc.tgz, 放到~根目录下
-
切换到~目录,执行命令
sudo tar xvf cudnn-7.0-linux-x64-v4.0-rc.tgz
cd cuda/include
sudo cp *.h /usr/local/include/
cd ../lib64
sudo cp lib* /usr/local/lib/
cd /usr/local/lib
sudo chmod +r libcudnn.so.4.0.4
sudo ln -sf libcudnn.so.4.0.4 libcudnn.so.4
sudo ln -sf libcudnn.so.4 libcudnn.so
sudo ldconfig
-
修改Makefile.config
# cuDNN acceleration switch (uncomment to build with cuDNN).
USE_CUDNN := 1
cudnn版本问题
在make工程的时候出现以下错误:
...
NVCC src/caffe/layers/deconv_layer.cu
NVCC src/caffe/layers/cudnn_conv_layer.cu
src/caffe/layers/cudnn_conv_layer.cu(81): error: argument of type "cudnnAddMode_t" is incompatible with parameter of type "const void *"
detected during instantiation of "void caffe::CuDNNConvolutionLayer<Dtype>::Forward_gpu(const std::vector<caffe::Blob<Dtype> *, std::allocator<caffe::Blob<Dtype> *>> &, const std::vector<caffe::Blob<Dtype> *, std::allocator<caffe::Blob<Dtype> *>> &) [with Dtype=float]"
(157): here
...
20 errors detected in the compilation of "/tmp/tmpxft_00002703_00000000-16_cudnn_conv_layer.compute_50.cpp1.ii".
make: *** [.build_release/cuda/src/caffe/layers/cudnn_conv_layer.o] Error 1
make: *** Waiting for unfinished jobs....
解决方案:
更换V3版本cudnn Caffe 工程的一些编译错误以及解决方案
$ cd lib64/
$ sudo cp lib* /usr/local/cuda/lib64/
$ cd ../include/
$ sudo cp cudnn.h /usr/local/cuda/include/
$ cd /usr/local/cuda/lib64/
$ sudo rm -r libcudnn.so libcudnn.so.7.0
$ sudo ln -sf libcudnn.so.7.0.64 libcudnn.so.7.0
$ sudo ln -sf libcudnn.so.7.0 libcudnn.so
$ sudo ldconfig
重新编译测试caffe
-
编译
sudo make clean
sudo make all
-
sample测试: ( 比不使用cudnn快很多)
sh data/mnist/get_mnist.sh
sh examples/mnist/create_mnist.sh
-
我们可以将迭代次数增加到50000次
sudo gedit examples/mnist/lenet_solver.prototxt
修改max_iter: 50000
最后:
sh examples/mnist/train_lenet.sh
编译Python接口
依赖项
sudo apt-get install -y 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 vi ~/.bashrc
添加: export PYTHONPATH=/home/dl/caffe/python:$PYTHONPATH
sudo ldconfig
sudo make pycaffe -j8
编译matlab接口
- 安装matlab2014
sh /usr/local/MATLAB/R2014a/bin/matlab - Makefile.config 中修改 : MATLAB_DIR := /usr/local/MATLAB/R2014a
- sudo make matcaffe -j8
参考:http://blog.csdn.net/zengdong_1991/article/details/51174044