Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / tf / mvn_unrolled_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 import unittest
17
18 from extensions.front.tf.mvn_unrolled import MVNUnrolled
19 from mo.ops.op import Op
20 from mo.utils.unittest.graph import compare_graphs, build_graph_with_attrs
21 from extensions.ops.mvn import MVN
22
23
24 class MVNUnrolledMatchingTests(unittest.TestCase):
25     @classmethod
26     def setUpClass(cls):
27         Op.registered_ops['MVN'] = MVN
28
29     def test(self):
30         pattern_matcher = MVNUnrolled()
31         pattern = pattern_matcher.pattern()
32         graph = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'], edges_with_attrs=pattern['edges'], update_edge_attrs=None,
33                                        new_nodes_with_attrs=[('reduction_indicies', {'kind': 'data'}),
34                                                              ('conv2d', {'kind': 'op'}),
35                                                              ('variance_reduction', {'kind': 'data'}),
36                                                              ('pow2', {'kind': 'data'}),
37                                                              ('eps', {'kind': 'data'}),
38                                                              ('next_op', {'kind': 'op'})],
39                                        new_edges_with_attrs=[('reduction_indicies', 'mean', {'in': 1}),
40                                                              ('conv2d', 'mean',{'in': 0, 'out': 1}),
41                                                              ('variance_reduction', 'variance', {'in': 1}),
42                                                              ('pow2', 'pow', {'in': 1}),
43                                                              ('eps', 'add'), ('truediv', 'next_op')])
44         graph.graph['layout'] = 'NHWC'
45         pattern_matcher.find_and_replace_pattern(graph)
46
47         graph_ref = build_graph_with_attrs(nodes_with_attrs=pattern['nodes'][:-1],
48                                            edges_with_attrs=pattern['edges'][:-2], update_edge_attrs=None,
49                                            new_nodes_with_attrs=[('reduction_indicies', {'kind':'data'}),
50                                                                  ('conv2d', {'kind':'op'}),
51                                                                  ('variance_reduction', {'kind':'data'}),
52                                                                  ('pow2', {'kind': 'data'}),
53                                                                  ('eps', {'kind': 'data'}),
54                                                                  ('mvn', {'kind': 'op', 'op': 'MVN'}),
55                                                                  ('next_op', {'kind': 'op'})],
56                                            new_edges_with_attrs=[('reduction_indicies', 'mean', {'in':1}),
57                                                                  ('conv2d', 'mean', {'in': 0}),
58                                                                  ('variance_reduction', 'variance',{'in': 1}),
59                                                                  ('pow2', 'pow', {'in': 1}),
60                                                                  ('eps', 'add'),
61                                                                  ('conv2d', 'mvn',{'in': 0}),
62                                                                  ('reduction_indicies', 'mvn', {'in': 1}),
63                                                                  ('variance_reduction', 'mvn',{'in': 2}),
64                                                                  ('pow2', 'mvn', {'in': 3}),
65                                                                  ('eps', 'mvn',{'in': 4}),
66                                                                  ('mvn', 'next_op')])
67
68         (flag, resp) = compare_graphs(graph, graph_ref, 'next_op', check_op_attrs=True)
69         self.assertTrue(flag, resp)