From 7e2201571de1715e24484d637a2bceca97f9add5 Mon Sep 17 00:00:00 2001 From: Shiven Solanki Date: Sun, 22 Nov 2026 21:05:51 +0800 Subject: [PATCH] Merge pull request #31025 from sshivensolanki/dnn-onehot-uint32-bool dnn: support CV_32U, CV_64U and CV_Bool values in OneHot Add the missing case labels, calling the existing runKernel, runKernel and runKernel. tensorToScalar could not read CV_64U tensors. It now reads them as uint64_t and writes the result without going through int64_t, so values above INT64_MAX are not cut down. The output conversion moved into a small storeScalar template, called with double, int64_t or uint64_t; the results for the other input types are unchanged. --- modules/dnn/src/layers/layers_common.cpp | 75 +++++++++++++++--------- modules/dnn/src/layers/onehot_layer.cpp | 3 + modules/dnn/test/test_layers.cpp | 38 ++++++++++++ 3 files changed, 87 insertions(+), 29 deletions(-) diff --git a/modules/dnn/src/layers/layers_common.cpp b/modules/dnn/src/layers/layers_common.cpp index 3f796672fb..ecb1c28935 100644 --- a/modules/dnn/src/layers/layers_common.cpp +++ b/modules/dnn/src/layers/layers_common.cpp @@ -389,12 +389,54 @@ MatShape tensorToShape(const Mat& shapeTensor) return MatShape(shapeSpecVec); } +template +static void storeScalar(_Ts v, int depth, void* value) +{ + if (depth == CV_8U) + *reinterpret_cast(value) = saturate_cast(v); + else if (depth == CV_8S) + *reinterpret_cast(value) = saturate_cast(v); + else if (depth == CV_16U) + *reinterpret_cast(value) = saturate_cast(v); + else if (depth == CV_16S) + *reinterpret_cast(value) = saturate_cast(v); + else if (depth == CV_32U) + *reinterpret_cast(value) = saturate_cast(v); + else if (depth == CV_32S) + *reinterpret_cast(value) = saturate_cast(v); + else if (depth == CV_64U) + *reinterpret_cast(value) = saturate_cast(v); + else if (depth == CV_64S) + *reinterpret_cast(value) = saturate_cast(v); + else if (depth == CV_32F) + *reinterpret_cast(value) = saturate_cast(v); + else if (depth == CV_64F) + *reinterpret_cast(value) = saturate_cast(v); + else if (depth == CV_16F) + *reinterpret_cast(value) = saturate_cast(v); + else if (depth == CV_16BF) + *reinterpret_cast(value) = saturate_cast(v); + else if (depth == CV_Bool) + *reinterpret_cast(value) = (uint8_t)(v != 0); + else { + CV_Error_(Error::StsNotImplemented, ("type %s is not supported", typeToString(depth).c_str())); + } +} + void tensorToScalar(const Mat& tensor, int type, void* value) { CV_Assert(tensor.total() == 1); int type0 = tensor.type(); int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); CV_Assert(cn == 1); + + // uint64 values above INT64_MAX do not fit in int64, so read them separately + if (type0 == CV_64U) + { + storeScalar(*tensor.ptr(), depth, value); + return; + } + double v = 0; int64_t iv = 0; bool isflt = type0 == CV_32F || type0 == CV_64F || type0 == CV_16F || type0 == CV_16BF; @@ -427,35 +469,10 @@ void tensorToScalar(const Mat& tensor, int type, void* value) CV_Error_(Error::StsNotImplemented, ("type %s is not supported", typeToString(type0).c_str())); } - if (depth == CV_8U) - *reinterpret_cast(value) = isflt ? saturate_cast(v) : saturate_cast(iv); - else if (depth == CV_8S) - *reinterpret_cast(value) = isflt ? saturate_cast(v) : saturate_cast(iv); - else if (depth == CV_16U) - *reinterpret_cast(value) = isflt ? saturate_cast(v) : saturate_cast(iv); - else if (depth == CV_16S) - *reinterpret_cast(value) = isflt ? saturate_cast(v) : saturate_cast(iv); - else if (depth == CV_32U) - *reinterpret_cast(value) = isflt ? saturate_cast(v) : saturate_cast(iv); - else if (depth == CV_32S) - *reinterpret_cast(value) = isflt ? saturate_cast(v) : saturate_cast(iv); - else if (depth == CV_64U) - *reinterpret_cast(value) = isflt ? saturate_cast(v) : saturate_cast(iv); - else if (depth == CV_64S) - *reinterpret_cast(value) = isflt ? saturate_cast(v) : iv; - else if (depth == CV_32F) - *reinterpret_cast(value) = isflt ? (float)v : saturate_cast(iv); - else if (depth == CV_64F) - *reinterpret_cast(value) = isflt ? v : saturate_cast(iv); - else if (depth == CV_16F) - *reinterpret_cast(value) = isflt ? saturate_cast(v) : saturate_cast(iv); - else if (depth == CV_16BF) - *reinterpret_cast(value) = isflt ? saturate_cast(v) : saturate_cast(iv); - else if (depth == CV_Bool) - *reinterpret_cast(value) = isflt ? (uint8_t)(v != 0) : (uint8_t)(iv != 0); - else { - CV_Error_(Error::StsNotImplemented, ("type %s is not supported", typeToString(depth).c_str())); - } + if (isflt) + storeScalar(v, depth, value); + else + storeScalar(iv, depth, value); } } diff --git a/modules/dnn/src/layers/onehot_layer.cpp b/modules/dnn/src/layers/onehot_layer.cpp index ea8f395166..0cda4cbc3c 100644 --- a/modules/dnn/src/layers/onehot_layer.cpp +++ b/modules/dnn/src/layers/onehot_layer.cpp @@ -248,12 +248,15 @@ public: case CV_8S: runKernel(indices, (int)depth64, values, Y); break; case CV_16U: runKernel(indices, (int)depth64, values, Y); break; case CV_16S: runKernel(indices, (int)depth64, values, Y); break; + case CV_32U: runKernel(indices, (int)depth64, values, Y); break; case CV_32S: runKernel(indices, (int)depth64, values, Y); break; + case CV_64U: runKernel(indices, (int)depth64, values, Y); break; case CV_64S: runKernel(indices, (int)depth64, values, Y); break; case CV_16F: runKernel(indices, (int)depth64, values, Y); break; case CV_16BF: runKernel(indices, (int)depth64, values, Y); break; case CV_32F: runKernel(indices, (int)depth64, values, Y); break; case CV_64F: runKernel(indices, (int)depth64, values, Y); break; + case CV_Bool: runKernel(indices, (int)depth64, values, Y); break; default: CV_Error(Error::BadDepth, "OneHot: unsupported output type"); } diff --git a/modules/dnn/test/test_layers.cpp b/modules/dnn/test/test_layers.cpp index 49c830b535..0ce62623c1 100644 --- a/modules/dnn/test/test_layers.cpp +++ b/modules/dnn/test/test_layers.cpp @@ -5023,6 +5023,44 @@ INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Hardmax, Values( make_tuple(CV_Bool, 0.0) )); +typedef testing::TestWithParam Layer_Test_OneHot_ValuesDepth; +TEST_P(Layer_Test_OneHot_ValuesDepth, Accuracy) +{ + const int depth = GetParam(); + + LayerParams lp; + lp.type = "OneHot"; + lp.name = "test_onehot"; + lp.set("axis", -1); + + Ptr layer = LayerFactory::createLayerInstance(lp.type, lp); + ASSERT_TRUE(layer); + + // indices {0, 2, 1}, depth 3, values {off = 0, on = 1} + std::vector idx = {0, 2, 1}; + std::vector numClasses = {3}; + Mat values; + Mat(std::vector{0.f, 1.f}).convertTo(values, depth); + + std::vector inputs{Mat(idx).reshape(1, std::vector{3}), + Mat(numClasses).reshape(1, std::vector{1}), + values.reshape(1, std::vector{2})}, outputs; + runLayer(layer, inputs, outputs); + + // Each row has `on` at its index and `off` everywhere else + Mat ref; + Mat(std::vector{1, 0, 0, + 0, 0, 1, + 0, 1, 0}).reshape(1, 3).convertTo(ref, depth); + ASSERT_EQ(outputs.size(), (size_t)1); + EXPECT_EQ(outputs[0].depth(), depth); + EXPECT_EQ(0, cvtest::norm(outputs[0], ref, NORM_INF)); +} + +INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_OneHot_ValuesDepth, Values( + CV_8U, CV_32S, CV_32F, CV_32U, CV_64U, CV_Bool +)); + static void testClipMinGreaterThanMax(int depth, bool dynamic) { const int total = 64;