Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / depth_to_space.py
1 """
2  Copyright (c) 2017-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 import numpy as np
20
21 from mo.front.common.partial_infer.utils import int64_array
22 from mo.graph.graph import Node, Graph
23 from mo.ops.op import Op
24
25
26 class DepthToSpaceOp(Op):
27     op = 'DepthToSpace'
28
29     def __init__(self, graph: Graph, attrs: dict):
30         mandatory_props = {
31             'op': __class__.op,
32             'in_ports_count': 1,
33             'out_ports_count': 1,
34             'infer': DepthToSpaceOp.depth_to_space_infer
35         }
36         super().__init__(graph, mandatory_props, attrs)
37
38     @staticmethod
39     def depth_to_space_infer(node: Node):
40         in_shape = node.in_node().shape
41         if in_shape.size != 4:
42             log.error('TensorFlow DepthToSpace operation is supported for 4D \'NHWC\' input layout only. '
43                       'Current input shape is \'{}\''.format(in_shape))
44             return
45         N, H, W, C = in_shape
46         block_size = node['block_size']
47         if C % (block_size ** 2):
48             log.error('Feature dimensions of input tensor of DepthToSpace operation have to be divisible by square of '
49                       'DepthToSpace \'block_size\' parameter. Input tensor shape = {}. Feature dimension = {}. '
50                       'block_size = {}'.format(in_shape, C, block_size))
51             return
52         out_shape = [N, int(H * block_size), int(W * block_size), int(C / (block_size ** 2))]
53         if np.prod(in_shape) != np.prod(out_shape):
54             return
55         node.out_node().shape = int64_array(out_shape)