Ask Your Question
0

how to make openCV use GPU on google colab

asked 2020-08-10 12:23:25 -0600

weights gravatar image

updated 2020-08-10 13:14:16 -0600

I'm trying to make OpenCV use GPU on google Colab but I can' find any good tutorial what I fond is a tutorial for Ubuntu I followed these steps

Step 1: Install NVIDIA CUDA drivers, CUDA Toolkit, and cuDNN "collab already have the drivers"

step 2: Install OpenCV and “dnn” GPU dependencies

! sudo apt-get update
! sudo apt-get upgrade
! sudo apt-get install build-essential cmake unzip pkg-config
! sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
! sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
! sudo apt-get install libv4l-dev libxvidcore-dev libx264-dev
! sudo apt-get install libgtk-3-dev
! sudo apt-get install libatlas-base-dev gfortran
! sudo apt-get install python3-dev

step 3: Download OpenCV source code

!cd ~
! wget -O opencv.zip https://github.com/opencv/opencv/archive/4.2.0.zip
! wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.2.0.zip
! unzip opencv.zip
! unzip opencv_contrib.zip
! mv opencv-4.2.0 opencv
! mv opencv_contrib-4.2.0 opencv_contrib

step 4:

! wget https://bootstrap.pypa.io/get-pip.py
! sudo python3 get-pip.py
! sudo pip install virtualenv virtualenvwrapper
! sudo rm -rf ~/get-pip.py ~/.cache/pip
$ nano ~/.bashrc   <----- problem here I can't open this file to edit it in colab

but I get stuck at step4 cuz this tutorial is not for colab and colab give me error

/bin/bash: nano: command not found
edit retag flag offensive close merge delete

Comments

please don't refer to a 3rd party tutorial, but report the steps YOU took, and the resp. errors.

berak gravatar imageberak ( 2020-08-10 12:26:43 -0600 )edit

also: "use CPU" means what, exactly ? CUDA support for cv2 ?

berak gravatar imageberak ( 2020-08-10 12:28:32 -0600 )edit

exactly CUDA support for cv2 I followed the tutorial which is not for colab and step 4 give me an error /bin/bash: nano: command not found

weights gravatar imageweights ( 2020-08-10 12:41:38 -0600 )edit

again, please edit your question, and add the steps you took.

(why would it want nano ? that's just plan bs)

berak gravatar imageberak ( 2020-08-10 12:48:07 -0600 )edit

did you try to build it on colab ? did you enable a gpu supported runtime there ?

berak gravatar imageberak ( 2020-08-10 12:54:24 -0600 )edit

I did enable GPU but when I use OpenCV’s “dnn” module with yolo it takes too long to get results that's mean that openCV doesn't use GPU

weights gravatar imageweights ( 2020-08-10 13:13:13 -0600 )edit

/archive/4.2.0.zip why the fck try with last years model ?

berak gravatar imageberak ( 2020-08-10 13:46:09 -0600 )edit

actually Idk I'm just following the steps I'm not an expert in OpenCV if you have any tutorial or better steps to make OpenCV use GPU on google colab I will be appreciated

weights gravatar imageweights ( 2020-08-10 13:56:29 -0600 )edit
1

i think that's why they use 4.2 "Led by dlib’s Davis King, and implemented by Yashas Samaga, OpenCV 4.2 now supports NVIDIA GPUs for inference using OpenCV’s dnn module, improving inference speed by up to 1549%!"

weights gravatar imageweights ( 2020-08-10 13:59:08 -0600 )edit

yea, you need at least 4.2 for CUDA support

you probably should skip the whole virtualenv crap, colab notebooks are one-off/throwaway things anyway.

(thev's no /~ home directory, no .bashrc and you can't use nano on a webserver)

berak gravatar imageberak ( 2020-08-11 02:55:56 -0600 )edit

1 answer

Sort by » oldest newest most voted
2

answered 2020-08-11 05:14:11 -0600

berak gravatar image

updated 2020-08-11 05:34:01 -0600

it's probably easier, than you think, colab already has all dependancies onboard (given you choose the python3 CUDA runtime). you also don't need to mess with virtual enviroments imho

so, here's my receipe:

%cd /content
!git clone https://github.com/opencv/opencv
!git clone https://github.com/opencv/opencv_contrib
!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 \         <-- those
       -DWITH_CUBLAS=ON \     <-- are
       -DWITH_CUDNN=ON \      <-- important !
       -DOPENCV_DNN_CUDA=ON \     <-- !!!
       /content/opencv

!make -j8 install

import cv2
cv2.__version__

(should print: 4.4.0)

but alas, your cv2 install is NOT persistant, next colab allocation will wipe it all clean, so you need to copy the so to some folder on your own drive:

!mkdir  "/content/drive/My Drive/cv2_cuda"
!cp  /content/build/lib/python3/cv2.cpython-36m-x86_64-linux-gnu.so   "/content/drive/My Drive/cv2_cuda"

then, the next time you want to use it, copy it back to your current workdir :

!cp "/content/drive/My Drive/cv2_cuda/cv2.cpython-36m-x86_64-linux-gnu.so" .

restart your python runtime (so it forgets about the prev. installed cv2) and it should pick up your local, CUDA enabled cv2.so.

it all worked like 3 weeks ago, but who knows, --- good luck ;)

edit flag offensive delete link more

Comments

thank you for the solution I think It didn't work well when I print the version it says 4.1.2 and I reformated your code and run !cmake in one line and it worked

!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

and when the run is finished there is a small error

CMake Error: The source directory "/content/build" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
make: *** No rule to mak

here is a screenshot any solution

weights gravatar imageweights ( 2020-08-11 07:14:48 -0600 )edit

and it worked

no, it didn't

you have to remove the backslashes from your cmake cmdline then (and replace with spaces)

berak gravatar imageberak ( 2020-08-11 07:22:04 -0600 )edit

it worked thank you so much but when I tried to use cv2.destroyallwindows() it shows this any solutions?

weights gravatar imageweights ( 2020-08-11 14:07:33 -0600 )edit

cv2.destroyallwindows() was working fine but after I restarted colab and start using new OpenCV 4.4.0 it shows the error

weights gravatar imageweights ( 2020-08-11 14:09:16 -0600 )edit

so I found a solution

!pip  install libgtk-3-dev and pkg-config

and then I have to rerun cmake with these options

D WITH_GTK=ON \
D WITH_OPENGL=ON

should I put these options to the cmake command and run it?

I'm sorry for asking a lot of question I'm just in the learning phase

weights gravatar imageweights ( 2020-08-11 14:20:07 -0600 )edit

no use trying ANY opencv gui on colab, and no use linking gtk or qt

(don't you get it ?, it's a webserver, where you install / run things)

berak gravatar imageberak ( 2020-08-11 14:24:43 -0600 )edit
2

I managed to solve the problem if you are interested in the results the testing speed increases by 200% with YOLO model thank you for your valuable time

weights gravatar imageweights ( 2020-08-11 17:32:27 -0600 )edit
1

Thank you for all the effort berak. It can also be used in no colab environment, that saves me time in the future :)

holger gravatar imageholger ( 2020-08-11 19:48:53 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-08-10 12:23:25 -0600

Seen: 9,425 times

Last updated: Aug 11 '20