22

I'm trying to use opencv-python with GPU on windows 10.

I installed opencv-contrib-python using pip and it's v4.4.0.42, I also have Cuda on my computer and in path.

Anyway, here is a (simple) code that I'm trying to compile:

import cvlib as cv
from cvlib.object_detection import draw_bbox

bbox, label, conf = cv.detect_common_objects(img,confidence=0.5,model='yolov3-worker',enable_gpu=True)

output_image = draw_bbox(img, bbox, label, conf)

First, here is the line that tell me that tf is ok with cuda:

2020-08-26 5:51:55.718555: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll

but when I try to use my GPU to analyse the image, here is what happen:

[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-j8nxabm_\opencv\modules\dnn\src\dnn.cpp (1429) cv::dnn::dnn4_v20200609::Net::Impl::setUpNet DNN module was not built with CUDA backend; switching to CPU

Is there a way to solve this without install opencv using cmake? It's a mess on windows...

4 Answers 4

10

The problem here is that version of opencv distributed with your system (Windows in this case) was not compiled with Cuda support. Therefore, you cannot use any cuda related function with this build.

If you want to have an opencv with cuda support, you will have to either compile it yourself (which may be tedious on windows) or find a prebuilt one somewhere. In case you want to go for the 1st solution, here is a link that may help you with the process: https://programming.vip/docs/compile-opencv-with-cuda-support-on-windows-10.html. Keep in mind that this will require you to install a bunch of SDK in the process.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot for your answer. I installed opencv-python using pip (and the contrib one), there is no way to compile Cuda with pip? Because I don't want to use Cmake...
Sorry for the delayed anwnser. Unfortunately no, pip is only a package manager wich serve the purpose of package distribution between user. I doupt there is a version of python-opencv distributed with cuda support and it is neither planed by the opencv team unfortunatly. The main problem is the runtime dependencies implied to run cuda program and maybe also some license issues.
However, I would advise you to not being afraid by compiling it through cmake. I had to do it a few months ago (on Linux however) and the process was pretty straightforward even with cutting-edge gcc and cuda installed. This not always the case for such big projets.
Yeah, I understand and that's true that you need an account for, at least, cudnn. So I'm doing it with Cmake, it's a bit easier than last time I tryied but it worked (expect when I turn on the GPU but it's, I think, because I use cuda 10.1 with cudnn 8.something, I'm doing it with Cuda 10.2 and the same version of cudnn to see what will happn). Thanks a lot for you answer Harry and have a good day.
8

Things seem to have changed a little since this question was asked initially:

From https://github.com/opencv/opencv-python

Option 1 - Main modules package: pip install opencv-python

Option 2 - Full package (contains both main modules and contrib/extra modules): pip install opencv-contrib-python (check contrib/extra modules listing from OpenCV documentation) ==> https://docs.opencv.org/master/

Sadly, not all of the modules listed above seem to be available in the "Full package" eg. cudafilters. If anyone knows any better, I for one would be very grateful to learn more.

Comments

6

For those who can get the same issue. As Harry mentionned, it's not possible to use GPU with opencv from pip, you have to "manually" build it using Cmake (for windows).

It's a bit tricky but there are many tutorials which are here to help you. I spent two days trying to make cvlib works and that's why: one of the cudnn.dll curently available from Nvidia website is named:

Cudnn64_8.dll

and opencv (or tensorflow to be more precise) needs

Cudnn64_7.dll

in fact you just have to replace the 8 by the 7 ! ;) That was the only hard part and I believed it came from the cmake process.

Thanks again Harry.

2 Comments

Can you elaborate on this? I'm trying to build opencv 4.5 on ubuntu 20 and nothing i'm doing seems to get it to find cudnn. Which cmake file is pointing to the wrong cudnn library?
If you use Ubuntu, I would hardly recommend you to build opencv by yourself. It'll be really easier.
6

There are unofficial pre-built wheels available but for a limited number of CUDA versions (see OpenCV Python CUDA wheels)

Because each wheel has to be built against a specific version of CUDA only the most recent compination of OpenCV and CUDA are supported.

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.