1

I'm trying to make OpenCV use GPU on google Colab but I can' find any tutorial what I fond is this tutorial

but I get stuck at step3 cuz this tutorial is not for colab

4
  • Check this out: medium.com/deep-learning-turkey/… Commented Aug 10, 2020 at 17:25
  • I installed OpenCV like the tutorial but it didn't use the GPU Commented Aug 10, 2020 at 17:36
  • @Axiumin_ that tutorial, great as it is, is for using the GPU with colab, not what the question requires Commented May 27, 2021 at 3:00
  • @all_alone I haven't tried it but it seems that in that tutorial, the standard version of opencv is installed, therefore CUDA is not installed. The tutorial does not provide a solution Commented May 27, 2021 at 3:01

1 Answer 1

1

the default cv2 version installed on colab is 4.1.0 (very old !) and indeed, it has no CUDA support builtin. however, this is a google machine, it has all the tools, nooks & crannies to build your own ! latest cv2 ! with cuda ! (might take a good two hrs, though)

## 1st: switch to a gpu enabled runtime (from menu)

!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 \
       -DWITH_CUBLAS=ON \
       -DWITH_CUDNN=ON \
       -DOPENCV_DNN_CUDA=ON \
       /content/opencv

!make -j8

## the new cv2.so is now in /content/build/lib/python3/
## you are not allowed a normal install on colab, (not root)
## but you can copy it into your work folder and restart the runtime, so it forgets about the old version !

import cv2
cv2.__version__

!ls -l

## save for later use:
!cp  /content/build/lib/python3/cv2.cpython-37m-x86_64-linux-gnu.so   "/content/drive/My Drive/cv2_cuda"

## next time, load it into your work folder:
## dont forget to restart the runtime, so it forgets about the old version !
!cp "/content/drive/My Drive/cv2_cuda/cv2.cpython-37m-x86_64-linux-gnu.so" .
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.