ubuntu16.04 安装caffe OpenCV TensorFlow

本文介绍在Ubuntu16.04下安装opencv3、caffe、 tensorflow,在安装tensorflow的GPU版本时,由于cuda和cudnn版本的不同,使用pip安装的tensorflow有可能和自己的cuda环境冲突,所以可以采用源码编译,编译tensorflow需要用到bazel,bazel的版本要和tensorflow的版本对应,不然编译不了。

安装OpenCV3.3

$ sudo apt-get update
$ sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy python3-numpy python3-dev python3-pip libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
$ sudo apt-get install libopenexr-dev
$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

安装Python常用包

sudo apt-get install python-numpy python3-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflags

下载并解压opencv安装包
// 创建build文件夹

mkdir build && cd build
精简版
cmake -D CMAKE_BUILD_TYPE=release -D CMAKE_INSTALL_PREFIX=/usr/local ..
基础版

安装opencv 第三方库, 注意第三方库opencv_contrib-3.3.1的路径

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/home/mzn/Software/opencv-3.3.1/opencv_contrib-3.3.1/modules/ .. 
升级版

!!!请注意将下列路径修改为自己的路径

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/home/u11266/.local \
-D PYTHON3_EXECUTABLE=/home/u11266/.local/bin/python3 \
-D PYTHON3_INCLUDE_DIR=/home/u11266/.local/include/python3.5m \
-D PYTHON_INCLUDE_DIR=/home/u11266/.local/include/python3.5m \
-D PYTHON_LIBRARY=/home/u11266/.local/lib/libpython3.5m.so \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/home/u11266/.local/lib/python3.5/site-packages/numpy/core/include \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D PYTHON_EXECUTABLE=/home/u11266/.local/lib/python3.5\
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules \
-D BUILD_EXAMPLES=ON ..

添加OpenGL VTK模块(根据需要进行选择)

cmake -D WITH_GTK_2_X=ON -D WITH_OPENGL=ON -D CMAKE_BUILD_TYPE=RELEASE -D VTK_DIR=/home/super/Downloads/VTK-7.1.1/build -D CMAKE_INSTLL_PREFIX=/usr/local ..

Finally

make -j4  #注意如果电脑运行内存不够大,使用make -j 可能导致电脑卡死,如果电脑配置较低,建议使用较小的数字 -j2
make install  #注意安装路径所需权限,若按照在/usr/local/下,需root权限,命令前加sudo

安装caffe依赖包

$ sudo apt-get update
$ sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev  libhdf5-serial-dev protobuf-compiler
$ sudo apt-get install --no-install-recommends libboost-all-dev
$ sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev libatlas-base-dev python-dev

下载caffe安装包

$ git clone https://github.com/BVLC/caffe.git

修改配置文件

$ cd caffe
$ vim Makefile.conf 

若使用OpenCV3, 需要将21行 OPENCV_VERSION := 3 取消注释
修改第95行 在后面加上 /usr/include/hdf5/serial
在96行后面加上 /usr/lib/x86_64-linux-gnu/hdf5/serial

94 # Whatever else you find you need goes here.
95 INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
96 LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial

使用 make -j4 编译caffe

安装pycaffe

安装依赖包

$ sudo apt-get install python-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflags ipython

编译

$ cd ~/caffe
$ make pycaffe

添加环境变量~/caffe/python到$PYTHONPATH:

$ sudo vim /etc/profile    #添加全局环境变量
$ vim ~/.bashrc   # 添加个人环境变量
export PYTHONPATH=~/caffe/python:$PYTHONPATH  //caffe路径下的Python目录
$ source /etc/profile # 使之环境变量生效

测试Python是否可以使用:

$ python
……
>>>import caffe
>>>

ubuntu下使用pip安装TensorFlow

注意!下面请看完再执行命令

$ sudo apt-get install python-pip python-dev   # for Python 2.7
$ sudo apt-get install python3-pip python3-dev # for Python 3.n

如果使用python2.7,就运行第一行命令,如果使用python3, 就使用第二条命令

$ pip install tensorflow      # Python 2.7; CPU support (no GPU support)
$ pip3 install tensorflow     # Python 3.n; CPU support (no GPU support)
$ pip install tensorflow-gpu  # Python 2.7;  GPU support
$ pip3 install tensorflow-gpu # Python 3.n; GPU support 

如果使用默认的pip源下载安装可能会稍微有点慢,请耐心等待,或使用下面的方法

  1. 下载Python2.7 版本的Tensorlfow-CPU
