Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / regionyolo.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 numpy as np
18
19 from mo.front.caffe.extractors.utils import get_canonical_axis_index
20 from mo.front.common.layout import get_batch_dim, get_height_dim, get_width_dim, shape_for_layout
21 from mo.front.extractor import attr_getter
22 from mo.graph.graph import Node, Graph
23 from mo.ops.op import Op
24
25
26 class RegionYoloOp(Op):
27     op = 'RegionYolo'
28
29     def __init__(self, graph: Graph, attrs: Node):
30         mandatory_props = {
31             'type': __class__.op,
32             'op': __class__.op,
33             'in_ports_count': 1,
34             'out_ports_count': 1,
35             'infer': RegionYoloOp.regionyolo_infer
36         }
37         super().__init__(graph, mandatory_props, attrs)
38
39     def supported_attrs(self):
40         return [
41             'coords',
42             'classes',
43             'num',
44             'axis',
45             'end_axis',
46             'do_softmax',
47             'anchors',
48             'mask'
49         ]
50
51     def backend_attrs(self):
52         return [
53             'coords',
54             'classes',
55             'num',
56             'axis',
57             'end_axis',
58             'do_softmax',
59             ('anchors', lambda node: attr_getter(node, 'anchors')),
60             ('mask', lambda node: attr_getter(node, 'mask'))
61         ]
62
63     @staticmethod
64     def regionyolo_infer(node: Node):
65         input_shape = node.in_node(0).shape
66         if input_shape is None:
67             return
68         axis = get_canonical_axis_index(input_shape, node.axis)
69         end_axis = get_canonical_axis_index(input_shape, node.end_axis)
70         node.axis = axis
71         node.end_axis = end_axis
72         if node.do_softmax:
73             flat_dim = np.prod(input_shape[axis: end_axis + 1])
74             node.out_node().shape = np.array([*input_shape[:axis], flat_dim, *input_shape[end_axis + 1:]])
75         else:
76             layout = node.graph.graph['layout']
77             assert len(layout) == 4
78
79             node.out_node().shape = shape_for_layout(layout,
80                                                      batch=input_shape[get_batch_dim(layout, 4)],
81                                                      features=(node.classes + node.coords + 1) * len(node.mask),
82                                                      height=input_shape[get_height_dim(layout, 4)],
83                                                      width=input_shape[get_width_dim(layout, 4)])