Skip to content

Added CMake configuration OPENCV_DNN_BACKEND_DEFAULT#24218

Merged
asmorkalov merged 1 commit intoopencv:4.xfrom
CSBVision:patch-5
Sep 8, 2023
Merged

Added CMake configuration OPENCV_DNN_BACKEND_DEFAULT#24218
asmorkalov merged 1 commit intoopencv:4.xfrom
CSBVision:patch-5

Conversation

@CSBVision
Copy link
Copy Markdown
Contributor

@CSBVision CSBVision commented Sep 4, 2023

Until some recent changes were made, OpenCV selected the default DNN backend either as DNN_BACKEND_INFERENCE_ENGINE, if available, or DNN_BACKEND_OPENCV, otherwise. Recently, this was removed because OpenCV supports multiple plugins/backends, as discussed in #24085.

This patch has two aims: First, fix the documentation since there is no extended default backend logic anymore. Next, it introduces a CMake variable OPENCV_DNN_BACKEND_DEFAULT that can be used to define a user-specific default backend. If this is not set, it defaults to DNN_BACKEND_OPENCV. Still, compiling with OpenVINO/Inference Engine support allows to default to it by adding the CMake argument -D "OPENCV_DNN_BACKEND_DEFAULT=DNN_BACKEND_INFERENCE_ENGINE". Selecting a different default backend is similarly possible, only define OPENCV_DNN_BACKEND_DEFAULT accordingly (e.g. for CUDA). In any case, defining a configuration parameter (e.g. as an environment variable) overrules the compiled-in default such that no existing logic is changed.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@fengyuentau
Copy link
Copy Markdown
Member

What if default backend is set to cuda in the compiling stage, but target is not set or set to non-cuda targets?

@CSBVision
Copy link
Copy Markdown
Contributor Author

Then you are into the same situation as defining the respective backend by an environment variable. Currently, the parameters only support defining the backend but not the target (see dnn_params.cpp). We think that extending this logic for the target is possible, but requires an equivalent default target with correspoding parameter and compiled-in default, which to the best of our knowledge do not exist yet.

@fengyuentau
Copy link
Copy Markdown
Member

I just want to say that the backend-target combinations are confusing to most of the first-time users already, and yet some users may be even confused why they cannot run on cuda with OPENCV_DNN_BACKEND_DEFAULT set to cuda in the compiling stage.

@CSBVision
Copy link
Copy Markdown
Contributor Author

Yes, understood, we agree with that. All we wanted to say is that it's similarly confusing to set a backend alone (either by a configuration parameter or by code) might not be sufficient to actually enable CUDA, OpenVINO or whatever backend you selected. This could be resolved by extending the current logic such that model and backend are set simlutaneously (i.e. unify setPreferableBackend() and setPreferableTarget() functions somehow) or introduce some logic that setPreferableBackend() checks for a compatible target and switches to it. In the error case (e.g. requesting CUDA without CUDA support) using a fallback like DNN_BACKEND_OPENCV and print a warning or throw an expection are both reasonable options. We think these are useful extensions, however these are beyond the scope of this PR.

Copy link
Copy Markdown
Contributor

@asmorkalov asmorkalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@asmorkalov asmorkalov self-assigned this Sep 8, 2023
@asmorkalov asmorkalov merged commit e60825e into opencv:4.x Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DNN module does not use OpenVINO backend by default

5 participants