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.
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
26 class FakeBNProtoLayer:
27 def __init__(self, eps):
28 self.batch_norm_param = FakeParam('eps', eps)
31 class TestShapesParsing(unittest.TestCase):
32 def test_bn_ext_no_ml_no_pb(self):
33 self.assertRaises(AssertionError, batch_norm_ext, None, None)
35 def test_bn_ext_no_ml(self):
36 res = batch_norm_ext(FakeBNProtoLayer(10), None)
38 'op': 'BatchNormalization',
39 'type': 'BatchNormalization',
41 'infer': copy_shape_infer
43 self.assertEqual(res, exp_res)
45 def test_bn_ext_ml_one_blob(self):
46 self.assertRaises(AssertionError, batch_norm_ext, FakeBNProtoLayer(10), FakeModelLayer([np.array([1, 2])]))
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))
55 'type': 'BatchNormalization',
57 'infer': copy_shape_infer,
59 'variance': variance_blob,
70 if i in ('mean', 'variance'):
71 np.testing.assert_array_equal(res[i], exp_res[i])
73 self.assertEqual(res[i], exp_res[i])
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))
83 'type': 'BatchNormalization',
85 'infer': copy_shape_infer,
86 'mean': mean_blob * 0.2,
87 'variance': variance_blob * 0.2,
98 if i in ('mean', 'variance'):
99 np.testing.assert_array_equal(res[i], exp_res[i])
101 self.assertEqual(res[i], exp_res[i])
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))
111 'type': 'BatchNormalization',
113 'infer': copy_shape_infer,
114 'mean': mean_blob * 0.,
115 'variance': variance_blob * 0.,
126 if i in ('mean', 'variance'):
127 np.testing.assert_array_equal(res[i], exp_res[i])
129 self.assertEqual(res[i], exp_res[i])