Repository navigation
dnn: fix Reduce over three or more axes - #30781
Conversation
Port of opencv#30108 (5.x, reduce2_layer.cpp) to the 4.x Reduce layer. ReduceInvoker walks the reduced block with an odometer over every reduced axis but the innermost one. On a carry it assigned the step of the next outer axis (j - 1) instead of removing the offset of the axis that wrapped and adding one step of the next. projected_steps is shared by all output elements, so once an outer reduced axis has three or more elements every output was wrong. Carry the same way as the unprojected axes below. This also makes test_mvn_expanded pass, so it is removed from the parser denylist.
There was a problem hiding this comment.
Status: 🟡 Yellow
The projected-axis carry now removes the wrapped-axis offset and adds the next outer step, matching the already-correct unreduced-axis odometer and the merged 5.x Reduce2 fix. The regression covers several three- and four-axis layouts, and re-enabling test_mvn_expanded exercises the reported ONNX path. No actionable issue found; LGTM pending exact-head CI and conformance-data validation.
| struct Case { std::vector<int> sizes; std::vector<int> axes; }; | ||
| const Case cases[] = { | ||
| { {4, 3, 5, 4}, {0, 2, 3} }, { {4, 3, 5, 4}, {0, 1, 3} }, | ||
| { {4, 3, 5, 4}, {1, 2, 3} }, { {4, 3, 5, 4}, {0, 1, 2} }, | ||
| { {2, 3, 2, 3, 2}, {0, 1, 2, 4} }, { {3, 3, 3, 2, 3}, {0, 2, 3, 4} } | ||
| }; | ||
| for (size_t c = 0; c < sizeof(cases) / sizeof(cases[0]); c++) | ||
| { |
There was a problem hiding this comment.
the multiple (sizes, axes) cases are in a for loop inside one TEST. Please pass them as parameters using TEST_P for each case.
There was a problem hiding this comment.
Done in dca3d75. The loop is now a TEST_P (Layer_Test_Reduce_MultiAxes) with each (sizes, axes) pair as its own INSTANTIATE_TEST_CASE_P value. All six cases are kept; each fails without the reduce_layer.cpp change and passes with it.
Pass each (sizes, axes) case to TEST_P instead of looping inside one TEST.
There was a problem hiding this comment.
Status: 🟡 Yellow
The update converts all six multi-axis Reduce cases into separately reported parameterized tests without changing their reference calculation or coverage. This resolves the outstanding review request, and I found no remaining actionable issue in the delta. Exact-head CI and the re-enabled ONNX conformance case have not run yet.
Port of #30108 (5.x,
reduce2_layer.cpp, byofficialarghya29) to the 4.x Reduce layer. The 5.xreduce_layer.cppstill has the same lines (about 376 and 392) and the same bug.The Reduce layer returns wrong values when it reduces three or more axes, for example
ReduceMean(axes=[0, 2, 3])of an NCHW tensor.ReduceInvokerwalks the reduced block with an odometer over all reduced axes but the innermost one. On a carry it assigned the step of the next outer axis (current_step = steps_src[reduced_axes[j - 1]]) instead of removing the offset of the axis that wrapped and adding one step of the next.projected_stepsis shared by every output element, so once an outer reduced axis has three or more elements the outputs are wrong. The loop for the unreduced axes just below already carries this way; the same lines are used.Layer_Test_Reduce.three_reduced_axessums random tensors (4-D with four sets of three reduced axes, and two 5-D cases with four reduced axes) and compares with a straightforward reference. It fails on 4.x without the change (case 0: normInf 14.6) and passes with it. The ONNX conformance casetest_mvn_expanded(a MeanVarianceNormalization decomposed into ReduceMean over three axes) fails on 4.x and passes with the change, so it is removed fromtest_onnx_conformance_layer_parser_denylist.inl.hpp; I ran it with only its three files from opencv_extra. Other ONNX Reduce tests need opencv_extra and were not run.Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.