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 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
25 class NearestNeighborUpsampling(FrontReplacementSubgraph):
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')),
46 ('shape', 'strided_slice'),
47 ('strided_slice', 'pack_1'),
48 ('strided_slice', 'pack_2'),
49 ('pack_1', 'reshape_1'),
50 ('pack_2', 'reshape_2'),
57 def replace_sub_graph(self, graph: Graph, match: dict):
58 log.debug('Matched NearestNeighborUpsampling pattern: {}'.format([node.id for node in match.values()]))
60 input_height = match['pack_1'].in_node(1).value.item()
61 input_width = match['pack_1'].in_node(3).value.item()
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.')
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']])
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])