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.
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
24 class ConvFrontExtractor(FrontExtractorOp):
30 attr = get_mxnet_layer_attrs(node.symbol_dict)
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'
40 final_dilations = np.array([1, 1, *[d for d in dilate]], dtype=np.int64) if dilate is not None else None
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,
51 'stride': np.array([1, 1, *[s for s in stride]], dtype=np.int64),
54 'kernel_spatial': np.array([k for k in kernel], dtype=np.int64),
56 'input_feature_channel': 1,
57 'output_feature_channel': 0,
58 'kernel_spatial_idx': None,
59 'reshape_kernel': True,
62 'channel_dims': np.array([1], dtype=np.int64),
63 'batch_dims': np.array([0], dtype=np.int64),
67 # update the attributes of the node
68 Convolution.update_node_stat(node, node_attrs)
69 return __class__.enabled
72 class DeconvFrontExtractor(FrontExtractorOp):
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)
87 attr = get_mxnet_layer_attrs(node.symbol_dict)
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)
98 target_shape = np.array(target_shape, dtype=np.int64)
100 final_dilations = np.array([1, 1, *[d for d in dilate]], dtype=np.int64) if dilate is not None else None
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),
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,
120 'spatial_dims': None,
121 'channel_dims': np.array([1], dtype=np.int64),
122 'batch_dims': np.array([0], dtype=np.int64),
124 'get_pad': DeconvFrontExtractor.get_pad,
127 # update the attributes of the node
128 Convolution.update_node_stat(node, node_attrs)
129 return __class__.enabled