Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / caffe / extractors / utils_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, call
19
20 import numpy as np
21
22 from mo.front.caffe.extractors.utils import weights_biases, embed_input, get_canonical_axis_index
23 from mo.utils.unittest.extractors import FakeModelLayer
24
25
26 class TestWeightsBiases(unittest.TestCase):
27     def test_weights_biases_no_layer_no_bias(self):
28         res = weights_biases(False, None)
29         self.assertEqual(res, {})
30
31     @patch('mo.front.caffe.extractors.utils.embed_input')
32     def test_weights_biases_layer_no_bias(self, embed_input_mock):
33         weights_biases(False, FakeModelLayer([[1, 2], ]))
34         calls = [call({}, 1, 'weights', [1, 2])]
35         embed_input_mock.assert_has_calls(calls)
36
37     @patch('mo.front.caffe.extractors.utils.embed_input')
38     def test_weights_biases_layer_bias(self, embed_input_mock):
39         weights_biases(True, FakeModelLayer([[1, 2], [3, 4]]))
40         calls = [call({}, 1, 'weights', [1, 2]), call({}, 2, 'biases', [3, 4])]
41         embed_input_mock.assert_has_calls(calls)
42
43
44 class TestEmbedInput(unittest.TestCase):
45     def test_embed_input_no_bin_name_no_bias(self):
46         attrs = {}
47         blob = np.array([1, 2])
48         name = 'weights'
49         embed_input(attrs, 1, name, blob, None)
50         exp_res = {
51             'weights': blob,
52             'embedded_inputs': [
53                 (1, name, {'bin': name})
54             ]
55         }
56         for key in exp_res.keys():
57             if key == name:
58                 np.testing.assert_equal(attrs[key], exp_res[key])
59             else:
60                 self.assertEqual(attrs[key], exp_res[key])
61
62     def test_embed_input_w_bin_name(self):
63         attrs = {}
64         blob = np.array([1, 2])
65         name = 'weights'
66         embed_input(attrs, 1, name, blob, 'special_name')
67         exp_res = {
68             'weights': blob,
69             'embedded_inputs': [
70                 (1, name, {'bin': 'special_name'})
71             ]
72         }
73         for key in exp_res.keys():
74             if key == name:
75                 np.testing.assert_equal(attrs[key], exp_res[key])
76             else:
77                 self.assertEqual(attrs[key], exp_res[key])
78
79
80 class TestCanonicalAxisIndex(unittest.TestCase):
81     def test_negative_index(self):
82         shape = [1, 2, 3, 4]
83         inds = [-4, -3, -2, -1]
84         expected_inds = [0, 1, 2, 3]
85         for i in range(len(inds)):
86             assert get_canonical_axis_index(shape, inds[i]) == expected_inds[i]
87
88     def test_posirive_index(self):
89         shape = [1, 2, 3, 4]
90         inds = [0, 1, 2, 3]
91         expected_inds = [0, 1, 2, 3]
92         for i in range(len(inds)):
93             assert get_canonical_axis_index(shape, inds[i]) == expected_inds[i]