module onnxrt.ops_cpu.op_resize#

Inheritance diagram of mlprodict.onnxrt.ops_cpu.op_resize

Short summary#

module mlprodict.onnxrt.ops_cpu.op_resize

Runtime operator.

source on GitHub

Classes#

class

truncated documentation

Resize

Resize ====== Resize the input tensor. In general, it calculates every value in the output tensor as a weighted average …

Functions#

function

truncated documentation

_cartesian

From https://stackoverflow.com/a/1235363 Generate a cartesian product of input arrays. Parameters ———- …

_cubic_coeffs

_get_all_coords

_get_neighbor

_get_neighbor_idxes

_interpolate_1d_with_x

_interpolate_nd

_interpolate_nd_with_x

_linear_coeffs

_nearest_coeffs

Properties#

property

truncated documentation

args_default

Returns the list of arguments as well as the list of parameters with the default values (close to the signature). …

args_default_modified

Returns the list of modified parameters.

args_mandatory

Returns the list of optional arguments.

args_optional

Returns the list of optional arguments.

atts_value

Returns all parameters in a dictionary.

Methods#

method

truncated documentation

__init__

_run

Documentation#

Runtime operator.

source on GitHub

class mlprodict.onnxrt.ops_cpu.op_resize.Resize(onnx_node, desc=None, expected_attributes=None, **options)#

Bases: OpRun


Resize the input tensor. In general, it calculates every value in the output tensor as a weighted average of neighborhood (a.k.a. sampling locations) in the input tensor. Each dimension value of the output tensor is: <br/>

output_dimension = floor(input_dimension * (roi_end - roi_start) * scale) <br/>

if input "sizes" is not specified.

Attributes

  • antialias: If set to 1, “linear” and “cubic” interpolation modes will use an antialiasing filter when downscaling. Antialiasing is achieved by stretching the resampling filter by a factor max(1, 1 / scale), which means that when downsampling, more input pixels contribute to an output pixel. Default value is nameantialiasi0typeINT (INT)

  • axes: If provided, it specifies a subset of axes that ‘roi’, ‘scales’ and ‘sizes’ refer to. If not provided, all axes are assumed [0, 1, …, r-1], where r = rank(data). Non-specified dimensions are interpreted as non-resizable. Negative value means counting dimensions from the back. Accepted range is [-r, r-1], where r = rank(data). Behavior is undefined if an axis is repeated. default value cannot be automatically retrieved (INTS)

  • coordinate_transformation_mode:

This attribute describes how to transform the coordinate in the resized tensor to the coordinate in the original tensor. <br/>

The coordinate of each dimension is transformed individually. Let’s describe a case using axis x as an example. Denote x_resized as the coordinate of axis x in the resized tensor, x_original as the coordinate of axis x in the original tensor, length_original as the length of the original tensor in axis x, length_resized as the length of the resized tensor in axis x, roi_x = (start_x, end_x) of the axis x in input “roi”, scale = length_resized / length_original, <br/>

if coordinate_transformation_mode is “half_pixel”, <br/> x_original = (x_resized + 0.5) / scale - 0.5 <br/>

if coordinate_transformation_mode is “pytorch_half_pixel”, <br/> x_original = length_resized > 1 ? (x_resized + 0.5) / scale - 0.5 : 0 <br/>

if coordinate_transformation_mode is “align_corners”, <br/> x_original = x_resized * (length_original - 1) / (length_resized - 1) <br/>

if coordinate_transformation_mode is “asymmetric”, <br/> x_original = x_resized / scale <br/>

if coordinate_transformation_mode is “tf_crop_and_resize”, <br/> x_original = length_resized > 1 ? start_x * (length_original - 1) + x_resized * (end_x - start_x) * (length_original - 1) / (length_resized - 1) : 0.5 * (start_x + end_x) * (length_original - 1) . Default value is namecoordinatetransformationmodeshalfpixeltypeSTRING (STRING) * cubic_coeff_a: The coefficient ‘a’ used in cubic interpolation. Two common choice are -0.5 (in some cases of TensorFlow) and -0.75 (in PyTorch). Check out Equation (4) in https://ieeexplore.ieee.org/document/1163711 for the details. This attribute is valid only if mode is “cubic”. Default value is namecubiccoeffaf-0.75typeFLOAT (FLOAT) * exclude_outside: If set to 1, the weight of sampling locations outside the tensor will be set to 0 and the weight will be renormalized so that their sum is 1.0. The default value is 0. Default value is nameexcludeoutsidei0typeINT (INT) * extrapolation_value: When coordinate_transformation_mode is “tf_crop_and_resize” and x_original is outside the range [0, length_original - 1], this value is used as the corresponding output value. Default is 0.0f. Default value is nameextrapolationvaluef0.0typeFLOAT (FLOAT) * keep_aspect_ratio_policy: This attribute describes how to interpret the sizes input with regard to keeping the original aspect ratio of the input, and it is not applicable when the scales input is used. <br/>

Given a set of sizes, associated with a subset of axes (explicitly provided or default), and assuming d = axes[i], with i being the index of the provided sizes. <br/>

If keep_aspect_ratio_policy is “stretch”, the original aspect ratio is disregarded, and the input is resized to the specified size: <br/> out_size[d] = sizes[i] <br/>

If keep_aspect_ratio_policy is “not_larger”, the sizes are adjusted so that no extent of the output is larger than the specified size, while keeping the original aspect ratio: <br/> scale = Min(sizes[i] / in_size[d]) <br/> out_size[d] = round_int(scale * in_size[i]) <br/>

