6c9299b1411f1a91163c7b7788ef6a326f010866
[platform/upstream/dldt.git] / model-optimizer / mo / ops / pooling.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 networkx as nx
18 import numpy as np
19
20 from mo.front.common.partial_infer.utils import tf_window_op_pad_infer
21 from mo.front.extractor import attr_getter
22 # from mo.front.common.partial_infer.pooling import pool_explicit_padding_infer
23 from mo.front.extractor import spatial_getter
24 from mo.front.onnx.extractors.utils import get_backend_pad
25 from mo.graph.graph import Node
26 from mo.ops.op import Op, PermuteAttrs
27
28
29 class Pooling(Op):
30     op = 'Pooling'
31
32     def __init__(self, graph: nx.MultiDiGraph, attrs: dict):
33         super().__init__(graph, {
34             'kind': 'op',
35             'type': __class__.op,
36             'op': __class__.op,
37             'infer': __class__.infer,
38         }, attrs)
39
40     def backend_attrs(self):
41         return [
42             ('strides', lambda node: ','.join(map(str, node['stride'][node.spatial_dims]))),
43             ('kernel', lambda node: ','.join(map(str, node['window'][node.spatial_dims]))),
44
45             ('pads_begin', lambda node: ','.join(map(str, get_backend_pad(node.pad, node.spatial_dims, 0)))),
46             ('pads_end', lambda node: ','.join(map(str, get_backend_pad(node.pad, node.spatial_dims, 1)))),
47
48             ('pool-method', 'pool_method'),
49             ('exclude-pad', 'exclude_pad'),
50
51             'rounding_type',
52             'auto_pad',
53         ]
54
55     def backend_attrs_v2(self):
56         return [
57             ('stride', lambda node: attr_getter(node, 'stride')),
58
59             spatial_getter('stride-x', 'stride', 1),
60             spatial_getter('stride-y', 'stride', 0),
61             spatial_getter('kernel-x', 'window', 1),
62             spatial_getter('kernel-y', 'window', 0),
63             spatial_getter('pad-x', 'pad', 1, lambda x: x[0]),
64             spatial_getter('pad-y', 'pad', 0, lambda x: x[0]),
65
66             ('pool-method', 'pool_method'),
67             ('exclude-pad', 'exclude_pad'),
68
69             'rounding_type',
70             'auto_pad',
71         ]
72
73     @staticmethod
74     def infer(node: Node):
75         assert (len(node.in_nodes()) == 1)
76         input_shape = node.in_node(0).shape
77         if input_shape is None:
78             return
79
80         if not node.has_valid('spatial_dims'):
81             node['spatial_dims'] = np.delete([x for x in range(len(input_shape))],
82                                              [node.batch_dims[0], node.channel_dims[0]])
83
84         input_spatial_shape = input_shape[node.spatial_dims]
85
86         # Setting default pad and stride attrs in case of None specified
87         if not node.has_valid('pad'):
88             node['pad'] = np.array([[0, 0] for x in range(len(input_shape))], dtype=np.int64)
89         if not node.has_valid('pad_spatial_shape'):
90             node['pad_spatial_shape'] = node.pad[node.spatial_dims]
91         if not node.has_valid('stride'):
92             node['stride'] = np.array([1 for x in range(len(input_shape))], dtype=np.int64)
93
94         if node.has_and_set('global_pool'):
95             node.window[node.spatial_dims] = input_spatial_shape
96         window_spatial_shape = node.window[node.spatial_dims]
97         stride_spatial = node.stride[node.spatial_dims]
98         assert any(stride_spatial), 'Stride can not be zero in node {}'.format(node.id)
99
100         if node.has_valid('auto_pad'):
101             node.pad_spatial_shape, node.output_spatial_shape = tf_window_op_pad_infer(input_spatial_shape,
102                                                                                        window_spatial_shape,
103                                                                                        stride_spatial, node.auto_pad)
104             pad = np.zeros((len(input_shape), 2), dtype=np.int64)
105             pad[node.spatial_dims] = node.pad_spatial_shape
106             node.pad = pad
107         else:
108
109             pad_spatial_shape = np.add.reduce(node.pad_spatial_shape, axis=1)
110
111             rounding = np.floor
112             if node.has_valid('pooling_convention') and node.pooling_convention == 'full':
113                 rounding = np.ceil
114             output_spatial_shape = np.array(rounding(
115                 np.array(input_spatial_shape + pad_spatial_shape - window_spatial_shape,
116                          dtype=np.float) / stride_spatial),
117                 dtype=np.int64) + 1
118
119             original_pads = np.array([i[1] for i in node.pad_spatial_shape])
120
121             for i in range(len(input_spatial_shape)):
122                 if original_pads[i] and (output_spatial_shape[i] - 1) * stride_spatial[i] >= \
123                         input_spatial_shape[i] + original_pads[i]:
124                     output_spatial_shape[i] -= 1
125
126             node['output_spatial_shape'] = output_spatial_shape
127
128         output_shape = input_shape.copy()
129         output_shape[node.spatial_dims] = node.output_spatial_shape
130         node.out_node().shape = output_shape
131
132         # Add permute_attrs
133         PermuteAttrs.create_permute_attrs(node, attrs=[('pad', 'input:0'),
134                                                        ('stride', 'input:0'),
135                                                        ('window', 'input:0'),
136                                                        ('spatial_dims', 'input:0')])