From 12aa0fe89819c02db53677c5749771a1007f623e Mon Sep 17 00:00:00 2001 From: Dhanwanth1803 <147172285+Dhanwanth1803@users.noreply.github.com> Date: Fri, 16 Feb 2024 20:49:29 +0530 Subject: [PATCH] Merge pull request #24985 from Dhanwanth1803:hardswish Fixes #24974 support HardSwishInt8 #24985 As given very clearly in the issue #24974 I made the required 2 changes to implement HardSwish Layer in INT8. Requesting comments. resolves https://github.com/opencv/opencv/issues/24974 - [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 - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake Co-authored-by: Dhanwanth1803 --- modules/dnn/src/init.cpp | 1 + modules/dnn/src/int8layers/elementwise_layers.cpp | 2 ++ modules/dnn/src/tflite/tflite_importer.cpp | 2 ++ 3 files changed, 5 insertions(+) diff --git a/modules/dnn/src/init.cpp b/modules/dnn/src/init.cpp index 2170aafc4b..e8450c18f9 100644 --- a/modules/dnn/src/init.cpp +++ b/modules/dnn/src/init.cpp @@ -212,6 +212,7 @@ void initializeLayerFactory() CV_DNN_REGISTER_LAYER_CLASS(SigmoidInt8, ActivationLayerInt8); CV_DNN_REGISTER_LAYER_CLASS(TanHInt8, ActivationLayerInt8); CV_DNN_REGISTER_LAYER_CLASS(SwishInt8, ActivationLayerInt8); + CV_DNN_REGISTER_LAYER_CLASS(HardSwishInt8, ActivationLayerInt8); CV_DNN_REGISTER_LAYER_CLASS(MishInt8, ActivationLayerInt8); CV_DNN_REGISTER_LAYER_CLASS(ELUInt8, ActivationLayerInt8); CV_DNN_REGISTER_LAYER_CLASS(BNLLInt8, ActivationLayerInt8); diff --git a/modules/dnn/src/int8layers/elementwise_layers.cpp b/modules/dnn/src/int8layers/elementwise_layers.cpp index 2f40a4039f..5c533840f3 100644 --- a/modules/dnn/src/int8layers/elementwise_layers.cpp +++ b/modules/dnn/src/int8layers/elementwise_layers.cpp @@ -267,6 +267,8 @@ public: res = std::make_shared(input, 1.0f); } else if (type == "MishInt8") { res = std::make_shared(input); + } else if (type == "HardSwishInt8") { + res = std::make_shared(input); } else if (type == "AbsValInt8") { res = std::make_shared(input); } else if (type == "SigmoidInt8") { diff --git a/modules/dnn/src/tflite/tflite_importer.cpp b/modules/dnn/src/tflite/tflite_importer.cpp index ed51b4461a..f0e1546306 100644 --- a/modules/dnn/src/tflite/tflite_importer.cpp +++ b/modules/dnn/src/tflite/tflite_importer.cpp @@ -939,6 +939,8 @@ void TFLiteImporter::parseActivation(const Operator& op, const std::string& opco y = std::min(std::max(x, 0.f), 6.f); else if (opcode == "LOGISTIC") y = 1.0f / (1.0f + std::exp(-x)); + else if (opcode == "HARD_SWISH") + y = x * max(0.f, min(1.f, x / 6.f + 0.5f)); else CV_Error(Error::StsNotImplemented, "Lookup table for " + opcode);