69c861712453adf5e5ae82d4aaf079a560b57d1c
[platform/upstream/dldt.git] / model-optimizer / extensions / front / mxnet / pooling_ext.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
19 from mo.front.common.extractors.utils import layout_attrs
20 from mo.front.extractor import FrontExtractorOp
21 from mo.front.mxnet.extractors.utils import get_mxnet_layer_attrs
22 from mo.ops.pooling import Pooling
23
24
25 class PoolingFrontExtractor(FrontExtractorOp):
26     op = 'Pooling'
27     enabled = True
28
29     @staticmethod
30     def extract(node):
31         attrs = get_mxnet_layer_attrs(node.symbol_dict)
32
33         kernel = attrs.tuple("kernel", int, None)
34         stride = attrs.tuple("stride", int, (1, 1))
35         padding = attrs.tuple("pad", int, (0, 0))
36         method = attrs.str("pool_type", None)
37         rt = 'floor'
38
39         data = {
40             'window': np.array([1, 1, kernel[0], kernel[1]], dtype=np.int64),
41             'stride': np.array([1, 1, stride[0], stride[1]], dtype=np.int64),
42             'pad': np.array([[0, 0], [0, 0], [padding[0], padding[0]], [padding[1], padding[1]]], dtype=np.int64),
43             'pad_spatial_shape': np.array([[padding[0], padding[0]], [padding[1], padding[1]]], dtype=np.int64),
44             'pool_method': method,
45             'exclude_pad': 'false',
46             'output_spatial_shape': None,
47             'rounding_type': rt,
48         }
49
50         data.update(layout_attrs())
51
52         pooling_conv = attrs.str("pooling_convention", 'valid')
53         if pooling_conv:
54             data["pooling_convention"] = pooling_conv
55             if pooling_conv == 'full':
56                 data["rounding_type"] = 'ceil'
57
58         global_pool = attrs.bool("global_pool", False)
59         if global_pool:
60             data["global_pool"] = global_pool
61
62         # update the attributes of the node
63         Pooling.update_node_stat(node, data)
64         return __class__.enabled