Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / caffe / python_layer_extractor_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
17 import unittest
18
19 from mo.front.caffe.python_layer_extractor import PythonFrontExtractorOp
20 from mo.front.extractor import CaffePythonFrontExtractorOp
21 from mo.graph.graph import Node
22 from mo.utils.unittest.extractors import FakeMultiParam
23 from mo.utils.unittest.graph import FakeNode
24
25
26 class FakePythonProtoLayer:
27     def __init__(self, params: FakeMultiParam):
28         self.type = 'Python'
29         self.python_param = params
30
31
32 class FakePythonExtractor:
33     @staticmethod
34     def extract(node: Node):
35         return True
36
37
38 class TestPythonLayerExtractor(unittest.TestCase):
39     def test_python_extractor_for_op(self):
40         module = 'test_module'
41         layer = 'test_layer'
42         CaffePythonFrontExtractorOp.registered_ops['{}.{}'.format(module, layer)] = \
43             lambda node: CaffePythonFrontExtractorOp.parse_param_str(node.pb.python_param.param_str)
44         params = FakeMultiParam({
45             'module': module,
46             'layer': layer,
47             'param_str': "'feat_stride': 16"
48         })
49         ext = PythonFrontExtractorOp.extract(FakeNode(FakePythonProtoLayer(params), None))
50         self.assertEqual({'feat_stride': 16}, ext)
51
52     def test_python_extractor_for_extractors(self):
53         module = 'test_module'
54         layer = 'test_layer'
55         CaffePythonFrontExtractorOp.registered_ops['{}.{}'.format(module, layer)] = FakePythonExtractor
56         params = FakeMultiParam({
57             'module': module,
58             'layer': layer,
59             'param_str': "'feat_stride': 16"
60         })
61         self.assertTrue(PythonFrontExtractorOp.extract(FakeNode(FakePythonProtoLayer(params), None)))