2 Copyright (c) 2019 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.
21 from mo.middle.passes.eliminate import graph_clean_up
22 from extensions.middle.MeanToAvgPool import MeanToAvgPool
23 from mo.utils.unittest.graph import build_graph, compare_graphs
26 'placeholder_1': {'shape': None, 'type': 'Placeholder', 'kind': 'op', 'op': 'Placeholder'},
27 'placeholder_1_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
29 'mean_1': {'type': 'Pooling', 'kind': 'op', 'op': 'Mean', 'keep_dims': True},
30 'mean_axis': {'value': None, 'shape': None, 'kind': 'data'},
31 'mean_1_data': {'value': None, 'shape': None, 'kind': 'data'},
33 'pool_1': {'type': 'Pooling', 'kind': 'op', 'op': 'Power', 'scale': None, 'shift': None, 'power': None},
34 'pool_1_data': {'value': None, 'shape': None, 'kind': 'data'},
36 'reshape_1': {'type': 'Reshape', 'kind': 'op', 'op': 'Reshape'},
37 'reshape_1_data': {'value': None, 'shape': None, 'kind': 'data'},
39 'op_output': {'kind': 'op', 'op': 'OpOutput', 'type': 'OpOutput'}
43 class MeanToAvgPoolTests(unittest.TestCase):
44 def _create_graph_with_mean(self, axis, keep_dims=True, mean_out_shape=np.array([1, 227, 227, 3])):
45 graph = build_graph(nodes_attributes,
46 [('placeholder_1', 'placeholder_1_data'),
47 ('placeholder_1_data', 'mean_1'),
48 ('mean_axis', 'mean_1'),
49 ('mean_1', 'mean_1_data'),
50 ('mean_1_data', 'op_output')
53 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
54 'mean_1': {'shape': np.array([1, 227, 227, 3]), 'keep_dims': keep_dims},
55 'mean_axis': {'shape': np.array(axis.shape) if axis is not None else None,
56 'value': np.array(axis) if axis is not None else None},
57 'mean_1_data': {'shape': mean_out_shape},
59 del graph['mean_1']['mean_1_data'][0]['in']
62 def test_mean_to_avg_1(self):
63 graph = self._create_graph_with_mean(axis=np.array([1, 2]))
65 graph_ref = build_graph(nodes_attributes,
66 [('placeholder_1', 'placeholder_1_data'),
67 ('placeholder_1_data', 'pool_1'),
68 ('pool_1', 'pool_1_data'),
69 ('pool_1_data', 'op_output'),
71 {'pool_1': {'pool_method': 'avg', 'rounding_type': 'ceil', 'exclude_pad': 'true',
72 'op': 'AvgPool', 'shape': np.array([1, 227, 227, 3])},
73 'pool_1_data': {'shape': np.array([1, 227, 227, 3])}
76 MeanToAvgPool().find_and_replace_pattern(graph)
78 (flag, resp) = compare_graphs(graph, graph_ref, 'mean_1_data', 'pool_1_data', check_op_attrs=True)
79 self.assertTrue(flag, resp)
81 def test_mean_to_avg_2(self):
82 graph = self._create_graph_with_mean(axis=np.array([0]), keep_dims=False,
83 mean_out_shape=np.array([227, 227, 3]))
85 graph_ref = build_graph(nodes_attributes,
86 [('placeholder_1', 'placeholder_1_data'),
87 ('placeholder_1_data', 'pool_1'),
88 ('pool_1', 'pool_1_data'),
89 ('pool_1_data', 'reshape_1'),
90 ('reshape_1', 'reshape_1_data'),
91 ('reshape_1_data', 'op_output')
93 {'pool_1': {'pool_method': 'avg', 'rounding_type': 'ceil', 'exclude_pad': 'true',
94 'op': 'AvgPool', 'shape': np.array([1, 227, 227, 3])},
95 'pool_1_data': {'shape': np.array([1, 227, 227, 3])},
96 'reshape_1_data': {'shape': np.array([227, 227, 3])},
99 MeanToAvgPool().find_and_replace_pattern(graph)
100 graph_clean_up(graph)
101 (flag, resp) = compare_graphs(graph, graph_ref, 'mean_1_data', 'reshape_1_data', check_op_attrs=True)
102 self.assertTrue(flag, resp)