Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / caffe / pooling_ext_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 extensions.front.caffe.pooling_ext import PoolingFrontExtractor
22 from mo.front.common.extractors.utils import layout_attrs
23 from mo.ops.pooling import Pooling
24 from mo.utils.unittest.extractors import PB, FakeMultiParam
25
26
27 class FakeProtoLayer:
28     def __init__(self, val):
29         self.pooling_param = val
30
31
32 class TestPooling(unittest.TestCase):
33     def test_pooling_ext_global(self):
34         params = {
35             'kernel_size': 1,
36             'stride': 2,
37             'pad': 3,
38             'pool': 0,
39             'global_pooling': 1,
40             'ceil_mode': 1
41         }
42         node = PB({'pb': FakeProtoLayer(FakeMultiParam(params))})
43         PoolingFrontExtractor.extract(node)
44         res = node
45         exp_res = {
46             'window': np.array([1, 1, 0, 0], dtype=np.int64),
47             'stride': np.array([1, 1, 1, 1], dtype=np.int64),
48             'pad': np.array([[0, 0], [0, 0], [0, 0], [0, 0]], dtype=np.int64),
49             'pad_spatial_shape': np.array([[0, 0], [0, 0]], dtype=np.int64),
50             'pool_method': 'max',
51             'exclude_pad': 'true',
52             'infer': Pooling.infer,
53             'global_pool': 1,
54             'output_spatial_shape': None,
55             'pooling_convention': 'full',
56             'rounding_type': 'ceil'
57
58         }
59         exp_res.update(layout_attrs())
60         for i in exp_res.keys():
61             if i in ('window', 'stride',
62                      'pad', 'pad_spatial_shape',
63                      'spatial_dims', 'batch_dims',
64                      'channel_dims'):
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_pooling_ext(self):
70         params = {
71             'kernel_size': 1,
72             'stride': 2,
73             'pad': 3,
74             'pool': 1,
75             'global_pooling': 0,
76             'ceil_mode': 0
77         }
78         node = PB({'pb': FakeProtoLayer(FakeMultiParam(params))})
79         PoolingFrontExtractor.extract(node)
80         res = node
81         exp_res = {
82             'window': np.array([1, 1, 1, 1], dtype=np.int64),
83             'stride': np.array([1, 1, 2, 2], dtype=np.int64),
84             'pad': np.array([[0, 0], [0, 0], [3, 3], [3, 3]], dtype=np.int64),
85             'pad_spatial_shape': np.array([[3, 3], [3, 3]], dtype=np.int64),
86             'pool_method': 'avg',
87             'exclude_pad': 'false',
88             'infer': Pooling.infer,
89             'global_pool': 0,
90             'output_spatial_shape': None,
91             'pooling_convention': 'valid'
92         }
93         exp_res.update(layout_attrs())
94         for i in exp_res.keys():
95             if i in ('window', 'stride',
96                      'pad', 'pad_spatial_shape',
97                      'spatial_dims', 'batch_dims',
98                      'channel_dims'):
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_pooling_ext_exception(self):
104         params = {
105             'kernel_size': 1,
106             'stride': 2,
107             'pad': 3,
108             'pool': 3,
109             'global_pooling': 1
110         }
111         node = PB({'pb': FakeProtoLayer(FakeMultiParam(params))})
112         self.assertRaises(ValueError, PoolingFrontExtractor.extract, node)