Installing Deep Learning Frameworks on Ubuntu with CUDA support

from: https://www.learnopencv.com/installing-deep-learning-frameworks-on-ubuntu-with-cuda-support/

Installing Deep Learning Frameworks on Ubuntu with CUDA support

This post is part of the series on Deep Learning for Beginners, which consists of the following tutorials :

  1. Neural Networks : A 30,000 Feet View for Beginners
  2. Installation of Deep Learning frameworks (Tensorflow and Keras with CUDA support )
  3. Introduction to Keras
  4. Understanding Feedforward Neural Networks
  5. Image Classification using Feedforward Neural Networks
  6. Image Recognition using Convolutional Neural Network
  7. Understanding Activation Functions
  8. Understanding AutoEncoders using Tensorflow
  9. Image Classification using pre-trained models in Keras
  10. Transfer Learning using pre-trained models in Keras
  11. Fine-tuning pre-trained models in Keras
  12. More to come . . .

In this article, we will learn how to install Deep Learning Frameworks like TensorFlow, Theano, Keras and PyTorch on a machine having a NVIDIA graphics card.

If you have a brand new computer with a graphics card and you don’t know what libraries to install to start your deep learning journey, this article will help you.

We will install CUDA, cuDNN, Python 2, Python 3, TensorFlow, Theano, Keras, Pytorch, OpenCV, Dlib along with other Python Machine Learning libraries step-by-step.

We have tested the instructions on a system with the following configuration:

Processor : Intel core i7 6850K with 6 cores and 40 PCIe lines
Motherboard : Gigabyte X99P – SLI
RAM : 32 GB
Graphics Card : Zotac GeForce GTX 1080 Ti with 11 GB RAM

We will be assuming a fresh Ubuntu 16.04 installation. i.e nothing has been installed on the system earlier.

Step 1 : Install Prerequisites

Before installing anything, let us first update the information about the packages stored on the computer and upgrade the already installed packages to their latest versions.

1

2

sudo apt-get update

sudo apt-get upgrade

Next, we will install some basic packages which we might need during the installation process as well in future. Also, remove the packages which are not needed.

1

2

3

sudo apt-get install -y build-essential cmake gfortran git pkg-config

sudo apt-get install -y python-dev software-properties-common wget vim

sudo apt-get autoremove

Step 2 : Install CUDA

CUDA ( Compute Unified Device Architecture ) is a parallel computing platform and API developed by NVIDIA which utilizes the parallel computing capabilities of the GPUs. In order to use the graphics card, we need to have CUDA drivers installed on our system.

If you do not have a NVIDIA CUDA supported Graphics Card, then you can skip this step. and go to Step 4.

Download the CUDA driver from the official nvidia website. We recommend you download the deb ( local ) version from Installer type as shown in the screenshot below.

https://developer.nvidia.com/cuda-downloads

After downloading the file, go to the folder where you have downloaded the file and run the following commands from the terminal to install the CUDA drivers.

Please make sure that the filename used in the command below is the same as the downloaded file.

1

2

3

sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb

sudo apt-get update

sudo apt-get install -y cuda-8.0

Run the following command to check whether the driver has installed successfully by running NVIDIA’s System Management Interface (nvidia-smi). It is a tool used for monitoring the state of the GPU.

1

nvidia-smi

You should get an output as shown below.

output of nvidia-smi command

As a side note, I found that apart from getting better resolution options for display, installing the CUDA driver lowers the power consumption of the graphics card from 71W to 16W for a NVIDIA GTX 1080 Ti GPU attached via PCIe x16.

Step 3 : Install cuDNN

CUDA Deep Neural Network (cuDNN) is a library used for further optimizing neural network computations. It is written using the CUDA API.

Go to official cudnn website and fill out the form for downloading the cuDNN library. After you get to the download link ( sample shown below ), you should download the “cuDNN v6.0 Library for Linux” from the options.

https://developer.nvidia.com/rdp/cudnn-download

Now, go to the folder where you have downloaded the “.tgz” file and from the command line execute the following.

1

2

3

tar xvf cudnn-8.0-linux-x64-v6.0.tgz

