How to use GPU-acceleration on openCV dnn module on python (Windows)?

Viewed 5693

I want to use opencv-python4.4 to do inference for YOLOv3. But it uses CPU by default. I have read this instruction here, saying the new version opencv4.2.0 has already supported GPU. But the instruction is only for Ubuntu but mine is Windows10.

I tried to Could you please walk me through how to pull this off in Wondows10, python? As I directly used it, the error occurred:

cv::dnn::dnn4_v20191202::Net::Impl::setUpNet DNN module was not built with CUDA backend; switching to CPU

It seems that I have to manually compile opencv but isn't it for Cpp? But I only want to implement it on python. Could anyone give me some instructions?

2 Answers

You will indeed need to build OpenCV yourself.

While you're using the Python bindings for OpenCV, the OpenCV library itself is written in C++ instead of Python. That also explains how OpenCV can use CUDA, another C++ library to access NVidia GPU's.

The instructions you linked are from a person not associated with OpenCV, who admits to an anti-Windows bias. That means those instructions are not useful to you.

These instructions do cover the Windows build.

Summary:

  • Use Visual Studio 2019 (Latest C++ compiler from Microsoft) and CMake
  • Download & install OpenCV
  • Download & install CUDA and cuDNN
  • Download & install Anaconda3 and use it as default Python
  • Set environment variables so CMake can find your installed libraries
  • Set the environment variables and options so CMake knows that you also want the Python bindings for OpenCV
  • Use CMake to create .sln file for Visual Studio
  • Open .sln in Visual Studio and build it.
  • This generates the cv2.cp37-win_amd64.pyd file you need.

I have figured it out... both

-DBUILD_opencv_python3=ON
-DBUILD_opencv_python2=OFF

must be specified, otherwise the to be built will not include any python module at all.

Related