Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / kaldi / extractors / splice_component_ext.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 numpy as np
17
18 from extensions.ops.splice import Splice
19 from mo.front.extractor import FrontExtractorOp
20 from mo.front.kaldi.loader.utils import find_next_tag, read_placeholder, read_binary_integer32_token, \
21     collect_until_whitespace
22 from mo.front.kaldi.utils import read_binary_vector
23 from mo.utils.error import Error
24
25
26 class SpliceFrontExtractor(FrontExtractorOp):
27     op = 'splicecomponent'
28     enabled = True
29
30     @staticmethod
31     def extract(node):
32         pb = node.parameters
33         mapping_rule = {
34             'context': list()
35         }
36         tag = find_next_tag(pb)
37         if tag == '<LeftContext>':
38             read_placeholder(pb, 1)
39             l_context = read_binary_integer32_token(pb)
40             tag = find_next_tag(pb)
41             if tag != '<RightContext>':
42                 raise Error('Unknown token {} in SpliceComponent node {}'.format(tag, node.id))
43             read_placeholder(pb, 1)
44             r_context = read_binary_integer32_token(pb)
45             for i in range(-l_context, r_context + 1):
46                 mapping_rule['context'].append(i)
47         elif tag == '<Context>':
48             collect_until_whitespace(pb)
49             mapping_rule['context'] = read_binary_vector(pb, False, dtype=np.int32)
50         else:
51             raise Error('Unknown token {} in SpliceComponent node {}'.format(tag, node.id))
52         Splice.update_node_stat(node, mapping_rule)
53         return __class__.enabled