Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0/1D test for tile layer #25409

Merged
merged 2 commits into from May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 29 additions & 15 deletions modules/dnn/src/layers/tile_layer.cpp
Expand Up @@ -42,14 +42,20 @@ class TileLayerImpl CV_FINAL : public TileLayer
CV_CheckEQ(inputs.size(), 1ull, "Tile: one input is expected");

// repeats must have the same length as input's dimension number
// FIXIT: it breaks when the input is 1d tensor (represented as 2d mat with size=2 in opencv dnn)
CV_CheckEQ(inputs[0].size(), repeats.size(), "Tile: repeats must be a 1D tensor of the same length as input's dimension number");

outputs.assign(1, inputs[0]);
for (int i = 0; i < repeats.size(); i++)
{
outputs[0][i] *= repeats[i];
if (inputs[0].size() > 1) {
CV_CheckEQ(inputs[0].size(), repeats.size(), "Tile: repeats must be a 1D tensor of the same length as input's dimension number");
outputs.assign(1, inputs[0]);
for (int i = 0; i < repeats.size(); i++)
{
outputs[0][i] *= repeats[i];
}
} else {
CV_CheckGE((int)repeats.size(), 1, "Tile: Provide at least one repeat along any dimension");
outputs.assign(1, repeats);
if (inputs[0].size() == 1)
outputs[0][repeats.size() - 1] *= inputs[0][0];
}

return false;
}

Expand Down Expand Up @@ -79,18 +85,26 @@ class TileLayerImpl CV_FINAL : public TileLayer
MatShape out_shape = shape(out);
int rep_i, ndims = data.dims;
int dims = 1;
for (int i = 0; i < ndims; i++)
{
rep_i = repeats[i];
if (rep_i != 1)
if (ndims > 1){
for (int i = 0; i < ndims; i++)
{
rep_i = repeats[i];
if (rep_i != 1)
{
tmp = tmp.reshape(0, dims);
tmp = cv::repeat(tmp, 1, rep_i);
}
dims *= out_shape[i];
}
tmp = tmp.reshape(0, out_shape);
} else {
for (int i = 0; i < repeats.size(); i++){
tmp = tmp.reshape(0, dims);
tmp = cv::repeat(tmp, 1, rep_i);
tmp = cv::repeat(tmp, repeats[i], 1);
dims *= out_shape[i];
}
dims *= out_shape[i];
tmp = tmp.reshape(0, out_shape);
Comment on lines +88 to +106
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these changes imply we need to do something to cv::repeat so that it can be used in the same way regardless 1D or 2D mat?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be. @dkurt what do you think?

}
tmp = tmp.reshape(0, out_shape);

tmp.copyTo(out);
}

Expand Down
42 changes: 42 additions & 0 deletions modules/dnn/test/test_layers_1d.cpp
Expand Up @@ -682,6 +682,48 @@ INSTANTIATE_TEST_CASE_P(/*nothing*/, Layer_Const_Test, testing::Values(
std::vector<int>({4, 1})
));

typedef testing::TestWithParam<std::vector<int>> Layer_Tile_Test;
TEST_P(Layer_Tile_Test, Accuracy_01D){

std::vector<int> input_shape = GetParam();
std::vector<int> repeats = {2, 2};

LayerParams lp;
lp.type = "Tile";
lp.name = "TileLayer";
lp.set("repeats", DictValue::arrayInt(repeats.data(), repeats.size()));
Ptr<TileLayer> layer = TileLayer::create(lp);

cv::Mat input = cv::Mat(input_shape.size(), input_shape.data(), CV_32F);
cv::randn(input, 0, 1);

std::vector<Mat> inputs{input};
std::vector<Mat> outputs;

runLayer(layer, inputs, outputs);

// Manually create the expected output for verification
cv::Mat output_ref = input.clone();
for (int i = 0; i < repeats.size(); ++i) {
cv::Mat tmp;
cv::repeat(output_ref, (i == 0 ? repeats[i] : 1), (i == 1 ? repeats[i] : 1), tmp);
output_ref = tmp;
}

ASSERT_EQ(outputs.size(), 1);
ASSERT_EQ(shape(outputs[0]), shape(output_ref));
normAssert(output_ref, outputs[0]);

}
INSTANTIATE_TEST_CASE_P(/*nothing*/, Layer_Tile_Test,
/*input blob shape*/ testing::Values(
std::vector<int>({}),
std::vector<int>({2}),
std::vector<int>({2, 1}),
std::vector<int>({1, 2}),
std::vector<int>({2, 2})
));

typedef testing::TestWithParam<tuple<std::vector<int>, std::string>> Layer_Einsum_Test;
TEST_P(Layer_Einsum_Test, Accuracy_01D)
{
Expand Down