mirror of
https://github.com/opencv/opencv.git
synced 2026-11-20 08:15:26 -06:00
core: reduceND, used by the DNN Reduce2 layer - #30157 Split out of #30082, which now carries only the resize half. Adds `cv::reduceND`, which reduces an n-dimensional array over a set of axes with any of the `ReduceTypes`, extended with `REDUCE_PROD`, `REDUCE_L1`, `REDUCE_L2`, `REDUCE_LOG_SUM` and `REDUCE_LOG_SUM_EXP`. `cv::reduce` accepts n-dimensional input through it. `sum`, `mean` and `minMaxIdx` now share one parallel driver (`stat_parallel.hpp`), so they parallelize over large arrays the same way `reduceND` does. The new graph engine's `Reduce2` layer calls `cv::reduceND` instead of its own kernels, so `reduce2_kernels.simd.hpp` and the hand-written reduction loops in `reduce2_layer.cpp` go away. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
55 lines
2.4 KiB
C++
55 lines
2.4 KiB
C++
// This file is part of OpenCV project.
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
// of this distribution and at http://opencv.org/license.html.
|
|
// Copyright (C) 2026, BigVision LLC, all rights reserved.
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
// Reduce2 (a layer of the new graph engine), called directly.
|
|
|
|
#include "perf_precomp.hpp"
|
|
|
|
namespace opencv_test {
|
|
|
|
typedef tuple<std::vector<int>, std::vector<int>, std::string, perf::MatType> Reduce2Params;
|
|
typedef TestBaseWithParam<Reduce2Params> Layer_Reduce2;
|
|
|
|
PERF_TEST_P_(Layer_Reduce2, reduce)
|
|
{
|
|
const std::vector<int> shape = get<0>(GetParam()), axes = get<1>(GetParam());
|
|
const std::string op = get<2>(GetParam());
|
|
const int type = get<3>(GetParam());
|
|
|
|
LayerParams lp;
|
|
lp.type = "Reduce2";
|
|
lp.name = "testLayer";
|
|
lp.set("reduce", op);
|
|
lp.set("keepdims", true);
|
|
lp.set("axes", DictValue::arrayInt(axes.data(), (int)axes.size()));
|
|
Ptr<Layer> layer = LayerFactory::createLayerInstance("Reduce2", lp);
|
|
|
|
Mat input(shape, type);
|
|
randu(input, 0, 10);
|
|
std::vector<Mat> inputs{input}, outputs(1), internals;
|
|
layer->forward(inputs, outputs, internals);
|
|
TEST_CYCLE()
|
|
{
|
|
layer->forward(inputs, outputs, internals);
|
|
}
|
|
SANITY_CHECK_NOTHING();
|
|
}
|
|
|
|
INSTANTIATE_TEST_CASE_P(/**/, Layer_Reduce2, testing::Values(
|
|
make_tuple(std::vector<int>{1, 256, 80, 80}, std::vector<int>{1}, "mean", perf::MatType(CV_32F)),
|
|
make_tuple(std::vector<int>{1, 256, 80, 80}, std::vector<int>{2, 3}, "mean", perf::MatType(CV_32F)),
|
|
make_tuple(std::vector<int>{1, 64, 160, 160}, std::vector<int>{0, 1, 2, 3}, "sum", perf::MatType(CV_32F)),
|
|
make_tuple(std::vector<int>{32, 1024, 256}, std::vector<int>{0}, "max", perf::MatType(CV_32F)),
|
|
make_tuple(std::vector<int>{4096, 768}, std::vector<int>{1}, "l2", perf::MatType(CV_32F)),
|
|
make_tuple(std::vector<int>{64, 1000}, std::vector<int>{1}, "log_sum_exp", perf::MatType(CV_32F)),
|
|
make_tuple(std::vector<int>{1, 256, 80, 80}, std::vector<int>{1}, "sum", perf::MatType(CV_16F)),
|
|
make_tuple(std::vector<int>{1, 3, 640, 640}, std::vector<int>{2, 3}, "max", perf::MatType(CV_8U)),
|
|
make_tuple(std::vector<int>{1, 256, 80, 80}, std::vector<int>{1}, "sum", perf::MatType(CV_32S)),
|
|
make_tuple(std::vector<int>{8, 196, 768}, std::vector<int>{2}, "mean", perf::MatType(CV_32F))
|
|
));
|
|
|
|
} // namespace opencv_test
|