Merge pull request #25208 from Abdurrahheem:ash/0D-fullyConnected-test

Fully connected 0D test. #25208

This PR introduces parametrized `0/1D` input support test for `Fullyconnected` layer.

### 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
- [x] There is a reference to the original bug report and related work
- [x] 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
This commit is contained in:
Abduragim Shtanchaev 2024-04-15 09:15:36 +03:00 committed by GitHub
parent 282c762ead
commit 869016d8b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 2 deletions

View File

@ -167,10 +167,11 @@ public:
cAxis = normalize_axis(axis, inputsTmp[0]);
}
MatShape outShape(cAxis + 1);
MatShape outShape((!inputs[0].empty()) ? cAxis + 1 : cAxis);
for (int i = 0; i < cAxis; ++i)
outShape[i] = inputsTmp[0][i];
outShape.back() = numOutput;
if (!inputs[0].empty())
outShape.back() = numOutput;
outputs.resize(1, outShape);
return false;

View File

@ -567,4 +567,40 @@ INSTANTIATE_TEST_CASE_P(/*nothing*/, Layer_Slice_Test,
std::vector<int>({1, 4})
));
typedef testing::TestWithParam<tuple<std::vector<int>>> Layer_FullyConnected_Test;
TEST_P(Layer_FullyConnected_Test, Accuracy_01D)
{
LayerParams lp;
lp.type = "InnerProduct";
lp.name = "InnerProductLayer";
lp.set("num_output", 1);
lp.set("bias_term", false);
lp.set("axis", 0);
std::vector<int> input_shape = get<0>(GetParam());
RNG& rng = TS::ptr()->get_rng();
float inp_value = rng.uniform(0.0, 10.0);
Mat weights(std::vector<int>{total(input_shape), 1}, CV_32F, inp_value);
lp.blobs.push_back(weights);
Ptr<Layer> layer = LayerFactory::createLayerInstance("InnerProduct", lp);
Mat input(input_shape.size(), input_shape.data(), CV_32F);
randn(input, 0, 1);
Mat output_ref = input.reshape(1, 1) * weights;
output_ref.dims = 1;
std::vector<Mat> inputs{input};
std::vector<Mat> outputs;
runLayer(layer, inputs, outputs);
normAssert(output_ref, outputs[0]);
}
INSTANTIATE_TEST_CASE_P(/*nothting*/, Layer_FullyConnected_Test,
testing::Values(
std::vector<int>({}),
std::vector<int>({1}),
std::vector<int>({4})
));
}}