Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / kaldi / add_permute_after_convolution_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 import numpy as np
19
20 from extensions.front.kaldi.add_permute_after_convolution import ReplaceConvolutionPermute
21 from mo.graph.graph import Node
22 from mo.utils.unittest.graph import build_graph
23
24
25 class ReplaceConvolutionPermuteTests(unittest.TestCase):
26     nodes_attributes = {
27         'conv': {'kind': 'op', 'op': 'Convolution'},
28         'reshape_conv': {'kind': 'op', 'op': 'Reshape'},
29         'reshape_pool': {'kind': 'op', 'op': 'Reshape'},
30         'pool': {'kind': 'op', 'op': 'Pooling'},
31         'reshape_after_pool': {'kind': 'op', 'op': 'Reshape'},
32         'act': {'kind': 'op', 'op': 'Activation'},
33         'fc': {'kind': 'op', 'op': 'FullyConnected'},
34         'scale_shift': {'kind': 'op', 'op': 'ScaleShift'}
35     }
36
37     def test_simple_convolution(self):
38         graph = build_graph(self.nodes_attributes, [
39             ('conv', 'reshape_conv'),
40             ('reshape_conv', 'scale_shift'),
41         ])
42         ReplaceConvolutionPermute().find_and_replace_pattern(graph)
43         conv_node = Node(graph, graph.nodes['conv']['name'])
44         permute = conv_node.out_node()
45         self.assertEqual(permute.op, 'Permute')
46         self.assertTrue(np.array_equal(permute.order, np.array([0, 3, 2, 1])))
47
48     def test_conv_pool(self):
49         graph = build_graph(self.nodes_attributes, [
50             ('conv', 'reshape_conv'),
51             ('reshape_conv', 'reshape_pool'),
52             ('reshape_pool', 'pool'),
53             ('pool', 'reshape_after_pool'),
54             ('reshape_after_pool', 'fc'),
55         ])
56         ReplaceConvolutionPermute().find_and_replace_pattern(graph)
57         pool_node = Node(graph, graph.nodes['pool']['name'])
58         permute = pool_node.out_node()
59         self.assertEqual(permute.op, 'Permute')
60         self.assertTrue(np.array_equal(permute.order, np.array([0, 3, 2, 1])))
61
62     def test_conv_act_pool(self):
63         graph = build_graph(self.nodes_attributes, [
64             ('conv', 'reshape_conv'),
65             ('reshape_conv', 'act'),
66             ('act', 'reshape_pool'),
67             ('reshape_pool', 'pool'),
68             ('pool', 'reshape_after_pool'),
69             ('reshape_after_pool', 'fc'),
70         ])
71         ReplaceConvolutionPermute().find_and_replace_pattern(graph)
72         pool_node = Node(graph, graph.nodes['pool']['name'])
73         permute = pool_node.out_node()
74         self.assertEqual(permute.op, 'Permute')
75         self.assertTrue(np.array_equal(permute.order, np.array([0, 3, 2, 1])))