Alight, so you have the NVIDIA CUDA Toolkit and cuDNN library installed 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 — 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, 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 to familiarize yourself with them. The RealPython.com blog also has an excellent article on Python virtual environments 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 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 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:
However, the /mnt
volume has 64GB of space, which is more than enough for our compile:
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_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, 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:
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:
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/
- About site-packages: https://python4astronomers.github.io/installation/packages.html
- Symbolic links: https://kb.iu.edu/d/abbe
What's next? We recommend PyImageSearch University.
86 total classes • 115+ hours of on-demand code walkthrough videos • Last updated: October 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:
- ✓ 86 courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 86 Certificates of Completion
- ✓ 115+ 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 540+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
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!
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.