Install Cuda 8 Ubuntu

Installing Nvidia CUDA 8.0 on Ubuntu 16.04 for Linux GPU Computing (New Troubleshooting Guide) Published on April 1. Install CUDA 8.0. For CUDA 8.0 in step 1 click 'Legacy Releases' and select 'CUDA Toolkit 8.0 GA 2'. Then Linux, x86_64, Ubuntu, 16.04, runfile (local). I did not accept the packaged driver and instead used my previously installed 384.130 drivers for my GPU.

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.

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:

  1. Experience with Linux
  2. Attention to detail
  3. 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:

  1. SSH into your GPU instance (with X server off/disabled).
  2. 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:

  1. Turn off your machine.
  2. Unplug your monitor.
  3. Reboot.
  4. SSH into your machine from a separate system.
  5. 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:

  1. Close all running applications.
  2. Press ctrl+alt+F2 .
  3. Login with your username and password.
  4. Stop X server by executing sudo service lightdm stop .
  5. 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:

Setting up Ubuntu 16.04 + CUDA + GPU for deep learning with Python
2
$sudo apt-getupgrade

Then, let’s install some necessary development tools, image/video I/O, GUI operations and various other packages:

Setting up Ubuntu 16.04 + CUDA + GPU for deep learning with Python
2
$sudo apt-getinstall linux-image-generic linux-image-extra-virtual
$sudo apt-getinstall linux-source linux-headers-generic

That’s it for Step #1, so let’s continue on.

Step #2: Install CUDA Toolkit

The CUDA Toolkit installation step requires attention to detail for it to go smoothly.

First disable the Nouveau kernel driver by creating a new file:

Setting up Ubuntu 16.04 + CUDA + GPU for deep learning with Python
2
4
6
8
$mkdirbuild
$cmake-DCMAKE_BUILD_TYPE=RELEASE
-DWITH_CUDA=OFF
-DOPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules

Note: I turned CUDA off as it can lead to compile errors on some machines. The CUDA optimizations would internally be used for C++ functions so it doesn’t make much of a difference with Python + OpenCV. Again, the primary use of CUDA in this blog post is to optimize our deep learning libraries, not OpenCV itself.

For CMake, it is important that your flags match mine for compatibility. Also, make sure that your opencv_contrib version is the exact same as the opencv version you downloaded (in this case version 3.3.0 ).

Before we move on to the actual compilation step, make sure you examine the output of CMake.

Start by scrolling to the section titled Python3 .

Make sure that your Python 3 section looks like the figure below:

Figure 6: Verifying that CMake has properly set up the compile to use the correct Python 3 Interpreter and version of NumPy. Both Python 3 and NumPy should be pulled from the dl4cv virtual environment.

Ensure that the Interpreter points to our python3.5 binary located in the dl4cv virtual environment while numpy points to our NumPy install.

In either case if you do not see the dl4cv virtual environment in these variables’ paths, then it’s almost certainly because you are NOT in the dl4cv virtual environment prior to running CMake!

If this is the case, access the dl4cv virtual environment using workon dl4cv and re-run the command outlined above.

Compiling OpenCV

Now we are now ready to compile OpenCV :

Setting up Ubuntu 16.04 + CUDA + GPU for deep learning with Python

Note:If you run into compilation errors, you may run the command makeclean and then just compile without the flag: make . You can adjust the number of processor cores you use the compile OpenCV via the -j switch (in the example above, I’m compiling OpenCV with four cores).

From there, all you need to do is to install OpenCV 3.3:

Setting up Ubuntu 16.04 + CUDA + GPU for deep learning with Python
2
$sudo ldconfig

You can also delete your opencv and opencv_contrib directories to free up space on your system; however, I highly recommend that you wait until the end of this tutorial and ensured OpenCV has been correctly installed before you delete these files (otherwise you’ll have to download them again).

Symbolic linking OpenCV to your virtual environment

To sym-link our OpenCV bindings into the dl4cv virtual environment, issue the following commands

Setting up Ubuntu 16.04 + CUDA + GPU for deep learning with Python
2
$pip install imutils h5py requests progressbar2

Next, install Tensorflow (GPU version):

Setting up Ubuntu 16.04 + CUDA + GPU for deep learning with Python

You can verify that TensorFlow has been installed by importing it in your Python shell:

Setting up Ubuntu 16.04 + CUDA + GPU for deep learning with Python
2
>>>importtensorflow

Now we’re ready to install Keras:

Setting up Ubuntu 16.04 + CUDA + GPU for deep learning with Python

Install Cuda 8 On Ubuntu 16.04

Again, you can verify Keras has been installed via your Python shell:

Setting up Ubuntu 16.04 + CUDA + GPU for deep learning with Python
2
$git clone--recursive https://github.com/apache/incubator-mxnet.gitmxnet--branch0.11.0

We can them compile mxnet:

Setting up Ubuntu 16.04 + CUDA + GPU for deep learning with Python