mirror of
https://github.com/opencv/opencv.git
synced 2026-12-03 05:33:03 -06:00
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<uint32_t>, runKernel<uint64_t> and runKernel<bool>. 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.
This commit is contained in:
@@ -389,12 +389,54 @@ MatShape tensorToShape(const Mat& shapeTensor)
|
||||
return MatShape(shapeSpecVec);
|
||||
}
|
||||
|
||||
template<typename _Ts>
|
||||
static void storeScalar(_Ts v, int depth, void* value)
|
||||
{
|
||||
if (depth == CV_8U)
|
||||
*reinterpret_cast<uint8_t*>(value) = saturate_cast<uint8_t>(v);
|
||||
else if (depth == CV_8S)
|
||||
*reinterpret_cast<int8_t*>(value) = saturate_cast<int8_t>(v);
|
||||
else if (depth == CV_16U)
|
||||
*reinterpret_cast<uint16_t*>(value) = saturate_cast<uint16_t>(v);
|
||||
else if (depth == CV_16S)
|
||||
*reinterpret_cast<int16_t*>(value) = saturate_cast<int16_t>(v);
|
||||
else if (depth == CV_32U)
|
||||
*reinterpret_cast<uint32_t*>(value) = saturate_cast<uint32_t>(v);
|
||||
else if (depth == CV_32S)
|
||||
*reinterpret_cast<int32_t*>(value) = saturate_cast<int32_t>(v);
|
||||
else if (depth == CV_64U)
|
||||
*reinterpret_cast<uint64_t*>(value) = saturate_cast<uint64_t>(v);
|
||||
else if (depth == CV_64S)
|
||||
*reinterpret_cast<int64_t*>(value) = saturate_cast<int64_t>(v);
|
||||
else if (depth == CV_32F)
|
||||
*reinterpret_cast<float*>(value) = saturate_cast<float>(v);
|
||||
else if (depth == CV_64F)
|
||||
*reinterpret_cast<double*>(value) = saturate_cast<double>(v);
|
||||
else if (depth == CV_16F)
|
||||
*reinterpret_cast<hfloat*>(value) = saturate_cast<hfloat>(v);
|
||||
else if (depth == CV_16BF)
|
||||
*reinterpret_cast<bfloat*>(value) = saturate_cast<bfloat>(v);
|
||||
else if (depth == CV_Bool)
|
||||
*reinterpret_cast<uint8_t*>(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<uint64_t>(), 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<uint8_t*>(value) = isflt ? saturate_cast<uint8_t>(v) : saturate_cast<uint8_t>(iv);
|
||||
else if (depth == CV_8S)
|
||||
*reinterpret_cast<int8_t*>(value) = isflt ? saturate_cast<int8_t>(v) : saturate_cast<int8_t>(iv);
|
||||
else if (depth == CV_16U)
|
||||
*reinterpret_cast<uint16_t*>(value) = isflt ? saturate_cast<uint16_t>(v) : saturate_cast<uint16_t>(iv);
|
||||
else if (depth == CV_16S)
|
||||
*reinterpret_cast<int16_t*>(value) = isflt ? saturate_cast<int16_t>(v) : saturate_cast<int16_t>(iv);
|
||||
else if (depth == CV_32U)
|
||||
*reinterpret_cast<uint32_t*>(value) = isflt ? saturate_cast<uint32_t>(v) : saturate_cast<uint32_t>(iv);
|
||||
else if (depth == CV_32S)
|
||||
*reinterpret_cast<int32_t*>(value) = isflt ? saturate_cast<int32_t>(v) : saturate_cast<int32_t>(iv);
|
||||
else if (depth == CV_64U)
|
||||
*reinterpret_cast<uint64_t*>(value) = isflt ? saturate_cast<uint64_t>(v) : saturate_cast<uint64_t>(iv);
|
||||
else if (depth == CV_64S)
|
||||
*reinterpret_cast<int64_t*>(value) = isflt ? saturate_cast<int64_t>(v) : iv;
|
||||
else if (depth == CV_32F)
|
||||
*reinterpret_cast<float*>(value) = isflt ? (float)v : saturate_cast<float>(iv);
|
||||
else if (depth == CV_64F)
|
||||
*reinterpret_cast<double*>(value) = isflt ? v : saturate_cast<double>(iv);
|
||||
else if (depth == CV_16F)
|
||||
*reinterpret_cast<hfloat*>(value) = isflt ? saturate_cast<hfloat>(v) : saturate_cast<hfloat>(iv);
|
||||
else if (depth == CV_16BF)
|
||||
*reinterpret_cast<bfloat*>(value) = isflt ? saturate_cast<bfloat>(v) : saturate_cast<bfloat>(iv);
|
||||
else if (depth == CV_Bool)
|
||||
*reinterpret_cast<uint8_t*>(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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -248,12 +248,15 @@ public:
|
||||
case CV_8S: runKernel<schar>(indices, (int)depth64, values, Y); break;
|
||||
case CV_16U: runKernel<uint16_t>(indices, (int)depth64, values, Y); break;
|
||||
case CV_16S: runKernel<int16_t>(indices, (int)depth64, values, Y); break;
|
||||
case CV_32U: runKernel<uint32_t>(indices, (int)depth64, values, Y); break;
|
||||
case CV_32S: runKernel<int>(indices, (int)depth64, values, Y); break;
|
||||
case CV_64U: runKernel<uint64_t>(indices, (int)depth64, values, Y); break;
|
||||
case CV_64S: runKernel<int64_t>(indices, (int)depth64, values, Y); break;
|
||||
case CV_16F: runKernel<hfloat>(indices, (int)depth64, values, Y); break;
|
||||
case CV_16BF: runKernel<bfloat>(indices, (int)depth64, values, Y); break;
|
||||
case CV_32F: runKernel<float>(indices, (int)depth64, values, Y); break;
|
||||
case CV_64F: runKernel<double>(indices, (int)depth64, values, Y); break;
|
||||
case CV_Bool: runKernel<bool>(indices, (int)depth64, values, Y); break;
|
||||
default: CV_Error(Error::BadDepth, "OneHot: unsupported output type");
|
||||
}
|
||||
|
||||
|
||||
@@ -5023,6 +5023,44 @@ INSTANTIATE_TEST_CASE_P(/**/, Layer_Test_Hardmax, Values(
|
||||
make_tuple(CV_Bool, 0.0)
|
||||
));
|
||||
|
||||
typedef testing::TestWithParam<MatDepth> 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> layer = LayerFactory::createLayerInstance(lp.type, lp);
|
||||
ASSERT_TRUE(layer);
|
||||
|
||||
// indices {0, 2, 1}, depth 3, values {off = 0, on = 1}
|
||||
std::vector<int64_t> idx = {0, 2, 1};
|
||||
std::vector<int64_t> numClasses = {3};
|
||||
Mat values;
|
||||
Mat(std::vector<float>{0.f, 1.f}).convertTo(values, depth);
|
||||
|
||||
std::vector<Mat> inputs{Mat(idx).reshape(1, std::vector<int>{3}),
|
||||
Mat(numClasses).reshape(1, std::vector<int>{1}),
|
||||
values.reshape(1, std::vector<int>{2})}, outputs;
|
||||
runLayer(layer, inputs, outputs);
|
||||
|
||||
// Each row has `on` at its index and `off` everywhere else
|
||||
Mat ref;
|
||||
Mat(std::vector<float>{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;
|
||||
|
||||
Reference in New Issue
Block a user