Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / tf / conv_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 numpy as np
18
19 from extensions.front.tf.conv_ext import Conv2DFrontExtractor, DepthwiseConv2dNativeFrontExtractor
20 from mo.utils.unittest.extractors import PB, BaseExtractorsTestingClass
21
22
23 class ConvExtractorTest(BaseExtractorsTestingClass):
24     @classmethod
25     def setUpClass(cls):
26         cls.strides = [1, 2, 3, 4]
27         cls.dilations = [1, 1, 1, 1]
28
29     def test_conv_2d_defaults(self):
30         node = PB({'pb': PB({'attr': {
31             'data_format': PB({
32                 's': b"NHWC"
33             }),
34             'strides': PB({
35                 'list': PB({"i": self.strides})
36             }),
37             'padding': PB({
38                 's': b'VALID'
39             }),
40             'dilations': PB({
41                 'list': PB({"i": [1, 1, 1, 1]})
42             })
43         }})})
44         self.expected = {
45             'bias_addable': True,
46             'dilation': np.array([1, 1, 1, 1], dtype=np.int8),
47             'type': 'Convolution',
48             'layout': 'NHWC',
49         }
50         Conv2DFrontExtractor.extract(node)
51         self.res = node
52         self.expected_call_args = (None, False)
53         self.compare()
54
55     def test_conv2d_nhwc(self):
56         node = PB({'pb': PB({'attr': {
57             'data_format': PB({
58                 's': b"NHWC"
59             }),
60             'strides': PB({
61                 'list': PB({"i": self.strides})
62             }),
63             'padding': PB({
64                 's': b'VALID'
65             }),
66             'dilations': PB({
67                 'list': PB({"i": [1, 1, 1, 1]})
68             })
69         }})})
70         self.expected = {
71             # spatial_dims = [1, 2] will be detected in infer function
72             "channel_dims": [3],
73             "batch_dims": [0],
74             "input_feature_channel": 2,
75             "output_feature_channel": 3,
76             'dilation': np.array([1, 1, 1, 1], dtype=np.int8),
77             'stride': np.array(self.strides, dtype=np.int8),
78         }
79         Conv2DFrontExtractor.extract(node)
80         self.res = node
81         self.expected_call_args = (None, False)
82         self.compare()
83
84     def test_conv2d_nchw(self):
85         node = PB({'pb': PB({'attr': {
86             'data_format': PB({
87                 's': b"NCHW"
88             }),
89             'strides': PB({
90                 'list': PB({"i": self.strides})
91             }),
92             'padding': PB({
93                 's': b'VALID'
94             }),
95             'dilations': PB({
96                 'list': PB({"i": [1, 1, 1, 1]})
97             })
98         }})})
99         self.expected = {
100             # spatial_dims = [2, 3] will be detected in infer function
101             "channel_dims": [1],
102             "batch_dims": [0],
103             "input_feature_channel": 2,
104             "output_feature_channel": 3,
105             'dilation': np.array([1, 1, 1, 1], dtype=np.int8),
106             'stride': np.array(self.strides, dtype=np.int8),
107         }
108         Conv2DFrontExtractor.extract(node)
109         self.res = node
110         self.expected_call_args = (None, False)
111         self.compare()
112
113     def test_conv2d_depthwise(self):
114         node = PB({'pb': PB({'attr': {
115             'data_format': PB({
116                 's': b"NHWC"
117             }),
118             'strides': PB({
119                 'list': PB({"i": self.strides}),
120             }),
121             'dilations': PB({
122                 'list': PB({"i": self.dilations}),
123             }),
124             'padding': PB({
125                 's': b'VALID'
126             })
127         }})})
128         self.expected = {
129             # spatial_dims = [1, 2] will be detected in infer function
130             "channel_dims": [3],
131             "batch_dims": [0],
132             "input_feature_channel": 2,
133             "output_feature_channel": 2,
134             'dilation': np.array([1, 1, 1, 1], dtype=np.int8),
135             'stride': np.array(self.strides, dtype=np.int8),
136         }
137         DepthwiseConv2dNativeFrontExtractor.extract(node)
138         self.res = node
139         self.expected_call_args = (None, True)
140         self.compare()