If keep_aspect_ratio_policy is “not_smaller”, the sizes are adjusted so that no extent of the output is smaller than the specified size, while keeping the original aspect ratio: <br/> scale = Max(sizes[i] / in_size[d]) <br/> out_size[d] = round_int(scale * in_size[i]) <br/>

For non-resizable axes (those not specified in axes), the output size will be equal to the input size.

Note: round_int stands for computing the nearest integer value, rounding halfway cases up. Default value is namekeepaspectratiopolicysstretchtypeSTRING (STRING) * mode: Three interpolation modes: “nearest” (default), “linear” and “cubic”. The “linear” mode includes linear interpolation for 1D tensor and N-linear interpolation for N-D tensor (for example, bilinear interpolation for 2D tensor). The “cubic” mode includes cubic interpolation for 1D tensor and N-cubic interpolation for N-D tensor (for example, bicubic interpolation for 2D tensor). Default value is namemodesnearesttypeSTRING (STRING) * nearest_mode: Four modes: “round_prefer_floor” (default, as known as round half down), “round_prefer_ceil” (as known as round half up), “floor”, “ceil”. Only used by nearest interpolation. It indicates how to get “nearest” pixel in input tensor from x_original, so this attribute is valid only if “mode” is “nearest”. Default value is namenearestmodesroundpreferfloortypeSTRING (STRING)

Inputs

Between 1 and 4 inputs.

  • X (heterogeneous)T1: N-D tensor

  • roi (optional, heterogeneous)T2: 1-D tensor given as [start1, …, startN, end1, …, endN], where N is the rank of X or the length of axes, if provided. The RoIs’ coordinates are normalized in the coordinate system of the input image. It only takes effect when coordinate_transformation_mode is “tf_crop_and_resize”

  • scales (optional, heterogeneous)tensor(float): The scale array along each dimension. It takes value greater than 0. If it’s less than 1, it’s sampling down, otherwise, it’s upsampling. The number of elements of ‘scales’ should be the same as the rank of input ‘X’ or the length of ‘axes’, if provided. One of ‘scales’ and ‘sizes’ MUST be specified and it is an error if both are specified. If ‘sizes’ is needed, the user can use an empty string as the name of ‘scales’ in this operator’s input list.

  • sizes (optional, heterogeneous)tensor(int64): Target size of the output tensor. Its interpretation depends on the ‘keep_aspect_ratio_policy’ value.The number of elements of ‘sizes’ should be the same as the rank of input ‘X’, or the length of ‘axes’, if provided. Only one of ‘scales’ and ‘sizes’ can be specified.

Outputs

  • Y (heterogeneous)T1: N-D tensor after resizing

Type Constraints

  • T1 tensor(uint8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(int8), tensor(int16), tensor(int32), tensor(int64), tensor(bfloat16), tensor(float16), tensor(float), tensor(double), tensor(string), tensor(bool), tensor(complex64), tensor(complex128): Constrain input ‘X’ and output ‘Y’ to all tensor types.

  • T2 tensor(float16), tensor(float), tensor(double): Constrain roi type to float or double.

Version

Onnx name: Resize

This version of the operator has been available since version 18.

Runtime implementation: Resize

__init__(onnx_node, desc=None, expected_attributes=None, **options)#
_run(X, roi, scales=None, sizes=None, attributes=None, verbose=0, fLOG=None)#

Should be overwritten.

source on GitHub

mlprodict.onnxrt.ops_cpu.op_resize._cartesian(arrays, out=None)#

From https://stackoverflow.com/a/1235363 Generate a cartesian product of input arrays. Parameters ———- arrays : list of array-like

1-D arrays to form the cartesian product of.

outndarray

Array to place the cartesian product in.

Returns#

outndarray

2-D array of shape (M, len(arrays)) containing cartesian products formed of input arrays.

Examples#

>>> cartesian(([1, 2, 3], [4, 5], [6, 7]))
array([[1, 4, 6],
       [1, 4, 7],
       [1, 5, 6],
       [1, 5, 7],
       [2, 4, 6],
       [2, 4, 7],
       [2, 5, 6],
       [2, 5, 7],
       [3, 4, 6],
       [3, 4, 7],
       [3, 5, 6],
       [3, 5, 7]])

source on GitHub

mlprodict.onnxrt.ops_cpu.op_resize._cubic_coeffs(ratio, A=-0.75)#
mlprodict.onnxrt.ops_cpu.op_resize._get_all_coords(data)#
mlprodict.onnxrt.ops_cpu.op_resize._get_neighbor(x, n, data)#
mlprodict.onnxrt.ops_cpu.op_resize._get_neighbor_idxes(x, n, limit)#
mlprodict.onnxrt.ops_cpu.op_resize._interpolate_1d_with_x(data, scale_factor, x, get_coeffs, roi=None, extrapolation_value=0.0, coordinate_transformation_mode=b'half_pixel', exclude_outside=False)#
mlprodict.onnxrt.ops_cpu.op_resize._interpolate_nd(data, get_coeffs, output_size=None, scale_factors=None, roi=None, **kwargs)#
mlprodict.onnxrt.ops_cpu.op_resize._interpolate_nd_with_x(data, n, scale_factors, x, get_coeffs, roi=None, **kwargs)#
mlprodict.onnxrt.ops_cpu.op_resize._linear_coeffs(ratio)#
mlprodict.onnxrt.ops_cpu.op_resize._nearest_coeffs(ratio, mode=b'round_prefer_floor')#