深度学习caffe:Ubuntu16.04安装指南(2)
caffe [CPU ONLY] 2017-01-19
最简单的安装配置方式: 不用GPU加速,使用OPENCV3.1图像库+系统自带的python2.7, 由官方教程(链接如下)简化而得到.
Ubuntu 16.04 or 15.10 Installation Guide
0. Ubuntu16.04 安装
如果对于安装Ubuntu的过程不是很熟悉的话,这里有不错的教程和常见问题解决方案.
Ubuntu16.04 U盘安装Ubuntu16.04制作 光盘刻录 安装与简介
Ubuntu 16.04 installation failure
1. 更新软件源
使用默认的软件源特别的慢,搜索国内Ubuntu16.04 软件源得到相关网页很多,
操作命令
# 在修改source.list前,最好先备份一份 sudo cp /etc/apt/sources.list /etc/apt/sources.list.old # 执行命令打开source.list文件,清空内容,输入清华的软件源 sudo gedit /etc/apt/sources.list # 开始更新 sudo apt-get update
注: 在bjtu,推荐清华大学的软件源,速度更快[也可以使用aliyun]
清华大学
# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial universe deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates universe deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial multiverse deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates multiverse deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security universe deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security multiverse
阿里云
# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial universe deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties deb http://archive.canonical.com/ubuntu xenial partner deb-src http://archive.canonical.com/ubuntu xenial partner deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
2. 安装需求软件
caffe的编译/运行需要依赖一些其它的程序和各种库
sudo apt-get update sudo apt-get install -y build-essential cmake git pkg-config sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler sudo apt-get install -y libatlas-base-dev sudo apt-get install -y --no-install-recommends libboost-all-dev sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev # (Python general) sudo apt-get install -y python-pip # (Python 2.7 development files) sudo apt-get install -y python-dev sudo apt-get install -y python-numpy python-scipy
建议:
安装过程最好一条命令一条命令的执行,出现了错误方便及时发现.
安装过程中因为网络原因,难免会出现安装失败的情况,不用担心,重新执行命令,多试几次就好啦~
3. OPENCV3.1
下载
项目地址: https://github.com/opencv/opencv.git
直接下载: opencv-master.zip
依赖项安装
sudo apt-get install --assume-yes build-essential cmake git sudo apt-get install --assume-yes build-essential pkg-config unzip ffmpeg qtbase5-dev python-dev python3-dev python-numpy python3-numpy sudo apt-get install --assume-yes libopencv-dev libgtk-3-dev libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev sudo apt-get install --assume-yes libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev sudo apt-get install --assume-yes libv4l-dev libtbb-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev sudo apt-get install --assume-yes libvorbis-dev libxvidcore-dev v4l-utils
解压,进入opencv-master目录 ,开始编译
# 解压进入源码目录 unzip opencv-master.zip cd opencv-master # 创建build目录,用于编译 mkdir build cd build/ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_CUBLAS=ON -DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" .. make -j $(($(nproc) + 1))
安装
# 当前在build目录下 sudo make install sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf' sudo ldconfig sudo apt-get update
测试:
到了这一步,如果没有报错,说明已经安装成功了,为了确认一下,可以用一个例子实际跑一下.
具体可以参考我的这一篇博文: OPENCV3.1测试demo
4. 配置libhdf5
直接执行下列命令即可,否则在第五步执行"make all" 会报hdf5相关错误
find . -type f -exec sed -i -e 's^"hdf5.h"^"hdf5/serial/hdf5.h"^g' -e 's^"hdf5_hl.h"^"hdf5/serial/hdf5_hl.h"^g' '{}' \; cd /usr/lib/x86_64-linux-gnu sudo ln -s libhdf5_serial.so.10.1.0 libhdf5.so sudo ln -s libhdf5_serial_hl.so.10.0.2 libhdf5_hl.so
5. caffe
下载
项目地址: https://github.com/BVLC/caffe
直接下载: caffe-master.zip
注: 通过git命令直接clone项目(git clone https://github.com/BVLC/caffe.git ),但是速度一般都特别慢,建议直接下载
python库支持
解压caffe-master.zip之后,进入caffe-master/python,安装python需求库
unzip caffe-master.zip cd caffe-master # 第一步,安装python需求库 cd python for req in $(cat requirements.txt); do pip install $req; done
配置文件修改: Makefile和Makefile.config[当前目录caffe-master]
Makefile
gedit ./Makefile # 替换 NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS) # 为 NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
Makefile.config
cp Makefile.config.example Makefile.config gedit ./Makefile.config
修改配置项如下
# 取消注释 CPU_ONLY := 1 OPENCV_VERSION := 3 # 包含和库路径保持同下面一致 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 /usr/local/share/OpenCV/3rdparty/lib/
编译运行
# 多核并行,加快执行速度: -j $(($(nproc) + 1)) make all -j $(($(nproc) + 1)) make test -j $(($(nproc) + 1)) make runtest -j $(($(nproc) + 1)) make pycaffe -j $(($(nproc) + 1)) make distribute -j $(($(nproc) + 1))
注: 编译运行过程若是报错提示缺少某个软件或是库,一般直接安装就可以解决. 重新编译命令为 make clean
给当前用户bash添加python环境变量
gedit ~/.bashrc # 最后一行添加 export PYTHONPATH=/path/to/caffe-master/python:$PYTHONPATH # 立即生效 source ~/.bashrc
6. 运行caffe自带的两个DEMO: minster和cifar10
实验部分链接: 运行caffe自带的DEMO