mirror of
https://github.com/opencv/opencv.git
synced 2026-07-21 19:33:03 +04:00
Merge pull request #29172 from hcy11123323:op
Fast-path transposeND for identity and 2D transpose orders #29172 ### 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 - [ ] There is a reference to the original bug report and related work - [ ] 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 <!-- Note!!! If you are an automated agent, we have a special process for you: add 🤖🤖🤖 to the end of the PR title. --> This PR adds fast paths to cv::transposeND() for two common cases: - identity permutation: dispatch to copyTo() - 2D permutation {1, 0}: dispatch to transpose() All other permutations continue to use the existing generic ND implementation. Why: transposeND() currently falls back to the generic memcpy/index-update loop even for the common 2D transpose case, while OpenCV already has an optimized transpose() path. Reusing that path avoids unnecessary index arithmetic and improves performance for 2D inputs passed through transposeND(). Performance: [BinaryOpTest.transposeND/21 (1920x1080, 8UC3): 28.21 ms -> 0.66 ms (-97.7%)] [BinaryOpTest.transposeND/22 (1920x1080, 8UC4): 37.61 ms -> 0.88 ms (-97.7%)] [BinaryOpTest.transposeND/29 (1920x1080, 32FC1): 10.26 ms -> 0.75 ms (-92.7%)] Full transposeND perf subset: 6760 ms -> 592 ms (-91.2%)
This commit is contained in:
@@ -436,7 +436,22 @@ PERF_TEST_P_(BinaryOpTest, transpose2d)
|
||||
SANITY_CHECK_NOTHING();
|
||||
}
|
||||
|
||||
PERF_TEST_P_(BinaryOpTest, transposeND)
|
||||
static cv::Mat makeTransposeNDOutput(const cv::Mat& src, const std::vector<int>& order)
|
||||
{
|
||||
std::vector<int> new_sz(order.size());
|
||||
for (size_t i = 0; i < order.size(); ++i)
|
||||
new_sz[i] = src.size[order[i]];
|
||||
return Mat(static_cast<int>(new_sz.size()), new_sz.data(), src.type());
|
||||
}
|
||||
|
||||
static cv::Mat makeTransposeNDInput3D(Size sz, int type)
|
||||
{
|
||||
const int channels = CV_MAT_CN(type);
|
||||
int dims[] = { sz.height, sz.width, channels };
|
||||
return Mat(3, dims, CV_MAKETYPE(CV_MAT_DEPTH(type), 1));
|
||||
}
|
||||
|
||||
PERF_TEST_P_(BinaryOpTest, transposeND_identity_order)
|
||||
{
|
||||
Size sz = get<0>(GetParam());
|
||||
int type = get<1>(GetParam());
|
||||
@@ -444,14 +459,64 @@ PERF_TEST_P_(BinaryOpTest, transposeND)
|
||||
|
||||
std::vector<int> order(a.dims);
|
||||
std::iota(order.begin(), order.end(), 0);
|
||||
std::reverse(order.begin(), order.end());
|
||||
|
||||
std::vector<int> new_sz(a.dims);
|
||||
std::copy(a.size.p, a.size.p + a.dims, new_sz.begin());
|
||||
std::reverse(new_sz.begin(), new_sz.end());
|
||||
cv::Mat b = Mat(new_sz, type);
|
||||
cv::Mat b = makeTransposeNDOutput(a, order);
|
||||
|
||||
declare.in(a,WARMUP_RNG).out(b);
|
||||
declare.in(a, WARMUP_RNG).out(b);
|
||||
|
||||
TEST_CYCLE() cv::transposeND(a, order, b);
|
||||
|
||||
SANITY_CHECK_NOTHING();
|
||||
}
|
||||
|
||||
PERF_TEST_P_(BinaryOpTest, transposeND_2d_swap_order)
|
||||
{
|
||||
Size sz = get<0>(GetParam());
|
||||
int type = get<1>(GetParam());
|
||||
cv::Mat a = Mat(sz, type).reshape(1);
|
||||
|
||||
std::vector<int> order{1, 0};
|
||||
|
||||
cv::Mat b = makeTransposeNDOutput(a, order);
|
||||
|
||||
declare.in(a, WARMUP_RNG).out(b);
|
||||
|
||||
TEST_CYCLE() cv::transposeND(a, order, b);
|
||||
|
||||
SANITY_CHECK_NOTHING();
|
||||
}
|
||||
|
||||
|
||||
PERF_TEST_P_(BinaryOpTest, transposeND_generic_keep_tail_order)
|
||||
{
|
||||
Size sz = get<0>(GetParam());
|
||||
int type = get<1>(GetParam());
|
||||
cv::Mat a = makeTransposeNDInput3D(sz, type);
|
||||
|
||||
std::vector<int> order{1, 0, 2};
|
||||
|
||||
cv::Mat b = makeTransposeNDOutput(a, order);
|
||||
|
||||
randu(a, 0, 255);
|
||||
declare.in(a).out(b);
|
||||
|
||||
TEST_CYCLE() cv::transposeND(a, order, b);
|
||||
|
||||
SANITY_CHECK_NOTHING();
|
||||
}
|
||||
|
||||
PERF_TEST_P_(BinaryOpTest, transposeND_generic_move_tail_order)
|
||||
{
|
||||
Size sz = get<0>(GetParam());
|
||||
int type = get<1>(GetParam());
|
||||
cv::Mat a = makeTransposeNDInput3D(sz, type);
|
||||
|
||||
std::vector<int> order{2, 0, 1};
|
||||
|
||||
cv::Mat b = makeTransposeNDOutput(a, order);
|
||||
|
||||
randu(a, 0, 255);
|
||||
declare.in(a).out(b);
|
||||
|
||||
TEST_CYCLE() cv::transposeND(a, order, b);
|
||||
|
||||
|
||||
@@ -551,6 +551,33 @@ void transposeND(InputArray src_, const std::vector<int>& order, OutputArray dst
|
||||
CV_CheckEQ(inp.channels(), 1, "Input array should be single-channel");
|
||||
CV_CheckEQ(order.size(), static_cast<size_t>(inp.dims), "Number of dimensions shouldn't change");
|
||||
|
||||
bool isIdentityOrder = true;
|
||||
for (size_t i = 0; i < order.size(); ++i)
|
||||
{
|
||||
if (order[i] != static_cast<int>(i))
|
||||
{
|
||||
isIdentityOrder = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isIdentityOrder)
|
||||
{
|
||||
dst_.create(inp.dims, inp.size.p, inp.type());
|
||||
Mat out = dst_.getMat();
|
||||
CV_Assert(out.isContinuous());
|
||||
|
||||
if (inp.data != out.data)
|
||||
inp.copyTo(out);
|
||||
return;
|
||||
}
|
||||
|
||||
const bool is2DSwap = inp.dims == 2 && order.size() == 2 && order[0] == 1 && order[1] == 0;
|
||||
if (is2DSwap)
|
||||
{
|
||||
transpose(inp, dst_);
|
||||
return;
|
||||
}
|
||||
|
||||
auto order_ = order;
|
||||
std::sort(order_.begin(), order_.end());
|
||||
for (size_t i = 0; i < order_.size(); ++i)
|
||||
|
||||
Reference in New Issue
Block a user