How to use OpenCV with GPU on Colab?

Original article was published by C Kuan on Deep Learning on Medium

How to use OpenCV with GPU on Colab?

Run OpenCV’s “dnn” on Google Colab using its NVIDIA GPU

Photo by Nana Dua on Unsplash

OpenCV’s ‘Deep Neural Network’ (dnn) module is a convenient tool for computer vision, it is very easy to apply some techniques such as Yolo and OpenPose. However, the major drawback of OpenCV was the lack of GPU support, resulting in slow inference. Luckily since OpenCV 4.2, NVIDIA GPU/CUDA is supported.

It still required some works to use GPU, you can check Pyimagesearch’s article here, they demonstrate how to set up a Ubuntu machine.

If you do not have a machine with GPU like me, you can consider using Google Colab, which is a free service with powerful NVIDIA GPU. It is also a lot easier to set up, most of the requirements are already satisfied. In this article, I will share how I set up the Colab environment for OpenCV’s dnn with GPU in just a few lines of code. You can also check here, I made slight changes based on the answer.

The code to assign the dnnto GPU is simple:

import cv2
net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)

However, if you run this cell directly on Colab, you will see this error:

So we need to do something.

If you check the version of the pre-installed OpenCV on Colab, you will see this:

We need to install cv2 ourselves.

First, run this cell:

%cd /content!git clone!git clone!mkdir /content/build%cd /content/build!cmake -DOPENCV_EXTRA_MODULES_PATH=/content/opencv_contrib/modules  -DBUILD_SHARED_LIBS=OFF  -DBUILD_TESTS=OFF  -DBUILD_PERF_TESTS=OFF -DBUILD_EXAMPLES=OFF -DWITH_OPENEXR=OFF -DWITH_CUDA=ON -DWITH_CUBLAS=ON -DWITH_CUDNN=ON -DOPENCV_DNN_CUDA=ON /content/opencv!make -j8 install

you will see something like this:

It is going to take a while, after completion you can check the version of OpenCV.

Let is it! Now you should be able to set the dnn to CUDA without error.


But the first step takes a lot of time, and you need to do it every time you start the notebook. This is time-consuming and not ideal.

One thing you can do here is to save the result of the first step to your Google Drive (you have to mount it ).

!mkdir  "/content/gdrive/My Drive/cv2_gpu"!cp  /content/build/lib/python3/   "/content/gdrive/My Drive/cv2_gpu"

Then copy it to your working directory next time you start the notebook.

!cp "/content/gdrive/My Drive/cv2_gpu/" .

and check the version to make sure it is done.

If you do not want to mount your Google Drive, you can upload it to the cloud somewhere and download it using !wget to your working dir.

That is it. Thanks for reading. Have fun with the GPU.