Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / kaldi / extractors / max_pooling_ext.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 import numpy as np
17
18 from mo.front.common.extractors.utils import layout_attrs
19 from mo.front.extractor import FrontExtractorOp
20 from mo.front.kaldi.loader.utils import read_token_value, collect_until_whitespace, collect_until_token, \
21     read_binary_integer32_token, find_next_tag, read_placeholder
22 from mo.ops.pooling import Pooling
23 from mo.utils.error import Error
24
25
26 class MaxPoolingComponentFrontExtractor(FrontExtractorOp):
27     op = 'maxpoolingcomponent'
28     enabled = True
29
30     @staticmethod
31     def extract(node):
32         pb = node.parameters
33         collect_until_token(pb, b'<PoolSize>')
34         kernel = read_binary_integer32_token(pb)
35         tag = find_next_tag(pb)
36         if tag == '<PoolStep>':
37             read_placeholder(pb, 1)
38             stride = read_binary_integer32_token(pb)
39             pool_step = stride
40             pool_stride = read_token_value(pb, b'<PoolStride>')
41         elif tag == '<PoolStride>':
42             stride = 1
43             pool_step = None
44             read_placeholder(pb, 1)
45             pool_stride = read_binary_integer32_token(pb)
46         else:
47             raise Error('Can not extract parameters for {}'.format(node))
48
49         mapping_rule = {
50             'window': np.array([1, 1, 1, kernel], dtype=np.int64),
51             'stride': np.array([1, 1, stride, stride], dtype=np.int64),
52             'pool_stride': pool_stride,
53             'pool_step': pool_step,
54             'pad': np.array([[0, 0], [0, 0], [0, 0], [0, 0]], dtype=np.int64),
55             'pad_spatial_shape': np.array([[0, 0], [0, 0]], dtype=np.int64),
56             'pool_method': 'max',
57         }
58         mapping_rule.update(layout_attrs())
59         Pooling.update_node_stat(node, mapping_rule)
60         return __class__.enabled