Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / FusePermutesSequence_test.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 unittest
18
19 import numpy as np
20 from argparse import Namespace
21
22 from extensions.middle.FusePermutesSequence import FusePermutesSequence
23 from mo.middle.passes.eliminate_test import build_graph
24 from mo.middle.passes.fusing.fuse_linear_ops_test import compare_graphs
25
26 # The dictionary with nodes attributes used to build various graphs. A key is the name of the node and the value is the
27 # dictionary with node attributes.
28 nodes_attributes = {
29     'placeholder_1': {'name': 'placeholder_1', 'value': None, 'shape': None, 'type': 'Placeholder', 'kind': 'op',
30                       'op': 'Placeholder'},
31     'placeholder_1_data': {'name': 'placeholder_1_data', 'value': None, 'shape': None, 'kind': 'data',
32                            'data_type': None},
33     # Permute layers
34     'permute_1': {'type': 'Permute', 'value': None, 'kind': 'op', 'op': 'Permute'},
35     'permute_1_data': {'value': None, 'shape': None, 'kind': 'data'},
36
37     'permute_2': {'type': 'Permute', 'value': None, 'kind': 'op', 'op': 'Permute'},
38     'permute_2_data': {'value': None, 'shape': None, 'kind': 'data'},
39
40     'permute_3': {'type': 'Permute', 'value': None, 'kind': 'op', 'op': 'Permute'},
41     'permute_3_data': {'value': None, 'shape': None, 'kind': 'data'},
42     'op_output': { 'op': 'OpOutput', 'kind': 'op'}
43 }
44
45
46 class FusePermutesSequenceTest(unittest.TestCase):
47     def test_1(self):
48         #
49         #    NHWC         NCHW           NHWC
50         #   Input->DATA->Permute->DATA->Permute->DATA  => Input->DATA
51         #
52         graph = build_graph(nodes_attributes,
53                             [('placeholder_1', 'placeholder_1_data'),
54                              ('placeholder_1_data', 'permute_1'),
55                              ('permute_1', 'permute_1_data'),
56                              ('permute_1_data', 'permute_2'),
57                              ('permute_2', 'permute_2_data'),
58                              ('permute_2_data', 'op_output')
59                              ],
60                             {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
61
62                              'permute_1': {'order': np.array([0, 3, 1, 2])},
63                              'permute_1_data': {'shape': np.array([1, 3, 227, 227])},
64
65                              'permute_2': {'order': np.array([0, 2, 3, 1])},
66                              'permute_2_data': {'shape': np.array([1, 227, 227, 3])},
67                              }, nodes_with_edges_only=True)
68
69         graph.graph['layout'] = 'NHWC'
70         graph.graph['cmd_params'] = Namespace(keep_shape_ops=False)
71
72         graph_ref = build_graph(nodes_attributes,
73                                 [('placeholder_1', 'placeholder_1_data'),
74                                  ('placeholder_1_data', 'op_output')
75                                  ],
76                                 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])}},
77                                 nodes_with_edges_only=True)
78
79         pattern = FusePermutesSequence()
80         pattern.find_and_replace_pattern(graph)
81
82         (flag, resp) = compare_graphs(graph, graph_ref, 'placeholder_1_data', check_op_attrs=True)
83         self.assertTrue(flag, resp)
84
85     def test_2(self):
86         #
87         #   Input->DATA->Permute->DATA->Permute->DATA  => Input->DATA->Permute->DATA
88         #
89         graph = build_graph(nodes_attributes,
90                             [('placeholder_1', 'placeholder_1_data'),
91                              ('placeholder_1_data', 'permute_1'),
92                              ('permute_1', 'permute_1_data'),
93                              ('permute_1_data', 'permute_2'),
94                              ('permute_2', 'permute_2_data'),
95                              ('permute_2_data', 'op_output')
96                              ],
97                             {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
98
99                              'permute_1': {'order': np.array([0, 3, 1, 2])},
100                              'permute_1_data': {'shape': np.array([1, 3, 227, 227])},
101
102                              'permute_2': {'order': np.array([0, 1, 2, 3])},
103                              'permute_2_data': {'shape': np.array([1, 3, 227, 227])},
104                              }, nodes_with_edges_only=True)
105
106         graph.graph['layout'] = 'NHWC'
107         graph.graph['cmd_params'] = Namespace(keep_shape_ops=False)
108
109         graph_ref = build_graph(nodes_attributes,
110                                 [('placeholder_1', 'placeholder_1_data'),
111                                  ('placeholder_1_data', 'permute_1'),
112                                  ('permute_1', 'permute_1_data'),
113                                  ('permute_1_data', 'op_output')
114                                  ],
115                                 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
116                                  'permute_1': {'order': np.array([0, 3, 1, 2])},
117                                  'permute_1_data': {'shape': np.array([1, 3, 227, 227])},
118                                  }, nodes_with_edges_only=True)
119
120         pattern = FusePermutesSequence()
121         pattern.find_and_replace_pattern(graph)
122
123         (flag, resp) = compare_graphs(graph, graph_ref, 'placeholder_1_data', check_op_attrs=True)
124         self.assertTrue(flag, resp)
125
126
127 if __name__ == '__main__':
128     unittest.main()