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.ops.resample import ResampleOp
20 from mo.front.extractor import FrontExtractorOp
21 from mo.front.onnx.extractors.utils import onnx_attr
22 from mo.utils.error import Error
27 class UpsampleFrontExtractor(FrontExtractorOp):
33 mode = onnx_attr(node, 'mode', 's', default='nearest', dst_type=lambda x: x.decode())
34 scales = onnx_attr(node, 'scales', 'floats', dst_type=lambda x: np.array(x, dtype=np.float32))
35 width_scale = onnx_attr(node, 'width_scale', 'f')
36 height_scale = onnx_attr(node, 'height_scale', 'f')
38 supported_modes = ['nearest', 'linear']
39 if mode not in supported_modes:
41 'Error decoding Upsample node {}, mode = {} is not in the list of supported modes {}.',
47 # TODO: this is a temporary limitation
50 'Upsample mode {} for node {} is not supported. Only nearest is supported.',
55 # TODO: this is a temporary limitation
56 if scales is not None:
58 'Upsample scales attribute is defined for node {}. Only scale_width and scale_height are supported.',
62 if width_scale is None or height_scale is None:
64 'One/both of widths_scale = {} and height_scale = {} is not defined for Upsampe node {}.',
70 if width_scale != height_scale:
72 'Upsample node {} have different widths_scale = {} and height_scale = {}. It is not supported; they should match.',
78 mode_to_resample_type = {'nearest': 'caffe.ResampleParameter.NEAREST'}
79 assert mode in mode_to_resample_type
80 assert width_scale == height_scale
81 assert width_scale is not None
82 ResampleOp.update_node_stat(node, {'resample_type': mode_to_resample_type[mode], 'factor': width_scale, 'antialias': 0})
83 return __class__.enabled