Skip to content

Commit

Permalink
refactor the perf test for softmax
Browse files Browse the repository at this point in the history
  • Loading branch information
WanliZhong committed Oct 30, 2023
1 parent 81b433d commit cbf0474
Showing 1 changed file with 35 additions and 44 deletions.
79 changes: 35 additions & 44 deletions modules/dnn/perf/perf_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,58 +707,49 @@ INSTANTIATE_TEST_CASE_P(/**/, Layer_FullyConnected, Combine(
dnnBackendsAndTargets()
));

struct Layer_Softmax : public TestBaseWithParam<tuple<Backend, Target> >
{
void test_layer(const std::vector<int>& shape, int axis)
{
int backendId = get<0>(GetParam());
int targetId = get<1>(GetParam());

Mat data(shape, CV_32FC1);
Scalar mean = 0.f;
Scalar std = 1.f;
randn(data, mean, std);
typedef TestBaseWithParam<tuple<std::vector<int>, int, tuple<Backend, Target> > > Layer_Softmax;

Net net;
LayerParams lp;
lp.type = "Softmax";
lp.name = "testLayer";
lp.set("axis", axis);
PERF_TEST_P_(Layer_Softmax, softmax_3d) {
std::vector<int> shape = get<0>(GetParam());
int axis = get<1>(GetParam());
int backendId = get<0>(get<2>(GetParam()));
int targetId = get<1>(get<2>(GetParam()));

net.addLayerToPrev(lp.name, lp.type, lp);
// warmup
{
net.setInput(data);
net.setPreferableBackend(backendId);
net.setPreferableTarget(targetId);
Mat out = net.forward();
}
Mat data(shape, CV_32FC1);
Scalar mean = 0.f;
Scalar std = 1.f;
randn(data, mean, std);

TEST_CYCLE()
{
Mat res = net.forward();
}
Net net;
LayerParams lp;
lp.type = "Softmax";
lp.name = "testLayer";
lp.set("axis", axis);

SANITY_CHECK_NOTHING();
net.addLayerToPrev(lp.name, lp.type, lp);
// warmup
{
net.setInput(data);
net.setPreferableBackend(backendId);
net.setPreferableTarget(targetId);
Mat out = net.forward();
}
};

TEST_CYCLE() {
Mat res = net.forward();
}

PERF_TEST_P_(Layer_Softmax, Softmax_small)
{
test_layer({16, 50, 50}, 2);
}

PERF_TEST_P_(Layer_Softmax, Softmax_middle)
{
test_layer({16, 197, 197}, 2);
}

PERF_TEST_P_(Layer_Softmax, Softmax_large)
{
test_layer({16, 1080, 1920, 3}, 2);
SANITY_CHECK_NOTHING();
}

INSTANTIATE_TEST_CASE_P(/**/, Layer_Softmax, testing::Values(std::make_tuple(DNN_BACKEND_OPENCV, DNN_TARGET_CPU)));
INSTANTIATE_TEST_CASE_P(/**/, Layer_Softmax, Combine(
Values( // input size
std::vector<int>({16, 50, 50}),
std::vector<int>({16, 197, 197}),
std::vector<int>({16, 1024, 1024})
),
Values(0, 1, 2), // axis
dnnBackendsAndTargets(false, false, false, false, false, false, false, false) // only CPU
));

} // namespace

0 comments on commit cbf0474

Please sign in to comment.