Publishing R3
[platform/upstream/dldt.git] / model-optimizer / mo / front / common / partial_infer / expand_dims.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
22 def tf_expand_dims_infer(node):
23     input_node = node.in_nodes()[0]
24     output_node = node.out_node()
25     if input_node.shape is None:
26         return
27
28     # TensorFlow style with dynamic input
29     if len(node.in_nodes()) > 1:
30         axis_node = node.in_nodes()[1]
31         if isinstance(axis_node.value, np.ndarray) and axis_node.value.size > 1:
32             log.error("ExpandDims operation : axis should be scalar")
33             return
34         expand_axis = axis_node.value.item()
35         node.graph.remove_edge(axis_node.id, node.id)
36     else:
37         if not node.has_valid('expand_axis'):
38             log.error("ExpandDims axis is not defined")
39             return
40         expand_axis = node.expand_axis
41
42     if expand_axis is None:
43         return
44
45     output_node.shape = np.insert(input_node.shape, expand_axis, [1])
46     # convert data type of the shape to int64 explicitly
47     output_node.shape = output_node.shape.astype(np.int64)
48     if input_node.value is not None:
49         output_node.value = np.array(np.reshape(input_node.value, output_node.shape))
50
51     node['axis'] = 0
52     node['num_axes'] = -1
53     node['dim'] = output_node.shape