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.graph.graph import create_edge
21 from mo.middle.pattern_match import apply_pattern
22 from mo.ops.op import Op
23 from mo.ops.reshape import Reshape
26 def mean_to_avgpool_action(graph: nx.MultiDiGraph, matches: dict):
27 if matches['axis'].value is None or matches['input'].shape is None:
29 dims = len(matches['input'].shape)
30 ones = np.ones(dims, dtype=np.int64)
31 mean = graph.node[matches['mean'].node]
32 mean['stride'] = np.array(ones)
33 # TODO: need to check axis with real layout
34 spatial_dims = np.array(matches['axis'].value)
35 mean['spatial_dims'] = spatial_dims
36 mean['pad'] = np.zeros((dims, 2), np.int64)
37 mean['pad_spatial_shape'] = np.array(mean['pad'][spatial_dims])
38 window = np.array(ones)
39 window[spatial_dims] = matches['input'].shape[spatial_dims]
40 mean['window'] = window
41 mean['TF_op'] = mean['op']
42 mean['op'] = 'AvgPool'
43 mean['pool_method'] = 'avg'
44 mean['rounding_type'] = 'ceil'
45 mean['exclude_pad'] = 'true'
46 graph.remove_edge(matches['axis'].node, matches['mean'].node)
48 if matches['mean'].keep_dims == False:
49 output = matches['mean'].out_node()
50 pool_node = matches['mean']
52 # Keep dims for AvgPool
53 shape = np.array(output.shape)
54 for idx in spatial_dims:
55 shape = np.insert(shape, idx, 1)
57 graph.remove_edge(pool_node.id, output.id)
58 # Create new data for pool with all dims
59 pool_data = Op.create_data_node(graph, pool_node, {'shape': np.array(shape)})
60 # Create and connect reshape node
61 reshape_op = Reshape(graph, {'dim': np.array(output.shape)})
62 reshape_node = reshape_op.create_node([pool_data], dict(name='Reshape_'))
63 create_edge(reshape_node, output)
66 def mean_to_avgpool(graph: nx.MultiDiGraph):
68 Translate Mean as a average pooling with kernel size equals to reduced dimensions and with no padding.
73 ('input', dict(kind='data')),
74 ('axis', dict(kind='data')),
75 ('mean', dict(kind='op', op='Mean'))],
77 ('input', 'mean', {'in': 0}),
78 ('axis', 'mean', {'in': 1})],
79 action=mean_to_avgpool_action,
80 node_attrs=['kind', 'op'],