Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / caffe / extractors / eltwise_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.caffe.extractors.eltwise import eltwise_ext
21 from mo.utils.unittest.extractors import FakeMultiParam
22
23
24 class FakeProtoLayer:
25     def __init__(self, operation, coeff=[1]):
26         self.eltwise_param = FakeMultiParam({'operation': operation,
27                                              'coeff': coeff})
28
29
30 class TestEltwise(unittest.TestCase):
31     @patch('mo.front.caffe.extractors.eltwise.eltwise_infer')
32     def test_eltwise_op_mul(self, eltwise_infer_mock):
33         eltwise_infer_mock.return_value = {}
34         res = eltwise_ext(FakeProtoLayer(0), None)
35         exp_res = {
36             'op': 'Mul',
37             'operation': 'mul',
38             'infer': None
39         }
40
41         for i in exp_res.keys():
42             if i == 'infer':
43                 res['infer'](None)
44                 args = eltwise_infer_mock.call_args
45                 actual_lambda = args[0][1]
46                 self.assertTrue(eltwise_infer_mock.called)
47                 self.assertEqual(actual_lambda(3, 5), 3 * 5)
48             else:
49                 self.assertEqual(res[i], exp_res[i])
50
51     @patch('mo.front.caffe.extractors.eltwise.eltwise_infer')
52     def test_eltwise_op_add(self, eltwise_infer_mock):
53         eltwise_infer_mock.return_value = {}
54         res = eltwise_ext(FakeProtoLayer(1, coeff=[0.39]), None)
55         exp_res = {
56             'op': 'Add',
57             'operation': 'sum',
58             'coeff': '0.39',
59             'infer': None
60         }
61
62         for i in exp_res.keys():
63             if i == 'infer':
64                 res['infer'](None)
65                 args = eltwise_infer_mock.call_args
66                 actual_lambda = args[0][1]
67                 self.assertTrue(eltwise_infer_mock.called)
68                 self.assertEqual(actual_lambda(3, 5), 3 + 5)
69             else:
70                 self.assertEqual(res[i], exp_res[i])
71
72     @patch('mo.front.caffe.extractors.eltwise.eltwise_infer')
73     def test_eltwise_op_max(self, eltwise_infer_mock):
74         eltwise_infer_mock.return_value = {}
75         res = eltwise_ext(FakeProtoLayer(2), None)
76         exp_res = {
77             'op': 'Max',
78             'operation': 'max',
79             'infer': None
80         }
81
82         for i in exp_res.keys():
83             if i == 'infer':
84                 res['infer'](None)
85                 args = eltwise_infer_mock.call_args
86                 actual_lambda = args[0][1]
87                 self.assertTrue(eltwise_infer_mock.called)
88                 self.assertEqual(actual_lambda(3, 5), 5)
89             else:
90                 self.assertEqual(res[i], exp_res[i])
91
92     def test_eltwise_op_exeption(self):
93         self.assertRaises(Exception, eltwise_ext, FakeProtoLayer(4), None)