ubuntu16.04-caffe-tensorflow安装教程

1. 安装显卡驱动

https://tecadmin.net/install-latest-nvidia-drivers-ubuntu/

2. 安装cuda (8.0)

https://blog.csdn.net/wanzhen4330/article/details/81699769
https://www.cnblogs.com/go-better/p/7161006.html

注意点:
界面安装显卡驱动,(Install NVIDIA Accelerated Graphics Drives for Linux-x86_64) 这里选择no

3. 安装cudnn (6.0)

下载cudnn
[https://developer.nvidia.com/rdp/cudnn-archive](https://developer.nvidia.com/rdp/cudnn-archive
安装cudnn v6.1,方便后面安装tensorflow-gpu 1.4)
http://blogs.csdn.net/m037192554/article/details/81032426

4. 安装OpenCV依赖

sudo apt-get install build-essential # 安装各种开发工具,比如g++,libstdc++等
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev # 处理图像所需的包,可选
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install libxvidcore-dev libx264-dev # 处理视频所需的包
sudo apt-get install libatlas-base-dev gfortran # 优化opencv功能
sudo apt-get install ffmpeg

5. 安装caffe ,修改Makefile.config文件

caffe安装包和依赖库可在百度云中找到链接:https://pan.baidu.com/s/16gVpZIWgxhanXXVFjo1D1Q
提取码:qy12

sudo gedit Makefile.config #打开Makefile.config文件 

根据个人情况修改文件:
a.若使用cudnn,则
USE_CUDNN := 1 前 # 去掉

b.若使用的opencv版本是3的,则
OPENCV_VERSION := 3 前 # 去掉

c.若要使用python来编写layer,则
WITH_PYTHON_LAYER := 1 前 # 去掉

d. 将INCLUDE_DIRS和LIBRARY_DIRS中的Caffe依赖库修改为自己本地的路径,比如将Caffe依赖库放置/home/qq/usr/local/下面,那么INCLUDE_DIRS和LIBRARY_DIRS如下:

INCLUDE_DIRS := $(PYTHON_INCLUDE) \
        /usr/local/include/ \
        /home/qq/usr/local/Atlas/include/ \
        /home/qq/usr/local/Boost/Boost_1.61/include/ \
        /home/qq/usr/local/gflags/include/ \
        /home/qq/usr/local/glog/include/ \
        /home/qq/usr/local/hdf5/include/ \
        /home/qq/usr/local/hdf5_hl/include/ \
        /home/qq/usr/local/Leveldb/ \
        /home/qq/usr/local/Lmdb/include/ \
        /home/qq/usr/local/OpenCV/OpenCV_3.4.2_NoCUDA/so/include/ \
        /home/qq/usr/local/Protobuf/google/ \
        /home/qq/usr/local/Protobuf/ \
        /home/qq/usr/local/Snappy/include/

LIBRARY_DIRS := $(PYTHON_LIB) \
        /usr/local/lib/ \
        /usr/lib/ \
        /home/qq/usr/local/Atlas/lib/ \
        /home/qq/usr/local/Boost/Boost_1.61/lib/ \
        /home/qq/usr/local/gflags/lib/ \
        /home/qq/usr/local/glog/lib/ \
        /home/qq/usr/local/hdf5/lib/ \
        /home/qq/usr/local/hdf5_hl/lib/ \
        /home/qq/usr/local/Leveldb/lib/ \
        /home/qq/usr/local/Lmdb/lib/ \
        /home/qq/usr/local/OpenCV/OpenCV_3.4.2_NoCUDA/so/lib/ \
        /home/qq/usr/local/Protobuf/lib/ \
        /home/qq/usr/local/Snappy/lib/ \
        /home/qq/usr/local/3rdparty/
 

e. 修改其他相关的配置(如是否使用CUDA,CUDA路径,是否使用OpenCV3.x)

6. 修改Makefile文件

打开Makefile文件,搜索pathOfProtoc, pathOfProtoc的值就是Caffe依赖库ProtoBuffer的路径,将它的值修改为本地路径即可,比如Protobuffer放置在/home/qq/usr/local/Protobuf/路径中,则将pathOfProtoc的值修改为:

pathOfProtoc:=/home/qq/usr/local/Protobuf/bin/protoc

7. 修改动态链接库

将Makefile.config中LIBRARY_DIRS中的所有caffe依赖库加入系统的动态链接库

a. 执行:

sudo gedit /etc/ld.so.conf

b. 将Caffe依赖库路径添加到ld.so.conf文件中
比如将Caffe依赖库放入/home/qq/usr/local/,则在 ld.so.conf中添加如下内容:(注意最后没有反斜杠,跟第五步中d的区别)

/home/qq/usr/local/Atlas/lib/
/home/qq/usr/local/Boost/Boost_1.61/lib/
/home/qq/usr/local/gflags/lib/
/home/qq/usr/local/glog/lib/
/home/qq/usr/local/hdf5/lib/
/home/qq/usr/local/hdf5_hl/lib/
/home/qq/usr/local/Leveldb/lib/
/home/qq/usr/local/Lmdb/lib/
/home/qq/usr/local/OpenCV/OpenCV_3.4.2_NoCUDA/so/lib/
/home/qq/usr/local/Protobuf/lib/
/home/qq/usr/local/Snappy/lib/
/home/qq/usr/local/3rdparty/

c. 为了避免找不到CUDA路径,将下面的路径也放入ld.so.conf文件

/usr/local/cuda/lib64/

d. 执行:

sudo ldconfig

8. 编译Caffe

执行:
make all -j8
make runtest

注意:Caffe依赖库目前只支持Ubuntu16.04,如果你的Ubuntu是其他版本,需要重新编译

9.编译pycaffe‘

make pycaffe  -j8

配置环境变量,以便python调用:

sudo gedit ~/.bashrc

将export PYTHONPATH=/home/caffe/python:$PYTHONPATH添加到文件中

source ~/.bashrc

10. 安装tensorflow-gpu 1.4

pip3 install tensorflow-gpu

tensorflow-gpu 安装速度特别慢,且容易失败,可加镜像源
https://blog.csdn.net/wukai0909/article/details/62427437

另ubunu自带python版本2.7 和3.5 可自行切换,pip更新等参考博客【Ubuntu】Ubuntu修改默认Python版本 - CSDN博客

检查是否安装成功

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

显示Hello,tensorflow! 即可

你可能感兴趣的:(ubuntu16.04-caffe-tensorflow安装教程)