1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 15:23:05 +04:00

Merge pull request #27600 from atamas19:ov_output_clamping

G-API: Implement cfgClampOutputs option to OpenVINO Params #27600

Added the option `cfgClampOutputs` to control where output clamping is performed for OpenVINO models. When enabled, output values are clamped in the PrePostProcessor stage instead of by the device or plugin. This provides a consistent and standardized clamping method across devices, helping to maintain accuracy regardless of device-specific clamping behavior.

### Pull Request Readiness Checklist

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

- [x] I agree to contribute to the project under Apache 2 License.
- [x] 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
- [x] 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.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Andrei Tamas
2025-08-12 14:34:42 +03:00
committed by GitHub
parent 2ecc22fe03
commit 252403bbf2
2 changed files with 61 additions and 0 deletions
@@ -147,6 +147,25 @@ static int toCV(const ov::element::Type &type) {
return -1;
}
static inline std::pair<double, double> get_CV_type_range(int cv_type) {
switch (cv_type) {
case CV_8U:
return { static_cast<double>(std::numeric_limits<uint8_t>::min()),
static_cast<double>(std::numeric_limits<uint8_t>::max()) };
case CV_32S:
return { static_cast<double>(std::numeric_limits<int32_t>::min()),
static_cast<double>(std::numeric_limits<int32_t>::max()) };
case CV_32F:
return { static_cast<double>(std::numeric_limits<float>::lowest()),
static_cast<double>(std::numeric_limits<float>::max()) };
case CV_16F:
return { -65504.0, 65504.0 };
default:
GAPI_Error("OV Backend: Unsupported data type");
}
return {0.0, 0.0};
}
static void copyFromOV(const ov::Tensor &tensor, cv::Mat &mat) {
const auto total = mat.total() * mat.channels();
if (toCV(tensor.get_element_type()) != mat.depth() ||
@@ -1052,6 +1071,20 @@ public:
if (explicit_out_tensor_prec) {
m_ppp.output(output_name).tensor()
.set_element_type(toOV(*explicit_out_tensor_prec));
if (m_model_info.clamp_outputs) {
#if INF_ENGINE_RELEASE >= 2025020000
auto clamp_range = get_CV_type_range(*explicit_out_tensor_prec);
m_ppp.output(output_name).postprocess()
.clamp(clamp_range.first, clamp_range.second);
#else
static bool warned = false;
if (!warned) {
GAPI_LOG_WARNING(NULL, "cfgClampOutputs is enabled, but not supported in this OpenVINO version. Clamping will be ignored.");
warned = true;
}
#endif // INF_ENGINE_RELEASE >= 2025020000
}
}
}
}