Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / back / remove_last_softmax_pattern.py
index 488e161..243274c 100644 (file)
@@ -1,5 +1,5 @@
 """
- Copyright (c) 2018 Intel Corporation
+ Copyright (c) 2018-2019 Intel Corporation
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
 import networkx as nx
 
 from mo.back.replacement import BackReplacementPattern
+from mo.graph.graph import Graph
 from mo.middle.passes.eliminate import remove_op_node_with_data_node
 
 
@@ -27,27 +28,22 @@ class RemoveLastSoftMaxPattern(BackReplacementPattern):
     def pattern():
         return dict(
             nodes=[
-                ('softmax_node', dict(kind='op', op='SoftMax'))
+                ('softmax_node', dict(op='SoftMax')),
+                ('softmax_data', dict(kind='data')),
+                ('op_output', dict(op='OpOutput'))
             ],
-            edges=[]
+            edges=[
+                ('softmax_node', 'softmax_data'),
+                ('softmax_data', 'op_output')
+            ]
         )
 
     @staticmethod
-    def replace_pattern(graph: nx.MultiDiGraph, match: dict):
+    def replace_pattern(graph: Graph, match: dict):
         """
-        Need to find the pattern: Parent (any type) -> SoftMAx -> OpOutput
-
-        It is needed to remove output SoftMAx layer
-
-        Parameters
-        ----------
-        graph : nx.MultiDiGraph
-           Graph with loaded model.
-        match : dict
-           Patterns which were found in graph structure.
+        Removes output SoftMax layer
+        :param graph: graph to operate on
+        :param match: dictionary with matched nodes
         """
-        softmax = match['softmax_node']
-        child = softmax.out_node()
-        if not child.has_and_set('is_output'):
-            return
-        remove_op_node_with_data_node(graph, softmax)
+        if len(match['softmax_data'].out_nodes()) == 1:
+            remove_op_node_with_data_node(graph, match['softmax_node'])