Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / mxnet / conv_ext.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 mo.front.extractor import FrontExtractorOp
20 from mo.front.mxnet.extractors.utils import get_mxnet_layer_attrs
21 from mo.ops.convolution import Convolution
22 from mo.front.common.extractors.utils import layout_attrs
23
24 class ConvFrontExtractor(FrontExtractorOp):
25     op = 'Convolution'
26     enabled = True
27
28     @staticmethod
29     def extract(node):
30         attr = get_mxnet_layer_attrs(node.symbol_dict)
31
32         kernel = attr.tuple("kernel", int, None)
33         stride = attr.tuple("stride", int, tuple(np.ones(len(kernel), dtype=np.int64)))
34         padding = attr.tuple("pad", int, tuple(np.zeros(len(kernel), dtype=np.int64)))
35         dilate = attr.tuple("dilate", int, tuple(np.ones(len(kernel), dtype=np.int64)))
36         group = attr.int("num_group", 1)
37         output = attr.int("num_filter", None)
38         bias_term = attr.str("no_bias", 'False') == 'False'
39
40         final_dilations = np.array([1, 1, *[d for d in dilate]], dtype=np.int64) if dilate is not None else None
41
42         node_attrs = {
43             'op': __class__.op,
44             'bias_addable': True,
45             'bias_term': bias_term,
46             'pad': np.array([[0, 0], [0, 0], *[[pad, pad] for pad in padding]], dtype=np.int64),
47             'pad_spatial_shape': np.array([[pad, pad] for pad in padding], dtype=np.int64),
48             'dilation': final_dilations,
49             'output_spatial_shape': None,
50             'output_shape': None,
51             'stride': np.array([1, 1, *[s for s in stride]], dtype=np.int64),
52             'group': group,
53             'output': output,
54             'kernel_spatial': np.array([k for k in kernel], dtype=np.int64),
55
56             'input_feature_channel': 1,
57             'output_feature_channel': 0,
58             'kernel_spatial_idx': None,
59             'reshape_kernel': True,
60
61             'spatial_dims': None,
62             'channel_dims': np.array([1], dtype=np.int64),
63             'batch_dims': np.array([0], dtype=np.int64),
64             'layout': 'NCHW',
65         }
66
67         # update the attributes of the node
68         Convolution.update_node_stat(node, node_attrs)
69         return __class__.enabled
70
71
72 class DeconvFrontExtractor(FrontExtractorOp):
73     op = 'Deconvolution'
74     enabled = True
75
76     @staticmethod
77     def get_pad(node, input_shape, kernel_shape):
78         padding = np.add.reduce(node.pad, axis=1)
79         padding[node.spatial_dims] = node.stride[node.spatial_dims] * (input_shape[node.spatial_dims] - 1) + 1 + \
80                                      (kernel_shape[node.spatial_dims] - 1) * node.dilation[node.spatial_dims]
81         padding[node.spatial_dims] = padding[node.spatial_dims] - node.output_spatial_shape;
82         padding[node.spatial_dims] = (padding[node.spatial_dims] + 1) / 2
83         return np.array([[0, 0], [0, 0], *[[pad, pad] for pad in padding[2:]]], dtype=np.int64)
84
85     @staticmethod
86     def extract(node):
87         attr = get_mxnet_layer_attrs(node.symbol_dict)
88
89         kernel = attr.tuple("kernel", int, None)
90         stride = attr.tuple("stride", int, tuple(np.ones(len(kernel), dtype=np.int64)))
91         padding = attr.tuple("pad", int, tuple(np.zeros(len(kernel), dtype=np.int64)))
92         dilate = attr.tuple("dilate", int, tuple(np.ones(len(kernel), dtype=np.int64)))
93         group = attr.int("num_group", 1)
94         output = attr.int("num_filter", None)
95         bias_term = attr.str("no_bias", 'True') == 'False'
96         target_shape = attr.tuple("target_shape", int, None)
97         if target_shape:
98             target_shape = np.array(target_shape, dtype=np.int64)
99
100         final_dilations = np.array([1, 1, *[d for d in dilate]], dtype=np.int64) if dilate is not None else None
101         node_attrs = {
102             'op': __class__.op,
103             'type': 'Deconvolution',
104             'bias_addable': True,
105             'bias_term': bias_term,
106             'pad': np.array([[0, 0], [0, 0], *[[pad, pad] for pad in padding]], dtype=np.int64),
107             'pad_spatial_shape': np.array([[pad, pad] for pad in padding], dtype=np.int64),
108             'dilation': final_dilations,
109             'output_spatial_shape': target_shape,
110             'output_shape': None,
111             'stride': np.array([1, 1, *[s for s in stride]], dtype=np.int64),
112             'group': group,
113             'output': output,
114             'kernel_spatial': np.array([k for k in kernel], dtype=np.int64),
115             'input_feature_channel': 1,
116             'output_feature_channel': 0,
117             'kernel_spatial_idx': None,
118             'reshape_kernel': True,
119
120             'spatial_dims': None,
121             'channel_dims': np.array([1], dtype=np.int64),
122             'batch_dims': np.array([0], dtype=np.int64),
123             'layout': 'NCHW',
124             'get_pad': DeconvFrontExtractor.get_pad,
125         }
126
127         # update the attributes of the node
128         Convolution.update_node_stat(node, node_attrs)
129         return __class__.enabled