From 025e7602b9e0213652bbf90012489425c3059fc9 Mon Sep 17 00:00:00 2001 From: Yuantao Feng Date: Mon, 25 Mar 2024 14:47:28 +0800 Subject: [PATCH] Merge pull request #25166 from fengyuentau:fix_cann_gemm dnn (CANN): Fix incorrect shape of 1d bias in Gemm #25166 Gemm layer was refactored some time ago. Users found that the mobilenet example in https://github.com/opencv/opencv/wiki/Huawei-CANN-Backend does not work because of incorrect shape set for 1d bias in Gemm. This PR resolves this issue. ### 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 --- modules/dnn/src/layers/gemm_layer.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/dnn/src/layers/gemm_layer.cpp b/modules/dnn/src/layers/gemm_layer.cpp index fd10e9acdf..bbbb10d2df 100644 --- a/modules/dnn/src/layers/gemm_layer.cpp +++ b/modules/dnn/src/layers/gemm_layer.cpp @@ -269,7 +269,12 @@ public: } // set inputs : bias auto mat_C = have_bias && const_C ? blobs.back() : Mat::zeros(1, 1, CV_32F); - auto op_const_C = std::make_shared(mat_C.data, mat_C.type(), shape(mat_C), cv::format("%s_b", name.c_str())); + auto shape_C = shape(mat_C); + if (real_ndims_C == 1) { + int dim = static_cast(mat_C.total()); + shape_C = std::vector{dim}; + } + auto op_const_C = std::make_shared(mat_C.data, mat_C.type(), shape_C, cv::format("%s_b", name.c_str())); op->set_input_bias(*(op_const_C->getOp())); op->update_input_desc_bias(*(op_const_C->getTensorDesc()));