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 unittest.mock import patch, call
22 from mo.front.caffe.extractors.utils import weights_biases, embed_input, get_canonical_axis_index
23 from mo.utils.unittest.extractors import FakeModelLayer
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, {})
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)
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)
44 class TestEmbedInput(unittest.TestCase):
45 def test_embed_input_no_bin_name_no_bias(self):
47 blob = np.array([1, 2])
49 embed_input(attrs, 1, name, blob, None)
53 (1, name, {'bin': name})
56 for key in exp_res.keys():
58 np.testing.assert_equal(attrs[key], exp_res[key])
60 self.assertEqual(attrs[key], exp_res[key])
62 def test_embed_input_w_bin_name(self):
64 blob = np.array([1, 2])
66 embed_input(attrs, 1, name, blob, 'special_name')
70 (1, name, {'bin': 'special_name'})
73 for key in exp_res.keys():
75 np.testing.assert_equal(attrs[key], exp_res[key])
77 self.assertEqual(attrs[key], exp_res[key])
80 class TestCanonicalAxisIndex(unittest.TestCase):
81 def test_negative_index(self):
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]
88 def test_posirive_index(self):
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]