Add a section of how to link IE with CMake project (#99)
[platform/upstream/dldt.git] / model-optimizer / mo / middle / passes / pool_test.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 unittest
18
19 import numpy as np
20
21 from mo.middle.passes.eliminate import graph_clean_up
22 from mo.middle.passes.pool import mean_to_avgpool
23 from mo.utils.unittest.graph import build_graph, compare_graphs
24
25 nodes_attributes = {
26     'placeholder_1': {'shape': None, 'type': 'Placeholder', 'kind': 'op', 'op': 'Placeholder'},
27     'placeholder_1_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
28     # Mean layer
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'},
32     # AvgPool layer
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'},
35     # Reshape layer
36     'reshape_1': {'type': 'Reshape', 'kind': 'op', 'op': 'Reshape'},
37     'reshape_1_data': {'value': None, 'shape': None, 'kind': 'data'},
38 }
39
40
41 class MeanToAvgPoolTests(unittest.TestCase):
42     def _create_graph_with_mean(self, axis, keep_dims=True, mean_out_shape=np.array([1, 227, 227, 3])):
43         graph = build_graph(nodes_attributes,
44                             [('placeholder_1', 'placeholder_1_data'),
45                              ('placeholder_1_data', 'mean_1'),
46                              ('mean_1', 'mean_1_data'),
47                              ('mean_axis', 'mean_1'),
48                              ],
49                             {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
50                              'mean_1': {'shape': np.array([1, 227, 227, 3]), 'keep_dims': keep_dims},
51                              'mean_axis': {'shape': np.array(axis.shape) if axis is not None else None,
52                                            'value': np.array(axis) if axis is not None else None},
53                              'mean_1_data': {'shape': mean_out_shape, 'is_output': True},
54                              })
55         del graph['mean_1']['mean_1_data'][0]['in']
56         return graph
57
58     def test_mean_to_avg_1(self):
59         graph = self._create_graph_with_mean(axis=np.array([1, 2]))
60
61         graph_ref = build_graph(nodes_attributes,
62                                 [('placeholder_1', 'placeholder_1_data'),
63                                  ('placeholder_1_data', 'pool_1'),
64                                  ('pool_1', 'pool_1_data'),
65                                  ],
66                                 {'pool_1': {'pool_method': 'avg', 'rounding_type': 'ceil', 'exclude_pad': 'true',
67                                             'op': 'AvgPool', 'shape': np.array([1, 227, 227, 3])},
68                                  'pool_1_data': {'is_output': True, 'shape': np.array([1, 227, 227, 3])}
69                                  })
70
71         mean_to_avgpool(graph)
72         graph_clean_up(graph)
73         (flag, resp) = compare_graphs(graph, graph_ref, 'mean_1_data', 'pool_1_data', check_op_attrs=True)
74         self.assertTrue(flag, resp)
75
76     def test_mean_to_avg_2(self):
77         graph = self._create_graph_with_mean(axis=np.array([0]), keep_dims=False,
78                                              mean_out_shape=np.array([227, 227, 3]))
79
80         graph_ref = build_graph(nodes_attributes,
81                                 [('placeholder_1', 'placeholder_1_data'),
82                                  ('placeholder_1_data', 'pool_1'),
83                                  ('pool_1', 'pool_1_data'),
84                                  ('pool_1_data', 'reshape_1'),
85                                  ('reshape_1', 'reshape_1_data')
86                                  ],
87                                 {'pool_1': {'pool_method': 'avg', 'rounding_type': 'ceil', 'exclude_pad': 'true',
88                                             'op': 'AvgPool', 'shape': np.array([1, 227, 227, 3])},
89                                  'pool_1_data': {'shape': np.array([1, 227, 227, 3])},
90                                  'reshape_1_data': {'is_output': True, 'shape': np.array([227, 227, 3])},
91                                  })
92
93         mean_to_avgpool(graph)
94         graph_clean_up(graph)
95         (flag, resp) = compare_graphs(graph, graph_ref, 'mean_1_data', 'reshape_1_data', check_op_attrs=True)
96         self.assertTrue(flag, resp)