Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / caffe / 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.caffe.extractors.utils import get_spatial_attr
19 from mo.front.common.extractors.utils import layout_attrs
20 from mo.front.extractor import FrontExtractorOp
21 from mo.ops.pooling import Pooling
22
23
24 class PoolingFrontExtractor(FrontExtractorOp):
25     op = 'pooling'
26     enabled = True
27
28     @staticmethod
29     def extract(node):
30         proto_layer = node.pb
31         param = proto_layer.pooling_param
32
33         method = 'max'
34         exclude_pad = 'true'
35         kernel = [0, 0]
36         stride = [1, 1]
37         padding = [0, 0]
38         global_pooling = False
39
40         if hasattr(param, 'global_pooling') and param.global_pooling:
41             global_pooling = param.global_pooling
42         else:
43             kernel = get_spatial_attr(kernel, 'kernel_size', 'kernel', param)
44             padding = get_spatial_attr(padding, 'pad', 'pad', param)
45             stride = get_spatial_attr(stride, 'stride', 'stride', param)
46
47         if param.pool == 0:
48             method = 'max'
49             exclude_pad = 'true'
50         elif param.pool == 1:
51             method = 'avg'
52             exclude_pad = 'false'
53         else:
54             raise ValueError('Unknown Pooling Method!')
55
56         pooling_convention = 'full'  # for Caffe rounding type should be ceil
57         rt = 'ceil'
58
59         if hasattr(param, 'ceil_mode') and not param.ceil_mode:
60             # If pooling has ceil_mode and ceil_mode is False using floor for rounding shapes in partial_infer
61             pooling_convention = 'valid'
62             rt = 'floor'
63
64         attrs = {
65             'window': np.array([1, 1, kernel[1], kernel[0]], dtype=np.int64),
66             'stride': np.array([1, 1, stride[1], stride[0]], dtype=np.int64),
67             'pad': np.array([[0, 0], [0, 0], [padding[1], padding[1]], [padding[0], padding[0]]], dtype=np.int64),
68             'pad_spatial_shape': np.array([[padding[1], padding[1]], [padding[0], padding[0]]], dtype=np.int64),
69             'pool_method': method,
70             'exclude_pad': exclude_pad,
71             'global_pool': global_pooling,
72             'output_spatial_shape': None,
73             'rounding_type': rt
74         }
75
76         attrs.update(layout_attrs())
77         attrs['pooling_convention'] = pooling_convention
78
79         # update the attributes of the node
80         Pooling.update_node_stat(node, attrs)
81         return __class__.enabled