Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / common / partial_infer / squeeze.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 import logging as log
17
18 import numpy as np
19
20 from mo.front.caffe.extractors.utils import get_canonical_axis_index
21 from mo.front.common.layout import get_height_dim, get_width_dim, get_depth_dim
22 from mo.front.common.partial_infer.utils import int64_array
23 from mo.ops.op import PermuteAttrs
24 from mo.utils.error import Error
25
26
27 def is_spatial_squeeze(layout: str, input_shape: np.ndarray, squeeze_dims: np.ndarray):
28     """
29     Checks that the squeeze operation removes all spatial dimensions.
30     :param layout: graph layout.
31     :param input_shape: numpy array with input shape.
32     :param squeeze_dims: numpy array with dims to squeeze.
33     :return: result of the check.
34     """
35     if len(input_shape) < 4 or len(input_shape) > 5:
36         return False
37     spatial_dims = [get_height_dim(layout, len(input_shape)), get_width_dim(layout, len(input_shape))]
38     if len(input_shape) == 5:
39         spatial_dims.append(get_depth_dim(layout, len(input_shape)))
40     for dim in spatial_dims:
41         if input_shape[dim] != 1:
42             log.debug('The reshape from "{}" with squeezed dims "{}" is not a spatial squeeze'.format(input_shape,
43                                                                                                       squeeze_dims))
44             return False
45     if len(squeeze_dims) != len(spatial_dims):
46         log.debug('The reshape from "{}" with squeezed dims "{}" is not a spatial squeeze'.format(input_shape,
47                                                                                                   squeeze_dims))
48         return False
49     log.debug('The reshape from "{}" with squeezed dims "{}" is not a spatial squeeze'.format(input_shape,
50                                                                                               squeeze_dims))
51     return True
52
53
54 def tf_squeeze_infer(node):
55     if node.squeeze_dims is None:
56         # TODO: implement; there is no implementation now because no test
57         return
58
59     real_squeeze_dims = []
60     input_shape = node.in_node().shape
61     if input_shape is None:
62         return
63     # UGLY
64     output_shape = input_shape.copy()
65     for n in node.squeeze_dims:
66         if output_shape[n] == 1:
67             real_squeeze_dims.append(get_canonical_axis_index(output_shape, n))
68         else:
69             raise Error('Trying to squeeze dimension not equal to 1 for node "{}"'.format(node.soft_get('name')))
70
71     output_shape = np.delete(output_shape, real_squeeze_dims)
72     node.out_node().shape = output_shape
73
74     if is_spatial_squeeze(node.graph.graph['layout'], input_shape, output_shape):
75         output_shape = int64_array([0, -1])
76     node['dim'] = output_shape
77     if node.in_node().value is not None:
78         node.out_node().value = np.array(np.reshape(node.in_node().value, output_shape))
79
80     PermuteAttrs.create_permute_attrs(node, attrs=[('dim', 'output:0')])