mirror of
https://github.com/opencv/opencv.git
synced 2026-07-29 23:33:05 +04:00
implementation of scatter and scatternd with conformance tests enabled
This commit is contained in:
@@ -239,7 +239,178 @@ PERF_TEST_P_(Layer_Slice, FastNeuralStyle_eccv16)
|
||||
test_slice<4>(inputShape, begin, end);
|
||||
}
|
||||
|
||||
struct Layer_Scatter : public TestBaseWithParam<tuple<Backend, Target> >
|
||||
{
|
||||
void test_layer(const std::vector<int>& shape, const String reduction = "none", int axis = 0)
|
||||
{
|
||||
int backendId = get<0>(GetParam());
|
||||
int targetId = get<1>(GetParam());
|
||||
|
||||
Mat data(shape, CV_32FC1);
|
||||
Mat indices(shape, CV_32FC1);
|
||||
Mat updates(shape, CV_32FC1);
|
||||
|
||||
Scalar mean = 0.f;
|
||||
Scalar std = 1.f;
|
||||
randn(data, mean, std);
|
||||
randu(indices, 0, shape[axis]);
|
||||
randn(updates, mean, std);
|
||||
|
||||
indices.convertTo(indices, CV_32SC1, 1, -1);
|
||||
|
||||
Net net;
|
||||
LayerParams lp;
|
||||
lp.type = "Scatter";
|
||||
lp.name = "testLayer";
|
||||
lp.set("reduction", reduction);
|
||||
lp.set("axis", axis);
|
||||
|
||||
int id = net.addLayerToPrev(lp.name, lp.type, lp);
|
||||
net.connect(0, 0, id, 0);
|
||||
net.connect(0, 1, id, 1);
|
||||
net.connect(0, 2, id, 2);
|
||||
|
||||
// warmup
|
||||
{
|
||||
std::vector<String> inpNames(3);
|
||||
inpNames[0] = "data";
|
||||
inpNames[1] = "indices";
|
||||
inpNames[2] = "updates";
|
||||
net.setInputsNames(inpNames);
|
||||
net.setInput(data, inpNames[0]);
|
||||
net.setInput(indices, inpNames[1]);
|
||||
net.setInput(updates, inpNames[2]);
|
||||
|
||||
net.setPreferableBackend(backendId);
|
||||
net.setPreferableTarget(targetId);
|
||||
Mat out = net.forward();
|
||||
}
|
||||
|
||||
TEST_CYCLE()
|
||||
{
|
||||
Mat res = net.forward();
|
||||
}
|
||||
|
||||
SANITY_CHECK_NOTHING();
|
||||
}
|
||||
|
||||
int N = 8;
|
||||
int C = 256;
|
||||
int H = 128;
|
||||
int W = 100;
|
||||
};
|
||||
|
||||
PERF_TEST_P_(Layer_Scatter, DISABLED_Scatter)
|
||||
{
|
||||
test_layer({N, C, H, W});
|
||||
}
|
||||
|
||||
PERF_TEST_P_(Layer_Scatter, DISABLED_Scatter_add)
|
||||
{
|
||||
test_layer({N, C, H, W}, "add");
|
||||
}
|
||||
|
||||
struct Layer_ScatterND : public TestBaseWithParam<tuple<Backend, Target> >
|
||||
{
|
||||
void test_layer(const std::vector<int>& shape, const String reduction = "none")
|
||||
{
|
||||
int backendId = get<0>(GetParam());
|
||||
int targetId = get<1>(GetParam());
|
||||
|
||||
std::vector<int> indices_shape(shape);
|
||||
indices_shape.push_back(int(shape.size()));
|
||||
Mat data(shape, CV_32FC1);
|
||||
Mat indices(indices_shape, CV_32FC1);
|
||||
Mat updates(shape, CV_32FC1);
|
||||
|
||||
Scalar mean = 0.f;
|
||||
Scalar std = 1.f;
|
||||
randn(data, mean, std);
|
||||
randn(updates, mean, std);
|
||||
|
||||
// initialize the indices with index tuples like [0...N, 0...C, 0...H, 0...W]
|
||||
std::vector<int> current_index_tuple(shape.size());
|
||||
int total = data.total();
|
||||
std::vector<int> indices_step;
|
||||
for (int i = 0; i < indices.dims; i++)
|
||||
{
|
||||
int step = indices.step.p[i] / sizeof(float);
|
||||
indices_step.push_back(step);
|
||||
}
|
||||
int t, j, idx, offset_at_idx, offset;
|
||||
for (int i = 0; i < total; i++)
|
||||
{
|
||||
t = i;
|
||||
for (j = shape.size() - 1; j >= 0; j--)
|
||||
{
|
||||
idx = t / shape[j];
|
||||
offset_at_idx = (int)(t - idx * shape[j]);
|
||||
current_index_tuple[j] = offset_at_idx;
|
||||
t = idx;
|
||||
}
|
||||
|
||||
offset = 0;
|
||||
for (j = 0; j < shape.size(); j++)
|
||||
offset += current_index_tuple[j] * indices_step[j];
|
||||
|
||||
for (j = 0; j < shape.size(); j++)
|
||||
indices.at<float>(offset + j) = current_index_tuple[j];
|
||||
}
|
||||
|
||||
Net net;
|
||||
LayerParams lp;
|
||||
lp.type = "ScatterND";
|
||||
lp.name = "testLayer";
|
||||
lp.set("reduction", reduction);
|
||||
|
||||
int id = net.addLayerToPrev(lp.name, lp.type, lp);
|
||||
net.connect(0, 0, id, 0);
|
||||
net.connect(0, 1, id, 1);
|
||||
net.connect(0, 2, id, 2);
|
||||
|
||||
// warmup
|
||||
{
|
||||
std::vector<String> inpNames(3);
|
||||
inpNames[0] = "data";
|
||||
inpNames[1] = "indices";
|
||||
inpNames[2] = "updates";
|
||||
net.setInputsNames(inpNames);
|
||||
net.setInput(data, inpNames[0]);
|
||||
net.setInput(indices, inpNames[1]);
|
||||
net.setInput(updates, inpNames[2]);
|
||||
|
||||
net.setPreferableBackend(backendId);
|
||||
net.setPreferableTarget(targetId);
|
||||
Mat out = net.forward();
|
||||
}
|
||||
|
||||
TEST_CYCLE()
|
||||
{
|
||||
Mat res = net.forward();
|
||||
}
|
||||
|
||||
SANITY_CHECK_NOTHING();
|
||||
}
|
||||
|
||||
int N = 8;
|
||||
int C = 256;
|
||||
int H = 128;
|
||||
int W = 100;
|
||||
};
|
||||
|
||||
PERF_TEST_P_(Layer_ScatterND, DISABLED_ScatterND)
|
||||
{
|
||||
test_layer({N, C, H ,W});
|
||||
}
|
||||
|
||||
PERF_TEST_P_(Layer_ScatterND, DISABLED_ScatterND_add)
|
||||
{
|
||||
test_layer({N, C, H , W}, "add");
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Layer_Slice, dnnBackendsAndTargets(false, false));
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Layer_NaryEltwise, testing::Values(std::make_tuple(DNN_BACKEND_OPENCV, DNN_TARGET_CPU)));
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Layer_Scatter, testing::Values(std::make_tuple(DNN_BACKEND_OPENCV, DNN_TARGET_CPU)));
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Layer_ScatterND, testing::Values(std::make_tuple(DNN_BACKEND_OPENCV, DNN_TARGET_CPU)));
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user