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