-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Add Resize-18: Antialiasing, axes and keep_aspect_ratio_policy #4126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
d119e54
to
e50bcf6
Compare
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
AttributeProto::STRING, | ||
std::string("round_prefer_floor")) | ||
.Attr( | ||
"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.", | ||
AttributeProto::FLOAT, | ||
static_cast<float>(0)) | ||
.Attr( | ||
"antialias", | ||
"If set to 1, \"linear\" and \"cubic\" interpolation modes will use an antialiasing filter when downscaling. " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This wording is likely to go stale when a new interpolation method is added. We could say that it is not applicable to "nearest" interpolation method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is true. On the other hand, there is a chance that a new interpolation method is added that doesn't support antialiasing.
Signed-off-by: Joaquin Anton <janton@nvidia.com>
def round_half_up(x: float) -> int: | ||
""" | ||
Computes the nearest integer value, rounding halfway cases up | ||
""" | ||
fractional = x - int(x) | ||
if fractional == 0.5: | ||
return int(x) + 1 | ||
else: | ||
return round(x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this function necessary? Unless you are going for very explicit and clear code, the same could be acheived by int(x + 0.5)
, maybe enclosed in a lambda.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I'll take your suggestion
<dt><tt>antialias</tt> : int (default is 0)</dt> | ||
<dd>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.</dd> | ||
<dt><tt>axes</tt> : list of ints</dt> | ||
<dd>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.</dd> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user could be notified about a possible undefined behaviour. Maybe throwing an error (like when both scales
and sizes
are given in Inputs) or giving a warning would be beneficial?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added some more error checking
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
…xel_for_nn Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Should we be more precise in the ONNX spec about what antialias filter to use for various interpolation methods? For example, by giving the precise mathematical formula in cases of possible ambiguity. |
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
@yuanyao-nv There is this definition:
which is basically extending the neighborhood region by the inverse of the scale factor, resampling the interpolation filter weights accordingly. Do you think we should phrase it differently? Regarding being mathematically explicit, the formulas are in |
Thanks for the reply @jantonguirao . |
It seems to be the same filter as those two frameworks. Regarding Tensorflow: Regarding Pytorch: I made a demo comparing this proposal against OpenCV (no antialiasing) and Pillow (antialiasing): Notebook here. As you can see, the formulas I am proposing match what Pillow is doing. |
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
bool hasSizesInput = (3 < ctx.getNumInputs()) && (ctx.getInputType(3) != nullptr); | ||
|
||
if (hasScalesInput + hasSizesInput != 1) { | ||
fail_shape_inference("Either `sizes` or `scales` must be provided, but not both of them"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI this line causes shape inference failures with some of ONNX Model Zoo models. Please check this issue: #4380. Thanks!
…4126) * [WIP] Add Resize-16: Antialiasing filter Signed-off-by: Joaquin Anton <janton@nvidia.com> * Update to version 17 Signed-off-by: Joaquin Anton <janton@nvidia.com> * Add 'keep_aspect_ratio_policy' and 'axes' Signed-off-by: Joaquin Anton <janton@nvidia.com> * Add shape inference implementation Signed-off-by: Joaquin Anton <janton@nvidia.com> * Fix version converter Signed-off-by: Joaquin Anton <janton@nvidia.com> * Workaround windows build issue Signed-off-by: Joaquin Anton <janton@nvidia.com> * Fixes Signed-off-by: Joaquin Anton <janton@nvidia.com> * Code review fixes Signed-off-by: Joaquin Anton <janton@nvidia.com> * Apply code review suggestions Signed-off-by: Joaquin Anton <janton@nvidia.com> * Move new Resize to opset 18 Signed-off-by: Joaquin Anton <janton@nvidia.com> * Correct error checking Signed-off-by: Joaquin Anton <janton@nvidia.com> * Adjust converter to opset 18 Signed-off-by: Joaquin Anton <janton@nvidia.com> * Update DomainToVersionRange Signed-off-by: Joaquin Anton <janton@nvidia.com> * Apply clang-format Signed-off-by: Joaquin Anton <janton@nvidia.com> * Fix shape inference test Signed-off-by: Joaquin Anton <janton@nvidia.com> * Fix flake8 issue Signed-off-by: Joaquin Anton <janton@nvidia.com> * Delete generated test test_resize_downsample_sizes_nearest_tf_half_pixel_for_nn Signed-off-by: Joaquin Anton <janton@nvidia.com> * Check for repeated axes Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton janton@nvidia.com
Description
This PR extends Resize with new functionality:
New boolean parameter
antialias
, which enables an antialiasing filter when downscaling with linear and cubic interpolation modes.New
axes
argument, allowing to select a subset of dimensions thatsizes
,roi
andscales
refer to.New
keep_aspect_ratio_policy
argument, determining how to interpret the value ofsizes
. Three policies supported:Motivation and Context