Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / onnx / priorbox_ext_test.py
1 """
2  Copyright (c) 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 onnx
18 import unittest
19
20 import numpy as np
21
22 from extensions.front.onnx.priorbox_ext import PriorBoxFrontExtractor
23 from extensions.ops.priorbox import PriorBoxOp
24 from mo.ops.op import Op
25 from mo.utils.unittest.extractors import PB
26
27
28 class TestPriorBoxExt(unittest.TestCase):
29     @staticmethod
30     def _create_priorbox_node(aspect_ratio=[], min_size=np.array([]), max_size=np.array([]),
31                               flip=False, clip=False, variance=None, img_size=0, img_h=0,
32                               img_w=0, step=0, step_h=0, step_w=0, offset=0):
33         pb = onnx.helper.make_node(
34             'PriorBox',
35             inputs=['x'],
36             outputs=['y'],
37             aspect_ratio=aspect_ratio,
38             min_size=min_size,
39             max_size=max_size,
40             flip=flip,
41             clip=clip,
42             variance=variance,
43             img_size=img_size,
44             img_h=img_h,
45             img_w=img_w,
46             step=step,
47             step_h=step_h,
48             step_w=step_w,
49             offset=offset,
50         )
51         
52         node = PB({'pb': pb})
53         return node
54
55     @classmethod
56     def setUpClass(cls):
57         Op.registered_ops['PriorBox'] = PriorBoxOp
58
59     def test_priorbox_no_pb_no_ml(self):
60         self.assertRaises(AttributeError, PriorBoxFrontExtractor.extract, None)
61
62     def test_priorbox_ext_ideal_numbers(self):
63         node = self._create_priorbox_node(aspect_ratio=np.array([2, 3], dtype=np.float),
64                                           variance=np.array([0.2, 0.3, 0.2, 0.3]),
65                                           img_size=300, step=5.0, offset=0.6, flip=True)
66
67         PriorBoxFrontExtractor.extract(node)
68
69         exp_res = {
70             'op': 'PriorBox',
71             'type': 'PriorBox',
72             'clip': 0,
73             'flip': 1,
74             'aspect_ratio': np.array([2, 3], dtype=np.float),
75             'variance': [0.2, 0.3, 0.2, 0.3],
76             'img_size': 300,
77             'img_h': 0,
78             'img_w': 0,
79             'step': 5,
80             'step_h': 0,
81             'step_w': 0,
82             'offset': 0.6
83         }
84
85         for key in exp_res.keys():
86             if key in ['variance', 'aspect_ratio', 'step_h', 'step_w', 'offset']:
87                 np.testing.assert_almost_equal(node[key], exp_res[key])
88             else:
89                 self.assertEqual(node[key], exp_res[key])