Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / EltwiseInputNormalization_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
21 from extensions.middle.EltwiseInputNormalization import EltwiseInputNormalize
22 from extensions.middle.EltwiseInputReshape import EltwiseInputReshape
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 layers
30     'placeholder_1': {'value': None, 'shape': None, 'type': 'Placeholder', 'kind': 'op', 'op': 'Placeholder'},
31     'placeholder_1_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
32     'placeholder_2_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
33     'placeholder_3_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
34     'placeholder_4_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
35
36     # Reshape layers
37     'reshape_1': {'type': 'Reshape', 'value': None, 'kind': 'op', 'op': 'Reshape'},
38     'reshape_1_data': {'value': None, 'shape': None, 'kind': 'data'},
39
40     'reshape_2': {'type': 'Reshape', 'value': None, 'kind': 'op', 'op': 'Reshape'},
41     'reshape_2_data': {'value': None, 'shape': None, 'kind': 'data'},
42
43     # Eltwise consumes layers
44     'eltwise_1': {'type': 'Eltwise', 'value': None, 'kind': 'op', 'op': 'Eltwise'},
45     'eltwise_1_data': {'value': None, 'shape': None, 'kind': 'data'},
46
47     'eltwise_2': {'type': 'Eltwise', 'value': None, 'kind': 'op', 'op': 'Eltwise'},
48     'eltwise_2_data': {'value': None, 'shape': None, 'kind': 'data'},
49
50     'eltwise_3': {'type': 'Eltwise', 'value': None, 'kind': 'op', 'op': 'Eltwise'},
51     'eltwise_3_data': {'value': None, 'shape': None, 'kind': 'data'},
52
53     'eltwise_4': {'type': 'Eltwise', 'value': None, 'kind': 'op', 'op': 'Eltwise'},
54     'eltwise_4_data': {'value': None, 'shape': None, 'kind': 'data'},
55
56     # Concat
57     'concat': {'type': 'Concat', 'kind': 'op', 'op': 'Concat'},
58 }
59
60
61 class EltwiseInputNormalizationTest(unittest.TestCase):
62     def test1_not_constant(self):
63         #
64         #   data1(1,3,64,64)----.                                                   data(1,3,64,64)-------.
65         #   data2(1,64,1)-------->Eltwise-->data(1,3,64,64)   =>    data(1,64,1)->Reshape->data(1,1,64,1)-->Eltwise->...
66         #   data3(64,1)------'                                       data(64,1)->Reshape->data(1,1,64,1)-'
67         #
68         graph = build_graph(nodes_attributes,
69                             [('placeholder_1_data', 'eltwise_1'),
70                              ('placeholder_2_data', 'eltwise_1'),
71                              ('placeholder_3_data', 'eltwise_1'),
72                              ('eltwise_1', 'eltwise_1_data')
73                              ],
74                             {'placeholder_1_data': {'shape': np.array([1, 3, 64, 64])},
75                              'placeholder_2_data': {'shape': np.array([1, 64, 1])},
76                              'placeholder_3_data': {'shape': np.array([64, 1])},
77                              'eltwise_1_data': {'shape': np.array([1, 3, 64, 64])}
78                              }, nodes_with_edges_only=True)
79
80         graph_ref = build_graph(nodes_attributes,
81                                 [('placeholder_1_data', 'eltwise_1'),
82                                  ('placeholder_2_data', 'reshape_1'),
83                                  ('placeholder_3_data', 'reshape_2'),
84                                  ('reshape_1', 'reshape_1_data'),
85                                  ('reshape_2', 'reshape_2_data'),
86                                  ('reshape_1_data', 'eltwise_1'),
87                                  ('reshape_2_data', 'eltwise_1'),
88                                  ('eltwise_1', 'eltwise_1_data')
89                                  ],
90                                 {'placeholder_1_data': {'shape': np.array([1, 3, 64, 64])},
91                                  'reshape_1': {'dim': np.array([1, 1, 64, 1])},
92                                  'reshape_1_data': {'shape': np.array([1, 1, 64, 1])},
93                                  'reshape_2': {'dim': np.array([1, 1, 64, 1])},
94                                  'reshape_2_data': {'shape': np.array([1, 1, 64, 1])},
95                                  'eltwise_1_data': {'shape': np.array([1, 3, 64, 64])}
96                                  }, nodes_with_edges_only=True)
97
98         pattern = EltwiseInputNormalize()
99         pattern.find_and_replace_pattern(graph)
100
101         (flag, resp) = compare_graphs(graph, graph_ref, 'eltwise_1', check_op_attrs=True)
102         self.assertTrue(flag, resp)
103
104     def test_mega_hardcore(self):
105         #   ORIGINAL GRAPH
106         #
107         #   data1(1,3,64,64)---,->Eltwise1->data(1,3,64,64)-----,->Eltwise2->data(1,3,64,64)---,->Eltwise4->data(1,3,64,64)
108         #                     /\                               /\                             /\
109         #   data2(64,1)-----,-'--------------------------------'------------------------------'
110         #                  \/                                 /
111         #   data3(64,1)----`-->Eltwise3->data(64,1)----------'
112         #
113         #   REFERENCE GRAPH AFTER TRANSFORMATION
114         #
115         #   data1(1,3,64,64)---,->Eltwise1->data(1,3,64,64)-----,->Eltwise2->data(1,3,64,64)---,->Eltwise4->data(1,3,64,64)
116         #                     /\                               /\                              /\
117         #   data2(1,1,64,1)---'--------------------------------'-------------------------------'
118         #                                                     /
119         #   data4(64,1)-------,                        Reshape(1,1,64,1)
120         #                    \/                           |
121         #   data3(64,1)------`---->Eltwise3->data(64,1)---'
122         #
123         graph = build_graph(nodes_attributes,
124                             [('placeholder_1_data', 'eltwise_1'),
125                              ('placeholder_2_data', 'eltwise_1'),
126                              ('eltwise_1', 'eltwise_1_data'),
127                              ('eltwise_1_data', 'eltwise_2'),
128                              ('placeholder_2_data', 'eltwise_3'),
129                              ('placeholder_3_data', 'eltwise_3'),
130                              ('eltwise_3', 'eltwise_3_data'),
131                              ('eltwise_3_data', 'eltwise_2'),
132                              ('eltwise_2', 'eltwise_2_data'),
133                              ('eltwise_2_data', 'eltwise_4'),
134                              ('placeholder_2_data', 'eltwise_4'),
135                              ('eltwise_4', 'eltwise_4_data'),
136                              ],
137                             {'placeholder_1_data': {'shape': np.array([1, 3, 64, 64])},
138                              'placeholder_2_data': {'shape': np.array([64, 1]), 'value': np.ones([64, 1])},
139                              'placeholder_3_data': {'shape': np.array([64, 1])},
140                              'eltwise_1_data': {'shape': np.array([1, 3, 64, 64])},
141                              'eltwise_2_data': {'shape': np.array([1, 3, 64, 64])},
142                              'eltwise_3_data': {'shape': np.array([64, 1])},
143                              'eltwise_4_data': {'shape': np.array([1, 3, 64, 64])}
144                              }, nodes_with_edges_only=True)
145
146         graph_ref = build_graph(nodes_attributes,
147                             [('placeholder_1_data', 'eltwise_1'),
148                              ('placeholder_2_data', 'eltwise_1'),
149                              ('eltwise_1', 'eltwise_1_data'),
150                              ('eltwise_1_data', 'eltwise_2'),
151                              ('placeholder_4_data', 'eltwise_3'),
152                              ('placeholder_3_data', 'eltwise_3'),
153                              ('eltwise_3', 'eltwise_3_data'),
154                              ('eltwise_3_data', 'reshape_1'),
155                              ('reshape_1', 'reshape_1_data'),
156                              ('reshape_1_data', 'eltwise_2'),
157                              ('eltwise_2', 'eltwise_2_data'),
158                              ('eltwise_2_data', 'eltwise_4'),
159                              ('placeholder_2_data', 'eltwise_4'),
160                              ('eltwise_4', 'eltwise_4_data'),
161                              ],
162                             {'placeholder_1_data': {'shape': np.array([1, 3, 64, 64])},
163                              'placeholder_2_data': {'shape': np.array([1, 1, 64, 1]), 'value': np.ones([1, 1, 64, 1])},
164                              'placeholder_3_data': {'shape': np.array([64, 1])},
165                              'placeholder_4_data': {'shape': np.array([64, 1]), 'value': np.ones([64, 1])},
166                              'reshape_1': {'dim': np.array([1,1,64,1])},
167                              'reshape_1_data': {'shape': np.array([1,1,64,1])},
168                              'eltwise_1_data': {'shape': np.array([1, 3, 64, 64])},
169                              'eltwise_2_data': {'shape': np.array([1, 3, 64, 64])},
170                              'eltwise_3_data': {'shape': np.array([64, 1])},
171                              'eltwise_4_data': {'shape': np.array([1, 3, 64, 64])}
172                              }, nodes_with_edges_only=True)
173
174         pattern = EltwiseInputNormalize()
175         pattern.find_and_replace_pattern(graph)
176
177         (flag, resp) = compare_graphs(graph, graph_ref, 'eltwise_1', check_op_attrs=True)
178         self.assertTrue(flag, resp)