Ubuntu18.04下 GPU版本 Tensorflow2.0+CUDA10.0 / Tensorflow2.1+CUDA10.1 虚拟环境配置

一定要注意版本匹配问题,经测 Tensorflow2.0+CUDA10.0Tensorflow2.1+CUDA10.1 这两种组合的GPU版本是没有问题的,我在安装过程中,由于已经安装了CUDA10.1,安装完Tensorflow2.0之后测试报错GPU不可用,卸载2.0版本之后重新安装了2.1版本,经测可用。虽然Anaconda安装方便,但是在Ubuntu系统下容易与系统环境变量冲突,在之前的使用过程中体验很差,因此选择virtualenv进行隔离安装。

注意:如果你是双系统且没有安装驱动,建议按照以下教程,先安装驱动。如果已经安装驱动,并且能正常开机,请跳过。双系统防止系统卡死安装显卡驱动

以下内容参考自:How to install TensorFlow 2.0 on Ubuntu

## 1.安装依赖库
1.1 更新系统

sudo apt-get update
sudo apt-get upgrade

1.2 安装编译工具

sudo apt-get install build-essential cmake unzip pkg-config
sudo apt-get install gcc-6 g++-6

1.3 安装screen,在同一窗口中使用多个终端的工具,可用于远程SSH连接。

sudo apt-get install screen

1.4 install X windows libraries and OpenGL libraries:

sudo apt-get install libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev

1.5 Along with image and video I/O libraries:

sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev

1.6 install optimization libraries:

sudo apt-get install libopenblas-dev libatlas-base-dev liblapack-dev gfortran

1.7 HDF5 for working with large datasets:

sudo apt-get install libhdf5-serial-dev

1.8 Python 3 development libraries including TK and GTK GUI support:

sudo apt-get install python3-dev python3-tk python-imaging-tk
sudo apt-get install libgtk-3-dev

2 安装显卡驱动和CUDA

在这里插入代码片

如果已经安装显卡驱动,前2.1到2.5可以跳过。
2.1 add an apt-get repository so that we can install NVIDIA GPU drivers.

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update

2.2 install your NVIDIA graphics driver:

sudo apt-get install nvidia-driver-418

2.3 reboot command and wait for your system to restart:

sudo reboot now

2.4 both download and install CUDA 10.0 right from your terminal

cd ~
mkdir installers
cd installers/
wget https://developer.nvidia.com/compute/cuda/10.0/Prod/local_installers/cuda_10.0.130_410.48_linux
mv cuda_10.0.130_410.48_linux cuda_10.0.130_410.48_linux.run
chmod +x cuda_10.0.130_410.48_linux.run
sudo ./cuda_10.0.130_410.48_linux.run --override

2.5 error process
You will be prompted to accept the End User License Agreement (EULA). During the process, you may encounter the following error:

Please make sure that
PATH includes /usr/local/cuda-10.0/bin
LD_LIBRARY_PATH includes /usr/local/cuda-10.0/lib64, or, add /usr/local/cuda-10.0/lib64 to /etc/ld.so.conf and run ldconfig as root
To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-10.0/bin
Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-10.0/doc/pdf for detailed information on setting up CUDA.
*WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 384.00 is required for CUDA 10.0 functionality to work.
To install the driver using this installer, run the following command, replacing  with the name of this run file:
sudo .run -silent -driver
Logfile is /tmp/cuda_install_25774.log

You may safely ignore this error message.
2.6 update bash profile

nano ~/.bashrc

Insert the following lines at the bottom of the profile:

# NVIDIA CUDA Toolkit
export PATH=/usr/local/cuda-10.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64

这里注意修改自己的CUDA版本。

source ~/.bashrc

2.7 query CUDA to ensure that it is successfully installed:

nvcc -V

2.8 cuDNN Library for Linux
cuDNN v7.6.4 for CUDA 10.0 from the following link: https://developer.nvidia.com/rdp/cudnn-archive

注意版本匹配问题

scp ~/Downloads/cudnn-10.0-linux-x64-v7.6.4.24.tgz \
    username@your_ip_address:~/installers
cd ~/installers
tar -zxf cudnn-10.0-linux-x64-v7.6.4.38.tgz
cd cuda
sudo cp -P lib64/* /usr/local/cuda/lib64/
sudo cp -P include/* /usr/local/cuda/include/
cd ~

3. 安装虚拟环境

3.1 download pip3

wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py

3.2 install virtual environment tools:

pip3 install virtualenv virtualenvwrapper

3.3 update bash profile

nano ~/.bashrc

insert the following lines at the end of the file:

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

这里可能会报错,是因为virtualenvwrapper.sh路径安装不对,如果报错请参考:/usr/local/bin/virtualenvwrapper.sh: 没有那个文件或目录 的解决办法

source ~/.bashrc

3.4 create Python 3 deep learning virtual environment named dl4cv:

mkvirtualenv dl4cv -p python3

4. Install TensorFlow 2.0 into your dl4cv virtual environment

如果安装的是CUDA10.1,请安装tensorflow-gpu2.1

4.1 activate the environment and install

workon dl4cv
pip install numpy
pip install tensorflow-gpu==2.0.0 # or tensorflow-gpu==2.1.0

4.2 nstalling standard image processing libraries including OpenCV:

pip install opencv-contrib-python
pip install scikit-image
pip install pillow
pip install imutils

4.3 install machine learning libraries and support libraries

pip install scikit-learn
pip install matplotlib
pip install progressbar2
pip install beautifulsoup4
pip install pandas

4.4 test

workon dl4cv
python
>>> import tensorflow as tf
>>> tf.__version__
2.0.0
>>> import tensorflow.keras
>>> import cv2
>>> cv2.__version__
4.1.2

4.5 check if TensorFlow 2.0’s installation is able to take advantage of your GPU:

workon dl4cv
python
>>> import tensorflow as tf
>>> tf.test.is_gpu_available()
True

4.6 deactivate the current virtual environment:

deactivate

你可能感兴趣的:(Ubuntu,&,Git,TensorFlow,&,Keras)