Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / common / layout_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 from mo.front.common.layout import get_batch_dim, get_width_dim, get_height_dim, get_features_dim, get_depth_dim, \
20     shape_for_layout
21 from mo.utils.error import Error
22
23
24 class TestLayoutFunctions(unittest.TestCase):
25     def test_get_batch_dim_NCHW(self):
26         self.assertEqual(get_batch_dim('NCHW', 4), 0)
27
28     def test_get_batch_dim_NHWC(self):
29         self.assertEqual(get_batch_dim('NHWC', 4), 0)
30
31     def test_get_batch_dim_NCDHW(self):
32         self.assertEqual(get_batch_dim('NCHW', 5), 0)
33
34     def test_get_batch_dim_NDHWC(self):
35         self.assertEqual(get_batch_dim('NHWC', 5), 0)
36
37     def test_get_features_dim_NCHW(self):
38         self.assertEqual(get_features_dim('NCHW', 4), 1)
39
40     def test_get_features_dim_NHWC(self):
41         self.assertEqual(get_features_dim('NHWC', 4), 3)
42
43     def test_get_features_dim_NCDHW(self):
44         self.assertEqual(get_features_dim('NCHW', 5), 1)
45
46     def test_get_features_dim_NDHWC(self):
47         self.assertEqual(get_features_dim('NHWC', 5), 4)
48
49     def test_get_width_dim_NCHW(self):
50         self.assertEqual(get_width_dim('NCHW', 4), 3)
51
52     def test_get_width_dim_NHWC(self):
53         self.assertEqual(get_width_dim('NHWC', 4), 2)
54
55     def test_get_width_dim_NCDHW(self):
56         self.assertEqual(get_width_dim('NCHW', 5), 4)
57
58     def test_get_width_dim_NDHWC(self):
59         self.assertEqual(get_width_dim('NHWC', 5), 3)
60
61     def test_get_height_dim_NCHW(self):
62         self.assertEqual(get_height_dim('NCHW', 4), 2)
63
64     def test_get_height_dim_NHWC(self):
65         self.assertEqual(get_height_dim('NHWC', 4), 1)
66
67     def test_get_height_dim_NCDHW(self):
68         self.assertEqual(get_height_dim('NCHW', 5), 3)
69
70     def test_get_height_dim_NDHWC(self):
71         self.assertEqual(get_height_dim('NHWC', 5), 2)
72
73     def test_get_depth_dim_NCDHW(self):
74         self.assertEqual(get_depth_dim('NCHW', 5), 2)
75
76     def test_get_depth_dim_NDHWC(self):
77         self.assertEqual(get_depth_dim('NHWC', 5), 1)
78
79     def test_get_batch_dim_wrong_layout(self):
80         self.assertRaises(AssertionError, get_batch_dim, 'NCDHW', 5)
81
82     def test_get_width_dim_wrong_layout(self):
83         self.assertRaises(AssertionError, get_width_dim, 'NCDHW', 5)
84
85     def test_get_height_dim_wrong_layout(self):
86         self.assertRaises(AssertionError, get_height_dim, 'NCDHW', 5)
87
88     def test_get_features_dim_wrong_layout(self):
89         self.assertRaises(AssertionError, get_features_dim, 'NCDHW', 5)
90
91     def test_shape_for_layout_NCHW(self):
92         self.assertListEqual([2, 3, 4, 5], list(shape_for_layout('NCHW', batch=2, features=3, height=4, width=5)))
93
94     def test_shape_for_layout_NHWC(self):
95         self.assertListEqual([2, 4, 5, 3], list(shape_for_layout('NHWC', batch=2, features=3, height=4, width=5)))
96
97     def test_shape_for_layout_missing_batch(self):
98         with self.assertRaises(Error):
99             shape_for_layout('NCHW', features=3, height=4, width=5)
100
101     def test_shape_for_layout_missing_features(self):
102         with self.assertRaises(Error):
103             shape_for_layout('NCHW', batch=2, height=4, width=5)
104
105     def test_shape_for_layout_missing_height(self):
106         with self.assertRaises(Error):
107             shape_for_layout('NHWC', batch=2, features=3, width=5)
108
109     def test_shape_for_layout_missing_width(self):
110         with self.assertRaises(Error):
111             shape_for_layout('NHWC', batch=2, features=3, height=4)
112
113     def test_shape_for_layout_unknown_parameter(self):
114         with self.assertRaises(Error):
115             shape_for_layout('NHWC', batch=2, features=3, height=4, width=5, unknown_parameter=123)