Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / kaldi / extractors / convolutional_1d_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 mo.front.caffe.extractors.utils import embed_input
19 from mo.front.common.extractors.utils import layout_attrs
20 from mo.front.extractor import FrontExtractorOp
21 from mo.front.kaldi.loader.utils import read_token_value, collect_until_whitespace, find_next_tag
22 from mo.front.kaldi.utils import read_learning_info, read_binary_matrix, read_binary_vector
23 from mo.graph.graph import Node
24 from mo.ops.convolution import Convolution
25 from mo.utils.error import Error
26 from mo.utils.utils import refer_to_faq_msg
27
28
29 class ConvolutionalComponentFrontExtractor(FrontExtractorOp):
30     op = 'convolutional1dcomponent'  # Naming like in Kaldi
31     enabled = True
32
33     @staticmethod
34     def extract(node: Node) -> bool:
35         """
36         Extract conv parameters from node.parameters.
37         node.parameters like file descriptor object.
38         :param node: Convolution node
39         :return:
40         """
41         pb = node.parameters
42         read_learning_info(pb)
43
44         kernel = read_token_value(pb, b'<PatchDim>')
45         stride = read_token_value(pb, b'<PatchStep>')
46         patch_stride = read_token_value(pb, b'<PatchStride>')
47
48         token = find_next_tag(pb)
49         if token == '<AppendedConv>':
50             appended_conv = True
51             token = find_next_tag(pb)
52         if token != '<FilterParams>':
53             raise Error('Can not load token {} from Kaldi model'.format(token) +
54                         refer_to_faq_msg(94))
55         collect_until_whitespace(pb)
56         weights, weights_shape = read_binary_matrix(pb)
57
58         collect_until_whitespace(pb)
59         biases = read_binary_vector(pb)
60
61         if (patch_stride - kernel) % stride != 0:
62             raise Error(
63                 'Kernel size and stride does not correspond to `patch_stride` attribute of Convolution layer. ' +
64                 refer_to_faq_msg(93))
65
66         output = biases.shape[0]
67         if weights_shape[0] != output:
68             raise Error('Weights shape does not correspond to the `output` attribute of Convolution layer. ' +
69                         refer_to_faq_msg(93))
70
71         mapping_rule = {
72             'output': output,
73             'patch_stride': patch_stride,
74             'bias_term': None,
75             'pad': np.array([[0, 0], [0, 0], [0, 0], [0, 0]], dtype=np.int64),
76             'pad_spatial_shape': np.array([[0, 0], [0, 0]], dtype=np.int64),
77             'dilation': np.array([1, 1, 1, 1], dtype=np.int64),
78             'kernel': np.array([1, 1, 1, kernel], dtype=np.int64),
79             'stride': np.array([1, 1, 1, stride], dtype=np.int64),
80             'kernel_spatial': np.array([1, kernel], dtype=np.int64),
81             'input_feature_channel': 1,
82             'output_feature_channel': 0,
83             'kernel_spatial_idx': [2, 3],
84             'group': 1,
85             'reshape_kernel': True,
86             'appended_conv': appended_conv
87         }
88
89         mapping_rule.update(layout_attrs())
90         embed_input(mapping_rule, 1, 'weights', weights)
91         embed_input(mapping_rule, 2, 'biases', biases)
92
93         mapping_rule['bias_addable'] = len(biases) > 0
94
95         Convolution.update_node_stat(node, mapping_rule)
96         return __class__.enabled