add_permute_after_convolution_test.py 3.04 KB
Newer Older
1
"""
2
 Copyright (C) 2018-2020 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
"""
import unittest

import numpy as np

20
from extensions.front.kaldi.add_permute_after_convolution import ReplaceConvolutionTranspose
21 22 23 24
from mo.graph.graph import Node
from mo.utils.unittest.graph import build_graph


25
class ReplaceConvolutionTransposeTests(unittest.TestCase):
26 27 28 29 30 31
    nodes_attributes = {
        'conv': {'kind': 'op', 'op': 'Convolution'},
        'reshape_conv': {'kind': 'op', 'op': 'Reshape'},
        'reshape_pool': {'kind': 'op', 'op': 'Reshape'},
        'pool': {'kind': 'op', 'op': 'Pooling'},
        'reshape_after_pool': {'kind': 'op', 'op': 'Reshape'},
32
        'act': {'kind': 'op', 'op': 'Sigmoid'},
33 34 35 36 37 38 39 40 41
        'fc': {'kind': 'op', 'op': 'FullyConnected'},
        'scale_shift': {'kind': 'op', 'op': 'ScaleShift'}
    }

    def test_simple_convolution(self):
        graph = build_graph(self.nodes_attributes, [
            ('conv', 'reshape_conv'),
            ('reshape_conv', 'scale_shift'),
        ])
42
        ReplaceConvolutionTranspose().find_and_replace_pattern(graph)
43 44
        conv_node = Node(graph, graph.nodes['conv']['name'])
        permute = conv_node.out_node()
45 46
        self.assertEqual(permute.op, 'Transpose')
        self.assertTrue(np.array_equal(permute.in_node(1).in_node().value, np.array([0, 3, 2, 1])))
47 48 49 50 51 52 53 54 55

    def test_conv_pool(self):
        graph = build_graph(self.nodes_attributes, [
            ('conv', 'reshape_conv'),
            ('reshape_conv', 'reshape_pool'),
            ('reshape_pool', 'pool'),
            ('pool', 'reshape_after_pool'),
            ('reshape_after_pool', 'fc'),
        ])
56
        ReplaceConvolutionTranspose().find_and_replace_pattern(graph)
57 58
        pool_node = Node(graph, graph.nodes['pool']['name'])
        permute = pool_node.out_node()
59 60
        self.assertEqual(permute.op, 'Transpose')
        self.assertTrue(np.array_equal(permute.in_node(1).in_node().value, np.array([0, 3, 2, 1])))
61 62 63 64 65 66 67 68 69 70

    def test_conv_act_pool(self):
        graph = build_graph(self.nodes_attributes, [
            ('conv', 'reshape_conv'),
            ('reshape_conv', 'act'),
            ('act', 'reshape_pool'),
            ('reshape_pool', 'pool'),
            ('pool', 'reshape_after_pool'),
            ('reshape_after_pool', 'fc'),
        ])
71
        ReplaceConvolutionTranspose().find_and_replace_pattern(graph)
72 73
        pool_node = Node(graph, graph.nodes['pool']['name'])
        permute = pool_node.out_node()
74 75
        self.assertEqual(permute.op, 'Transpose')
        self.assertTrue(np.array_equal(permute.in_node(1).in_node().value, np.array([0, 3, 2, 1])))