From d2e3e1fc006fe16699528a3191c5ec3450814e90 Mon Sep 17 00:00:00 2001 From: Maksim Doronin Date: Mon, 30 Nov 2020 18:51:51 +0300 Subject: [PATCH] [IE][VPU]: Mish decomposition (#3412) Introduce vpu ngraph transformation that decomposes Mish layer with the following formula: mish(x) = x * tanh(ln(exp(x) + 1)). It is needed to fix the accuracy drop of Yolo-v4 public model that happened due to Mish implementation in the myriad plugin is not accurate enough. It is just a workaround and will be removed when Mish layer will be improved. map: 70.97% (ref: 70.96%) coco_precision: 73.50% (ref 73.49%) --- ...convert_extract_image_patches_to_reorg_yolo.hpp | 1 + .../ngraph/transformations/mish_decomposition.hpp | 18 ++++++++ ...convert_extract_image_patches_to_reorg_yolo.cpp | 2 + .../ngraph/transformations/mish_decomposition.cpp | 49 ++++++++++++++++++++++ .../graph_transformer/src/frontend/frontend.cpp | 3 ++ 5 files changed, 73 insertions(+) create mode 100644 inference-engine/src/vpu/common/include/vpu/ngraph/transformations/mish_decomposition.hpp create mode 100644 inference-engine/src/vpu/common/src/ngraph/transformations/mish_decomposition.cpp diff --git a/inference-engine/src/vpu/common/include/vpu/ngraph/transformations/convert_extract_image_patches_to_reorg_yolo.hpp b/inference-engine/src/vpu/common/include/vpu/ngraph/transformations/convert_extract_image_patches_to_reorg_yolo.hpp index aeef206..743ae69 100644 --- a/inference-engine/src/vpu/common/include/vpu/ngraph/transformations/convert_extract_image_patches_to_reorg_yolo.hpp +++ b/inference-engine/src/vpu/common/include/vpu/ngraph/transformations/convert_extract_image_patches_to_reorg_yolo.hpp @@ -10,6 +10,7 @@ namespace vpu { class ConvertExtractImagePatchesToReorgYolo : public ngraph::pass::MatcherPass { public: + NGRAPH_RTTI_DECLARATION; ConvertExtractImagePatchesToReorgYolo(); }; diff --git a/inference-engine/src/vpu/common/include/vpu/ngraph/transformations/mish_decomposition.hpp b/inference-engine/src/vpu/common/include/vpu/ngraph/transformations/mish_decomposition.hpp new file mode 100644 index 0000000..f9252ca --- /dev/null +++ b/inference-engine/src/vpu/common/include/vpu/ngraph/transformations/mish_decomposition.hpp @@ -0,0 +1,18 @@ +// Copyright (C) 2020 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include + +namespace vpu { + +class MishDecomposition : public ngraph::pass::MatcherPass { +public: + NGRAPH_RTTI_DECLARATION; + MishDecomposition(); +}; + +} // namespace vpu + diff --git a/inference-engine/src/vpu/common/src/ngraph/transformations/convert_extract_image_patches_to_reorg_yolo.cpp b/inference-engine/src/vpu/common/src/ngraph/transformations/convert_extract_image_patches_to_reorg_yolo.cpp index 4a28a14..c1cf63e 100644 --- a/inference-engine/src/vpu/common/src/ngraph/transformations/convert_extract_image_patches_to_reorg_yolo.cpp +++ b/inference-engine/src/vpu/common/src/ngraph/transformations/convert_extract_image_patches_to_reorg_yolo.cpp @@ -10,6 +10,8 @@ #include #include +NGRAPH_RTTI_DEFINITION(vpu::ConvertExtractImagePatchesToReorgYolo, "ConvertExtractImagePatchesToReorgYolo", 0); + namespace vpu { ConvertExtractImagePatchesToReorgYolo::ConvertExtractImagePatchesToReorgYolo() { diff --git a/inference-engine/src/vpu/common/src/ngraph/transformations/mish_decomposition.cpp b/inference-engine/src/vpu/common/src/ngraph/transformations/mish_decomposition.cpp new file mode 100644 index 0000000..76fb4d6 --- /dev/null +++ b/inference-engine/src/vpu/common/src/ngraph/transformations/mish_decomposition.cpp @@ -0,0 +1,49 @@ +// Copyright (C) 2020 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "vpu/ngraph/transformations/mish_decomposition.hpp" + +#include +#include +#include + +#include +#include + +NGRAPH_RTTI_DEFINITION(vpu::MishDecomposition, "MishDecomposition", 0); + +namespace vpu { + +MishDecomposition::MishDecomposition() { + const auto mishPattern = ngraph::pattern::wrap_type(); + + ngraph::matcher_pass_callback callback = [this](ngraph::pattern::Matcher &matcher) { + const auto& mish = ngraph::as_type_ptr(matcher.get_match_root()); + + if (!mish || m_transformation_callback(mish)) { + return false; + } + + const auto inputType = mish->input_value(0).get_element_type(); + const auto addConst = ngraph::opset5::Constant::create(inputType, ngraph::Shape{}, {1.0f}); + + const auto exp = std::make_shared(mish->input_value(0)); + const auto add = std::make_shared(exp, addConst); + const auto log = std::make_shared(add); + const auto tanh = std::make_shared(log); + const auto mul = std::make_shared(mish->input_value(0), tanh); + + mul->set_friendly_name(mish->get_friendly_name()); + ngraph::copy_runtime_info(mish, {addConst, exp, add, log, tanh, mul}); + ngraph::replace_node(mish, mul); + + return true; + }; + + const auto matcher = std::make_shared(mishPattern, "MishDecomposition"); + register_matcher(matcher, callback); +} + +} // namespace vpu + diff --git a/inference-engine/src/vpu/graph_transformer/src/frontend/frontend.cpp b/inference-engine/src/vpu/graph_transformer/src/frontend/frontend.cpp index f4fa3b6..58ac221 100644 --- a/inference-engine/src/vpu/graph_transformer/src/frontend/frontend.cpp +++ b/inference-engine/src/vpu/graph_transformer/src/frontend/frontend.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include "vpu/ngraph/transformations/dynamic_to_static_shape.hpp" #include "vpu/ngraph/transformations/eliminate_shapeof_after_dsr.hpp" @@ -181,6 +182,8 @@ ie::ICNNNetwork::Ptr FrontEnd::convertNetwork(ie::ICNNNetwork& network) { manager.register_pass(); manager.register_pass(); manager.register_pass(); + // WA: Mish is not accurate enough. Remove this decomposition when mish is improved + manager.register_pass(); manager.register_pass(); manager.register_pass(); manager.register_pass(); -- 2.7.4