OpenCV GPU on Google Cloud

Andrey Senin
4 min readDec 26, 2020

Using a cloud virtual machine for running computationally expensive experiments is a great alternative to a personal GPU rig if you’re using it just for periodic runs. And Google Cloud offers excellent options for that. Let’s see what it takes to start a GPU-powered VM machine on Google Cloud and use it to compile OpenCV with GPU support.

Step 1. Creating a virtual machine

If this is your first virtual machine it is likely you don’t have a quota to use GPUs and need to request that. Go to “IAM & Admin > Quotas”, filter by “Service: Compute Engine API” and “GPUs” and click to “GPUs (all regions)”, “ALL QUOTAS”.

On the next image edit the new limit, provide a short comment and click “Next”:

After some time you shall receive a confirmation of the new limit.

After that you’re ready to create your first VM at “Compute Engine” > “VM instances”. For my experiment I created 4 CPU, 26 GB machine with Tesla P4 GPU. The machine runs Ubuntu 20.04 LTS with 256 GB disk:

I enabled HTTP/HTTPS traffic and turned on the display device in the settings.

When the machine is created you can click “SSH” button to proceed to the next step:

Step 2. Installing Chrome Remote Desktop

For OpenCV experiments it might be useful to have Remote Desktop/VNC enabled on the virtual machine. Chrome RDP might be a good solution if you’re using Chrome.

Before you start it is recommended to change your password by running the “passwd” command. Then we can follow the guide for the installation:

wget https://dl.google.com/linux/direct/chrome-remote-desktop_current_amd64.debsudo apt update && sudo apt install --assume-yes --fix-brokensudo dpkg --install chrome-remote-desktop_current_amd64.debsudo apt install --assume-yes --fix-brokensudo apt install lightdmsudo DEBIAN_FRONTEND=noninteractive apt install --assume-yes cinnamon-core desktop-basesudo bash -c 'echo "exec /etc/X11/Xsession /usr/bin/cinnamon-session-cinnamon2d" > /etc/chrome-remote-desktop-session'
sudo apt install --assume-yes cinnamon-desktop-environment
sudo systemctl disable lightdm.servicewget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.debsudo dpkg --install google-chrome-stable_current_amd64.debsudo apt install --assume-yes --fix-broken

If all is installed successfully navigate to https://remotedesktop.google.com/headless, follow the screen instructions and execute the command from the “Debian Linux” window in the virtual machine command line. If all is done correctly you shall see the new machine in the list of online machines at https://remotedesktop.corp.google.com/:

If you click the newly created VM you shall see the Ubuntu GUI:

Step 3. Installing GPU drivers

In order to use the GPU card for computation you’ll need to install the latest drivers and NVidia CUDA toolkit (see the guide for more details):

curl -O https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pinsudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pubsudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /"sudo apt updatesudo apt install cudasudo rebootsudo apt install nvidia-cuda-toolkit

Step 4. Building OpenCV

The final step is building OpenCV (and OpenCV-contrib): see the guide for more details.

First install the required packages for OpenCV:

sudo apt install cmake cmake-guisudo apt install build-essentialsudo apt install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-devsudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-devsudo apt install tesseract-ocr libgflags-dev libgoogle-glog-dev libprotobuf-dev libgtkglext1 libgtkglext1-dev vlc libavresample-devgit clone https://github.com/opencv/opencv.gitgit clone https://github.com/opencv/opencv_contrib.git

Then create an OpenCV build folder:

cd opencvmkdir buildcd build

And run building OpenCV:

cmake -D CMAKE_BUILD_TYPE=Release -D BUILD_EXAMPLES=ON -D BUILD_opencv_java=OFF -D BUILD_opencv_python2=ON -D ENABLE_NEON=OFF -D WITH_OPENCL=OFF -D WITH_OPENMP=OFF -D WITH_FFMPEG=ON -D WITH_GSTREAMER=OFF -D WITH_GSTREAMER_0_10=OFF -D WITH_CUDA=ON -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.0 -D WITH_GTK=ON -D WITH_VTK=OFF -D WITH_TBB=ON -D WITH_1394=OFF -D WITH_OPENEXR=OFF -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_TESTS=OFF -D WITH_OPENGL=ON -DCUDA_ARCH_BIN=6.1 -DCMAKE_C_COMPILER=/usr/bin/gcc-8 -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..make -j 4

If the compilation succeed you can go to the bin folder and run one of the examples to verify it works correctly:

cd bin./example_gpu_bgfg_segm --file=../../samples/data/vtest.avi

You should see something similar to the image below:

With the selected configuration I see about 1800 FPS on the background/foreground segmentation test with the vtest.avi video.

--

--

Andrey Senin

SWE at Google. Work on fraud detection on YouTube. Previously at Itseez, consulting in computer vision. All opinions are personal.