Download modules/dnn/src/net_impl_backend.cpp from tgollogly/test: direct link, hf CLI and curl.
- Browser
- Download file 9.05 kB
-
https://huggingface.co/tgollogly/test/resolve/main/modules/dnn/src/net_impl_backend.cpp
- Command line
-
hf download hf://tgollogly/test/modules/dnn/src/net_impl_backend.cpp
-
curl -L -o net_impl_backend.cpp https://huggingface.co/tgollogly/test/resolve/main/modules/dnn/src/net_impl_backend.cpp
9.05 kB
| // This file is part of OpenCV project. | |
| // It is subject to the license terms in the LICENSE file found in the top-level directory | |
| // of this distribution and at http://opencv.org/license.html. | |
| namespace cv { | |
| namespace dnn { | |
| CV__DNN_INLINE_NS_BEGIN | |
| Ptr<BackendWrapper> Net::Impl::wrap(Mat& host) | |
| { | |
| if (preferableBackend == DNN_BACKEND_OPENCV && | |
| (preferableTarget == DNN_TARGET_CPU || preferableTarget == DNN_TARGET_CPU_FP16)) | |
| return Ptr<BackendWrapper>(); | |
| MatShape shape(host.dims); | |
| for (int i = 0; i < host.dims; ++i) | |
| shape[i] = host.size[i]; | |
| void* data = host.data; | |
| if (backendWrappers.find(data) != backendWrappers.end()) | |
| { | |
| Ptr<BackendWrapper> baseBuffer = backendWrappers[data]; | |
| if (preferableBackend == DNN_BACKEND_OPENCV) | |
| { | |
| CV_Assert(IS_DNN_OPENCL_TARGET(preferableTarget)); | |
| return OpenCLBackendWrapper::create(baseBuffer, host); | |
| CV_Error(Error::StsInternal, ""); | |
| } | |
| else if (preferableBackend == DNN_BACKEND_HALIDE) | |
| { | |
| CV_Assert(haveHalide()); | |
| return Ptr<BackendWrapper>(new HalideBackendWrapper(baseBuffer, shape)); | |
| } | |
| else if (preferableBackend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019) | |
| { | |
| CV_ERROR_DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019; | |
| } | |
| else if (preferableBackend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) | |
| { | |
| return wrapMat(preferableBackend, preferableTarget, host); | |
| } | |
| else if (preferableBackend == DNN_BACKEND_WEBNN) | |
| { | |
| return wrapMat(preferableBackend, preferableTarget, host); | |
| } | |
| else if (preferableBackend == DNN_BACKEND_VKCOM) | |
| { | |
| return Ptr<BackendWrapper>(new VkComBackendWrapper(baseBuffer, host)); | |
| } | |
| else if (preferableBackend == DNN_BACKEND_CUDA) | |
| { | |
| CV_Assert(haveCUDA()); | |
| switch (preferableTarget) | |
| { | |
| case DNN_TARGET_CUDA: | |
| return CUDABackendWrapperFP32::create(baseBuffer, shape); | |
| case DNN_TARGET_CUDA_FP16: | |
| return CUDABackendWrapperFP16::create(baseBuffer, shape); | |
| default: | |
| CV_Assert(IS_DNN_CUDA_TARGET(preferableTarget)); | |
| } | |
| } | |
| else if (preferableBackend == DNN_BACKEND_TIMVX) | |
| { | |
| return Ptr<BackendWrapper>(new TimVXBackendWrapper(baseBuffer, host)); | |
| } | |
| else if (preferableBackend == DNN_BACKEND_CANN) | |
| { | |
| CV_Assert(0 && "Internal error: DNN_BACKEND_CANN must be implemented through inheritance"); | |
| } | |
| else | |
| CV_Error(Error::StsNotImplemented, "Unknown backend identifier"); | |
| } | |
| Ptr<BackendWrapper> wrapper = wrapMat(preferableBackend, preferableTarget, host); | |
| backendWrappers[data] = wrapper; | |
| return wrapper; | |
| } | |
| void Net::Impl::initBackend(const std::vector<LayerPin>& blobsToKeep_) | |
| { | |
| CV_TRACE_FUNCTION(); | |
| if (preferableBackend == DNN_BACKEND_OPENCV) | |
| { | |
| CV_Assert(preferableTarget == DNN_TARGET_CPU || preferableTarget == DNN_TARGET_CPU_FP16 || IS_DNN_OPENCL_TARGET(preferableTarget)); | |
| } | |
| else if (preferableBackend == DNN_BACKEND_HALIDE) | |
| { | |
| initHalideBackend(); | |
| CV_Error(Error::StsNotImplemented, "This OpenCV version is built without support of Halide"); | |
| } | |
| else if (preferableBackend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) | |
| { | |
| CV_Assert(0 && "Inheritance must be used with OpenVINO backend"); | |
| } | |
| else if (preferableBackend == DNN_BACKEND_WEBNN) | |
| { | |
| initWebnnBackend(blobsToKeep_); | |
| CV_Error(Error::StsNotImplemented, "This OpenCV version is built without support of WebNN"); | |
| } | |
| else if (preferableBackend == DNN_BACKEND_VKCOM) | |
| { | |
| initVkComBackend(); | |
| CV_Error(Error::StsNotImplemented, "This OpenCV version is built without support of Vulkan"); | |
| } | |
| else if (preferableBackend == DNN_BACKEND_CUDA) | |
| { | |
| initCUDABackend(blobsToKeep_); | |
| CV_Error(Error::StsNotImplemented, "This OpenCV version is built without support of CUDA/CUDNN"); | |
| } | |
| else if (preferableBackend == DNN_BACKEND_TIMVX) | |
| { | |
| initTimVXBackend(); | |
| CV_Error(Error::StsNotImplemented, "This OpenCV version is built without support of TimVX"); | |
| } | |
| else if (preferableBackend == DNN_BACKEND_CANN) | |
| { | |
| CV_Assert(0 && "Internal error: DNN_BACKEND_CANN must be implemented through inheritance"); | |
| } | |
| else | |
| { | |
| CV_Error(Error::StsNotImplemented, cv::format("Unknown backend identifier: %d", preferableBackend)); | |
| } | |
| } | |
| void Net::Impl::setPreferableBackend(Net& net, int backendId) | |
| { | |
| if (backendId == DNN_BACKEND_DEFAULT) | |
| backendId = (Backend)getParam_DNN_BACKEND_DEFAULT(); | |
| if (backendId == DNN_BACKEND_INFERENCE_ENGINE) | |
| backendId = DNN_BACKEND_INFERENCE_ENGINE_NGRAPH; // = getInferenceEngineBackendTypeParam(); | |
| if (netWasQuantized && backendId != DNN_BACKEND_OPENCV && backendId != DNN_BACKEND_TIMVX && | |
| backendId != DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) | |
| { | |
| CV_LOG_WARNING(NULL, "DNN: Only default, TIMVX and OpenVINO backends support quantized networks"); | |
| backendId = DNN_BACKEND_OPENCV; | |
| } | |
| if (netWasQuantized && backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && INF_ENGINE_VER_MAJOR_LT(INF_ENGINE_RELEASE_2023_0)) | |
| { | |
| CV_LOG_WARNING(NULL, "DNN: OpenVINO 2023.0 and higher is required to supports quantized networks"); | |
| backendId = DNN_BACKEND_OPENCV; | |
| } | |
| if (preferableBackend != backendId) | |
| { | |
| clear(); | |
| if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) | |
| { | |
| switchToOpenVINOBackend(net); | |
| auto& networkBackend = dnn_backend::createPluginDNNNetworkBackend("openvino"); | |
| networkBackend.switchBackend(net); | |
| CV_Error(Error::StsNotImplemented, "OpenVINO backend is not available in the current OpenCV build"); | |
| } | |
| else if (backendId == DNN_BACKEND_CANN) | |
| { | |
| switchToCannBackend(net); | |
| CV_Error(Error::StsNotImplemented, "CANN backend is not availlable in the current OpenCV build"); | |
| } | |
| else | |
| { | |
| preferableBackend = backendId; | |
| } | |
| } | |
| } | |
| void Net::Impl::setPreferableTarget(int targetId) | |
| { | |
| if (netWasQuantized && targetId != DNN_TARGET_CPU && | |
| targetId != DNN_TARGET_OPENCL && targetId != DNN_TARGET_OPENCL_FP16 && targetId != DNN_TARGET_NPU) | |
| { | |
| CV_LOG_WARNING(NULL, "DNN: Only CPU, OpenCL/OpenCL FP16 and NPU targets are supported by quantized networks"); | |
| targetId = DNN_TARGET_CPU; | |
| } | |
| if (preferableTarget != targetId) | |
| { | |
| preferableTarget = targetId; | |
| if (IS_DNN_OPENCL_TARGET(targetId)) | |
| { | |
| if (preferableBackend == DNN_BACKEND_OPENCV) | |
| if (preferableBackend == DNN_BACKEND_DEFAULT || | |
| preferableBackend == DNN_BACKEND_OPENCV) | |
| preferableTarget = DNN_TARGET_CPU; | |
| bool fp16 = ocl::Device::getDefault().isExtensionSupported("cl_khr_fp16"); | |
| if (!fp16 && targetId == DNN_TARGET_OPENCL_FP16) | |
| preferableTarget = DNN_TARGET_OPENCL; | |
| } | |
| if (IS_DNN_CUDA_TARGET(targetId)) | |
| { | |
| preferableTarget = DNN_TARGET_CPU; | |
| if (cuda4dnn::doesDeviceSupportFP16() && targetId == DNN_TARGET_CUDA_FP16) | |
| preferableTarget = DNN_TARGET_CUDA_FP16; | |
| else | |
| preferableTarget = DNN_TARGET_CUDA; | |
| } | |
| if (targetId == DNN_TARGET_CPU_FP16) | |
| { | |
| CV_LOG_WARNING(NULL, "DNN: fall back to DNN_TARGET_CPU. Only ARM v8 CPU is supported by DNN_TARGET_CPU_FP16."); | |
| targetId = DNN_TARGET_CPU; | |
| } | |
| 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); | |
| } | |
| } | |
| } | |
| } | |
| CV__DNN_INLINE_NS_END | |
| }} // namespace cv::dnn | |