Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / MinumumMiddleReplacer_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.MinimumMiddleReplacer import MinimumMiddleReplacer
22 from mo.utils.unittest.graph import build_graph, compare_graphs
23
24 nodes_attributes = {
25     'placeholder_1': {'type': 'Placeholder', 'kind': 'op', 'op': 'Placeholder'},
26     'placeholder_1_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
27
28     'placeholder_2': {'type': 'Placeholder', 'kind': 'op', 'op': 'Placeholder'},
29     'placeholder_2_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
30     # minimum node:
31     'minimum': {'type': 'Minimum', 'kind': 'op', 'op': 'Minimum'},
32     # negates
33     'negate_1': {'type': 'Power', 'kind': 'op', 'op': 'Power', 'power': 1, 'scale': -1, 'shift': 0},
34     'negate_1_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
35
36     'negate_2': {'type': 'Power', 'kind': 'op', 'op': 'Power', 'power': 1, 'scale': -1, 'shift': 0},
37     'negate_2_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
38
39     'negate_output': {'type': 'Power', 'kind': 'op', 'op': 'Power', 'power': 1, 'scale': -1, 'shift': 0},
40     'negate_output_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
41
42     # Maximum
43     'maximum': {'type': 'Eltwise', 'kind': 'op', 'op': 'Max'},
44     'maximum_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
45     # output
46     'output_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
47 }
48
49
50 class MinumumMiddleReplacer_test(unittest.TestCase):
51     def test_1(self):
52         graph = build_graph(nodes_attributes,
53                             [('placeholder_1', 'placeholder_1_data'),
54                              ('placeholder_2', 'placeholder_2_data'),
55                              ('placeholder_1_data', 'minimum'),
56                              ('placeholder_2_data', 'minimum'),
57                              ('minimum', 'output_data')
58                              ],
59                             {'placeholder_1_data': {'value': 3, 'shape': np.array([])},
60                              'placeholder_2_data': {'value': None, 'shape': np.array([5, 5])},
61                              })
62
63         graph_ref = build_graph(nodes_attributes,
64                                 [('placeholder_1', 'placeholder_1_data'),
65                                  ('placeholder_2', 'placeholder_2_data'),
66                                  ('placeholder_1_data', 'negate_1'),
67                                  ('placeholder_2_data', 'negate_2'),
68                                  ('negate_1', 'negate_1_data'),
69                                  ('negate_2', 'negate_2_data'),
70                                  ('negate_1_data', 'maximum'),
71                                  ('negate_2_data', 'maximum'),
72                                  ('maximum', 'maximum_data'),
73                                  ('maximum_data', 'negate_output'),
74                                  ('negate_output', 'negate_output_data')
75                                  ])
76
77         graph.graph['layout'] = 'NHWC'
78
79         tested_class = MinimumMiddleReplacer()
80         tested_class.find_and_replace_pattern(graph=graph)
81
82         (flag, resp) = compare_graphs(graph, graph_ref, 'minimum/negate_out_', last_node_ref='negate_output',
83                                       check_op_attrs=True)
84         self.assertTrue(flag, resp)