Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / tf / ConvFlatten.py
1 """
2  Copyright (c) 2018-2019 Intel Corporation
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 """
16
17 import numpy as np
18
19 from mo.front.subgraph_matcher import SubgraphMatch
20 from mo.front.tf.replacement import FrontReplacementFromConfigFileSubGraph
21 from mo.graph.graph import Graph
22 from mo.ops.permute import Permute
23
24
25 class ConvFlattenReplacement(FrontReplacementFromConfigFileSubGraph):
26     replacement_id = 'ConvFlatten'
27
28     def output_edges_match(self, graph: Graph, match: SubgraphMatch, new_sub_graph: dict):
29         return {}
30
31     def input_edges_match(self, graph: Graph, match: SubgraphMatch, new_sub_graph: dict):
32         return {}
33
34     def nodes_to_remove(self, graph: Graph, match: SubgraphMatch):
35         # no need to remove any of matched nodes. We just insert 'Permute' node before the matched sub-graph.
36         return []
37
38     def generate_sub_graph(self, graph: Graph, match: SubgraphMatch):
39         permute_op = Permute(graph, {'order': np.array([0, 2, 3, 1])})
40         permute_node = permute_op.add_node({'name': match.scope + '_permute_'})
41
42         reshape_node = match.node_by_pattern('flatten/Reshape$')
43
44         # reshape_in_node is the node after which we should insert Permute
45         reshape_in_node = reshape_node.in_nodes()[0]
46         reshape_in_node.insert_node_after(permute_node, 0)
47         return {}