Files
opencv-MIRROR/modules/imgproc/perf/perf_resize_params.cpp
Abhishek Gola cc07c72547 Add Batch Resize Support (#29830)
batch resize support
2026-12-01 23:18:38 +05:30

194 lines
7.5 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.
#include "perf_precomp.hpp"
namespace opencv_test {
// ResizeCoord as an int, so the report lists the mode next to its timing:
// 0 PIXEL_CENTER, 1 HALF_PIXEL, 2 PYTORCH_HALF_PIXEL, 3 ALIGN_CORNERS,
// 4 ASYMMETRIC, 5 TF_HALF_PIXEL_FOR_NN, 6 HALF_PIXEL_SYMMETRIC.
typedef tuple<MatType, int, int> Type_Interp_Coord_t;
typedef TestBaseWithParam<Type_Interp_Coord_t> ResizeParams_CoordMode;
// The modes only change tables; an outlier means a mode left the fast path.
PERF_TEST_P(ResizeParams_CoordMode, coord_modes_1080p_to_VGA,
testing::Values(
Type_Interp_Coord_t(CV_8UC3, INTER_LINEAR, 1),
Type_Interp_Coord_t(CV_8UC3, INTER_LINEAR, 3),
Type_Interp_Coord_t(CV_8UC3, INTER_NEAREST, 4),
Type_Interp_Coord_t(CV_32FC1, INTER_CUBIC, 1),
Type_Interp_Coord_t(CV_32FC1, INTER_CUBIC, 6),
Type_Interp_Coord_t(CV_32FC1, INTER_LANCZOS4, 2)))
{
const int matType = get<0>(GetParam());
const int interp = get<1>(GetParam());
const int coord = get<2>(GetParam());
Mat src(sz1080p, matType), dst(szVGA, matType);
RNG(0x5eed).fill(src, RNG::UNIFORM, 0, 255);
declare.in(src).out(dst);
ResizeParams params(szVGA, 0, 0, interp);
params.coordMode = (ResizeCoord)coord;
TEST_CYCLE() resize(src, dst, params);
SANITY_CHECK_NOTHING();
}
typedef TestBaseWithParam<tuple<MatType, int> > ResizeParams_NearestMode;
// nearest_mode picks the rounding rule while the table is built.
PERF_TEST_P(ResizeParams_NearestMode, nearest_modes_1080p_to_VGA,
testing::Values(
make_tuple(MatType(CV_8UC3), (int)ResizeNearest::ROUND_PREFER_FLOOR),
make_tuple(MatType(CV_16UC1), (int)ResizeNearest::FLOOR)))
{
const int matType = get<0>(GetParam());
const int round = get<1>(GetParam());
Mat src(sz1080p, matType), dst(szVGA, matType);
RNG(0x5eed).fill(src, RNG::UNIFORM, 0, 255);
declare.in(src).out(dst);
ResizeParams params(szVGA, 0, 0, INTER_NEAREST);
params.coordMode = ResizeCoord::HALF_PIXEL;
params.nearestMode = (ResizeNearest)round;
TEST_CYCLE() resize(src, dst, params);
SANITY_CHECK_NOTHING();
}
typedef TestBaseWithParam<bool> ResizeParams_ExcludeOutside;
// exclude_outside only zeroes and renormalises weights in the table, so it is free at run time.
PERF_TEST_P(ResizeParams_ExcludeOutside, exclude_outside_cubic_1080p_to_VGA, testing::Bool())
{
const bool exclude = GetParam();
Mat src(sz1080p, CV_8UC3), dst(szVGA, CV_8UC3);
RNG(0x5eed).fill(src, RNG::UNIFORM, 0, 255);
declare.in(src).out(dst);
ResizeParams params(szVGA, 0, 0, INTER_CUBIC);
params.coordMode = ResizeCoord::HALF_PIXEL;
params.excludeOutside = exclude;
TEST_CYCLE() resize(src, dst, params);
SANITY_CHECK_NOTHING();
}
// RAGGED cycles input widths 64 to 4096, which only a vector can hold.
enum BatchLayout { TENSOR, VECTOR, RAGGED };
typedef tuple<int, MatType, Size, Size, int, int> BatchCase_t; // N, type, from, to, interpolation, layout
// The first six hold equal total pixels, so throughput should stay level across N.
#define RESIZE_BATCH_CASES \
testing::Values( \
BatchCase_t(1, CV_8UC3, Size(2048, 2048), Size(1536, 1536), INTER_LINEAR, TENSOR), \
BatchCase_t(2, CV_8UC3, Size(2048, 1024), Size(1536, 768), INTER_LINEAR, VECTOR), \
BatchCase_t(4, CV_8UC3, Size(1024, 1024), Size(768, 768), INTER_LINEAR, TENSOR), \
BatchCase_t(16, CV_8UC3, Size(512, 512), Size(384, 384), INTER_LINEAR, VECTOR), \
BatchCase_t(256, CV_8UC3, Size(128, 128), Size(96, 96), INTER_LINEAR, TENSOR), \
BatchCase_t(4096, CV_8UC3, Size(32, 32), Size(24, 24), INTER_LINEAR, VECTOR), \
BatchCase_t(64, CV_32FC1, Size(128, 128), Size(224, 224), INTER_LINEAR, TENSOR), \
BatchCase_t(3, CV_8UC3, Size(1920, 1080), Size(640, 480), INTER_CUBIC, VECTOR), \
BatchCase_t(128, CV_32FC1, Size(224, 224), Size(112, 112), INTER_NEAREST, TENSOR), \
BatchCase_t(4, CV_8UC3, Size(2048, 2048), Size(224, 224), INTER_AREA, VECTOR), \
BatchCase_t(32, CV_8UC3, Size(0, 256), Size(0, 192), INTER_LINEAR, RAGGED) \
)
static void makeBatch(const BatchCase_t& p, std::vector<Mat>& src, std::vector<Mat>& dst)
{
const int n = get<0>(p), matType = get<1>(p), layout = get<5>(p);
const Size from = get<2>(p), to = get<3>(p);
static const int raggedWidths[] = { 64, 256, 1024, 4096 };
RNG rng(0x5eed);
src.resize(n);
dst.resize(n);
for (int i = 0; i < n; i++)
{
const int w = layout == RAGGED ? raggedWidths[i % 4] : from.width;
src[i].create(from.height, w, matType);
rng.fill(src[i], RNG::UNIFORM, 0, 255);
dst[i].create(to.height, layout == RAGGED ? w*3/4 : to.width, matType);
}
}
// Resizes images [r.start, r.end) one by one, as a caller's own parallel loop does.
class PerImageResize : public ParallelLoopBody
{
public:
PerImageResize(const std::vector<Mat>& _src, std::vector<Mat>& _dst, int _interp)
: src(_src), dst(_dst), interp(_interp) {}
void operator()(const Range& r) const CV_OVERRIDE
{
for (int i = r.start; i < r.end; i++)
resize(src[i], dst[i], dst[i].size(), 0, 0, interp);
}
private:
const std::vector<Mat>& src;
std::vector<Mat>& dst;
int interp;
};
typedef TestBaseWithParam<BatchCase_t> ResizeBatch;
// Baseline: what callers write today, one cv::resize per image, spread over the threads.
PERF_TEST_P(ResizeBatch, per_image, RESIZE_BATCH_CASES)
{
const int n = get<0>(GetParam()), interp = get<4>(GetParam());
std::vector<Mat> src, dst;
makeBatch(GetParam(), src, dst);
const PerImageResize body(src, dst, interp);
TEST_CYCLE()
{
if (n == 1)
resize(src[0], dst[0], dst[0].size(), 0, 0, interp);
else
parallel_for_(Range(0, n), body);
}
SANITY_CHECK_NOTHING();
}
// The batched overload: shared tables, one parallel loop over the batch.
PERF_TEST_P(ResizeBatch, batched, RESIZE_BATCH_CASES)
{
const int n = get<0>(GetParam()), matType = get<1>(GetParam()), interp = get<4>(GetParam());
const int layout = get<5>(GetParam());
const Size from = get<2>(GetParam()), to = get<3>(GetParam());
if (layout == TENSOR)
{
int shape[] = { n, from.height, from.width };
Mat src(3, shape, matType), dst;
RNG(0x5eed).fill(src, RNG::UNIFORM, 0, 255);
const ResizeParams params(to, 0, 0, interp);
TEST_CYCLE() resize(src, dst, params);
}
else
{
std::vector<Mat> src, dst;
makeBatch(GetParam(), src, dst);
// A ragged batch gets one output size per image only through the scale.
const ResizeParams params = layout == RAGGED ? ResizeParams(Size(), 0.75, (double)to.height/from.height, interp)
: ResizeParams(to, 0, 0, interp);
TEST_CYCLE() resize(src, dst, params);
}
SANITY_CHECK_NOTHING();
}
} // namespace