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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章