2 Copyright (c) 2018-2019 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 class KaldiFrontExtractorTest(unittest.TestCase):
33 cls.nodes_attributes = {
35 'name': 'input_data_node',
37 'shape': np.array([1, 32, 1, 40], dtype=np.int64),
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'],
51 'name': 'output_data_node',
57 cls.test_node = Node(cls.graph, 'test_node')
58 cls.graph.add_node(cls.test_node.id, type='test_node')
60 cls.create_pb_for_test_node()
64 raise NotImplementedError('Please, implement register_op')
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)
74 def create_pb_for_test_node(cls):
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)
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))
92 def generate_vector(size: int) -> bytes:
93 pb = KaldiFrontExtractorTest.write_tag_with_value('FV', size)
94 pb += KaldiFrontExtractorTest.generate_blob(size)
98 def generate_blob(size: int) -> bytes:
100 for i in range(size):
101 pb += TestKaldiUtilsLoading.pack_value(i, TestKaldiUtilsLoading.float32_fmt)
105 def write_tag_with_value(tag: str, value) -> bytes:
106 pb = bytes(tag + ' ', 'ascii')
107 return pb + KaldiFrontExtractorTest.write_int_value(value)
110 def write_int_value(value) -> bytes:
111 pb = TestKaldiUtilsLoading.pack_value(4, 'B')
112 pb += TestKaldiUtilsLoading.pack_value(value, TestKaldiUtilsLoading.uint32_fmt)