Added unit tests and readme for model optimizer (#79)
[platform/upstream/dldt.git] / model-optimizer / extensions / back / ShufflenetReLUReorder_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.back.ShufflenetReLUReorder import ShufflenetReLUReorder
22 from mo.utils.unittest.graph import build_graph, compare_graphs
23
24 # The dictionary with nodes attributes used to build various graphs. A key is the name of the node and the value is the
25 # dictionary with node attributes.
26 nodes_attributes = {
27     'placeholder_1': {'shape': None, 'type': 'Placeholder', 'kind': 'op', 'op': 'Placeholder'},
28     'placeholder_1_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
29     # ReLU
30     'relu_1': {'type': 'ReLU', 'kind': 'op', 'op': 'ReLU'},
31     'relu_1_data': {'value': None, 'shape': None, 'kind': 'data'},
32     # Reshape layers
33     'reshape_1': {'type': 'Reshape', 'kind': 'op', 'op': 'Reshape'},
34     'reshape_1_data': {'value': None, 'shape': None, 'kind': 'data'},
35     'reshape_2': {'type': 'Reshape', 'kind': 'op', 'op': 'Reshape'},
36     'reshape_2_data': {'value': None, 'shape': None, 'kind': 'data'},
37     'reshape_3': {'type': 'Reshape', 'kind': 'op', 'op': 'Reshape'},
38     'reshape_3_data': {'value': None, 'shape': None, 'kind': 'data'},
39     # Transpose layer
40     'transpose_1': {'type': 'Permute', 'kind': 'op', 'op': 'Transpose'},
41     'transpose_1_data': {'value': None, 'shape': None, 'kind': 'data'},
42     # Conv layer
43     'conv_1': {'type': 'Convolution', 'kind': 'op', 'op': 'Conv2d'},
44     'conv_1_data': {'value': None, 'shape': None, 'kind': 'data'},
45 }
46
47
48 class ShufflenetReLUReorderTests(unittest.TestCase):
49     def test_1(self):
50         graph = build_graph(nodes_attributes,
51                             [('placeholder_1', 'placeholder_1_data'),
52                              ('placeholder_1_data', 'relu_1'),
53                              ('relu_1', 'relu_1_data'),
54                              ('relu_1_data', 'reshape_1'),
55                              ('reshape_1', 'reshape_1_data'),
56                              ('reshape_1_data', 'transpose_1'),
57                              ('transpose_1', 'transpose_1_data'),
58                              ('transpose_1_data', 'reshape_2'),
59                              ('reshape_2', 'reshape_2_data'),
60                              ('reshape_2_data', 'conv_1'),
61                              ('conv_1', 'conv_1_data')
62                              ],
63                             {'placeholder_1_data': {'shape': np.array([1, 227, 227, 112])},
64                              'relu_1_data': {'shape': np.array([1, 227, 227, 112])},
65                              'reshape_1_data': {'shape': np.array([227, 227, 4, 28])},
66                              'transpose_1': {'order': np.array([0, 1, 3, 2])},
67                              'transpose_1_data': {'shape': np.array([227, 227, 28, 4])},
68                              'reshape_2_data': {'shape': np.array([1, 227, 227, 112])},
69                              'conv_1_data': {'shape': np.array([1, 227, 227, 112])},
70                              'conv_1': {'pad': np.array([1, 1])}
71                              })
72         graph.graph['layout'] = 'NHWC'
73
74         graph_ref = build_graph(nodes_attributes,
75                                 [('placeholder_1', 'placeholder_1_data'),
76                                  ('placeholder_1_data', 'reshape_1'),
77                                  ('reshape_1', 'reshape_1_data'),
78                                  ('reshape_1_data', 'transpose_1'),
79                                  ('transpose_1', 'transpose_1_data'),
80                                  ('transpose_1_data', 'reshape_2'),
81                                  ('reshape_2', 'reshape_2_data'),
82                                  ('reshape_2_data', 'relu_1'),
83                                  ('relu_1', 'relu_1_data'),
84                                  ('relu_1_data', 'conv_1'),
85                                  ('conv_1', 'conv_1_data')
86                                  ],
87                                 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 112])},
88                                  'relu_1_data': {'shape': np.array([1, 227, 227, 112])},
89                                  'reshape_1_data': {'shape': np.array([227, 227, 4, 28])},
90                                  'transpose_1': {'order': np.array([0, 1, 3, 2])},
91                                  'transpose_1_data': {'shape': np.array([227, 227, 28, 4])},
92                                  'reshape_2_data': {'shape': np.array([1, 227, 227, 112])},
93                                  'conv_1_data': {'shape': np.array([1, 227, 227, 112])},
94                                  })
95
96         pattern = ShufflenetReLUReorder()
97         pattern.find_and_replace_pattern(graph)
98
99         (flag, resp) = compare_graphs(graph, graph_ref, 'conv_1_data', check_op_attrs=True)
100         self.assertTrue(flag, resp)
101
102     def test_2_neg(self):
103         graph = build_graph(nodes_attributes,
104                             [('placeholder_1', 'placeholder_1_data'),
105                              ('placeholder_1_data', 'reshape_1'),
106                              ('reshape_1', 'reshape_1_data'),
107                              ('reshape_1_data', 'transpose_1'),
108                              ('transpose_1', 'transpose_1_data'),
109                              ('transpose_1_data', 'reshape_2'),
110                              ('reshape_2', 'reshape_2_data'),
111                              ('reshape_2_data', 'conv_1'),
112                              ('conv_1', 'conv_1_data')
113                              ],
114                             {'placeholder_1_data': {'shape': np.array([1, 227, 227, 112])},
115                              'relu_1_data': {'shape': np.array([1, 227, 227, 112])},
116                              'reshape_1_data': {'shape': np.array([227, 227, 4, 28])},
117                              'transpose_1': {'order': np.array([0, 1, 3, 2])},
118                              'transpose_1_data': {'shape': np.array([227, 227, 28, 4])},
119                              'reshape_2_data': {'shape': np.array([1, 227, 227, 112])},
120                              'conv_1_data': {'shape': np.array([1, 227, 227, 112])},
121                              })
122         graph.graph['layout'] = 'NHWC'
123
124         graph_ref = build_graph(nodes_attributes,
125                                 [('placeholder_1', 'placeholder_1_data'),
126                                  ('placeholder_1_data', 'reshape_1'),
127                                  ('reshape_1', 'reshape_1_data'),
128                                  ('reshape_1_data', 'transpose_1'),
129                                  ('transpose_1', 'transpose_1_data'),
130                                  ('transpose_1_data', 'reshape_2'),
131                                  ('reshape_2', 'reshape_2_data'),
132                                  ('reshape_2_data', 'conv_1'),
133                                  ('conv_1', 'conv_1_data')
134                                  ],
135                                 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 112])},
136                                  'relu_1_data': {'shape': np.array([1, 227, 227, 112])},
137                                  'reshape_1_data': {'shape': np.array([227, 227, 4, 28])},
138                                  'transpose_1': {'order': np.array([0, 1, 3, 2])},
139                                  'transpose_1_data': {'shape': np.array([227, 227, 28, 4])},
140                                  'reshape_2_data': {'shape': np.array([1, 227, 227, 112])},
141                                  'conv_1_data': {'shape': np.array([1, 227, 227, 112])},
142                                  })
143
144         pattern = ShufflenetReLUReorder()
145         pattern.find_and_replace_pattern(graph)
146
147         (flag, resp) = compare_graphs(graph, graph_ref, 'conv_1_data', check_op_attrs=True)
148         self.assertTrue(flag, resp)