sudo cp -P cuda/lib64/* /usr/local/cuda/lib64/

sudo cp cuda/include/* /usr/local/cuda/include/

Next, update the paths for CUDA library and executables.

1

2

3

4

echo 'export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"' >> ~/.bashrc

echo 'export CUDA_HOME=/usr/local/cuda' >> ~/.bashrc

echo 'export PATH="/usr/local/cuda/bin:$PATH"' >> ~/.bashrc

source ~/.bashrc

This should get everything sorted out with respect to CUDA and cuDNN

Step 4 : Install requirements for DL Frameworks

Install dependencies of Deep Learning Frameworks

1

2

sudo apt-get update

sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler libopencv-dev

NOTE : If you get a warning saying
/usr/lib/nvidia-375/libEGL.so.1 not a symbolic link
Then execute the following commands.

1

2

3

4

sudo mv /usr/lib/nvidia-375/libEGL.so.1 /usr/lib/nvidia-375/libEGL.so.1.org

sudo mv /usr/lib32/nvidia-375/libEGL.so.1 /usr/lib32/nvidia-375/libEGL.so.1.org

sudo ln -s /usr/lib/nvidia-375/libEGL.so.375.82 /usr/lib/nvidia-375/libEGL.so.1

sudo ln -s /usr/lib32/nvidia-375/libEGL.so.375.82 /usr/lib32/nvidia-375/libEGL.so.1

Next, we install python 2 and 3 along with other important packages like boost, lmdb, glog, blas etc.

1

2

3

4

5

6

sudo apt-get install -y --no-install-recommends libboost-all-dev doxygen

sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev libblas-dev

sudo apt-get install -y libatlas-base-dev libopenblas-dev libgphoto2-dev libeigen3-dev libhdf5-dev

 

sudo apt-get install -y python-dev python-pip python-nose python-numpy python-scipy

sudo apt-get install -y python3-dev python3-pip python3-nose python3-numpy python3-scipy

Step 5 : Enable Virtual Environments

Most of us work on different projects and like to keep the settings for these projects separate too. This can be done using Virtual environments in Python. In a virtual environment, you can install any python library without affecting the global installation or other virtual environments. This way, even if you damage the libraries in one virtual environment, your rest of the projects remain safe. It is highly recommended to use virtual environments.

Install the virtual environment wrapper which enables us to create and work on virtual environments in python.

1

2

3

4

5

sudo pip2 install virtualenv virtualenvwrapper

sudo pip3 install virtualenv virtualenvwrapper

echo "# Virtual Environment Wrapper"  >> ~/.bashrc

echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

source ~/.bashrc

Step 6 : Install Deep Learning frameworks

Now, we install Tensorflow, Keras, PyTorch, dlib along with other standard Python ML libraries like numpy, scipy, sklearn etc.
We will create virtual environments and install all the deep learning frameworks inside them. We create separate environments for Python 2 and 3.

NOTE that PyTorch is in beta at the time of writing this article. So, the download link for PyTorch can change in future. You can visit this link to get the correct download link according to your desktop configuration.

For Python 2

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

# create a virtual environment for python 2

mkvirtualenv virtual-py2 -p python2

# Activate the virtual environment

workon virtual-py2

 

pip install numpy scipy matplotlib scikit-image scikit-learn ipython protobuf jupyter

 

# If you do not have CUDA installed

pip install tensorflow

# If you have CUDA installed

pip install tensorflow-gpu

 

pip install Theano

pip install keras

pip install http://download.pytorch.org/whl/cu80/torch-0.2.0.post3-cp27-cp27mu-manylinux1_x86_64.whl

pip install dlib

 

deactivate

For Python 3

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

# create a virtual environment for python 3

mkvirtualenv virtual-py3 -p python3

# Activate the virtual environment

workon virtual-py3

 

pip install numpy scipy matplotlib scikit-image scikit-learn ipython protobuf jupyter

 

# If you do not have CUDA installed

pip install tensorflow

# If you have CUDA installed

pip install tensorflow-gpu

 

pip install Theano

pip install keras

pip install http://download.pytorch.org/whl/cu80/torch-0.2.0.post3-cp35-cp35m-manylinux1_x86_64.whl

pip install dlib

 

deactivate

Check Installation of Frameworks

1

2

3

4

5

6

7

8

9

10

11

12

13

14

workon virtual-py2

python

import numpy

numpy.__version__

import theano

theano.__version__

import tensorflow

tensorflow.__version__

import keras

keras.__version__

import torch

torch.__version__

import cv2

cv2.__version__

You should get an output similar to the figure shown below
output of import statements

If you want to install OpenCV 3.3, follow along

Step 7 : Install OpenCV 3.3

First we will install the dependencies

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

sudo apt-get remove x264 libx264-dev

sudo apt-get install -y checkinstall yasm

sudo apt-get install -y libjpeg8-dev libjasper-dev libpng12-dev

 

# If you are using Ubuntu 14.04

sudo apt-get install -y libtiff4-dev

 

# If you are using Ubuntu 16.04

sudo apt-get install -y libtiff5-dev

sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev

 

sudo apt-get install -y libxine2-dev libv4l-dev

sudo apt-get install -y libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev

sudo apt-get install -y libqt4-dev libgtk2.0-dev libtbb-dev

sudo apt-get install -y libfaac-dev libmp3lame-dev libtheora-dev

sudo apt-get install -y libvorbis-dev libxvidcore-dev

sudo apt-get install -y libopencore-amrnb-dev libopencore-amrwb-dev

sudo apt-get install -y x264 v4l-utils

Download OpenCV and OpenCV-contrib

1

2

3

4

git clone https://github.com/opencv/opencv.git

cd opencv

git checkout 3.3.0

cd ..

1

2

3

4

git clone https://github.com/opencv/opencv_contrib.git

cd opencv_contrib

git checkout 3.3.0

cd ..

Configure and generate the MakeFile

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

cd opencv

mkdir build

cd build

#Remove the line WITH_CUDA=ON if you dont have CUDA in your system

 

cmake -D CMAKE_BUILD_TYPE=RELEASE \

      -D CMAKE_INSTALL_PREFIX=/usr/local \

      -D INSTALL_C_EXAMPLES=ON \

      -D INSTALL_PYTHON_EXAMPLES=ON \

      -D WITH_TBB=ON \

      -D WITH_V4L=ON \

      -D WITH_QT=ON \

      -D WITH_OPENGL=ON \

      -D WITH_CUDA=ON \

      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \

      -D BUILD_EXAMPLES=ON ..

Compile and Install

NOTE : The make operation takes quite a long time, almost an hour using 12 cores on an i7 processor. Also, it might get stuck for long at some places, but don’t worry unless it is stuck for more than an hour.

1

2

3

4

make -j4

sudo make install

sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'

sudo ldconfig

Link OpenCV to your virtual environments

1

2

# Link the opencv to python virtual environment

find /usr/local/lib/ -type f -name "cv2*.so"

It should give an output similar to the one shown below

/usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so
/usr/local/lib/python2.7/dist-packages/cv2.so

These are the locations where OpenCV’s Python runtime library file ( cv2.so ) is located. We need to create symlinks to these files from our virtual environment in order to use OpenCV inside them without reinstalling OpenCV.

Note the exact path of the cv2.so file. In my system, it is located in dist-packages. But in most systems, it is located in site-packages directory.

The creation of symlinks is done as follows

1

2

3

4

5

6

7

############ For Python 2 ############

cd ~/.virtualenvs/virtual-py2/lib/python2.7/site-packages

ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so

   

############ For Python 3 ############

cd ~/.virtualenvs/virtual-py3/lib/python3.5/site-packages

ln -s /usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so cv2.so

Check OpenCV Installation

1

2

3

4

workon virtual-py2

python

import cv2

cv2.__version__

You should get an output similar to the figure given below
output of import cv2 command

What next?

Check out our next posts on Keras Basics and Feedforward Neural Networks Basics. More posts on Deep Learning to follow. Stay Tuned!

Subscribe & Download Code

If you liked this article and would like to download code (C++ and Python) and example images used all the posts of this blog, please subscribe to our newsletter. You will also receive a free Computer Vision Resource Guide. In our newsletter, we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.

Subscribe Now

References

Here is a list of other resources you may find useful.

https://github.com/floydhub/dl-setup

http://www.born2data.com/2017/deeplearning_install-part1.html

/install-opencv3-on-ubuntu/

http://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值