From 1491934d17e582378272dfdbdee00dc0ea5faa86 Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Mon, 22 Jun 2020 23:22:21 +0300 Subject: [PATCH] Optimize Mish for CPU backend --- modules/dnn/src/layers/elementwise_layers.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/modules/dnn/src/layers/elementwise_layers.cpp b/modules/dnn/src/layers/elementwise_layers.cpp index 776053e2b0..1bd8abef87 100644 --- a/modules/dnn/src/layers/elementwise_layers.cpp +++ b/modules/dnn/src/layers/elementwise_layers.cpp @@ -667,8 +667,11 @@ struct MishFunctor : public BaseFunctor { for( int i = 0; i < len; i++ ) { + // Use fast approximation introduced in https://github.com/opencv/opencv/pull/17200 float x = srcptr[i]; - dstptr[i] = x * tanh(log(1.0f + exp(x))); + float eX = exp(std::min(x, 20.f)); + float n = (eX + 2) * eX; + dstptr[i] = (x * n) / (n + 2); } } }