Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / tf / deconv_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 import numpy as np
17
18 from mo.front.common.partial_infer.utils import convert_tf_padding_to_str, int64_array
19 from mo.front.extractor import FrontExtractorOp
20 from mo.front.tf.extractors.utils import tf_data_format_spatial, tf_data_format_channel, tf_data_format_batch, \
21     tf_int_list
22 from mo.ops.deconvolution import Deconvolution
23 from mo.ops.op import PermuteAttrs
24
25
26 class Conv2DBackpropInputFrontExtractor(FrontExtractorOp):
27     op = 'Conv2DBackpropInput'
28     enabled = True
29
30     @staticmethod
31     def extract(node):
32         attrs = tf_create_attrs(node, 3, 2)
33         attrs.update({'op': __class__.op,
34                       'get_weights_permute': PermuteAttrs.Permutation(perm=int64_array([3, 2, 0, 1]),
35                                                                       inv=int64_array([2, 3, 1, 0]))
36                       })
37
38         # update the attributes of the node
39         Deconvolution.update_node_stat(node, attrs)
40         return __class__.enabled
41
42
43 class Conv3DBackpropInputV2InputFrontExtractor(FrontExtractorOp):
44     op = 'Conv3DBackpropInputV2'
45     enabled = True
46
47     @staticmethod
48     def extract(node):
49         attrs = tf_create_attrs(node, 4, 3)
50         attrs.update({'op': __class__.op,
51                       'get_weights_permute': PermuteAttrs.Permutation(perm=int64_array([4, 3, 0, 1, 2]),
52                                                                       inv=int64_array([2, 3, 4, 1, 0]))
53                       })
54
55         # update the attributes of the node
56         Deconvolution.update_node_stat(node, attrs)
57         return __class__.enabled
58
59
60 def tf_create_attrs(node, input_feature_channel, output_feature_channel):
61     data_format = node.pb.attr["data_format"]
62
63     return {
64         'auto_pad': convert_tf_padding_to_str(node.pb.attr['padding']),
65         'bias_addable': True,
66         'bias_term': False,
67         'spatial_dims': tf_data_format_spatial(data_format),
68         'channel_dims': tf_data_format_channel(data_format),
69         'batch_dims': tf_data_format_batch(data_format),
70         'pad': None,  # will be inferred when input shape is known
71         'pad_spatial_shape': None,
72         'output_spatial_shape': None,
73         'output_shape': None,
74         'output': None,
75         'stride': tf_int_list(node.pb.attr["strides"].list),
76         'type': None,  # don't set type until we are sure it is really translated to correct IR; see infer function
77         'group': None,
78         'layout': data_format.s.decode(),
79         'input_feature_channel': input_feature_channel,
80         'output_feature_channel': output_feature_channel,
81     }