Posts Ubuntu16.04LTS+Tensorflow基本环境配置
Post
Cancel

Ubuntu16.04LTS+Tensorflow基本环境配置

参考博客

系统:Ubuntu16 (kernel:141)

安装Cuda驱动

采用下载文件安装,在系统设置选择软件和更新,更改下载源,选择aliyun,选择附件驱动,选择使用NVIDA binary driver.......nvidia-384: 图片1 提示重启后,终端输入:

1
sudo nvidia-settings

弹出界面: 图片2 则说明安装成功。通过nvidia-smi也可以得到GPU信息。

安装Cuda

官网下载cuda9.0deb文件。安装:

1
2
3
4
5
sudo dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64.deb(下载的文件名)
# Install the public CUDA GPG key:
sudo apt-key add /var/cuda-repo-9-0-local/7fa2af80.pub
sudo apt-get update
sudo apt-get install cuda

安装之后,需要修改配置文件:

1
sudo gedit /etc/profile

最后加入以下两行:

1
2
export PATH=/usr/local/cuda-9.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64:$LD_LIBRARY_PATH

然后使其生效:

1
source /etc/profile

可用以下语句验证:

1
nvcc --version

得到版本信息,测试Cuda的samples:

1
2
cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
sudo make

运行测试程序:

1
./deviceQuery

得到输出:

1
2
3
4
5
6
./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)
...
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 9.0, CUDA Runtime Version = 9.0, NumDevs = 1
Result = PASS

安装Cudnn

官网下载cudnn,需要注册登录,下载速度很慢,下载cudnn7.4.1 Library for Linux版本(与cuda9.0匹配),为tgz压缩文件,解压文件后进入include文件,进行头文件的复制:

1
sudo cp cudnn.h /usr/local/cuda/include/

再转到lib64目录下:

1
2
3
4
5
6
sudo cp lib* /usr/local/cuda/lib64/
cd /usr/local/cuda/lib64/
sudo rm -rf libcudnn.so libcudnn.so.7
sudo ln -s libcudnn.so.7.4.1 libcudnn.so.7
sudo ln -s libcudnn.so.7 libcudnn.so
sudo ldconfig

其中libcudnn.so.7.4.1等文件可能因版本不同而不同,注意和自己版本对照。

Python以及相关库

Ubuntu16自带的Python版本为2.73.5,可通过以下命令得到:

1
2
ls /usr/bin/python*
#结果:/usr/bin/python  /usr/bin/python2  /usr/bin/python2.7  /usr/bin/python3  /usr/bin/python3.5  /usr/bin/python3.5m  /usr/bin/python3m

默认的Python版本为(通过python --version得到):

1
Python 2.7.12

可通过修改bash文件得到,在文件~/.bashrc文件末尾加入一行:

1
alias python='/usr/bin/python3.5'

脚本生效:

1
source ~/.bashrc

安装python环境下的依赖库,有时候需要更新一下pip:

1
2
sudo pip2 install --upgrade pip
sudo pip3 install --upgrade pip

之后安装相关依赖包:

1
2
3
sudo apt-get install python-dev python-pip python3-dev python3-pip
sudo -H pip2 install -U pip numpy scipy matplotlib scikit-image scikit-learn ipython==5.4 pandas
sudo -H pip3 install -U pip numpy scipy matplotlib scikit-image scikit-learn ipython pandas

安装OpenCV3.4.5

官网下载linux版本的OpenCV3.4.5以及OpenCV3.4.5_contrib库(将contrib库解压到OpenCV3.4.5下的文件夹内),先安装Python依赖项,依赖项解释如下图: 图片3 命令如下:

