Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / kaldi / extractors / convolutional_component_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 import numpy as np
17
18 from mo.front.kaldi.extractors.common_ext_test import KaldiFrontExtractorTest
19 from mo.front.kaldi.extractors.convolutional_component_ext import ConvolutionalComponentFrontExtractor
20 from mo.front.kaldi.loader.utils_test import TestKaldiUtilsLoading
21 from mo.ops.convolution import Convolution
22 from mo.ops.op import Op
23
24
25 class ConvolutionalComponentFrontExtractorTest(KaldiFrontExtractorTest):
26     @classmethod
27     def register_op(cls):
28         Op.registered_ops['Convolution'] = Convolution
29
30     @classmethod
31     def create_pb_for_test_node(cls):
32         pb = KaldiFrontExtractorTest.write_tag_with_value('<PatchDim>', 2)
33         pb += KaldiFrontExtractorTest.write_tag_with_value('<PatchStep>', 2)
34         pb += KaldiFrontExtractorTest.write_tag_with_value('<PatchStride>', 4)
35         pb += KaldiFrontExtractorTest.generate_learn_info()
36         pb += b'<Filters> '
37         pb += KaldiFrontExtractorTest.generate_matrix([2, 1])
38         pb += b'<Bias> '
39         pb += KaldiFrontExtractorTest.generate_vector(2)
40         cls.test_node['parameters'] = TestKaldiUtilsLoading.bytesio_from(pb)
41         ConvolutionalComponentFrontExtractor.extract(cls.test_node)
42
43     def test_assertion(self):
44         self.assertRaises(AttributeError, ConvolutionalComponentFrontExtractor.extract, None)
45
46     def test_attrs(self):
47         val_attrs = {
48             'kernel': [1, 1, 1, 2],
49             'stride': [1, 1, 1, 2],
50             'pad': [[[0, 0], [0, 0], [0, 0], [0, 0]]],
51             'output': 2,
52             'patch_stride': 4,
53             'spatial_dims': [2, 3],
54             'channel_dims': [1],
55             'batch_dims': [0],
56             'dilation': [1, 1, 1, 1]
57         }
58         for attr in val_attrs:
59             if isinstance(val_attrs[attr], list):
60                 self.assertTrue((self.test_node[attr] == val_attrs[attr]).all())
61             else:
62                 self.assertEqual(self.test_node[attr], val_attrs[attr])
63
64     def test_convolution_blobs(self):
65         self.assertTrue(np.array_equal(self.test_node.weights, [0, 1]))
66         self.assertTrue(np.array_equal(self.test_node.biases, [0, 1]))
67