Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / priorbox_clustered.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 networkx as nx
18 import numpy as np
19
20 from mo.front.common.layout import get_width_dim, get_height_dim
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 PriorBoxClusteredOp(Op):
27     op = 'PriorBoxClustered'
28
29     def __init__(self, graph: Graph, attrs: dict):
30         mandatory_props = {
31             'type': __class__.op,
32             'op': __class__.op,
33             'in_ports_count': 2,
34             'out_ports_count': 1,
35             'infer': PriorBoxClusteredOp.priorbox_clustered_infer
36         }
37         super().__init__(graph, mandatory_props, attrs)
38
39     def supported_attrs(self):
40         return [
41             'width',
42             'height',
43             'flip',
44             'clip',
45             'variance',
46             'img_size',
47             'img_h',
48             'img_w',
49             'step',
50             'step_h',
51             'step_w',
52             'offset'
53         ]
54
55     def backend_attrs(self):
56         return [
57             'flip',
58             'clip',
59             'img_size',
60             'img_h',
61             'img_w',
62             'step',
63             'step_h',
64             'step_w',
65             'offset',
66             ('variance', lambda node: attr_getter(node, 'variance')),
67             ('width', lambda node: attr_getter(node, 'width')),
68             ('height', lambda node: attr_getter(node, 'height'))
69         ]
70
71     @staticmethod
72     def priorbox_clustered_infer(node: Node):
73         layout = node.graph.graph['layout']
74         data_shape = node.in_node(0).shape
75         num_ratios = len(node.width)
76
77         res_prod = data_shape[get_height_dim(layout, 4)] * data_shape[get_width_dim(layout, 4)] * num_ratios * 4
78         node.out_node(0).shape = np.array([1, 2, res_prod], dtype=np.int64)