您当前的位置:首页 > 计算机 > 编程开发 > Other

Ubuntu16.04+CUDA9+cudnn7+python3.5+源码编译Tensorflow1.4+opencv3.3

时间:03-27来源:作者:点击数:

为了加快环境所需依赖的下载速度,首先更新 apt-get 源Ubuntu16.04[清华大学映像]

如何修改默认的更新源呢:

首先备份:$sudo cp /etc/apt/sources.list etc/apt/sources.list_backup

修改:$sudo gedit /etc/apt/sources.list

清空并替换为

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse

执行

$sudo apt-get update
$sudo apt-get upgrade
$sudo apt-get install python3-pip #安装pip

更新pip源

#在 ~/.config 下新建pip文件夹
#$cd ~/.config/pip
#$vim pip.conf

[global] 
index-url = https://pypi.tuna.tsinghua.edu.cn/simple 

保存退出

安装 numpy,是编译 tensorflow 和 opencv3 的依赖

$sudo pip3 install numpy

安装Nvidia驱动(也可在安装CUDA9同时安装驱动)

驱动版本查询Nvidia驱动程序

执行以下步骤

$sudo apt-get purge nvidia* #卸载所有显卡驱动 
$sudo add-apt-repository ppa:graphics-drivers/ppa #Personal Package Archives,也就是个人软件包集,添加驱动程序下载源
$sudo apt-get update && sudo apt-get install nvidia-384 nvidia-settings 

重启

$ nvidia-smi #输入命令,输出显卡信息及驱动程序版本

安装CUDA9

CUDA Toolkits download

选择CUDA9.0 ubuntu16.04 deb(local)

下载完成后执行

$sudo dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64.deb
$sudo apt-key add /var/cuda-repo-<version>/7fa2af80.pub
$sudo apt-get update
$sudo apt-get install cuda

测试CUDA9.0

$nvcc -V #输出CUDA版本信息

测试Samples

$mkdir test
$cd test
$cp -r /usr/local/cuda-9.0/samples/ . 
$cd samples/ 
$make -j16
$cd ./1_Utilities/bandwidthTest
$./bandwidthTest 

查看最后结果,如果是Result = PASS,那就ok。

接着cd到根目录。编辑.bashrc文件

$sudo gedit .bashrc

在最后加上

#cuda9.0
export PATH=/usr/local/cuda/bin${PATH:+:${PATH}} 
export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} 
export CUDA_HOME=/usr/local/cuda

保存退出。接着在命令窗口输入

$source .bashrc

让文件生效。

(请勿选择9.1,否则编译Tensorflow会出现问题)

如果误装了其他版本可通过以下方法卸载并重装

$sudo apt-get --purge remove cuda
$sudo apt autoremove
$sudo apt-get clean
$sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb
$sudo apt-get update
$sudo apt-get install cuda-9-0

安装cudnn7

下载地址

注意自己的路径及cudnn版本

tar -zxvf cudnn-9.0-linux-x64-v7.tgz 
sudo cp cuda/include/cudnn.h /usr/local/cuda/include/ 
sudo cp -a cuda/lib64/libcudnn* /usr/local/cuda/lib64/ 
sudo chmod a+r /usr/local/cuda/include/cudnn.h 
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*

注意上面第二条指令,这里多加了一个-a,在官方给的方案里面没有,我建议你这样做,否则会出现连接出错的问题。如果你忘记了,可以参考下面做法

#软链接至关重要
cd /usr/local/cuda/lib64/
sudo rm -rf libcudnn.so libcudnn.so.7.0  
sudo ln -s libcudnn.so.7.0.5 libcudnn.so.7.0  
sudo ln -s libcudnn.so.7.0 libcudnn.so 

安装Bazel

官方文档


配置Tensorflow1.4

获取源码

$git clone --recurse-submodules https://github.com/tensorflow/tensorflow
$cd tensorflow/

下面是我配置tensorflow的log,默认python3.5

注意:python和cuda的路径

