Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / kaldi / add_reshape_around_convolution.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 from mo.front.common.replacement import FrontReplacementOp
18 from mo.graph.graph import Node, Graph
19 from mo.ops.convolution import Convolution
20 from mo.ops.reshape import Reshape
21
22
23 class ReplaceConvolutionReshape(FrontReplacementOp):
24     """
25        This pass adds Reshapes around a Convolution layer for reshaping from NH to NCHW
26        For example:
27            Let's suppose we have next graph:
28
29            Prev_Layer [N, H] -> Convolution [N, C, H, W] -> Next_Layer [N, H]
30
31            In this case Convolution takes only [N, H] from input tensor in 3rd dim
32            So this pass will convert this graph to the next one:
33
34            Prev_Layer [N, H] -> Reshape [N, 1, H, 1] -> Convolution [N, C=1, H, W=1] -> Reshape [N, 1, H, 1] -> Next_Layer [N, H]
35
36    """
37     op = "Convolution"
38     enabled = True
39
40     def replace_op(self, graph: Graph, node: Node):
41         input_node = node.in_node(0)
42         port = graph.get_edge_data(input_node.id, node.id)[0]['out']
43         input_reshape_node = Reshape(graph,
44                                      {
45                                          'name': '/Reshape/' + node.name,
46                                          'axis': 1,
47                                          'infer': Reshape.kaldi_infer
48                                      }).create_node([(input_node, port)])
49
50         convolution_node = Convolution(graph,
51                                        node.attrs()
52                                        ).create_node([input_reshape_node])
53
54         output_reshape_node = Reshape(graph,
55                                       {
56                                           'name': node.name + '/Reshape/',
57                                           'axis': 1,
58                                           'infer': Reshape.kaldi_infer
59                                       }).create_node([convolution_node])
60
61         return [output_reshape_node.id]