Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / priorbox_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 extensions.ops.priorbox import PriorBoxOp
22 from mo.graph.graph import Node
23 from mo.utils.unittest.graph import build_graph
24
25 nodes_attributes = {'node_1': {'type': 'Identity', 'value': None, 'kind': 'data'},
26                     'pb': {'type': 'PriorBox', 'value': None, 'kind': 'op'},
27                     'node_3': {'type': 'Identity', 'value': None, 'kind': 'data'},
28                     'op_output': { 'kind': 'op', 'op': 'OpOutput'}
29                     }
30
31
32 class TestPriorBoxPartialInfer(unittest.TestCase):
33     def test_caffe_priorbox_infer(self):
34         graph = build_graph(nodes_attributes,
35                             [
36                                 ('node_1', 'pb'),
37                                 ('pb', 'node_3'),
38                                 ('node_3', 'op_output')
39                             ],
40                             {
41                                 'node_3': {'shape': None},
42                                 'node_1': {'shape': np.array([1, 384, 19, 19])},
43                                 'pb': {
44                                     'aspect_ratio': np.array([1]),
45                                     'flip': 0,
46                                     'min_size': np.array([1]),
47                                     'max_size': np.array([1])
48                                 }
49                             })
50         graph.graph['layout'] = 'NCHW'
51         pb_node = Node(graph, 'pb')
52         PriorBoxOp.priorbox_infer(pb_node)
53         exp_shape = np.array([1, 2, 4 * 19 * 19 * 2])
54         res_shape = graph.node['node_3']['shape']
55         for i in range(0, len(exp_shape)):
56             self.assertEqual(exp_shape[i], res_shape[i])
57
58     def test_caffe_priorbox_flip_infer(self):
59         graph = build_graph(nodes_attributes,
60                             [
61                                 ('node_1', 'pb'),
62                                 ('pb', 'node_3'),
63                                 ('node_3', 'op_output')
64                             ],
65                             {
66                                 'node_3': {'shape': None},
67                                 'node_1': {'shape': np.array([1, 384, 19, 19])},
68                                 'pb': {
69                                     'aspect_ratio': np.array([1, 2, 0.5]),
70                                     'flip': 1,
71                                     'min_size': np.array([1]),
72                                     'max_size': np.array([1])
73                                 }
74                             })
75         graph.graph['layout'] = 'NCHW'
76         pb_node = Node(graph, 'pb')
77         PriorBoxOp.priorbox_infer(pb_node)
78         exp_shape = np.array([1, 2, 4 * 19 * 19 * 4])
79         res_shape = graph.node['node_3']['shape']
80         for i in range(0, len(exp_shape)):
81             self.assertEqual(exp_shape[i], res_shape[i])
82
83     def test_tf_priorbox_infer(self):
84         graph = build_graph(nodes_attributes,
85                             [
86                                 ('node_1', 'pb'),
87                                 ('pb', 'node_3'),
88                                 ('node_3', 'op_output')
89                             ],
90                             {
91                                 'node_3': {'shape': None},
92                                 'node_1': {'shape': np.array([1, 19, 19, 384])},
93                                 'pb': {
94                                     'aspect_ratio': np.array([1]),
95                                     'flip': 0,
96                                     'min_size': np.array([1]),
97                                     'max_size': np.array([1])
98                                 }
99                             })
100         graph.graph['layout'] = 'NHWC'
101         pb_node = Node(graph, 'pb')
102         PriorBoxOp.priorbox_infer(pb_node)
103         exp_shape = np.array([1, 2, 4 * 19 * 19 * 2])
104         res_shape = graph.node['node_3']['shape']
105         for i in range(0, len(exp_shape)):
106             self.assertEqual(exp_shape[i], res_shape[i])
107
108     def test_tf_priorbox_flip_infer(self):
109         graph = build_graph(nodes_attributes,
110                             [
111                                 ('node_1', 'pb'),
112                                 ('pb', 'node_3'),
113                                 ('node_3', 'op_output')
114                             ],
115                             {
116                                 'node_3': {'shape': None},
117                                 'node_1': {'shape': np.array([1, 19, 19, 384])},
118                                 'pb': {
119                                     'aspect_ratio': np.array([1, 2, 0.5]),
120                                     'flip': 1,
121                                     'min_size': np.array([1]),
122                                     'max_size': np.array([1])
123                                 }
124                             })
125         graph.graph['layout'] = 'NHWC'
126         pb_node = Node(graph, 'pb')
127         PriorBoxOp.priorbox_infer(pb_node)
128         exp_shape = np.array([1, 2, 4 * 19 * 19 * 4])
129         res_shape = graph.node['node_3']['shape']
130         for i in range(0, len(exp_shape)):
131             self.assertEqual(exp_shape[i], res_shape[i])