Publishing R3
[platform/upstream/dldt.git] / model-optimizer / extensions / front / tf / nearest_neighbor_upsampling.py
1 """
2  Copyright (c) 2018 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 logging as log
18 import networkx as nx
19
20 from extensions.ops.resample import ResampleOp
21 from extensions.front.tf.Pack import Pack
22 from mo.front.common.replacement import FrontReplacementSubgraph
23 from mo.graph.graph import replace_node
24
25
26 class NearestNeighborUpsampling(FrontReplacementSubgraph):
27     enabled = True
28
29     def run_before(self):
30         return [Pack]
31
32     def pattern(self):
33         return dict(
34             nodes=[('op', dict(kind='op')),
35                    ('shape', dict(kind='op', op='Shape')),
36                    ('strided_slice', dict(kind='op', op='StridedSlice')),
37                    ('pack_1', dict(kind='op', op='Pack')),
38                    ('reshape_1', dict(kind='op', op='Reshape')),
39                    ('mul_const', dict(kind='op', op='Const')),
40                    ('mul', dict(kind='op', op='Mul')),
41                    ('pack_2', dict(kind='op', op='Pack')),
42                    ('reshape_2', dict(kind='op', op='Reshape')),
43                    ],
44             edges=[
45                 ('op', 'shape'),
46                 ('op', 'reshape_1'),
47                 ('shape', 'strided_slice'),
48                 ('strided_slice', 'pack_1'),
49                 ('strided_slice', 'pack_2'),
50                 ('pack_1', 'reshape_1'),
51                 ('pack_2', 'reshape_2'),
52                 ('reshape_1', 'mul'),
53                 ('mul_const', 'mul'),
54                 ('mul', 'reshape_2'),
55             ],
56             node_attrs=['kind', 'op'],
57             edge_attrs=[])
58
59     def replace_sub_graph(self, graph: nx.MultiDiGraph, match: dict):
60         log.debug('Matched NearestNeighborUpsampling pattern: {}'.format([node.id for node in match.values()]))
61         try:
62             input_height = match['pack_1'].in_node(1).value.item()
63             input_width = match['pack_1'].in_node(3).value.item()
64
65             height_scale = match['mul_const'].shape[-4]
66             width_scale = match['mul_const'].shape[-2]
67         except Exception as ex:
68             log.warning('Failed to determine scaling parameters from the topology. Do not apply pattern.')
69             return
70
71         resample_op = ResampleOp(graph, {'width': input_width * width_scale, 'height': input_height * height_scale,
72                                          'name': 'Resample_', 'antialias': 0,
73                                          'resample_type': 'caffe.ResampleParameter.NEAREST'})
74         resample_node = resample_op.create_node([match['op']])
75
76         replace_node(match['reshape_2'], resample_node)
77         graph.remove_nodes_from([node.id for node in match.values() if node.id != match['op'].id])