2 Copyright (c) 2018 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
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
32 def __init__(self, graph: nx.MultiDiGraph, attrs: dict):
33 super().__init__(graph, {
37 'infer': __class__.infer,
40 def backend_attrs(self):
42 ('strides', lambda node: ','.join(map(str, node['stride'][node.spatial_dims]))),
43 ('kernel', lambda node: ','.join(map(str, node['window'][node.spatial_dims]))),
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)))),
48 ('pool-method', 'pool_method'),
49 ('exclude-pad', 'exclude_pad'),
55 def backend_attrs_v2(self):
57 ('stride', lambda node: attr_getter(node, 'stride')),
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]),
66 ('pool-method', 'pool_method'),
67 ('exclude-pad', 'exclude_pad'),
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:
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]])
84 input_spatial_shape = input_shape[node.spatial_dims]
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)
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)
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
109 pad_spatial_shape = np.add.reduce(node.pad_spatial_shape, axis=1)
112 if node.has_valid('pooling_convention') and node.pooling_convention == 'full':
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),
119 original_pads = np.array([i[1] for i in node.pad_spatial_shape])
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
126 node['output_spatial_shape'] = output_spatial_shape
128 output_shape = input_shape.copy()
129 output_shape[node.spatial_dims] = node.output_spatial_shape
130 node.out_node().shape = output_shape
133 PermuteAttrs.create_permute_attrs(node, attrs=[('pad', 'input:0'),
134 ('stride', 'input:0'),
135 ('window', 'input:0'),
136 ('spatial_dims', 'input:0')])