diff --git a/modules/dnn/src/layers/elementwise_layers.cpp b/modules/dnn/src/layers/elementwise_layers.cpp index 3459734a08..42b277838b 100644 --- a/modules/dnn/src/layers/elementwise_layers.cpp +++ b/modules/dnn/src/layers/elementwise_layers.cpp @@ -112,13 +112,18 @@ public: } }; - ElementWiseLayer(const Func &f=Func()) : run_parallel(false) { func = f; } + ElementWiseLayer(const Func &f=Func()) { func = f; } virtual bool supportBackend(int backendId) CV_OVERRIDE { return func.supportBackend(backendId, this->preferableTarget); } + virtual void finalize(InputArrayOfArrays, OutputArrayOfArrays) CV_OVERRIDE + { + func.finalize(); + } + virtual Ptr tryAttach(const Ptr& node) CV_OVERRIDE { switch (node->backendId) @@ -235,7 +240,6 @@ public: } Func func; - bool run_parallel; }; #ifdef HAVE_OPENCL @@ -250,7 +254,16 @@ static String oclGetTMacro(const UMat &m) } #endif -struct ReLUFunctor +struct BaseFunctor +{ + void finalize() {} + + bool tryFuse(Ptr&) { return false; } + + void getScaleShift(Mat&, Mat&) const {} +}; + +struct ReLUFunctor : public BaseFunctor { typedef ReLULayer Layer; float slope; @@ -376,14 +389,10 @@ struct ReLUFunctor } #endif // HAVE_DNN_NGRAPH - bool tryFuse(Ptr&) { return false; } - - void getScaleShift(Mat&, Mat&) const {} - int64 getFLOPSPerElement() const { return 1; } }; -struct ReLU6Functor +struct ReLU6Functor : public BaseFunctor { typedef ReLU6Layer Layer; float minValue, maxValue; @@ -486,14 +495,10 @@ struct ReLU6Functor } #endif // HAVE_DNN_NGRAPH - bool tryFuse(Ptr&) { return false; } - - void getScaleShift(Mat&, Mat&) const {} - int64 getFLOPSPerElement() const { return 2; } }; -struct TanHFunctor +struct TanHFunctor : public BaseFunctor { typedef TanHLayer Layer; @@ -565,14 +570,10 @@ struct TanHFunctor } #endif // HAVE_DNN_NGRAPH - bool tryFuse(Ptr&) { return false; } - - void getScaleShift(Mat&, Mat&) const {} - int64 getFLOPSPerElement() const { return 1; } }; -struct SwishFunctor +struct SwishFunctor : public BaseFunctor { typedef SwishLayer Layer; @@ -645,15 +646,10 @@ struct SwishFunctor } #endif // HAVE_DNN_NGRAPH - bool tryFuse(Ptr&) { return false; } - - void getScaleShift(Mat&, Mat&) const {} - int64 getFLOPSPerElement() const { return 3; } - }; -struct MishFunctor +struct MishFunctor : public BaseFunctor { typedef MishLayer Layer; @@ -731,15 +727,10 @@ struct MishFunctor } #endif // HAVE_DNN_NGRAPH - bool tryFuse(Ptr&) { return false; } - - void getScaleShift(Mat&, Mat&) const {} - int64 getFLOPSPerElement() const { return 3; } - }; -struct SigmoidFunctor +struct SigmoidFunctor : public BaseFunctor { typedef SigmoidLayer Layer; @@ -811,19 +802,13 @@ struct SigmoidFunctor } #endif // HAVE_DNN_NGRAPH - bool tryFuse(Ptr&) { return false; } - - void getScaleShift(Mat&, Mat&) const {} - int64 getFLOPSPerElement() const { return 3; } }; -struct ELUFunctor +struct ELUFunctor : public BaseFunctor { typedef ELULayer Layer; - explicit ELUFunctor() {} - bool supportBackend(int backendId, int) { return backendId == DNN_BACKEND_OPENCV || backendId == DNN_BACKEND_HALIDE || @@ -892,14 +877,10 @@ struct ELUFunctor } #endif // HAVE_DNN_NGRAPH - bool tryFuse(Ptr&) { return false; } - - void getScaleShift(Mat&, Mat&) const {} - int64 getFLOPSPerElement() const { return 2; } }; -struct AbsValFunctor +struct AbsValFunctor : public BaseFunctor { typedef AbsLayer Layer; @@ -977,14 +958,10 @@ struct AbsValFunctor } #endif // HAVE_DNN_NGRAPH - bool tryFuse(Ptr&) { return false; } - - void getScaleShift(Mat&, Mat&) const {} - int64 getFLOPSPerElement() const { return 1; } }; -struct BNLLFunctor +struct BNLLFunctor : public BaseFunctor { typedef BNLLLayer Layer; @@ -1057,23 +1034,19 @@ struct BNLLFunctor } #endif // HAVE_DNN_NGRAPH - bool tryFuse(Ptr&) { return false; } - - void getScaleShift(Mat&, Mat&) const {} - int64 getFLOPSPerElement() const { return 5; } }; -struct PowerFunctor +struct PowerFunctor : public BaseFunctor { typedef PowerLayer Layer; - float power; - float scale; - float shift; + float power, scale, shift; + float originPower, originScale, originShift; explicit PowerFunctor(float power_ = 1.f, float scale_ = 1.f, float shift_ = 0.f) - : power(power_), scale(scale_), shift(shift_) {} + : power(power_), scale(scale_), shift(shift_), + originPower(power_), originScale(scale_), originShift(shift_) {} bool supportBackend(int backendId, int targetId) { @@ -1083,6 +1056,13 @@ struct PowerFunctor return backendId == DNN_BACKEND_OPENCV || backendId == DNN_BACKEND_HALIDE || backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH; } + void finalize() + { + power = originPower; + scale = originScale; + shift = originShift; + } + void apply(const float* srcptr, float* dstptr, int len, size_t planeSize, int cn0, int cn1) const { float a = scale, b = shift, p = power; @@ -1212,8 +1192,7 @@ struct PowerFunctor int64 getFLOPSPerElement() const { return power == 1 ? 2 : 10; } }; - -struct ChannelsPReLUFunctor +struct ChannelsPReLUFunctor : public BaseFunctor { typedef ChannelsPReLULayer Layer; Mat scale; @@ -1330,11 +1309,6 @@ struct ChannelsPReLUFunctor } #endif // HAVE_DNN_NGRAPH - - bool tryFuse(Ptr&) { return false; } - - void getScaleShift(Mat&, Mat&) const {} - int64 getFLOPSPerElement() const { return 1; } };