Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / caffe / extractors / scale_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 import numpy as np
20
21 from mo.front.caffe.extractors.scale import scale_ext
22 from mo.front.common.partial_infer.elemental import copy_shape_infer
23 from mo.utils.unittest.extractors import FakeMultiParam, FakeModelLayer
24
25
26 class FakeProtoLayer:
27     def __init__(self, val, bottom2=False):
28         self.scale_param = val
29         if bottom2:
30             self.bottom = {"bottom1", "bottom2"}
31         else:
32             self.bottom = {"bottom1"}
33
34
35 class TestScale(unittest.TestCase):
36     def test_scale_ext(self):
37         mean_blob = np.array([1., 2.])
38         variance_blob = np.array([3., 4.])
39         blobs = [mean_blob, variance_blob]
40         params = {
41             'type': 'Scale',
42             'axis': 0,
43             'bias_term': True
44         }
45
46         res = scale_ext(FakeProtoLayer(FakeMultiParam(params)), FakeModelLayer(blobs))
47         exp_res = {
48             'op': 'ScaleShift',
49             'type': 'ScaleShift',
50             'axis': 0,
51             'infer': copy_shape_infer,
52             'weights': mean_blob,
53             'biases': variance_blob,
54             'embedded_inputs': [
55                 (1, 'weights', {
56                     'bin': 'weights'
57                 }),
58                 (2, 'biases', {
59                     'bin': 'biases'
60                 })
61             ]
62         }
63         for i in exp_res:
64             if i in ('weights', 'biases'):
65                 np.testing.assert_array_equal(res[i], exp_res[i])
66             else:
67                 self.assertEqual(res[i], exp_res[i])
68
69     def test_scale_2inputs_ext(self):
70         params = {
71             'type': 'Scale',
72             'axis': 0,
73             'bias_term': False
74         }
75
76         res = scale_ext(FakeProtoLayer(FakeMultiParam(params), True), None)
77         exp_res = {
78             'op': 'ScaleShift',
79             'type': 'ScaleShift',
80             'axis': 0,
81             'infer': copy_shape_infer,
82         }
83         for i in exp_res:
84             self.assertEqual(res[i], exp_res[i])
85
86     def test_scale_2inputs_bias_ext(self):
87         variance_blob = np.array([3., 4.])
88         blobs = [variance_blob]
89
90         params = {
91             'type': 'Scale',
92             'axis': 0,
93             'bias_term': True
94         }
95
96         res = scale_ext(FakeProtoLayer(FakeMultiParam(params), True), FakeModelLayer(blobs))
97         exp_res = {
98             'op': 'ScaleShift',
99             'type': 'ScaleShift',
100             'axis': 0,
101             'infer': copy_shape_infer,
102             'biases': variance_blob,
103             'embedded_inputs': [
104                 (1, 'biases', {
105                     'bin': 'biases'
106                 })]
107         }
108         for i in exp_res:
109             if i in ('biases'):
110                 np.testing.assert_array_equal(res[i], exp_res[i])
111             else:
112                 self.assertEqual(res[i], exp_res[i])
113
114     def test_create_default_weights(self):
115         """
116         There are situations when scale layer doesn't have weights and biases. This test checks that if they are not
117         available in the caffemodel file then default values [1] and [0] are generated.
118         """
119         scale_blob = np.array([1])
120         bias_blob = np.array([0])
121         params = {
122             'type': 'Scale',
123             'axis': 0,
124             'bias_term': True
125         }
126
127         res = scale_ext(FakeProtoLayer(FakeMultiParam(params)), None)
128         exp_res = {
129             'op': 'ScaleShift',
130             'type': 'ScaleShift',
131             'axis': 0,
132             'infer': copy_shape_infer,
133             'weights': scale_blob,
134             'biases': bias_blob,
135             'embedded_inputs': [
136                 (1, 'weights', {
137                     'bin': 'weights'
138                 }),
139                 (2, 'biases', {
140                     'bin': 'biases'
141                 })
142             ]
143         }
144         self.assertDictEqual(exp_res, res)