$ ./configure 
You have bazel 0.8.1 installed. 
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3 
Found possible Python library paths: 
/home/shengchun/tensorflow/models/ 
/bin 
/usr/local/cuda/bin 
/sbin 
/home/shengchun/tensorflow/models/slim 
/usr/bin 
/usr/local/sbin 
/usr/local/games 
/usr/lib/python3/dist-packages 
/usr/games 
/home/shengchun/mxnet-ssd/mxnet/python 
/usr/sbin 
/usr/local/bin 
/usr/local/lib/python3.5/dist-packages 
Please input the desired Python library path to use. Default is [/home/shengchun/tensorflow/models/]/usr/local/lib/python3.5/dist-packages 
Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: [Enter] 
jemalloc as malloc support will be enabled for TensorFlow. 
Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: [Enter] 
Google Cloud Platform support will be enabled for TensorFlow. 
Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: [Enter] 
Hadoop File System support will be enabled for TensorFlow. 
Do you wish to build TensorFlow with XLA JIT support? [y/N]: [Enter] 
No XLA JIT support will be enabled for TensorFlow. 
Do you wish to build TensorFlow with GDR support? [y/N]: [Enter] 
No GDR support will be enabled for TensorFlow. 
Do you wish to build TensorFlow with VERBS support? [y/N]: [Enter] 
No VERBS support will be enabled for TensorFlow. 
Do you wish to build TensorFlow with OpenCL support? [y/N]: [Enter] 
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. 
Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 8.0]: 9.0
Please specify the location where CUDA 9.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: [Enter] 
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]: 7.0.5 
Please specify the location where cuDNN 7.0.3 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:[Enter] 
**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: 5.0]**[Enter] 
Do you want to use clang as CUDA compiler? [y/N]: [Enter] 
nvcc will be used as CUDA compiler. 
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: 
Do you wish to build TensorFlow with MPI support? [y/N]: [Enter] 
No MPI 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]: [Enter] 
Add “–config=mkl” to your bazel command to build with MKL support. 
Please note that MKL on MacOS or windows is still not supported. 
If you would like to use a local MKL instead of downloading, please set the environment variable “TF_MKL_ROOT” every time before build. 
Configuration finished

使用Bazel编译

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

编译时间有点儿长。

bazel编译命令建立了一个名为build_pip_package的脚本。运行如下的命令将会在 /tmp/tensorflow_pkg路径下生成一个.whl文件。

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

pip安装tensorflow

$sudo pip3 install /tmp/tensorflow_pkg/tensorflow-1.4.0-cp35-cp35m-linux_x86_64.whl

安装完毕 import tensorflow 进行验证。


安装opencv3.3

CUDA9.0只支持opencv3.3以上版本

1、下载opencv源码:https://github.com/opencv/opencv

2、下载 opencv_contrib 源程序https://github.com/opencv/opencv_contrib

下面开始安装

1、先将opencv和opencv_contrib解压到某个目录下,我的是在 /usr/opencv

2、依次执行下面的命令

$sudo apt-get update
$sudo apt-get upgrade

#opencv3.3的依赖
$sudo apt-get install build-essential cmake pkg-config
$sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

$sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

$sudo apt-get install libxvidcore-dev libx264-dev

$sudo apt-get install libgtk-3-dev

$sudo apt-get install libatlas-base-dev gfortran

$sudo apt-get install python3.5-dev python-numpy libtbb2 libtbb-dev  libdc1394-22-dev

进入opencv源代目录(如:/usr/opencv/opencv-master),创建build目录

mkdir build
cd build

执行编译命令

sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON3_EXECUTABLE=/usr/bin/python3 -D PYTHON_INCLUDE_DIR=/usr/include/python3.5 -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.5/dist-packages/numpy/core/include -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_EXTRA_MODULES_PATH=/usr/opencv/opencv_contrib-master/modules -D PYTHON_EXECUTABLE=/usr/lib/python3 -D BUILD_EXAMPLES=ON ..

注意:在执行上面命令时,正常来说会到ippicv处时执行不下去,编译会退出,因为需要依赖 ippicv 的包编译时下载很慢,出现网络异常下载不下来。解决方法是建议先手动下载

ippicv_2017u2_lnx_intel64_20170828.tgz(github下载链接)

同时用该文件覆盖 /usr/opencv/opencv-master/ .cache(注意有“.”)目录下的 ippicv文件(文件名这里注意是一个很长的通过guid生成的名字,不能改变)。

如果编译通过当我没说 :)

$sudo make -j16
$sudo make clean
$sudo make install

$sudo ldconfig

如果没有问题的话就大功告成了。


配置完成之后,我们可以用搭建好的环境进行各种各样的视频Tracking.

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门