Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / tf / 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.partial_infer.utils import convert_tf_padding_to_str
19 from mo.front.extractor import FrontExtractorOp
20 from mo.front.tf.extractors.utils import tf_data_format_spatial, tf_data_format_channel, tf_data_format_batch, \
21     tf_int_list
22 from mo.ops.pooling import Pooling
23
24
25 class AvgPoolFrontExtractor(FrontExtractorOp):
26     op = 'AvgPool'
27     enabled = True
28
29     @staticmethod
30     def extract(node):
31         attrs = create_pooling_attrs(node, 'avg')
32         attrs.update({'op': __class__.op})
33         # update the attributes of the node
34         Pooling.update_node_stat(node, attrs)
35         return __class__.enabled
36
37
38 class MaxPoolFrontExtractor(FrontExtractorOp):
39     op = 'MaxPool'
40     enabled = True
41
42     @staticmethod
43     def extract(node):
44         attrs = create_pooling_attrs(node, 'max')
45         attrs.update({'op': __class__.op})
46         # update the attributes of the node
47         Pooling.update_node_stat(node, attrs)
48         return __class__.enabled
49
50
51 class MaxPool3DFrontExtractor(FrontExtractorOp):
52     op = 'MaxPool3D'
53     enabled = True
54
55     @staticmethod
56     def extract(node):
57         attrs = create_pooling_attrs(node, 'max')
58         attrs.update({'op': __class__.op})
59         # update the attributes of the node
60         Pooling.update_node_stat(node, attrs)
61         return __class__.enabled
62
63
64 class AvgPool3DFrontExtractor(FrontExtractorOp):
65     op = 'AvgPool3D'
66     enabled = True
67
68     @staticmethod
69     def extract(node):
70         attrs = create_pooling_attrs(node, 'avg')
71         attrs.update({'op': __class__.op})
72         # update the attributes of the node
73         Pooling.update_node_stat(node, attrs)
74         return __class__.enabled
75
76
77 def create_pooling_attrs(node, pool_method):
78     data_format = node.pb.attr["data_format"]
79
80     attrs = {
81         'auto_pad': convert_tf_padding_to_str(node.pb.attr['padding']),
82         'window': tf_int_list(node.pb.attr["ksize"].list),
83         'spatial_dims': tf_data_format_spatial(data_format),
84         'pad': None,  # will be inferred when input shape is known
85         'stride': tf_int_list(node.pb.attr["strides"].list),
86         'pad_spatial_shape': None,
87         'output_spatial_shape': None,
88         'pool_method': pool_method,
89         'type': 'Pooling',
90         'layout': data_format.s.decode(),
91         'exclude_pad': 'true',
92     }
93     return attrs