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.
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
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
29 class ConvolutionalComponentFrontExtractor(FrontExtractorOp):
30 op = 'convolutionalcomponent' # Naming like in Kaldi
34 def extract(node: Node) -> bool:
36 Extract conv parameters from node.parameters.
37 node.parameters like file descriptor object.
38 :param node: Convolution node
42 kernel = read_token_value(pb, b'<PatchDim>')
43 stride = read_token_value(pb, b'<PatchStep>')
44 patch_stride = read_token_value(pb, b'<PatchStride>')
46 read_learning_info(pb)
48 collect_until_whitespace(pb)
49 weights, weights_shape = read_binary_matrix(pb)
51 collect_until_whitespace(pb)
52 biases = read_binary_vector(pb)
54 if (patch_stride - kernel) % stride != 0:
56 'Kernel size and stride does not correspond to `patch_stride` attribute of Convolution layer. ' +
59 output = biases.shape[0]
60 if weights_shape[0] != output:
61 raise Error('Weights shape does not correspond to the `output` attribute of Convolution layer. ' +
66 'patch_stride': patch_stride,
68 'pad': np.array([[0, 0], [0, 0], [0, 0], [0, 0]], dtype=np.int64),
69 'pad_spatial_shape': np.array([[0, 0], [0, 0]], dtype=np.int64),
70 'dilation': np.array([1, 1, 1, 1], dtype=np.int64),
71 'kernel': np.array([1, 1, 1, kernel], dtype=np.int64),
72 'stride': np.array([1, 1, 1, stride], dtype=np.int64),
73 'kernel_spatial': np.array([1, kernel], dtype=np.int64),
74 'input_feature_channel': 1,
75 'output_feature_channel': 0,
76 'kernel_spatial_idx': [2, 3],
78 'reshape_kernel': True,
81 mapping_rule.update(layout_attrs())
82 embed_input(mapping_rule, 1, 'weights', weights)
83 embed_input(mapping_rule, 2, 'biases', biases)
85 mapping_rule['bias_addable'] = len(biases) > 0
87 Convolution.update_node_stat(node, mapping_rule)
88 return __class__.enabled