Title: Compiling OpenCV with CUDA support - PyImageSearch

URL Source: https://pyimagesearch.com/2016/07/11/compiling-opencv-with-cuda-support/

Published Time: 2016-07-11T14:00:42+00:00

Markdown Content:
![Image 1: install_opencv_cuda_logos](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20274'%3E%3C/svg%3E)

Alight, so you have the [NVIDIA CUDA Toolkit and cuDNN library installed](https://pyimagesearch.com/2016/07/04/how-to-install-cuda-toolkit-and-cudnn-for-deep-learning/) on your GPU-enabled system.

What next?

**Let’s get OpenCV installed with CUDA support as well.**

While OpenCV _itself_ doesn’t play a critical role in deep learning, it _is_ used by _other_ deep learning libraries such as Caffe, specifically in “utility” programs (such as building a dataset of images). Simply put, having OpenCV installed makes it easier to write code to facilitate the procedure of pre-processing images _prior_ to feeding them into deep neural networks.

_Because of this, we should install OpenCV into the same environment as our deep learning libraries, to at the very least, make our lives easier._

Furthermore, in a GPU-enabled CUDA environment, there are a number of compile-time optimizations we can make to OpenCV, allowing it to take advantage of the GPU for faster computation (but mainly for C++ applications, not so much for Python, at least at the present time).

I’ll be making the assumption that you’ll be installing OpenCV into the _same environment_ as [last week’s blog post](https://pyimagesearch.com/2016/07/04/how-to-install-cuda-toolkit-and-cudnn-for-deep-learning/) — in this case, I’ll be continuing my example of using the Ubuntu 14.04 g2.2xlarge instance on Amazon EC2.

Truth be told, I’ve already covered installing OpenCV on Ubuntu in [_many_ previous blog posts](https://pyimagesearch.com/opencv-tutorials-resources-guides/), but I’ll explain the process here as well. Overall, the instructions are _near identical, but with **a few important changes**_ inside the `cmake` command, allowing us to compile OpenCV with CUDA support.

By the time you finish reading this blog post, you’ll have OpenCV with CUDA support compiled and installed in your deep learning development environment.

Installing OpenCV with CUDA support
-----------------------------------

Before we can compile OpenCV with CUDA support, we first need to install some prerequisites:

$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
$ sudo apt-get install libgtk2.0-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libatlas-base-dev gfortran
$ sudo apt-get install libhdf5-serial-dev
$ sudo apt-get install python2.7-dev

If you’re a follower of the PyImageSearch blog, then you’ll also know that I’m a _big fan_ of using `pip` , `virtualenv` , and `virtualenvwrapper` to create sequestered, independent Python virtual environments for each of our projects. You can install the virtual environment packages using the commands listed below (or you can skip this step if you already have Python virtual environments setup on your machine):

$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py
$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf get-pip.py ~/.cache/pip

If this is your first time using Python virtual environments, I would suggest reading the _first half_ of [this blog post](https://pyimagesearch.com/2016/05/02/accessing-rpi-gpio-and-gpio-zero-with-opencv-python/) to familiarize yourself with them. The RealPython.com blog also has an [excellent article on Python virtual environments](https://realpython.com/blog/python/python-virtual-environments-a-primer/) for the uninitiated.

Next, let’s use update our `~/.bashrc` file. Open this file using your favorite command line text editor (such as `nano` , `vi` , or `emacs` ):

$ nano ~/.bashrc

Then, scroll down to the bottom of the file, append the following lines, and save + exit the editor:

\# virtualenv and virtualenvwrapper
export WORKON\_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

At this point, we can create our `cv` virtual environment:

$ source ~/.bashrc
$ mkvirtualenv cv
$ pip install numpy

_**Note:** Again, you’ll want to read the first half of [this blog post](https://pyimagesearch.com/2016/05/02/accessing-rpi-gpio-and-gpio-zero-with-opencv-python/) to better understand Python virtual environments if this is your first time using them. I also explain them more thoroughly and how to properly use them in [other OpenCV installation guides](https://pyimagesearch.com/opencv-tutorials-resources-guides/) on this website._

Now, let’s download and unpack OpenCV. If you’re using the default Amazon EC2 g2.2xlarge instance, then _**I highly suggest**_ that you download the OpenCV sources and do your compiling on `/mnt` .

The default g2.2xlarge instance has only ~8GB of space, which once you factor in the system files, NVIDIA drivers, etc., is not enough room to compile OpenCV from source:

![Image 2: Figure 1: The default disk size for the g2.2xlarge instance is only 8GB, which doesn't leave enough space to compile OpenCV from source.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20351'%3E%3C/svg%3E)

**Figure 1:** The default disk size for the g2.2xlarge instance is only 8GB, which doesn’t leave enough space to compile OpenCV from source.

_However,_ the `/mnt` volume has 64GB of space, which is _more than enough_ for our compile:

![Image 3: Figure 2: However, if we use the '/mnt' volume instead, we have 64GB -- far more than what is required to compile OpenCV.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20351'%3E%3C/svg%3E)

**Figure 2:** However, if we use the ‘/mnt’ volume instead, we have 64GB — far more than what is required to compile OpenCV.

If you are indeed on an Amazon EC2 instance, be sure to change directory to `/mnt` and create a directory specifically for your OpenCV compile prior to downloading the source:

$ cd /mnt
$ sudo mkdir opencv\_compile
$ sudo chown -R ubuntu opencv\_compile
$ cd opencv\_compile

The above command will create a new directory named `opencv_compile` in the `/mnt` volume, followed by giving the `ubuntu` user permission to modify it at their will.

_**Note:** The `/mnt` volume is what Amazon calls “ephemeral storage”. Any data put on this volume will be lost when your system is stopped/rebooted. You don’t want to use `/mnt` to store long-term data, but it’s perfectly fine to use `/mnt` to compile OpenCV. Once OpenCV is compiled, it will be installed to the system drive — your OpenCV installation_ will not _disappear between reboots._

For this tutorial, I’ll be using OpenCV 3.1. But you could also use OpenCV 2.4.X or OpenCV 3.0. Use the following commands to download the source:

$ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
$ wget -O opencv\_contrib.zip https://github.com/Itseez/opencv\_contrib/archive/3.1.0.zip
$ unzip opencv.zip
$ unzip opencv\_contrib.zip

In case the URLs of the `.zip` archives are cutoff, I’ve included them below:

*   [https://github.com/Itseez/opencv/archive/3.1.0.zip](https://github.com/Itseez/opencv/archive/3.1.0.zip)
*   [https://github.com/Itseez/opencv\_contrib/archive/3.1.0.zip](https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip)

We are now ready to use `cmake` to configure our build. Take special care when running this command, as I’m introducing some configuration variables you may not be familiar with:

$ cd opencv-3.1.0
$ mkdir build
$ cd build
$ cmake -D CMAKE\_BUILD\_TYPE=RELEASE \\
    -D CMAKE\_INSTALL\_PREFIX=/usr/local \\
    -D WITH\_CUDA=ON \\
    -D ENABLE\_FAST\_MATH=1 \\
    -D CUDA\_FAST\_MATH=1 \\
    -D WITH\_CUBLAS=1 \\
    -D INSTALL\_PYTHON\_EXAMPLES=ON \\
    -D OPENCV\_EXTRA\_MODULES\_PATH=../../opencv\_contrib-3.1.0/modules \\
    -D BUILD\_EXAMPLES=ON ..

To start, take note of the `WITH_CUDA=ON` flag. Technically, this flag will be set to `ON` by default since CMake is smart enough to detect that the CUDA Toolkit is installed. But, just in case, we’ll manually set the variable to `WITH_CUDA=ON` to ensure CUDA support is compiled.

From there, we add in a few more optimizations, mainly around using [cuBLAS](http://docs.nvidia.com/cuda/cublas/#axzz4ASmGLst1), an implementation of the BLAS (Basic Linear Algebra Subprograms) library in the CUDA runtime.

We also indicate that we want to utilize the “fast math” optimizations, a series of _extremely fast_ mathematical routines that are optimized for speed (they are written in Assembly) — and essentially perform little-to-no error checking. Again, the FastMath libraries are geared towards pure speed and nothing else.

After running `cmake` , take a look at the _“NVIDIA CUDA”_ section — it should look similar to mine, which I have included below:

![Image 4: Figure 3: Examining the output of CMake to ensure OpenCV will be compiled with CUDA support.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20379'%3E%3C/svg%3E)

**Figure 3:** Examining the output of CMake to ensure OpenCV will be compiled with CUDA support.

Notice how CUDA support is going to be compiled using both cuBLAS and “fast math” optimizations.

Provided that your own CMake command exited without error, you can now compile and install OpenCV:

$ make -j8
$ sudo make install
$ sudo ldconfig

If all goes well, the `make` command should run successfully:

![Image 5: Figure 4: OpenCV with CUDA support has successfully compiled.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20402'%3E%3C/svg%3E)

Figure 4: OpenCV with CUDA support has successfully compiled.

Again, assuming your compile finished without error, OpenCV should now be installed in `/usr/local/lib/python2.7/site-packages` . You can verify this using the `ls` command:

$ ls -l /usr/local/lib/python2.7/site-packages
total 2092
-rw-r--r-- 1 root staff 2138812 Jun  2 14:11 cv2.so

_**Note:** You’ll want to find and make note of where your `cv2.so` file is on your system! Whenever we create a virtual environment (which we’ll be doing lots of to explore various deep learning libraries), you’ll want to sym-link the `cv2.so` file into the `site-packages` directory of your Python virtual environment so you have access to OpenCV._

The last step is to sym-link the `cv2.so` file (our Python bindings) into the `cv` virtual environment:

$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so

To verify our installation, open up a new terminal, access the `cv` virtual environment using the `workon` command, fire up a Python shell, and then import OpenCV:

$ cd ~
$ workon cv
$ python
>>> import cv2
>>> cv2.\_\_version\_\_
'3.1.0'
>>>

Finally, now that OpenCV is installed, let’s perform a bit of cleanup and remove the source files used for installation:

$ cd /mnt
$ sudo rm -rf opencv\_compile

Again, I can’t stress this point enough — you _**need**_ to get comfortable working with Python virtual environments, the `site-packages`  directory, and how to use symbolic links. I recommend the following tutorials to help understand each of them:

*   **Python Virtual Environments:** [https://realpython.com/blog/python/python-virtual-environments-a-primer/](https://realpython.com/blog/python/python-virtual-environments-a-primer/)
*   **About site-packages:** [https://python4astronomers.github.io/installation/packages.html](https://python4astronomers.github.io/installation/packages.html)
*   **Symbolic links:** [https://kb.iu.edu/d/abbe](https://kb.iu.edu/d/abbe)

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

Summary
-------

In today’s blog post, I detailed how to install OpenCV into our deep learning environment with CUDA support. While OpenCV itself isn’t _directly_ used for deep learning, other deep learning libraries (for example, Caffe) _indirectly_ use OpenCV.

Furthermore, by installing OpenCV with CUDA support, we can take advantage of the GPU for further optimized operations (at least from within C++ applications — there isn’t much support for Python + OpenCV + GPU, yet).

Next week, I’ll detail how to install the Keras Python package for deep learning and Convolutional Neural Networks — from there, the real fun will start!

![Image 6](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF

Enter your email address below to **join the PyImageSearch Newsletter** and **download my FREE 17-page Resource Guide PDF** on Computer Vision, OpenCV, and Deep Learning.