Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / PixelLinkReshape_test.py
1 """
2  Copyright (c) 2018-2019 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     # uncoment when strided slice will be enabled
42     # 'reshape_unpack/Permute_after_unpack': {'type': 'Permute', 'kind': 'op', 'op': 'Permute'},
43     # 'reshape_unpack/Permute_after_unpack_data': {'value': None, 'shape': None, 'kind': 'data'},
44     # Softmax layer
45     'softmax_1': {'type': 'SoftMax', 'kind': 'op', 'op': 'SoftMax'},
46     'softmax_1_data': {'value': None, 'shape': None, 'kind': 'data'},
47 }
48
49
50 class ReshapeSoftmaxReshapeTests(unittest.TestCase):
51     def test_1(self):
52         graph = build_graph(nodes_attributes,
53                             [('placeholder_1', 'placeholder_1_data'),
54                              ('placeholder_1_data', 'reshape_split'),
55                              ('reshape_split', 'reshape_split_data'),
56                              ('reshape_split_data', 'reshape_pack'),
57                              ('reshape_pack', 'reshape_pack_data'),
58                              ('reshape_pack_data', 'softmax_1'),
59                              ('softmax_1', 'softmax_1_data'),
60                              ('softmax_1_data', 'reshape_unpack'),
61                              ('reshape_unpack', 'reshape_unpack_data'),
62                              ('reshape_unpack_data', 'strided_slice'),
63                              ('strided_slice', 'strided_slice_data'),
64                              ],
65                             {'placeholder_1_data': {'shape': np.array([1, 227, 227, 16])},
66                              'reshape_split': {'dim': np.array([1, 227, 227, 8, 2])},
67                              'reshape_split_data': {'shape': np.array([1, 227, 227, 8, 2])},
68                              'softmax_1_data': {'shape': np.array([1 * 227 * 227 * 8, 2])},
69                              'reshape_pack': {'dim': np.array([1 * 227 * 227 * 8, 2])},
70                              'reshape_pack_data': {'shape': np.array([1 * 227 * 227 * 8, 2])},
71                              'reshape_unpack': {'dim': np.array([1, 227, 227, 8, 2])},
72                              'reshape_unpack_data': {'shape': np.array([1, 227, 227, 8, 2])},
73                              'strided_slice': {
74                                  'slices': [slice(0, 1, 1), slice(0, 227, 1), slice(0, 227, 1), slice(0, 8, 1),
75                                             slice(1, 2, 1)],
76                                  'shrink_axis_mask': [0, 0, 0, 0, 1],
77                                  'new_axis_mask': [0, 0, 0, 0, 0],
78                                  'ellipsis_mask': [0, 0, 0, 0, 0],
79                                  'begin_mask': [1, 1, 1, 1, 1],
80                                  'end_mask': [1, 1, 1, 1, 1], },
81                              'strided_slice_data': {'shape': np.array([1, 227, 227, 8])},
82                              })
83         graph.graph['layout'] = 'NHWC'
84
85         graph_ref = build_graph(nodes_attributes,
86                                 [('placeholder_1', 'placeholder_1_data'),
87                                  ('placeholder_1_data', 'reshape_split/Permute_before'),
88                                  ('reshape_split/Permute_before', 'reshape_split/Permute_before_data'),
89                                  ('reshape_split/Permute_before_data', 'reshape_split'),
90                                  ('reshape_split', 'reshape_split_data'),
91                                  ('reshape_split_data', 'reshape_pack'),
92                                  ('reshape_pack', 'reshape_pack/Permute_after_data'),
93                                  ('reshape_pack/Permute_after_data', 'reshape_pack/Permute_after'),
94                                  ('reshape_pack/Permute_after', 'reshape_pack_data'),
95                                  ('reshape_pack_data', 'softmax_1'),
96                                  ('softmax_1', 'softmax_1_data'),
97                                  # comment when strided slice will be enabled
98                                  ('softmax_1_data', 'strided_slice'),
99                                  ('strided_slice', 'reshape_unpack_data'),
100                                  ('reshape_unpack_data', 'reshape_unpack'),
101                                  ('reshape_unpack', 'strided_slice_data'),
102                                  # uncomment when strided slice will be enabled
103                                  # ('softmax_1_data', 'reshape_unpack'),
104                                  # ('reshape_unpack', 'reshape_unpack/Permute_after_unpack_data'),
105                                  # ('reshape_unpack/Permute_after_unpack_data', 'reshape_unpack/Permute_after_unpack'),
106                                  # ('reshape_unpack/Permute_after_unpack', 'reshape_unpack_data'),
107                                  # ('reshape_unpack_data', 'strided_slice'),
108                                  # ('strided_slice', 'strided_slice_data'),
109                                  ],
110                                 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 16])},
111                                  'reshape_split/Permute_before_data': {'shape': np.array([1, 227, 16, 227])},
112                                  'reshape_split_data': {'shape': np.array([1, 227, 227, 8, 2])},
113                                  'reshape_pack_data': {'shape': np.array([1, 2, 1 * 227 * 227 * 8])},
114                                  'reshape_pack/Permute_after_data': {'shape': np.array([1, 227 * 227 * 8, 2])},
115                                  'softmax_1_data': {'shape': np.array([1, 2, 1 * 227 * 227 * 8])},
116                                  # comment when strided slice will be enabled
117                                  'reshape_unpack_data': {'shape': np.array([1, 1, 227 * 227 * 8])},
118                                  # uncomment when strided slice will be enabled
119                                  # 'reshape_unpack_data': {'shape': np.array([1, 8, 227, 227, 2])},
120                                  # 'reshape_unpack/Permute_after_unpack_data': {'shape': np.array([1, 227, 227, 8, 2])},
121                                  'strided_slice_data': {'shape': np.array([1, 227, 227, 8])}
122                                  })
123
124         pattern = PixelLinkReshape()
125         pattern.find_and_replace_pattern(graph)
126
127         (flag, resp) = compare_graphs(graph, graph_ref, 'strided_slice_data', check_op_attrs=True)
128         self.assertTrue(flag, resp)