Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / onnx / squeeze_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 from generator import generator, generate
22
23 from extensions.front.onnx.squeeze_ext import SqueezeFrontExtractor
24 from mo.ops.op import Op
25 from mo.ops.squeeze import Squeeze
26 from mo.utils.unittest.extractors import PB
27
28
29 @generator
30 class TestSqueezeONNXExt(unittest.TestCase):
31     @staticmethod
32     def _create_squeeze_node(axes):
33         if axes is None:
34             pb = onnx.helper.make_node(
35                 'Squeeze',
36                 inputs=['x'],
37                 outputs=['y'],
38             )
39         else:
40             pb = onnx.helper.make_node(
41                 'Squeeze',
42                 inputs=['x'],
43                 outputs=['y'],
44                 axes=axes,
45             )
46
47         node = PB({'pb': pb})
48         return node
49
50     @classmethod
51     def setUpClass(cls):
52         Op.registered_ops['Squeeze'] = Squeeze
53
54     @generate(*[[0, 1, 2, 3], [1], None])
55     def test_squeeze_ext(self, axes):
56         node = self._create_squeeze_node(axes)
57         SqueezeFrontExtractor.extract(node)
58
59         exp_res = {
60             'type': 'Reshape',
61             'squeeze_dims': axes,
62         }
63
64         for key in exp_res.keys():
65             if type(node[key]) in [list, np.ndarray]:
66                 self.assertTrue(np.array_equal(np.array(node[key]), np.array(exp_res[key])))
67             else:
68                 self.assertEqual(node[key], exp_res[key])