1
sudo apt-get install build-essential checkinstall cmake pkg-config    yasm      gfortran && sudo apt-get install libjpeg8-dev  libjasper-dev     libpng12-dev  libtiff5-dev && sudo apt-get install libavcodec-dev libavformat-dev   libswscale-dev libdc1394-22-dev    x264        v4l-utils && sudo apt-get install libxine2-dev   libv4l-dev && sudo apt-get install libgstreamer0.10-dev  libgstreamer-plugins-base0.10-dev && sudo apt-get install qt5-default  libgtk2.0-dev && sudo apt-get install libtbb-dev           libatlas-base-dev && sudo apt-get install libfaac-dev     libmp3lame-dev   libtheora-dev libvorbis-dev libxvidcore-dev && sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev && sudo apt-get install libprotobuf-dev protobuf-compiler && sudo apt-get install libgoogle-glog-dev libgflags-dev && sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

OpenCV3.4.5中建立两个文件夹:buildinstall文件夹,前者构建make文件,后者为默认安装位置:

1
cd build

Cmake编译,通过cmake生成完整编译配置脚本,完成的工作如下:

  1. 构建正式RELEASE
  2. 编译好的库安装到/data/opencv/opencv-3.4.0/install/

  3. 安装C的例子

  4. 安装Python的例子

  5. 开启TBBV4LQtOpenGL特性

  6. 编译安装开源社区贡献库

  7. 编译其他例子

在安装过程中,会下载ippicv文件,网速会很慢,可采用手动下载tgz文件,放在opencv-3.4.5文件内,然后修改opencv-3.4.5/3rdparty/ippicv/ippicv.cmake文件,修改对应内容为下载文件所在地址:

1
2
3
4
5
6
ocv_download(FILENAME ${OPENCV_ICV_NAME}
               HASH ${OPENCV_ICV_HASH}
               URL
                 "${OPENCV_IPPICV_URL}"
                 "$ENV{OPENCV_IPPICV_URL}"
                 "file:///home/zheng/opencv-3.4.5/"

最后cmake

1
cmake  -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/home/zheng/opencv-3.4.5/install/ -D INSTALL_PYTHON_EXAMPLES=ON INSTALL_C_EXAMPLES=ON -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D OPENCV_EXTRA_MODULES_PATH=/home/zheng/opencv-3.4.5/opencv_contrib-3.4.5/modules -D BUILD_EXAMPLES=ON ..

支出Cuda的cmake:

1
cmake  -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/home/zheng/opencv-3.4.5/install/ -D INSTALL_PYTHON_EXAMPLES=ON INSTALL_C_EXAMPLES=ON -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D OPENCV_EXTRA_MODULES_PATH=/home/zheng/opencv-3.4.5/opencv_contrib-3.4.5/modules -D BUILD_EXAMPLES=ON -D WITH_CUDA=ON -D WITH_CUBLAS=ON -D DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" -D CUDA_ARCH_BIN="5.2" -D CUDA_ARCH_PTX="" -D CUDA_FAST_MATH=ON -D WITH_TBB=ON -D WITH_GTK=ON -D WITH_OPENGL=ON ..

其中,CUDA_ARCH_BIN="5.2"需要从官网确认GPU对应版本

最后编译安装:

1
2
sudo make -j4
sudo make install -j4

编译环境:

1
sudo gedit /etc/ld.so.conf.d/opencv.conf

写入OpenCV文件目录:

1
/home/zheng/opencv-3.4.5/install/lib

重启config文件:

1
sudo ldconfig

修改bashrc文件:

1
2
export PKG_CONFIG_PATH=/home/zheng/opencv-3.4.5/install/lib/ pkgconfig
export LD_LIBRARY_PATH=/home/zheng/opencv-3.4.5/install/lib

启动bashrc文件:

1
source ~/.bashrc

添加Python接口:

1
2
sudo ln -s /home/zheng/opencv-3.4.5/install/lib/python2.7/dist-packages/cv2/python-2.7/cv2.so /usr/local/lib/python2.7/dist-packages/
sudo ln -s /home/zheng/opencv-3.4.5/install/lib/python3.5/dist-packages/cv2/python-3.5/cv2.cpython-35m-x86_64-linux-gnu.so /usr/local/lib/python3.5/dist-packages

安装tensorflow-gpu和keras

1
2
sudo pip2 install tensorflow-gpu && sudo pip2 install keras
sudo pip3 install tensorflow-gpu && sudo pip3 install keras
This post is licensed under CC BY 4.0 by the author.