Optimize Mish for CPU backend
authorDmitry Kurtaev <dmitry.kurtaev+github@gmail.com>
Mon, 22 Jun 2020 20:22:21 +0000 (23:22 +0300)
committerDmitry Kurtaev <dmitry.kurtaev+github@gmail.com>
Mon, 22 Jun 2020 20:27:47 +0000 (23:27 +0300)
modules/dnn/src/layers/elementwise_layers.cpp

index 776053e..1bd8abe 100644 (file)
@@ -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);
             }
         }
     }