2 Copyright (c) 2018 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.
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
26 class NearestNeighborUpsampling(FrontReplacementSubgraph):
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')),
47 ('shape', 'strided_slice'),
48 ('strided_slice', 'pack_1'),
49 ('strided_slice', 'pack_2'),
50 ('pack_1', 'reshape_1'),
51 ('pack_2', 'reshape_2'),
56 node_attrs=['kind', 'op'],
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()]))
62 input_height = match['pack_1'].in_node(1).value.item()
63 input_width = match['pack_1'].in_node(3).value.item()
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.')
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']])
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])