Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / resample.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
19 from extensions.ops.resize_factor_utils import factor_update
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, Graph
22 from mo.ops.op import Op
23
24
25 class ResampleOp(Op):
26     op = 'Resample'
27
28     def __init__(self, graph: Graph, attrs: dict):
29         mandatory_props = {
30             'type': __class__.op,
31             'op': __class__.op,
32             'factor': None,
33             'in_ports_count': 2,
34             'out_ports_count': 1,
35             'infer': ResampleOp.resample_infer
36         }
37         super().__init__(graph, mandatory_props, attrs)
38
39     def supported_attrs(self):
40         return [
41             'antialias',
42             'height',
43             'width',
44             'resample_type',
45             'factor',
46         ]
47
48     def backend_attrs(self):
49         return [
50             'antialias',
51             'height',
52             'width',
53             ('type', 'resample_type'),
54             'factor'
55         ]
56
57     @staticmethod
58     def resample_infer(node: Node):
59         layout = node.graph.graph['layout']
60         assert len(layout) == 4
61
62         input_shape = node.in_node(0).shape
63         if input_shape is None:
64             return
65         in_height = input_shape[get_height_dim(layout, 4)]
66         in_width = input_shape[get_width_dim(layout, 4)]
67
68         if node.has('fw') and node.fw == 'tf':
69             dst_shape = node.in_node(1).value
70             if dst_shape is None or len(input_shape) != 4 or len(dst_shape) != 2:
71                 log.error(
72                     'Node {} with op {} cannot be converted to Resample layer because there is no enough info about '
73                     'src/dst shapes: src_shape = {}, dst_shape = {}'.format(node.name, node.op, input_shape, dst_shape))
74                 node.type = None  # prevent translation to a valid IE layer
75                 return
76             out_height = dst_shape[0]
77             out_width = dst_shape[1]
78             node.graph.remove_edge(node.in_node(1).id, node.id)
79         else:
80             if len(node.in_nodes()) == 1:
81                 if node.has('width') and node.has('height'):
82                     out_height = node.height
83                     out_width = node.width
84                 else:
85                     out_height = node.factor * in_height
86                     out_width = node.factor * in_width
87             else:
88                 out_height = node.in_node(1).shape[get_height_dim(layout, 4)]
89                 out_width = node.in_node(1).shape[get_width_dim(layout, 4)]
90
91         node.factor = factor_update(
92             node.factor,
93             [float(out_height) / in_height, float(out_width) / in_width],
94             [in_height, in_width],
95             [out_height, out_width],
96             node.soft_get('name'))
97
98         node.out_node().shape = shape_for_layout(layout,
99                                                  batch=input_shape[get_batch_dim(layout, 4)],
100                                                  features=input_shape[get_features_dim(layout, 4)],
101                                                  height=out_height,
102                                                  width=out_width)