Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / mxnet / loader_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 from unittest.mock import patch
19
20 from mo.front.mxnet.loader import load_symbol_nodes, parse_input_model
21
22
23 class MockSymbolLoadObj():
24     def tojson(self):
25         pass
26
27
28 class TestLoader(unittest.TestCase):
29     @patch('json.load')
30     @patch('json.loads')
31     @patch('os.path.isfile')
32     @patch('mxnet.symbol.load')
33     def test_load_symbol_nodes(self, mock_symbol_load, mock_isfile, mock_json_loads, mock_json_load):
34         mock_isfile.return_value = True
35         mock_json_load.return_value = {'nodes': ''}
36         mock_json_loads.return_value = {'nodes': {'node1': 1}}
37         mock_symbol_load_obj = MockSymbolLoadObj()
38         mock_symbol_load.return_value = mock_symbol_load_obj
39         with patch('mo.front.mxnet.loader.open') as mock_open:
40             self.assertEqual({'node1': 1}, load_symbol_nodes("model_name", True))
41
42     @patch('json.load')
43     @patch('json.loads')
44     @patch('os.path.isfile')
45     @patch('mxnet.symbol.load')
46     def test_load_symbol_with_custom_nodes(self, mock_symbol_load, mock_isfile, mock_json_loads, mock_json_load):
47         mock_isfile.return_value = True
48         mock_json_load.return_value = {'nodes': [{'op': 'custom_op'}, {'op': 'custom_op'}]}
49         mock_json_loads.return_value = {'nodes': {'node1': 1}}
50         mock_symbol_load_obj = MockSymbolLoadObj()
51         mock_symbol_load.return_value = mock_symbol_load_obj
52         with patch('mo.front.mxnet.loader.open') as mock_open:
53             list_nodes = load_symbol_nodes("model_name", False)
54             self.assertEqual(2, len(list_nodes))
55             for node in list_nodes:
56                 self.assertEqual({'op': 'custom_op'}, node)
57
58     def test_parse_input_model(self):
59         input_model = '/model-optimizer-mxnet/data/nd/vgg19-0015.params'
60         model_name, iteration_number = parse_input_model(input_model)
61         self.assertEqual(model_name, '/model-optimizer-mxnet/data/nd/vgg19')
62         self.assertEqual(iteration_number, 15)