Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / kaldi / extractors / common_ext_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 networkx as nx
20 import numpy as np
21
22 from mo.front.common.partial_infer.utils import int64_array
23 from mo.front.kaldi.loader.utils_test import TestKaldiUtilsLoading
24 from mo.graph.graph import Node, Graph
25 from mo.utils.unittest.graph import build_graph
26
27
28 class KaldiFrontExtractorTest(unittest.TestCase):
29     graph = Graph()
30
31     @classmethod
32     def setUp(cls):
33         cls.nodes_attributes = {
34             'input_data_node': {
35                 'name': 'input_data_node',
36                 'kind': 'data',
37                 'shape': np.array([1, 32, 1, 40], dtype=np.int64),
38             },
39             'weights': {
40                 'name': 'weights',
41                 'kind': 'data',
42                 'shape': np.array([10, 32, 1, 8], dtype=np.int64),
43                 'value': np.zeros((10, 32, 1, 8)),
44                 'dim_attrs': ['spatial_dims', 'channel_dims', 'batch_dims', 'axis'],
45             },
46             'test_node': {
47                 'name': 'test_node',
48                 'kind': 'op'
49             },
50             'output_data_node': {
51                 'name': 'output_data_node',
52                 'kind': 'data',
53                 'shape': None
54             }
55         }
56         cls.create_graph()
57         cls.test_node = Node(cls.graph, 'test_node')
58         cls.graph.add_node(cls.test_node.id, type='test_node')
59         cls.register_op()
60         cls.create_pb_for_test_node()
61
62     @staticmethod
63     def register_op():
64         raise NotImplementedError('Please, implement register_op')
65
66     @classmethod
67     def create_graph(cls):
68         cls.graph = build_graph(cls.nodes_attributes, [
69             ('input_data_node', 'test_node'),
70             ('test_node', 'output_data_node')
71         ], nodes_with_edges_only=True)
72
73     @classmethod
74     def create_pb_for_test_node(cls):
75         pass
76
77     @staticmethod
78     def generate_learn_info():
79         pb = KaldiFrontExtractorTest.write_tag_with_value('<LearnRateCoef>', 0)
80         pb += KaldiFrontExtractorTest.write_tag_with_value('<BiasLearnRateCoef>', 1)
81         pb += KaldiFrontExtractorTest.write_tag_with_value('<MaxNorm>', 2)
82         return pb
83
84     @staticmethod
85     def generate_matrix(shape):
86         pb = KaldiFrontExtractorTest.write_tag_with_value('FM', shape[0])
87         pb += KaldiFrontExtractorTest.write_int_value(shape[1])
88         pb += KaldiFrontExtractorTest.generate_blob(np.prod(shape))
89         return pb
90
91     @staticmethod
92     def generate_vector(size: int) -> bytes:
93         pb = KaldiFrontExtractorTest.write_tag_with_value('FV', size)
94         pb += KaldiFrontExtractorTest.generate_blob(size)
95         return pb
96
97     @staticmethod
98     def generate_blob(size: int) -> bytes:
99         pb = b''
100         for i in range(size):
101             pb += TestKaldiUtilsLoading.pack_value(i, TestKaldiUtilsLoading.float32_fmt)
102         return pb
103
104     @staticmethod
105     def write_tag_with_value(tag: str, value) -> bytes:
106         pb = bytes(tag + ' ', 'ascii')
107         return pb + KaldiFrontExtractorTest.write_int_value(value)
108
109     @staticmethod
110     def write_int_value(value) -> bytes:
111         pb = TestKaldiUtilsLoading.pack_value(4, 'B')
112         pb += TestKaldiUtilsLoading.pack_value(value, TestKaldiUtilsLoading.uint32_fmt)
113         return pb