Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / caffe / extractors / batchnorm_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.batchnorm import batch_norm_ext
22 from mo.front.common.partial_infer.elemental import copy_shape_infer
23 from mo.utils.unittest.extractors import FakeParam, FakeModelLayer
24
25
26 class FakeBNProtoLayer:
27     def __init__(self, eps):
28         self.batch_norm_param = FakeParam('eps', eps)
29
30
31 class TestShapesParsing(unittest.TestCase):
32     def test_bn_ext_no_ml_no_pb(self):
33         self.assertRaises(AssertionError, batch_norm_ext, None, None)
34
35     def test_bn_ext_no_ml(self):
36         res = batch_norm_ext(FakeBNProtoLayer(10), None)
37         exp_res = {
38             'op': 'BatchNormalization',
39             'type': 'BatchNormalization',
40             'epsilon': 10,
41             'infer': copy_shape_infer
42         }
43         self.assertEqual(res, exp_res)
44
45     def test_bn_ext_ml_one_blob(self):
46         self.assertRaises(AssertionError, batch_norm_ext, FakeBNProtoLayer(10), FakeModelLayer([np.array([1, 2])]))
47
48     def test_bn_ext_ml_two_blobs(self):
49         mean_blob = np.array([1., 2.])
50         variance_blob = np.array([3., 4.])
51         blobs = [mean_blob, variance_blob]
52         res = batch_norm_ext(FakeBNProtoLayer(10),
53                              FakeModelLayer(blobs))
54         exp_res = {
55             'type': 'BatchNormalization',
56             'epsilon': 10,
57             'infer': copy_shape_infer,
58             'mean': mean_blob,
59             'variance': variance_blob,
60             'embedded_inputs': [
61                 (1, 'mean', {
62                     'bin': 'biases'
63                 }),
64                 (2, 'variance', {
65                     'bin': 'weights'
66                 })
67             ]
68         }
69         for i in exp_res:
70             if i in ('mean', 'variance'):
71                 np.testing.assert_array_equal(res[i], exp_res[i])
72             else:
73                 self.assertEqual(res[i], exp_res[i])
74
75     def test_bn_ext_ml_three_blobs(self):
76         mean_blob = np.array([1., 2.])
77         variance_blob = np.array([3., 4.])
78         scale_blob = np.array([5., ])
79         blobs = [mean_blob, variance_blob, scale_blob]
80         res = batch_norm_ext(FakeBNProtoLayer(10),
81                              FakeModelLayer(blobs))
82         exp_res = {
83             'type': 'BatchNormalization',
84             'epsilon': 10,
85             'infer': copy_shape_infer,
86             'mean': mean_blob * 0.2,
87             'variance': variance_blob * 0.2,
88             'embedded_inputs': [
89                 (1, 'mean', {
90                     'bin': 'biases'
91                 }),
92                 (2, 'variance', {
93                     'bin': 'weights'
94                 })
95             ]
96         }
97         for i in exp_res:
98             if i in ('mean', 'variance'):
99                 np.testing.assert_array_equal(res[i], exp_res[i])
100             else:
101                 self.assertEqual(res[i], exp_res[i])
102
103     def test_bn_ext_ml_three_blobs_zero_scale(self):
104         mean_blob = np.array([1., 2.])
105         variance_blob = np.array([3., 4.])
106         scale_blob = np.array([0., ])
107         blobs = [mean_blob, variance_blob, scale_blob]
108         res = batch_norm_ext(FakeBNProtoLayer(10),
109                              FakeModelLayer(blobs))
110         exp_res = {
111             'type': 'BatchNormalization',
112             'epsilon': 10,
113             'infer': copy_shape_infer,
114             'mean': mean_blob * 0.,
115             'variance': variance_blob * 0.,
116             'embedded_inputs': [
117                 (1, 'mean', {
118                     'bin': 'biases'
119                 }),
120                 (2, 'variance', {
121                     'bin': 'weights'
122                 })
123             ]
124         }
125         for i in exp_res:
126             if i in ('mean', 'variance'):
127                 np.testing.assert_array_equal(res[i], exp_res[i])
128             else:
129                 self.assertEqual(res[i], exp_res[i])