Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / common / partial_infer / roipooling.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 logging as log
18 import numpy as np
19
20 from mo.front.common.layout import get_batch_dim, get_features_dim, get_height_dim, get_width_dim, shape_for_layout
21 from mo.graph.graph import Node
22
23
24 def roipooling_infer(node: Node):
25     """
26     Sets shape of output node according specified parameters input blobs and node
27     Sets number from the first input blob, channels from the second one, height and width are specified
28     Parameters
29     ----------
30     node
31     """
32     shapes = [node.in_node(i).shape for i in range(len(node.in_nodes()))]
33     if any(s is None for s in shapes):
34         return
35     if len(node.in_nodes()) == 4:  # TensorFlow case of CropAndResize operation
36         crop_size = node.in_node(3).value
37         if crop_size is None:
38             log.error('The ROIPooling size is not known for node {}'.format(node.soft_get('name')))
39             return
40         if not isinstance(crop_size, np.ndarray) or len(crop_size) != 2:
41             log.error('The ROIPooling size is should have 2 elements for node {}'.format(node.soft_get('name')))
42         node.pooled_h = crop_size[0]
43         node.pooled_w = crop_size[1]
44         node.graph.remove_edge(node.in_node(3).id, node.id)
45         node.graph.remove_edge(node.in_node(2).id, node.id)
46
47     layout = node.graph.graph['layout']
48     assert len(layout) == 4
49
50     node.out_node().shape = shape_for_layout(layout,
51                                              batch=shapes[1][get_batch_dim(layout, 4)],
52                                              features=shapes[0][get_features_dim(layout, 4)],
53                                              height=node.pooled_h,
54                                              width=node.pooled_w)