Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / onnx / slice_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.slice_ext import SliceFrontExtractor
24 from mo.ops.op import Op
25 from mo.ops.slice import Slice
26 from mo.utils.unittest.extractors import PB
27
28
29 @generator
30 class TestSliceONNXExt(unittest.TestCase):
31     @staticmethod
32     def _create_slice_node(axes, starts, ends):
33         if axes is None:
34             pb = onnx.helper.make_node(
35                 'Slice',
36                 inputs=['x'],
37                 outputs=['y'],
38                 starts=starts,
39                 ends=ends,
40             )
41         else:
42             pb = onnx.helper.make_node(
43                 'Slice',
44                 inputs=['x'],
45                 outputs=['y'],
46                 axes=axes,
47                 starts=starts,
48                 ends=ends,
49             )
50
51         node = PB({'pb': pb})
52         return node
53
54     @classmethod
55     def setUpClass(cls):
56         Op.registered_ops['Slice'] = Slice
57
58     @generate(*[([0, 1], [0, 0], [28, 28]), (None, [0, 0], [28, 28])])
59     def test_slice_ext(self, axes, starts, ends):
60         node = self._create_slice_node(axes, starts, ends)
61         SliceFrontExtractor.extract(node)
62
63         exp_res = {
64             'op': 'Slice',
65             'axis': axes,
66             'start': starts,
67             'end': ends,
68             'infer': Slice.infer
69         }
70
71         for key in exp_res.keys():
72             if type(node[key]) in [list, np.ndarray]:
73                 self.assertTrue(np.array_equal(np.array(node[key]), np.array(exp_res[key])))
74             else:
75                 self.assertEqual(node[key], exp_res[key])