updated readme files (#54)
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / PadToPoolingMiddleReplacer.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 numpy as np
18 import networkx as nx
19
20 from mo.ops.pooling import Pooling
21 from mo.graph.graph import unique_id
22 from mo.middle.replacement import MiddleReplacementPattern
23 from mo.front.common.layout import get_features_dim
24
25 class PadToPoolingMiddleReplacer(MiddleReplacementPattern):
26     op = "Pad"
27     enabled = False
28
29     def pattern(self):
30         return dict(
31             nodes=[
32                 ('pad', dict(kind='op', op='Pad'))
33             ],
34             edges=[]
35         )
36
37     def replace_pattern(self, graph: nx.MultiDiGraph, match: dict):
38         node = match['pad']
39         input = node.in_node()
40         output = node.out_node()
41         if len(output.out_nodes()) > 0:
42             ndim = len(input.shape)
43             pad = node.pads
44             graph.remove_edge(input.id, node.id)
45             graph.remove_edge(node.id, output.id)
46             pool_node = unique_id(graph, node.name + '/Pool_')
47             Pooling(graph, dict(name=pool_node, window=np.ones(ndim, dtype=np.int64),
48                                 output_spatial_shape=None,
49                                 batch_dims=np.array([0], dtype=np.int64),
50                                 channel_dims=np.array([get_features_dim(graph.graph['layout'], ndim)], dtype=np.int64),
51                                 stride=np.array(np.ones(ndim, dtype=np.int64)),
52                                 pad=pad, exclude_pad='false', pool_method='max')).create_node_with_data(inputs=[input], data_nodes=[output])