Welcome back! This is the fourth post in the deep learning development environment configuration series which accompany my new book, Deep Learning for Computer Vision with Python.
The.tgz installation approach doesn’t allow verification by running code samples (no way to install the code samples.deb after.tgz installation). The following steps are pretty much the same as the installation guide using.deb files (strange that the cuDNN guide is better than the CUDA one). Install NVIDIA CUDA Toolkit First, you will need to download the latest version of the CUDA Toolkit to your system. The CUDA Toolkit contains the CUDA driver and tools needed to create, build and run a CUDA application.
Today, we will configure Ubuntu + NVIDIA GPU + CUDA with everything you need to be successful when training your own deep learning networks on your GPU.
Links to related tutorials can be found here:
- Configuring Ubuntu for deep learning with Python (for a CPU only environment)
- Setting up Ubuntu 16.04 + CUDA + GPU for deep learning with Python (this post)
- Configuring macOS for deep learning with Python (releasing on Friday)
If you have an NVIDIA CUDA compatible GPU, you can use this tutorial to configure your deep learning development to train and execute neural networks on your optimized GPU hardware.
Let’s go ahead and get started!
Setting up Ubuntu 16.04 + CUDA + GPU for deep learning with Python
If you’ve reached this point, you are likely serious about deep learning and want to train your neural networks with a GPU.
Graphics Processing Units are great at deep learning for their parallel processing architecture — in fact, these days there are many GPUs built specifically for deep learning — they are put to use outside the domain of computer gaming.
NVIDIA is the market leader in deep learning hardware, and quite frankly the primary option I recommend if you are getting in this space. It is worth getting familiar with their lineup of products (hardware and software) so you know what you’re paying for if you’re using an instance in the cloud or building a machine yourself. Be sure to check out this developer page.
It is common to share high-end GPU machines at universities and companies. Alternatively, you may build one, buy one (as I did), or rent one in the cloud (as I still do today).
If you are just doing a couple of experiments then using a cloud service provider such as Amazon, Google, or FloydHub for a time-based usage charge is the way to go.
Longer term if you are working on deep learning experiments daily, then it would be wise to have one on hand for cost savings purposes (assuming you’re willing to keep the hardware and software updated regularly).
Note: For those utilizing AWS’s EC2, I recommend you select the p2.xlarge, p2.8xlarge, or p2.16xlarge machines for compatibility with these instructions (depending on your use case scenario and budget). The older instances, g2.2xlarge and g2.8xlarge are not compatible with the version of CUDA and cuDNN in this tutorial. I also recommend that you have about 32GB of space on your OS drive/partition. 16GB didn’t cut it for me on my EC2 instance.
It is important to point out that you don’t need access to an expensive GPU machine to get started with Deep Learning. Most modern laptop CPUs will do just fine with the small experiments presented in the early chapters in my book. As I say, “fundamentals before funds” — meaning, get acclimated with modern deep learning fundamentals and concepts before you bite off more than you can chew with expensive hardware and cloud bills. My book will allow you to do just that.
How hard is it to configure Ubuntu with GPU support for deep learning?
You’ll soon find out below that configuring a GPU machine isn’t a cakewalk. In fact, there are quite a few steps and potential for things to go sour. That’s why I have built a custom Amazon Machine Instance (AMI) pre-configured and pre-installed for the community to accompany my book.
I detailed how to get it loaded into your AWS account and how to boot it up in this previous post.
Using the AMI is by far the fastest way to get started with deep learning on a GPU. Even if you do have a GPU, it’s worth experimenting in the Amazon EC2 cloud so you can tear down an instance (if you make a mistake) and then immediately boot up a new, fresh one.
Configuring an environment on your own is directly related to your:
- Experience with Linux
- Attention to detail
- Patience.
First, you must be very comfortable with the command line.
Many of the steps below have commands that you can simply copy and paste into your terminal; however it is important that you read the output, note any errors, try to resolve them prior to moving on to the next step.
You must pay particular attention to the order of the instructions in this tutorial, and furthermore pay attention to the commands themselves.
I actually do recommend copying and pasting to make sure you don’t mess up a command (in one case below backticks versus quotes could get you stuck).
If you’re up for the challenge, then I’ll be right there with you getting your environment ready. In fact, I encourage you to leave comments so that the PyImageSearch community can offer you assistance. Before you leave a comment be sure to review the post and comments to make sure you didn’t leave a step out.
Without further ado, let’s get our hands dirty and walk through the configuration steps.
Step #0: Turn off X server/X window system
Before we get started I need to point out an important prerequisite. You need to perform one of the following prior to following the instructions below:
- SSH into your GPU instance (with X server off/disabled).
- Work directly on your GPU machine without your X server running (the X server, also known as X11, is your graphical user interface on the desktop). I suggest you try one of the methods outlined on this thread.
There are a few methods to accomplish this, some easy and others a bit more involved.
The first method is a bit of a hack, but it works:
- Turn off your machine.
- Unplug your monitor.
- Reboot.
- SSH into your machine from a separate system.
- Perform the install instructions.
This approach works great and is by far the easiest method. By unplugging your monitor X server will not automatically start. From there you can SSH into your machine from a separate computer and follow the instructions outlined in this post.
The second method assumes you have already booted the machine you want to configure for deep learning:
- Close all running applications.
- Press ctrl+alt+F2 .
- Login with your username and password.
- Stop X server by executing sudo service lightdm stop .
- Perform the install instructions.
Please note that you’ll need a separate computer next to you to read the instructions or execute the commands. Alternatively, you could use a text-based web browser.
Step #1: Install Ubuntu system dependencies
Now that we’re ready, let’s get our Ubuntu OS up to date:
2 | $sudo apt-getupgrade |
Then, let’s install some necessary development tools, image/video I/O, GUI operations and various other packages:
Make sure you are in the dl4cv virtual environment before firing up Python. You can accomplish this by running workon dl4cv .
When you print the OpenCV version in your Python shell it should match the version of OpenCV that you installed (in our case OpenCV 3.3.0 ).
When your compilation is 100% complete you should see output that looks similar to the following:
Figure 7: OpenCV 3.3.0 compilation is complete.
That’s it — assuming you didn’t have an import error, then you’re ready to go on to Step #6 where we will install Keras.
Step #6: Install Keras
For this step, make sure that you are in the dl4cv environment by issuing the workon dl4cv command.
From there we can install some required computer vision, image processing, and machine learning libraries:
2 4 6 | 'image_data_format':'channels_last', 'epsilon':1e-07, } |
Ensure that image_data_format is set to channels_last and backend is tensorflow .
Congratulations! You are now ready to begin your Deep learning for Computer Vision with Python journey (Starter Bundle and Practitioner Bundle readers can safely skipStep #7).
Step #7 Install mxnet (ImageNet Bundle only)
This step is only required for readers who purchased a copy of the ImageNet Bundle of Deep Learning for Computer Vision with Python. You may also choose to use these instructions if you want to configure mxnet on your system.
Either way, let’s first clone the mxnet repository and checkout branch 0.11.0 :
Ubuntu Install Cuda 8.0
THIS PAGE ISN'T FINALIZED !
CUDA (Compute Unified Device Architecture) is a parallel computing architecture developed by Nvidia for graphics processing.
This document provides instructions to install/remove Cuda 4.2 on Ubuntu 12.04.
Prerequisites
Install Cuda Ubuntu 14.04
It is necessary to have a Nvidia proprietary driver up to 295.xx (which can be installed automatically or manually). For Ubuntu versions under 12.04, you will have to install Nvidia driver manually.
Be sure your GPU is compatible with Cuda. Refer to this page. Then, install required packages:
Downloads
You will have to download two programs: 'CUDA Toolkit' and 'GPU Computing SDK'.
Let's download and save them on the Desktop.
32bits:
64bits:
Installation of CUDA Toolkit
Go to 'Desktop', add execution permissions of the cudatoolkit downloaded file, and execute it with root permissions:
When it will ask you:
type /opt.
Installation of GPU Computing SDK
As before, go to 'Desktop', add execution permissions of the gpucomputingsdk downloaded file, and execute it without root permissions:
When it will ask you:
press [enter] (to use default path), and type /opt when it will ask you:
Preparation
Update variables, and this on every boot:
Compilation of SDK
Install Cuda Ubuntu
Need to be tested !
We finally compile:
NOTE:In case you are using the latest (post-release updates; version current-updates) instead of the recommended (version current) Nvidia drivers, then change the LINKFLAGS to read:
You can check for which version you are using by running the 'Additional Drivers' program (System Settings->Hardware->Additional Drivers) and check which version is toggled ON.
Test
Now, let's test if Cuda is working great. Type:
As you can see on the following picture, it works !
If you are using optimus with bumblebee, run the above with optirun as below:
Will come soon...
Cleanup
Now Cuda is installed, the installers files are useless, you can remove them:
If you want to totally remove Cuda, juste delete the /opt/cuda and ~/NVIDIA_GPU_Computing_SDK folders:
and remove the export PATH=$PATH:/opt/cuda/bin and export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/cuda/lib:/opt/cuda/lib64 lines of the ~/.bash_profile file.