dnn: support UINT32 and UINT64 targets in Cast

This commit is contained in:
Shiven Solanki
2026-11-21 19:52:12 +08:00
parent e49cba7856
commit ad55604af8
2 changed files with 41 additions and 1 deletions

View File

@@ -61,6 +61,8 @@ namespace
case CV_16S: truncateToIntImpl<short>(src, dst); break;
case CV_32S: truncateToIntImpl<int>(src, dst); break;
case CV_64S: truncateToIntImpl<int64_t>(src, dst); break;
case CV_32U: truncateToIntImpl<uint32_t>(src, dst); break;
case CV_64U: truncateToIntImpl<uint64_t>(src, dst); break;
default: src.convertTo(dst, dst.depth()); break;
}
}
@@ -87,7 +89,8 @@ public:
{
const int v = params.get<int>("outputType");
if (v == CV_Bool || v == CV_8U || v == CV_8S || v == CV_16U || v == CV_16S ||
v == CV_32S || v == CV_64S || v == CV_32F || v == CV_64F || v == CV_16F || v == CV_16BF)
v == CV_32S || v == CV_64S || v == CV_32U || v == CV_64U ||
v == CV_32F || v == CV_64F || v == CV_16F || v == CV_16BF)
{
hasToParam = true;
toCvDepth_ = v;
@@ -457,6 +460,8 @@ private:
ONNX_DT_BOOL = 9,
ONNX_DT_FLOAT16 = 10,
ONNX_DT_DOUBLE = 11,
ONNX_DT_UINT32 = 12,
ONNX_DT_UINT64 = 13,
ONNX_DT_BFLOAT16 = 16
};
@@ -479,6 +484,8 @@ private:
case ONNX_DT_BOOL: return CV_Bool;
case ONNX_DT_FLOAT16: return CV_16F;
case ONNX_DT_DOUBLE: return CV_64F;
case ONNX_DT_UINT32: return CV_32U;
case ONNX_DT_UINT64: return CV_64U;
case ONNX_DT_BFLOAT16: return CV_16BF;
default: break;
}

View File

@@ -5099,6 +5099,39 @@ TEST(Layer_Test_Clip, DynamicMinGreaterThanMax_Float64)
testClipMinGreaterThanMax(CV_64F, true);
}
typedef testing::TestWithParam<tuple<int, MatDepth> > Layer_Test_Cast_Unsigned;
TEST_P(Layer_Test_Cast_Unsigned, FloatTruncates)
{
const int onnxType = get<0>(GetParam());
const int depth = get<1>(GetParam());
LayerParams lp;
lp.type = "Cast2";
lp.name = "test_cast";
lp.set("to", onnxType);
Ptr<Layer> layer = LayerFactory::createLayerInstance(lp.type, lp);
ASSERT_TRUE(layer);
// ONNX Cast truncates toward zero: 2.7 -> 2 and 5.9 -> 5, not rounded up
const float inVals[] = {0.f, 1.2f, 2.7f, 5.9f};
const double refVals[] = {0, 1, 2, 5};
Mat input(1, 4, CV_32F, (void*)inVals), ref;
Mat(1, 4, CV_64F, (void*)refVals).convertTo(ref, depth);
std::vector<Mat> inputs{input}, outputs;
runLayer(layer, inputs, outputs);
ASSERT_EQ(outputs.size(), (size_t)1);
EXPECT_EQ(depth, outputs[0].depth());
EXPECT_EQ(0, cvtest::norm(outputs[0], ref, NORM_INF));
}
INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Cast_Unsigned, Values(
make_tuple(12, CV_32U), // ONNX UINT32
make_tuple(13, CV_64U) // ONNX UINT64
));
static Mat runOneHot(const Mat& values)
{
LayerParams lp;