Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / ops / activation_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.activation import Activation
23 from mo.utils.unittest.graph import build_graph
24
25
26 class TestActivationOp(unittest.TestCase):
27     nodes_attributes = {
28         'node_1': {
29             'shape': np.array([227, 227, 227, 227]),
30             'value': None
31         },
32         'activation_node': {
33             'op': 'Activation',
34             'kind': 'op'
35         },
36         'node_3': {
37             'shape': None
38         }
39     }
40
41     def test_assertion_activation_infer(self):
42         graph = build_graph(self.nodes_attributes,
43                             [
44                                 ('node_1', 'activation_node'),
45                                 ('activation_node', 'node_3')
46                             ],
47                             {
48                                 'activation_node': {'operation': 'test'}
49                             })
50         activation_node = Node(graph, 'activation_node')
51         self.assertEqual(activation_node.op, 'Activation')
52         self.assertRaises(KeyError, Activation.infer, activation_node)
53
54     def test_activation_infer(self):
55         graph = build_graph(self.nodes_attributes,
56                             [
57                                 ('node_1', 'activation_node'),
58                                 ('activation_node', 'node_3')
59                             ],
60                             {
61                                 'node_1': {
62                                     'value': np.array([0, 7, 3, -1])
63                                 },
64                                 'activation_node': {
65                                     'operation': 'relu6'
66                                 },
67                                 'node_3': {
68                                     'value': None
69                                 }
70                             })
71         graph.graph['layout'] = 'NCHW'
72         activation_node = Node(graph, 'activation_node')
73         Activation.infer(activation_node)
74         exp_shape = np.array([227, 227, 227, 227])
75         res_shape = graph.node['node_3']['shape']
76         res_value = graph.node['node_3']['value']
77         exp_value = np.array([0, 6, 3, 0])
78         for i, value in enumerate(exp_shape):
79             self.assertEqual(res_shape[i], value)
80         for i, value in enumerate(exp_value):
81             self.assertEqual(res_value[i], value)
82
83     def test_activation_elu_infer(self):
84         graph = build_graph(self.nodes_attributes,
85                             [
86                                 ('node_1', 'activation_node'),
87                                 ('activation_node', 'node_3')
88                             ],
89                             {
90                                 'node_1': {
91                                     'value': np.array([6, -4, -2, -1])
92                                 },
93                                 'activation_node': {
94                                     'operation': 'elu',
95                                     'alpha': 1.0,
96                                 },
97                                 'node_3': {
98                                     'value': None
99                                 }
100                             })
101         graph.graph['layout'] = 'NCHW'
102         activation_node = Node(graph, 'activation_node')
103         Activation.infer(activation_node)
104         exp_shape = np.array([227, 227, 227, 227])
105         res_shape = graph.node['node_3']['shape']
106         res_value = graph.node['node_3']['value']
107         exp_value = np.array([6., -0.98168436, -0.86466472, -0.63212056])
108         for i, value in enumerate(exp_shape):
109             self.assertEqual(res_shape[i], value)
110         for i, value in enumerate(exp_value):
111             self.assertAlmostEqual(res_value[i], value)