Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / ops / power_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 mo.graph.graph import Node
22 from mo.ops.power import Power
23 from mo.utils.unittest.graph import build_graph
24
25
26 class TestPowerOp(unittest.TestCase):
27     @staticmethod
28     def create_graph(single_input=True):
29         nodes_attributes = {
30             'input1': {
31                 'kind': 'data',
32                 'shape': np.array([1, 3, 224, 224]),
33                 'value': None,
34             },
35             'input2': {
36                 'kind': 'data',
37                 'shape': np.array([]),
38                 'value': np.array(1.0),
39             },
40             'power': {
41                 'kind': 'op',
42                 'shape': np.array([1, 3, 224, 224]),
43             },
44             'power_data': {
45                 'kind': 'data',
46                 'shape': None,
47             },
48         }
49         if single_input:
50             return build_graph(nodes_attributes,
51                                [
52                                    ('input1', 'power'),
53                                    ('power', 'power_data')
54                                ])
55         else:
56             return build_graph(nodes_attributes,
57                                [
58                                    ('input1', 'power'),
59                                    ('input2', 'power'),
60                                    ('power', 'power_data')
61                                ])
62
63     def test_power_single_input_infer1(self):
64         graph = self.create_graph(single_input=True)
65         graph.graph['layout'] = 'NCHW'
66         power_node = Node(graph, 'power')
67         power_node['power'] = 1.0
68
69         Power.infer(power_node)
70
71         self.assertTrue(np.array_equal(power_node.out_node().shape, power_node.in_node(0).shape))
72
73     def test_power_two_input_infer1(self):
74         graph = self.create_graph(single_input=False)
75         graph.graph['layout'] = 'NCHW'
76         power_node = Node(graph, 'power')
77
78         Power.infer(power_node)
79
80         self.assertTrue(np.array_equal(power_node.out_node().shape, power_node.in_node(0).shape))
81
82     def test_power_two_input_infer2(self):
83         graph = self.create_graph(single_input=False)
84         power_node = Node(graph, 'power')
85         input2 = Node(graph, 'input2')
86         input2.value = np.ones((1, 2, 3))
87
88         Power.infer(power_node)
89
90         self.assertIsNone(power_node.out_node().shape)
91
92     def test_power_two_input_infer3(self):
93         graph = self.create_graph(single_input=False)
94         power_node = Node(graph, 'power')
95         input2 = Node(graph, 'input2')
96         input2.value = None
97
98         Power.infer(power_node)
99
100         self.assertIsNone(power_node.out_node().shape)