Publishing R3
[platform/upstream/dldt.git] / model-optimizer / mo / front / common / partial_infer / flatten.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 logging as log
18
19 import numpy as np
20
21 from mo.front.caffe.extractors.utils import get_canonical_axis_index
22
23
24 def flatten_infer(node):
25     """
26     Infers shape of flatten node as it is done in Caffe.
27     Output shape: [Batch is the same, Production of other dims]
28     Args:
29         node: graph flatten node
30
31     """
32     input_shape = node.in_node(0).shape
33     if input_shape is None:
34         return
35
36     # TODO: Should check that input_shape[1:] part doesn't contain -1 elements
37     axis = get_canonical_axis_index(input_shape, node.axis)
38     end_axis = node.end_axis if node.has('end_axis') else -1
39     end_axis = get_canonical_axis_index(input_shape, end_axis)
40     prod_axes = np.prod(input_shape[axis: end_axis + 1])
41     node.out_node(0).shape = np.array([*input_shape[0: axis], prod_axes, *input_shape[end_axis + 1:]], dtype=np.int64)
42     log.debug('input_shape: {}, output_shape: {}'.format(input_shape, node.out_node().shape))
43