2 Copyright (c) 2018-2019 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
21 from mo.front.common.partial_infer.multi_box_detection import multi_box_detection_infer
22 from mo.graph.graph import Node
23 from mo.utils.unittest.graph import build_graph
25 nodes_attributes = {'node_1': {'value': None, 'kind': 'data'},
26 'node_2': {'value': None, 'kind': 'data'},
27 'node_3': {'value': None, 'kind': 'data'},
28 'detection_output_1': {'type': 'DetectionOutput', 'value': None, 'kind': 'op'},
29 'node_4': {'value': None, 'kind': 'data'}
33 class TestMultiBoxDetectionInfer(unittest.TestCase):
34 def test_prior_box_infer_ideal(self):
35 graph = build_graph(nodes_attributes,
36 [('node_1', 'detection_output_1'),
37 ('node_2', 'detection_output_1'),
38 ('node_3', 'detection_output_1'),
39 ('detection_output_1', 'node_4')],
40 {'node_1': {'shape': np.array([1, 34928])},
41 'node_2': {'shape': np.array([1, 183372])},
42 'node_3': {'shape': np.array([1, 2, 34928])},
43 'detection_output_1': {"background_label_id": "0", "clip": "1",
44 "code_type": "caffe.PriorBoxParameter.CENTER_SIZE",
45 "confidence_threshold": "0.01", "keep_top_k": "200",
46 "nms_threshold": "0.5", "num_classes": "21",
47 "share_location": "1", "top_k": "200",
48 "variance_encoded_in_target": "0"},
49 'node_4': {'shape': np.array([1, 1, 200, 7])},
52 multi_box_detection_node = Node(graph, 'detection_output_1')
53 print(multi_box_detection_node)
55 multi_box_detection_infer(multi_box_detection_node)
56 exp_shape = np.array([1, 1, 200, 7])
57 res_shape = graph.node['node_4']['shape']
58 for i in range(0, len(exp_shape)):
59 self.assertEqual(exp_shape[i], res_shape[i])
61 self.assertEqual(multi_box_detection_node.background_label_id, '0')
62 self.assertEqual(multi_box_detection_node.clip, '1')
63 self.assertEqual(multi_box_detection_node.code_type, 'caffe.PriorBoxParameter.CENTER_SIZE')
64 self.assertEqual(multi_box_detection_node.confidence_threshold, '0.01')
65 self.assertEqual(multi_box_detection_node.keep_top_k, '200')
66 self.assertEqual(multi_box_detection_node.nms_threshold, '0.5')
67 self.assertEqual(multi_box_detection_node.num_classes, 21)
68 self.assertEqual(multi_box_detection_node.share_location, '1')
69 self.assertEqual(multi_box_detection_node.top_k, '200')
70 self.assertEqual(multi_box_detection_node.variance_encoded_in_target, '0')
72 def test_prior_box_infer_without_top_k(self):
73 graph = build_graph(nodes_attributes,
74 [('node_1', 'detection_output_1'),
75 ('node_2', 'detection_output_1'),
76 ('node_3', 'detection_output_1'),
77 ('detection_output_1', 'node_4')],
78 {'node_1': {'shape': np.array([1, 34928])},
79 'node_2': {'shape': np.array([1, 183372])},
80 'node_3': {'shape': np.array([1, 2, 34928])},
81 'detection_output_1': {"background_label_id": "0", "clip": "1",
82 "code_type": "caffe.PriorBoxParameter.CENTER_SIZE",
83 "confidence_threshold": "0.01", "keep_top_k": -1,
84 "nms_threshold": "0.5", "num_classes": "21",
85 "share_location": "1", "top_k": -1,
86 "variance_encoded_in_target": "0"},
87 'node_4': {'shape': np.array([1, 1, 69856, 7])},
90 multi_box_detection_node = Node(graph, 'detection_output_1')
92 multi_box_detection_infer(multi_box_detection_node)
93 exp_shape = np.array([1, 1, 8732, 7])
94 res_shape = graph.node['node_4']['shape']
95 for i in range(0, len(exp_shape)):
96 self.assertEqual(exp_shape[i], res_shape[i])
98 self.assertEqual(multi_box_detection_node.background_label_id, '0')
99 self.assertEqual(multi_box_detection_node.clip, '1')
100 self.assertEqual(multi_box_detection_node.code_type, 'caffe.PriorBoxParameter.CENTER_SIZE')
101 self.assertEqual(multi_box_detection_node.confidence_threshold, '0.01')
102 self.assertEqual(multi_box_detection_node.keep_top_k, 8732)
103 self.assertEqual(multi_box_detection_node.nms_threshold, '0.5')
104 self.assertEqual(multi_box_detection_node.num_classes, 21)
105 self.assertEqual(multi_box_detection_node.share_location, '1')
106 self.assertEqual(multi_box_detection_node.top_k, -1)
107 self.assertEqual(multi_box_detection_node.variance_encoded_in_target, '0')
109 def test_prior_box_infer_raise_error(self):
110 graph = build_graph(nodes_attributes,
111 [('node_1', 'detection_output_1'),
112 ('node_2', 'detection_output_1'),
113 ('node_3', 'detection_output_1'),
114 ('detection_output_1', 'node_4')],
115 {'node_1': {'shape': np.array([1, 34928])},
116 'node_2': {'shape': np.array([1, 183372])},
117 'node_3': {'shape': np.array([1, 3, 34928])},
118 'detection_output_1': {"background_label_id": "0", "clip": "1",
119 "code_type": "caffe.PriorBoxParameter.CENTER_SIZE",
120 "confidence_threshold": "0.01", "keep_top_k": -1,
121 "nms_threshold": "0.5", "num_classes": "21",
122 "share_location": "1", "top_k": -1,
123 "variance_encoded_in_target": 0},
124 'node_4': {'shape': np.array([1, 1, 69856, 7])},
127 multi_box_detection_node = Node(graph, 'detection_output_1')
129 self.assertIsNone(multi_box_detection_infer(multi_box_detection_node))