From 853e5dfcdf091023b78283d6dc03d91fd6763495 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Fri, 22 Dec 2023 09:22:31 +0300 Subject: [PATCH] Merge pull request #24709 from vpisarev:winograd_mode Try to enable Winograd by default in FP32 mode and disable it by default in FP16 mode #24709 Hopefully, it will resolve regressions since 4.8.1 (see also https://github.com/opencv/opencv/pull/24587) ### 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 --- modules/dnn/include/opencv2/dnn/all_layers.hpp | 2 +- modules/dnn/src/layers/layers_common.cpp | 2 +- modules/dnn/src/net_impl_backend.cpp | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/dnn/include/opencv2/dnn/all_layers.hpp b/modules/dnn/include/opencv2/dnn/all_layers.hpp index c408921c04..41fe0df70f 100644 --- a/modules/dnn/include/opencv2/dnn/all_layers.hpp +++ b/modules/dnn/include/opencv2/dnn/all_layers.hpp @@ -291,7 +291,7 @@ CV__DNN_INLINE_NS_BEGIN static Ptr create(const LayerParams& params); bool fusedActivation = false; bool fusedAdd = false; - bool useWinograd = false; // Flag whether to use Winograd to speed up 3x3 convolution. + bool useWinograd = true; // Flag whether to use Winograd to speed up 3x3 convolution. }; class CV_EXPORTS ConvolutionLayerInt8 : public BaseConvolutionLayer diff --git a/modules/dnn/src/layers/layers_common.cpp b/modules/dnn/src/layers/layers_common.cpp index 48401893f0..3b3a007b06 100644 --- a/modules/dnn/src/layers/layers_common.cpp +++ b/modules/dnn/src/layers/layers_common.cpp @@ -195,7 +195,7 @@ void getConvolutionKernelParams(const LayerParams ¶ms, std::vector& util::getStrideAndPadding(params, pads_begin, pads_end, strides, padMode, kernel.size()); util::getParameter(params, "dilation", "dilation", dilations, true, std::vector(kernel.size(), 1)); util::getParameter(params, "adj", "adj", adjust_pads, true, std::vector(kernel.size(), 0)); - useWinograd = params.get("use_winograd", true); + useWinograd = params.get("use_winograd", useWinograd); for (int i = 0; i < dilations.size(); i++) CV_Assert(dilations[i] > 0); diff --git a/modules/dnn/src/net_impl_backend.cpp b/modules/dnn/src/net_impl_backend.cpp index 9896153a82..b53908f8ec 100644 --- a/modules/dnn/src/net_impl_backend.cpp +++ b/modules/dnn/src/net_impl_backend.cpp @@ -251,6 +251,14 @@ void Net::Impl::setPreferableTarget(int targetId) #endif clear(); + + if (targetId == DNN_TARGET_CPU_FP16) + { + if (useWinograd) { + CV_LOG_INFO(NULL, "DNN: DNN_TARGET_CPU_FP16 is set => Winograd convolution is disabled by default to preserve accuracy. If needed, enable it explicitly using enableWinograd(true)."); + enableWinograd(false); + } + } } }