Publishing R3
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / priorbox_clustered.py
1 """
2  Copyright (c) 2018 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.extractor import attr_getter
21 from mo.graph.graph import Node
22 from mo.ops.op import Op
23
24
25 class PriorBoxClusteredOp(Op):
26     op = 'PriorBoxClustered'
27
28     def __init__(self, graph: nx.MultiDiGraph, attrs: dict):
29         mandatory_props = {
30             'type': __class__.op,
31             'op': __class__.op,
32             'infer': PriorBoxClusteredOp.priorbox_clustered_infer
33         }
34         super().__init__(graph, mandatory_props, attrs)
35
36     def supported_attrs(self):
37         return [
38             'width',
39             'height',
40             'flip',
41             'clip',
42             'variance',
43             'img_size',
44             'img_h',
45             'img_w',
46             'step',
47             'step_h',
48             'step_w',
49             'offset'
50         ]
51
52     def backend_attrs(self):
53         return [
54             'flip',
55             'clip',
56             'img_size',
57             'img_h',
58             'img_w',
59             'step',
60             'step_h',
61             'step_w',
62             'offset',
63             ('variance', lambda node: attr_getter(node, 'variance')),
64             ('width', lambda node: attr_getter(node, 'width')),
65             ('height', lambda node: attr_getter(node, 'height'))
66         ]
67
68     @staticmethod
69     def priorbox_clustered_infer(node: Node):
70         data_shape = node.in_node(0).shape
71         num_ratios = len(node.width)
72
73         res_prod = data_shape[2] * data_shape[3] * num_ratios * 4
74         node.out_node(0).shape = np.array([1, 2, res_prod], dtype=np.int64)