1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 15:53:03 +04:00

Merge pull request #29524 from abhishek-gola/onnx_coverage

Fix: 0-D / empty-Mat Python binding
This commit is contained in:
Abhishek Gola
2026-07-28 22:14:50 +05:30
committed by GitHub
3 changed files with 22 additions and 5 deletions
+3 -2
View File
@@ -52,7 +52,8 @@ public:
}
else
{
out.assign(1, MatShape(1, 1));
// Reduced (mean/sum) loss is a rank-0 scalar in ONNX, not a [1] tensor.
out.assign(1, MatShape::scalar());
}
return false;
}
@@ -164,7 +165,7 @@ public:
}
}
const float out = (reduction == LOSS_REDUCTION_SUM) ? static_cast<float>(num) : static_cast<float>((den > 0.0) ? (num / den) : 0.0);
out_loss.at<float>(0) = out;
out_loss.ptr<float>()[0] = out;
}
}
@@ -49,7 +49,8 @@ public:
for (size_t i = 2; i < x.size(); ++i) y.push_back(x[i]);
out.push_back(y);
} else {
out.push_back(MatShape(1,1));
// Reduced (mean/sum) loss is a rank-0 scalar in ONNX, not a [1] tensor.
out.push_back(MatShape::scalar());
}
if (requiredOutputs >= 2)
@@ -325,7 +326,7 @@ public:
const float out = (reduction == LOSS_REDUCTION_SUM) ? static_cast<float>(num)
: static_cast<float>((den > 0.0) ? (num / den) : 0.0);
out_loss.at<float>(0) = out;
out_loss.ptr<float>()[0] = out;
}
}
+16 -1
View File
@@ -315,8 +315,23 @@ bool pyopencv_to(PyObject* o, Mat& m, const ArgInfo& info)
template<>
PyObject* pyopencv_from(const cv::Mat& m)
{
if( !m.data )
if( m.empty() )
{
// empty() also catches a live buffer with a zero-length dim: return an empty array, not None.
if( m.dims >= 1 )
{
int cn = m.channels();
int typenum = cvDepthToNumpyType(m.depth());
int dims = m.dims;
cv::AutoBuffer<npy_intp> _sizes(dims + 1);
for( int i = 0; i < dims; i++ )
_sizes[i] = (npy_intp)m.size[i];
if( cn > 1 )
_sizes[dims++] = cn;
return PyArray_SimpleNew(dims, _sizes.data(), typenum);
}
Py_RETURN_NONE;
}
// 0D (scalar) Mat: return a true 0D numpy array.
if( m.dims == 0 )
{