wget https://pypi.tuna.tsinghua.edu.cn/packages/8a/de/1a8de6c1119cf595a38bedcb6072b5988ee92b0bf33b7b74b81aa7cffc49/tensorflow-1.4.1-cp27-cp27mu-manylinux1_x86_64.whl#md5=80aefda50210f7e1f617860ae3b38bbf
pip install tensorflow-1.4.1-cp27-cp27mu-manylinux1_x86_64.whl
  1. 下载Python3.5 版本的TensorFlow-CPU
wget https://pypi.tuna.tsinghua.edu.cn/packages/dc/de/640ff90d9555d21400abac5bebd2cec8c30015d5247159708291206cef52/tensorflow-1.4.1-cp35-cp35m-manylinux1_x86_64.whl#md5=68dafcc093cd6305dce37b71427082f4
pip3 install tensorflow-1.4.1-cp35-cp35m-manylinux1_x86_64.whl
tensoflow 安装出现futures requires Python ‘>=2.6, < 3’ but the running Python is 3.5.3的解决方法

futures是不能安装在python3中的,或者说安装了也没用
具体信息https://pypi.python.org/pypi/futures
但是tensoflow安装时却需要这个依赖。。。。。安装的时候就会报错
解决办法:

pip3 install future3==.1.1

bazel 编译安装TensorFlow

首先从github下载tensorflow源码,以及安装bazel 教程
下载tensorflow源码,并切换到你想要的版本,版本号可在github上 Tags里查看
在这里插入图片描述

git clone https://github.com/tensorflow/tensorflow 
git checkout v1.4.1
git checkout -b v1.4.1

下面开始编译

可以根据需要选择以下版本中选择编译的命令

  • 版本1
bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
  • 版本2
bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package --action_env="LD_LIBRARY_PATH=${LD_LIBRARY_PATH}"    
  • 版本3
    根据本人的编译经历,使用 --config=mkl 会编译不通过,错误显示找不到头文件,可以自行删除
bazel build -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-mfpmath=both --copt=-msse4.2 --config=mkl --config=cuda -k //tensorflow/tools/pip_package:build_pip_package
  • 版本4
bazel build -c opt --copt=-march=native --copt=-mfpmath=both --config=cuda -k //tensorflow/tools/pip_package:build_pip_package

生成TensorFlow whl安装包

bazel-bin/tensorflow/tools/pip_package/build_pip_package ~/tensorflow

安装cuda依赖库 sudo ./cuda_8.0.61_375.26_linux.run

sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
tensorflow源码安装配置:
$ sudo apt-get install python-numpy python-dev python-pip python-wheel
$ sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel
$ sudo apt-get install libcupti-dev

选择你想要支持的Python,以下路径请根据自己的实际情况修改

$ cd tensorflow  # cd to the top-level directory created
$ ./configure
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python2.7
Found possible Python library paths:
  /usr/local/lib/python2.7/dist-packages
  /usr/lib/python2.7/dist-packages
Please input the desired Python library path to use.  Default is [/usr/lib/python2.7/dist-packages]

Using python library path: /usr/local/lib/python2.7/dist-packages
Do you wish to build TensorFlow with MKL support? [y/N]
No MKL support will be enabled for TensorFlow
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:
Do you wish to use jemalloc as the malloc implementation? [Y/n]
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N]
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N]
No Hadoop File System support will be enabled for TensorFlow
Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N]
No XLA support will be enabled for TensorFlow
Do you wish to build TensorFlow with VERBS support? [y/N]
No VERBS support will be enabled for TensorFlow
Do you wish to build TensorFlow with OpenCL support? [y/N]
No OpenCL support will be enabled for TensorFlow
Do you wish to build TensorFlow with CUDA support? [y/N] Y
CUDA support will be enabled for TensorFlow
Do you want to use clang as CUDA compiler? [y/N]
nvcc will be used as CUDA compiler
Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 8.0]: 8.0
Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]: 6
Please specify the location where cuDNN 6 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size.
[Default is: "3.5,5.2"]: 3.0
Do you wish to build TensorFlow with MPI support? [y/N] 
MPI support will not be enabled for TensorFlow
Configuration finished


To build a pip package for TensorFlow with CPU-only support, you would typically invoke the following command:

To build a pip package for TensorFlow with GPU support, invoke the following command:

$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

$ sudo pip install /tmp/tensorflow_pkg/tensorflow-1.4.0-py2-none-any.whl

$ bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package 

$ bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值