Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / onnx / tanh_ext_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 import onnx
21
22 from extensions.front.onnx.tanh_ext import TanhFrontExtractor
23 from mo.utils.unittest.graph import build_graph
24 from mo.graph.graph import Node
25
26
27 class TanhONNXExtractorTest(unittest.TestCase):
28     @staticmethod
29     def _create_node():
30         pb = onnx.helper.make_node("Tanh", ["X"], ["Y"])
31         graph = build_graph({'node_0': {'pb': pb}}, [])
32         return Node(graph, 'node_0')
33
34     @staticmethod
35     def _base_attrs():
36         # Commonly used attributes in the tests
37         # Each test takes these ones and then adds/modifies/deletes particular fields
38         return (
39             # reference output Node attributes
40             dict(
41                 op='Activation',
42                 operation='tanh'
43             )
44         )
45
46     @staticmethod
47     def _extract():
48         node = __class__._create_node()
49         TanhFrontExtractor.extract(node)
50         return node.graph.node[node.id]
51
52     def _match(self, out, ref):
53         for key in ref.keys():
54             status = out[key] == ref[key]
55             if type(status) in [list, np.ndarray]:
56                 status = np.all(status)
57             self.assertTrue(status, 'Mismatch for field {}, observed: {}, expected: {}'.format(key, out[key], ref[key]))
58
59     def test_default(self):
60         ref = self._base_attrs()
61         out = self._extract()
62         self._match(out, ref)