From 0e2a3686c0e5ee289f9de45b80ff06e16d8d9228 Mon Sep 17 00:00:00 2001 From: Smirnov Egor Date: Tue, 30 Nov 2021 12:20:35 +0300 Subject: [PATCH] add alpha parameter to ELU layer --- modules/dnn/include/opencv2/dnn/all_layers.hpp | 2 ++ modules/dnn/src/layers/elementwise_layers.cpp | 18 ++++++++++++++---- modules/dnn/src/opencl/activations.cl | 5 +++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/modules/dnn/include/opencv2/dnn/all_layers.hpp b/modules/dnn/include/opencv2/dnn/all_layers.hpp index e92ce2f565..991abba03a 100644 --- a/modules/dnn/include/opencv2/dnn/all_layers.hpp +++ b/modules/dnn/include/opencv2/dnn/all_layers.hpp @@ -453,6 +453,8 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN class CV_EXPORTS ELULayer : public ActivationLayer { public: + float alpha; + static Ptr create(const LayerParams ¶ms); }; diff --git a/modules/dnn/src/layers/elementwise_layers.cpp b/modules/dnn/src/layers/elementwise_layers.cpp index d8f0b654d3..21838fbe49 100644 --- a/modules/dnn/src/layers/elementwise_layers.cpp +++ b/modules/dnn/src/layers/elementwise_layers.cpp @@ -740,6 +740,9 @@ const char* const SigmoidFunctor::BaseDefaultFunctor::ocl_kernel struct ELUFunctor : public BaseDefaultFunctor { typedef ELULayer Layer; + float alpha; + + explicit ELUFunctor(float alpha_ = 1.f) : alpha(alpha_) {} bool supportBackend(int backendId, int) { @@ -749,14 +752,19 @@ struct ELUFunctor : public BaseDefaultFunctor inline float calculate(float x) const { - return x >= 0.f ? x : exp(x) - 1.f; + return x >= 0.f ? x : alpha * (exp(x) - 1.f); + } + + inline void setKernelParams(ocl::Kernel& kernel) const + { + kernel.set(3, alpha); } #ifdef HAVE_HALIDE void attachHalide(const Halide::Expr& input, Halide::Func& top) { Halide::Var x("x"), y("y"), c("c"), n("n"); - top(x, y, c, n) = select(input >= 0.0f, input, exp(input) - 1); + top(x, y, c, n) = select(input >= 0.0f, input, alpha * (exp(input) - 1)); } #endif // HAVE_HALIDE @@ -770,7 +778,7 @@ struct ELUFunctor : public BaseDefaultFunctor #ifdef HAVE_DNN_NGRAPH std::shared_ptr initNgraphAPI(const std::shared_ptr& node) { - return std::make_shared(node, 1.0); + return std::make_shared(node, alpha); } #endif // HAVE_DNN_NGRAPH @@ -1263,8 +1271,10 @@ Ptr SigmoidLayer::create(const LayerParams& params) Ptr ELULayer::create(const LayerParams& params) { - Ptr l(new ElementWiseLayer(ELUFunctor())); + float alpha = params.get("alpha", 1.0f); + Ptr l(new ElementWiseLayer(ELUFunctor(alpha))); l->setParamsFrom(params); + l->alpha = alpha; return l; } diff --git a/modules/dnn/src/opencl/activations.cl b/modules/dnn/src/opencl/activations.cl index 68f0dd7268..a75370d1bd 100644 --- a/modules/dnn/src/opencl/activations.cl +++ b/modules/dnn/src/opencl/activations.cl @@ -131,13 +131,14 @@ __kernel void PowForward(const int n, __global const T* in, __global T* out, out[index] = pow(shift + scale * in[index], power); } -__kernel void ELUForward(const int n, __global const T* in, __global T* out) +__kernel void ELUForward(const int n, __global const T* in, __global T* out, + const KERNEL_ARG_DTYPE alpha) { int index = get_global_id(0); if (index < n) { T src = in[index]; - out[index] = (src >= 0.f) ? src : exp(src) - 1; + out[index] = (src >= 0.f) ? src : alpha * (exp(src) - 1); } } -- GitLab