Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / ScaleInput_test.py
1 """
2  Copyright (c) 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 import unittest
17 from argparse import Namespace
18
19 import numpy as np
20
21 from extensions.middle.ScaleInput import ScaleInput
22 from mo.utils.unittest.graph import build_graph, compare_graphs
23
24 nodes_attributes = {'node_1': {'type': 'Identity', 'value': None, 'kind': 'op'},
25                     'node_1_data': {'value': None, 'kind': 'data', 'data_type': None},
26                     'node_2': {'type': 'Identity', 'value': None, 'kind': 'op'},
27                     'concat': {'type': 'Concat', 'value': None, 'kind': 'op'},
28                     'node_3': {'type': 'Identity', 'value': None, 'kind': 'op'},
29                     'node_3_data': {'value': None, 'kind': 'data', 'data_type': None},
30                     # Placeholders
31                     'placeholder_1': {'shape': None, 'type': 'Input', 'kind': 'op', 'op': 'Placeholder'},
32                     'placeholder_1_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
33                     'placeholder_2': {'shape': None, 'type': 'Input', 'kind': 'op', 'op': 'Placeholder'},
34                     'pl_1': {'type': 'Placeholder', 'kind': 'op', 'op': 'Placeholder'},
35                     'pl_1_data': {'value': None, 'kind': 'data', 'data_type': None},
36                     'pl_2': {'type': 'Placeholder', 'kind': 'op', 'op': 'Placeholder'},
37                     'pl_2_data': {'value': None, 'kind': 'data', 'data_type': None},
38                     'placeholder_2_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
39                     # ScaleShift layer
40                     'scaleshift_1': {'type': 'ScaleShift', 'kind': 'op', 'op': 'ScaleShift'},
41                     'scaleshift_1_w': {'value': None, 'shape': None, 'kind': 'data'},
42                     'scaleshift_1_b': {'value': None, 'shape': None, 'kind': 'data'},
43                     'scaleshift_1_data': {'value': None, 'shape': None, 'kind': 'data'},
44                     # Mul op
45                     'mul_1': {'type': None, 'kind': 'op', 'op': 'Mul'},
46                     'mul_1_w': {'value': None, 'shape': None, 'kind': 'data'},
47                     'mul_1_data': {'value': None, 'shape': None, 'kind': 'data'},
48                     'op_output': {'kind': 'op', 'op': 'OpOutput', 'infer': lambda x: None}
49                     }
50
51
52 class ScaleInputTests(unittest.TestCase):
53     def test_scale_input_1(self):
54         graph = build_graph(nodes_attributes,
55                             [('placeholder_1', 'placeholder_1_data'),
56                              ('placeholder_1_data', 'op_output')
57                              ],
58                             {'placeholder_1': {'shape': np.array([1, 3, 224, 224])}},
59                             nodes_with_edges_only=True)
60
61         graph_ref = build_graph(nodes_attributes,
62                                 [('placeholder_1', 'mul_1_data'),
63                                  ('mul_1_data', 'mul_1'),
64                                  ('mul_1_w', 'mul_1'),
65                                  ('mul_1', 'placeholder_1_data'),
66                                  ('placeholder_1_data', 'op_output')
67                                  ],
68                                 {'mul_1_w': {'shape': np.array([1, 1, 1]), 'value': np.array([1 / 255])}},
69                                 nodes_with_edges_only=True)
70         graph.graph['layout'] = 'NCHW'
71         graph.graph['cmd_params'] = Namespace(scale=255)
72         ScaleInput().find_and_replace_pattern(graph)
73         (flag, resp) = compare_graphs(graph, graph_ref, 'placeholder_1_data')
74         self.assertTrue(flag, resp)
75
76     def test_scale_input_2(self):
77         graph = build_graph(nodes_attributes,
78                             [('placeholder_1', 'placeholder_1_data'),
79                              ('placeholder_1_data', 'op_output')
80                              ],
81                             nodes_with_edges_only=True)
82
83         graph_ref = build_graph(nodes_attributes,
84                                 [('placeholder_1', 'placeholder_1_data'),
85                                  ('placeholder_1_data', 'op_output')
86                                  ],
87                                 nodes_with_edges_only=True)
88         graph.graph['cmd_params'] = Namespace(scale=1)
89         ScaleInput().find_and_replace_pattern(graph)
90         (flag, resp) = compare_graphs(graph, graph_ref, 'placeholder_1_data')
91         self.assertTrue(flag, resp)