Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / priorbox.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.common.layout import get_width_dim, get_height_dim
20 from mo.front.extractor import attr_getter
21 from mo.graph.graph import Node, Graph
22 from mo.ops.op import Op
23
24
25 class PriorBoxOp(Op):
26     op = 'PriorBox'
27
28     def __init__(self, graph: Graph, attrs: dict):
29         mandatory_props = {
30             'type': __class__.op,
31             'op': __class__.op,
32             'flip': 1,
33             'max_size': np.array([]),
34             'min_size': np.array([]),
35             'aspect_ratio': np.array([]),
36             'in_ports_count': 2,
37             'out_ports_count': 1,
38             'infer': PriorBoxOp.priorbox_infer
39         }
40         super().__init__(graph, mandatory_props, attrs)
41
42     def supported_attrs(self):
43         return [
44             'min_size',
45             'max_size',
46             'aspect_ratio',
47             'flip',
48             'clip',
49             'variance',
50             'img_size',
51             'img_h',
52             'img_w',
53             'step',
54             'step_h',
55             'step_w',
56             'offset',
57         ]
58
59     def backend_attrs(self):
60         return [
61             'flip',
62             'clip',
63             'step',
64             'offset',
65             ('min_size', lambda node: attr_getter(node, 'min_size')),
66             ('max_size', lambda node: attr_getter(node, 'max_size')),
67             ('aspect_ratio', lambda node: attr_getter(node, 'aspect_ratio')),
68             ('variance', lambda node: attr_getter(node, 'variance')),
69         ]
70
71     @staticmethod
72     def priorbox_infer(node: Node):
73         layout = node.graph.graph['layout']
74         data_shape = node.in_node(0).shape
75
76         # calculate all different aspect_ratios (the first one is always 1)
77         # in aspect_ratio 1/x values will be added for all except 1 if flip is True
78         ar_seen = [1.0]
79         ar_seen.extend(node.aspect_ratio.copy())
80         if node.flip:
81             for s in node.aspect_ratio:
82                 ar_seen.append(1.0/s)
83
84         ar_seen = np.unique(np.array(ar_seen).round(decimals=6))
85         
86         num_ratios = 0
87         if len(node.min_size) > 0:
88             num_ratios = len(ar_seen) * len(node.min_size)
89
90         num_ratios = num_ratios + len(node.max_size)
91
92         res_prod = data_shape[get_height_dim(layout, 4)] * data_shape[get_width_dim(layout, 4)] * num_ratios * 4
93         node.out_node(0).shape = np.array([1, 2, res_prod], dtype=np.int64)