Added unit tests and readme for model optimizer (#79)
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / PixelLinkReshape_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 extensions.middle.PixelLinkReshape import PixelLinkReshape
22 from mo.utils.unittest.graph import build_graph, compare_graphs
23
24 nodes_attributes = {
25     'placeholder_1': {'shape': None, 'type': 'Placeholder', 'kind': 'op', 'op': 'Placeholder'},
26     'placeholder_1_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
27     # Reshape layers
28     'reshape_pack': {'type': 'Reshape', 'kind': 'op', 'op': 'Reshape'},
29     'reshape_pack_data': {'value': None, 'shape': None, 'kind': 'data'},
30     'reshape_split': {'type': 'Reshape', 'kind': 'op', 'op': 'Reshape'},
31     'reshape_split_data': {'value': None, 'shape': None, 'kind': 'data'},
32     'reshape_unpack': {'type': 'Reshape', 'kind': 'op', 'op': 'Reshape'},
33     'reshape_unpack_data': {'value': None, 'shape': None, 'kind': 'data'},
34     'strided_slice': {'type': 'StridedSlice', 'kind': 'op', 'op': 'StridedSlice'},
35     'strided_slice_data': {'value': None, 'shape': None, 'kind': 'data'},
36     # Transpose layer
37     'reshape_split/Permute_before': {'type': 'Permute', 'kind': 'op', 'op': 'Permute'},
38     'reshape_split/Permute_before_data': {'value': None, 'shape': None, 'kind': 'data'},
39     'reshape_pack/Permute_after': {'type': 'Permute', 'kind': 'op', 'op': 'Permute'},
40     'reshape_pack/Permute_after_data': {'value': None, 'shape': None, 'kind': 'data'},
41     # Softmax layer
42     'softmax_1': {'type': 'SoftMax', 'kind': 'op', 'op': 'SoftMax'},
43     'softmax_1_data': {'value': None, 'shape': None, 'kind': 'data'},
44 }
45
46
47 class ReshapeSoftmaxReshapeTests(unittest.TestCase):
48     def test_1(self):
49         graph = build_graph(nodes_attributes,
50                             [('placeholder_1', 'placeholder_1_data'),
51                              ('placeholder_1_data', 'reshape_split'),
52                              ('reshape_split', 'reshape_split_data'),
53                              ('reshape_split_data', 'reshape_pack'),
54                              ('reshape_pack', 'reshape_pack_data'),
55                              ('reshape_pack_data', 'softmax_1'),
56                              ('softmax_1', 'softmax_1_data'),
57                              ('softmax_1_data', 'reshape_unpack'),
58                              ('reshape_unpack', 'reshape_unpack_data'),
59                              ('reshape_unpack_data', 'strided_slice'),
60                              ('strided_slice', 'strided_slice_data'),
61                              ],
62                             {'placeholder_1_data': {'shape': np.array([1, 227, 227, 16])},
63                              'reshape_split': {'dim': np.array([1, 227, 227, 8, 2])},
64                              'reshape_split_data': {'shape': np.array([1, 227, 227, 8, 2])},
65                              'softmax_1_data': {'shape': np.array([1 * 227 * 227 * 8, 2])},
66                              'reshape_pack': {'dim': np.array([1 * 227 * 227 * 8, 2])},
67                              'reshape_pack_data': {'shape': np.array([1 * 227 * 227 * 8, 2])},
68                              'reshape_unpack': {'dim': np.array([1, 227, 227, 8, 2])},
69                              'reshape_unpack_data': {'shape': np.array([1, 227, 227, 8, 2])},
70                              'strided_slice': {
71                                  'slices': [slice(0, 1, 1), slice(0, 227, 1), slice(0, 227, 1), slice(0, 8, 1),
72                                             slice(1, 2, 1)],
73                                  'shrink_axis_mask': [False, False, False, False, True],
74                                  'new_axis_mask': [False, False, False, False, False]},
75                              'strided_slice_data': {'shape': np.array([1, 227, 227, 8])},
76                              })
77         graph.graph['layout'] = 'NHWC'
78
79         graph_ref = build_graph(nodes_attributes,
80                                 [('placeholder_1', 'placeholder_1_data'),
81                                  ('placeholder_1_data', 'reshape_split/Permute_before'),
82                                  ('reshape_split/Permute_before', 'reshape_split/Permute_before_data'),
83                                  ('reshape_split/Permute_before_data', 'reshape_split'),
84                                  ('reshape_split', 'reshape_split_data'),
85                                  ('reshape_split_data', 'reshape_pack'),
86                                  ('reshape_pack', 'reshape_pack/Permute_after_data'),
87                                  ('reshape_pack/Permute_after_data', 'reshape_pack/Permute_after'),
88                                  ('reshape_pack/Permute_after', 'reshape_pack_data'),
89                                  ('reshape_pack_data', 'softmax_1'),
90                                  ('softmax_1', 'softmax_1_data'),
91                                  ('softmax_1_data', 'strided_slice'),
92                                  ('strided_slice', 'reshape_unpack_data'),
93                                  ('reshape_unpack_data', 'reshape_unpack'),
94                                  ('reshape_unpack', 'strided_slice_data')
95                                  ],
96                                 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 16])},
97                                  'reshape_split/Permute_before_data': {'shape': np.array([1, 227, 16, 227])},
98                                  'reshape_split_data': {'shape': np.array([1, 227, 227, 8, 2])},
99                                  'reshape_pack_data': {'shape': np.array([1, 2, 1 * 227 * 227 * 8])},
100                                  'reshape_pack/Permute_after_data': {'shape': np.array([1, 227 * 227 * 8, 2])},
101                                  'softmax_1_data': {'shape': np.array([1, 2, 1 * 227 * 227 * 8])},
102                                  'reshape_unpack_data': {'shape': np.array([1, 1, 227 * 227 * 8])},
103                                  'strided_slice_data': {'shape': np.array([1, 227, 227, 8])}
104                                  })
105
106         pattern = PixelLinkReshape()
107         pattern.find_and_replace_pattern(graph)
108
109         (flag, resp) = compare_graphs(graph, graph_ref, 'strided_slice_data', check_op_attrs=True)
110         self.assertTrue(flag, resp)