Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / regionyolo_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.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
25
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'}
30                     }
31
32
33 class TestRegionYOLOCaffe(unittest.TestCase):
34     def test_region_infer(self):
35         graph = build_graph(nodes_attributes,
36                             [('node_1', 'region'),
37                              ('region', 'node_3'),
38                              ('node_3', 'op_output')
39                              ],
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()}
43                              })
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])
51
52     def test_region_infer_flatten(self):
53         graph = build_graph(nodes_attributes,
54                             [('node_1', 'region'),
55                              ('region', 'node_3'),
56                              ('node_3', 'op_output')
57                              ],
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()}
61                              })
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])
69
70     def test_region_infer_flatten_again(self):
71         graph = build_graph(nodes_attributes,
72                             [('node_1', 'region'),
73                              ('region', 'node_3'),
74                              ('node_3', 'op_output')
75                              ],
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()}
79                              })
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])
87
88     def test_region_infer_do_softmax(self):
89         graph = build_graph(nodes_attributes,
90                             [('node_1', 'region'),
91                              ('region', 'node_3'),
92                              ('node_3', 'op_output')
93                              ],
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()}
98                              })
99
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])
107
108
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')
115                              ],
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()}
119                              })
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])
127
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')
133                              ],
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()}
138                              })
139
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])