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 extensions.ops.regionyolo import RegionYoloOp
22 from mo.front.common.extractors.utils import layout_attrs
23 from mo.graph.graph import Node
24 from mo.utils.unittest.graph import build_graph
26 nodes_attributes = {'node_1': {'type': 'Identity', 'kind': 'op'},
27 'region': {'type': 'RegionYolo', 'kind': 'op'},
28 'node_3': {'type': 'Identity', 'kind': 'op'},
29 'op_output': { 'kind': 'op', 'op': 'OpOutput'}
33 class TestRegionYOLOCaffe(unittest.TestCase):
34 def test_region_infer(self):
35 graph = build_graph(nodes_attributes,
36 [('node_1', 'region'),
38 ('node_3', 'op_output')
40 {'node_3': {'shape': None},
41 'node_1': {'shape': np.array([1, 3, 227, 227])},
42 'region': {'axis': 1, 'end_axis': -1, 'do_softmax': 1, **layout_attrs()}
44 graph.graph['layout'] = 'NCHW'
45 reorg_node = Node(graph, 'region')
46 RegionYoloOp.regionyolo_infer(reorg_node)
47 exp_shape = np.array([1, 3 * 227 * 227])
48 res_shape = graph.node['node_3']['shape']
49 for i in range(0, len(exp_shape)):
50 self.assertEqual(exp_shape[i], res_shape[i])
52 def test_region_infer_flatten(self):
53 graph = build_graph(nodes_attributes,
54 [('node_1', 'region'),
56 ('node_3', 'op_output')
58 {'node_3': {'shape': None},
59 'node_1': {'shape': np.array([1, 3, 227, 227])},
60 'region': {'end_axis': 1, 'axis': 0, 'do_softmax': 1, **layout_attrs()}
62 graph.graph['layout'] = 'NCHW'
63 reorg_node = Node(graph, 'region')
64 RegionYoloOp.regionyolo_infer(reorg_node)
65 exp_shape = np.array([1 * 3, 227, 227])
66 res_shape = graph.node['node_3']['shape']
67 for i in range(0, len(exp_shape)):
68 self.assertEqual(exp_shape[i], res_shape[i])
70 def test_region_infer_flatten_again(self):
71 graph = build_graph(nodes_attributes,
72 [('node_1', 'region'),
74 ('node_3', 'op_output')
76 {'node_3': {'shape': None},
77 'node_1': {'shape': np.array([1, 3, 227, 227])},
78 'region': {'end_axis': 2, 'axis': 0, 'do_softmax': 1, **layout_attrs()}
80 graph.graph['layout'] = 'NCHW'
81 reorg_node = Node(graph, 'region')
82 RegionYoloOp.regionyolo_infer(reorg_node)
83 exp_shape = np.array([1 * 3 * 227, 227])
84 res_shape = graph.node['node_3']['shape']
85 for i in range(0, len(exp_shape)):
86 self.assertEqual(exp_shape[i], res_shape[i])
88 def test_region_infer_do_softmax(self):
89 graph = build_graph(nodes_attributes,
90 [('node_1', 'region'),
92 ('node_3', 'op_output')
94 {'node_3': {'shape': None},
95 'node_1': {'shape': np.array([1, 3, 227, 227])},
96 'region': {'do_softmax': 0, 'end_axis': -1, 'axis': 1, 'classes': 80, 'coords': 4,
97 'mask': np.array([6, 7, 8]), **layout_attrs()}
100 graph.graph['layout'] = 'NCHW'
101 reorg_node = Node(graph, 'region')
102 RegionYoloOp.regionyolo_infer(reorg_node)
103 exp_shape = np.array([1, (80 + 4 + 1) * 3, 227, 227])
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])
109 class TestRegionYOLOTF(unittest.TestCase):
110 def test_region_infer(self):
111 graph = build_graph(nodes_attributes,
112 [('node_1', 'region'),
113 ('region', 'node_3'),
114 ('node_3', 'op_output')
116 {'node_3': {'shape': None},
117 'node_1': {'shape': np.array([1, 227, 227, 3])},
118 'region': {'axis': 1, 'end_axis': -1, 'do_softmax': 1, **layout_attrs()}
120 graph.graph['layout'] = 'NHWC'
121 reorg_node = Node(graph, 'region')
122 RegionYoloOp.regionyolo_infer(reorg_node)
123 exp_shape = np.array([1, 3 * 227 * 227])
124 res_shape = graph.node['node_3']['shape']
125 for i in range(0, len(exp_shape)):
126 self.assertEqual(exp_shape[i], res_shape[i])
128 def test_region_infer_do_softmax(self):
129 graph = build_graph(nodes_attributes,
130 [('node_1', 'region'),
131 ('region', 'node_3'),
132 ('node_3', 'op_output')
134 {'node_3': {'shape': None},
135 'node_1': {'shape': np.array([1, 227, 227, 3])},
136 'region': {'do_softmax': 0, 'end_axis': -1, 'axis': 1, 'classes': 80, 'coords': 4,
137 'mask': np.array([6, 7, 8]), **layout_attrs()}
140 graph.graph['layout'] = 'NHWC'
141 reorg_node = Node(graph, 'region')
142 RegionYoloOp.regionyolo_infer(reorg_node)
143 exp_shape = np.array([1, 227, 227, (80 + 4 + 1) * 3])
144 res_shape = graph.node['node_3']['shape']
145 for i in range(0, len(exp_shape)):
146 self.assertEqual(exp_shape[i], res_shape[i])