This post is part of the series on Deep Learning for Beginners, which consists of the following tutorials :
- Neural Networks : A 30,000 Feet View for Beginners
- Installation of Deep Learning frameworks (Tensorflow and Keras with CUDA support )
- Introduction to Keras
- Understanding Feedforward Neural Networks
- Image Classification using Feedforward Neural Networks
- Image Recognition using Convolutional Neural Network
- Understanding Activation Functions
- Understanding AutoEncoders using Tensorflow
- Image Classification using pre-trained models in Keras
- Transfer Learning using pre-trained models in Keras
- Fine-tuning pre-trained models in Keras
- More to come . . .
In this article, we will learn how to install Deep Learning Frameworks like TensorFlow 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 3, TensorFlow, Pytorch, OpenCV, Dlib along with other Python Machine Learning libraries step-by-step. Note, that if you would like to use TensorFlow with Keras support, there is no need to install Keras package separately, since from TensorFlow2.0 Keras comes as tensorflow.keras submodule.
We have tested the instructions on a system with the following configuration:
Motherboard : Gigabyte X99P – SLI
RAM : 32 GB
Graphics Card : Zotac GeForce GTX 1080 Ti with 11 GB RAM
We will be assuming 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.
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.
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.
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.
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.
You should get an output as shown below.
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.
Now, go to the folder where you have downloaded the “.tgz” file and from the command line execute the following.
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:
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:
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 the following:
/usr/lib/nvidia-375/libEGL.so.1 not a symbolic link
Then execute the below commands:
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 3 along with other important packages like boost, lmdb, glog, blas etc.
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 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.
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, 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 a separate environment for Python 3:
# 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 torch pip install dlib deactivate
Check Installation of Frameworks
workon virtual-py3 python import numpy numpy.__version__ import tensorflow tensorflow.__version__ import torch torch.__version__ import cv2 cv2.__version__
If you want to install OpenCV 3.3, follow along
Step 7 : Install OpenCV 3.3
First we will install the dependencies:
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 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
git clone https://github.com/opencv/opencv.git cd opencv git checkout 3.3.0 cd .. git clone https://github.com/opencv/opencv_contrib.git cd opencv_contrib git checkout 3.3.0 cd ..
Configure and generate the MakeFile
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.
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
# Check the opencv .so was created find /usr/local/lib/ -type f -name "cv2*.so"
It should give an output similar to the one shown below
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:
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
workon virtual-py3 python import cv2 cv2.__version__
As an output you should get a corresponding OpenCV version.
Here is a list of other resources you may find useful:
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.