specificCreator for Transpose operation (#713)
authorGleb Kazantaev <gleb.kazantaev@intel.com>
Tue, 2 Jun 2020 14:15:36 +0000 (17:15 +0300)
committerGitHub <noreply@github.com>
Tue, 2 Jun 2020 14:15:36 +0000 (17:15 +0300)
* Updated Transpose node convertor; replaced get_vector with cast_vector

* Replaced NodeCreator with specificCreator

inference-engine/src/legacy_api/src/convert_function_to_cnn_network.cpp
inference-engine/src/legacy_api/src/ie_cnn_layer_builder_ngraph.cpp

index e56ed03e82acdf2732eae2bbbf2f283895e474b1..349b461597547c84dbcfcf9996fa9746bdab39a2 100644 (file)
@@ -461,6 +461,19 @@ InferenceEngine::details::CNNLayerCreator::CNNLayerCreator(const std::shared_ptr
         res->params = params;
         return res;
     });
+
+    addSpecificCreator({"Transpose"}, [](const std::shared_ptr<::ngraph::Node>& node,
+        const std::map<std::string, std::string> params) -> CNNLayerPtr {
+        LayerParams attrs = {node->get_friendly_name(), "Permute",
+            details::convertPrecision(node->get_output_element_type(0))};
+        auto res = std::make_shared<InferenceEngine::CNNLayer>(attrs);
+        res->params = params;
+        if (auto transpose_const = std::dynamic_pointer_cast<ngraph::op::Constant>(node->input_value(1).get_node_shared_ptr())) {
+            res->params["order"] = Builder::asString(transpose_const->cast_vector<int64_t>());
+        }
+        return res;
+
+    });
 }
 
 CNNLayerPtr InferenceEngine::details::CNNLayerCreator::create() {
@@ -580,7 +593,6 @@ std::shared_ptr<CNNNetworkImpl> convertFunctionToICNNNetwork(const std::shared_p
                 std::make_shared<Builder::NodeConverter<::ngraph::op::TileIE>>(),
                 std::make_shared<Builder::NodeConverter<::ngraph::op::v1::TopK>>(),
                 std::make_shared<Builder::NodeConverter<::ngraph::op::TopKIE>>(),
-                std::make_shared<Builder::NodeConverter<::ngraph::op::Transpose>>(),
                 std::make_shared<Builder::NodeConverter<::ngraph::op::Unsqueeze>>(),
                 std::make_shared<Builder::NodeConverter<::ngraph::op::TensorIterator>>(),
                 std::make_shared<Builder::NodeConverter<::ngraph::op::LSTMCellIE>>(),
index 07e6adf3e081802347fa4a981331f64511cebdd8..b1fc1e702a47823c228a0b1b836c996413d3f0a4 100644 (file)
@@ -1168,7 +1168,7 @@ CNNLayer::Ptr NodeConverter<ngraph::op::v1::Split>::createLayer(const std::share
     if (!axis_node_const) {
         THROW_IE_EXCEPTION << "Split " << castedLayer->get_friendly_name() << " has no axes as Constant";
     }
-    auto axis = axis_node_const->get_vector<int64_t>()[0];
+    auto axis = axis_node_const->cast_vector<int64_t>()[0];
     if (axis < 0) {
         axis += castedLayer->get_input_shape(0).size();
     }
@@ -1189,7 +1189,7 @@ CNNLayer::Ptr NodeConverter<ngraph::op::VariadicSplit>::createLayer(const std::s
     if (!axis_node_const) {
         THROW_IE_EXCEPTION << "Split " << castedLayer->get_friendly_name() << " has no axes as Constant";
     }
-    auto axis = axis_node_const->get_vector<int64_t>()[0];
+    auto axis = axis_node_const->cast_vector<int64_t>()[0];
     if (axis < 0) {
         axis += castedLayer->get_input_shape(0).size();
     }
@@ -1281,7 +1281,7 @@ CNNLayer::Ptr NodeConverter<ngraph::op::v1::Reshape>::createLayer(const std::sha
 
     const auto constNode = castedLayer->get_inputs()[1].get_output().get_node();
     if (auto constValue = ngraph::as_type_ptr<ngraph::op::Constant>(constNode)) {
-        auto value = constValue->get_vector<int64_t>();
+        auto value = constValue->cast_vector<int64_t>();
         for (auto & i : value) {
             if (i == 0 && !castedLayer->get_special_zero())
                 THROW_IE_EXCEPTION << "Reshape " << params.name << " has `special_zero`=False and zeros in second input. This combination is not supported";
@@ -1417,29 +1417,6 @@ CNNLayer::Ptr NodeConverter<ngraph::op::DetectionOutput>::createLayer(
     return res;
 }
 
-template <>
-CNNLayer::Ptr NodeConverter<ngraph::op::Transpose>::createLayer(const std::shared_ptr<ngraph::Node>& layer) const {
-    LayerParams params = {layer->get_friendly_name(), "Permute",
-                          details::convertPrecision(layer->get_output_element_type(0))};
-    auto res = std::make_shared<InferenceEngine::CNNLayer>(params);
-
-    NodeConverter<ngraph::op::Constant> converter;
-    const auto orderNode = layer->get_inputs()[1].get_output().get_node();
-    if (converter.canCreate(orderNode)) {
-        const auto& orderLayer = converter.createLayer(orderNode);
-        auto order = orderLayer->blobs["custom"];
-        int64_t* data = order->buffer().as<int64_t*>();
-        std::string orderStr;
-        for (size_t i = 0; i < order->size(); i++) {
-            if (!orderStr.empty()) orderStr += ",";
-            orderStr += asString(data[i]);
-        }
-        res->params["order"] = orderStr;
-    }
-
-    return res;
-}
-
 template <>
 CNNLayer::Ptr NodeConverter<ngraph::op::Proposal>::createLayer(const std::shared_ptr<ngraph::Node>& layer) const {
     THROW_IE_EXCEPTION << "Proposal operation should be converted to ProposalIE";