Skip to content

Conversation

jantonguirao
Copy link
Contributor

@jantonguirao jantonguirao commented May 5, 2022

Signed-off-by: Joaquin Anton janton@nvidia.com

Description

  • Adds a new function: CenterCropPad-18.
  • CenterCropPad is a typical operation in image processing application.
  • It has been implemented as a function in terms of Pad and Slice
  • Pad-18 to have axes attribute

Motivation and Context

  • Why is this change required? What problem does it solve?
    Enables writing image preprocessing pipelines (such as ResNet-50)

Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
@jantonguirao jantonguirao marked this pull request as ready for review May 9, 2022 08:01
@jantonguirao jantonguirao requested review from a team as code owners May 9, 2022 08:01
@jantonguirao jantonguirao changed the title Introduce ai.onnx.image.CenterCropPad Introduce CenterCropPad May 13, 2022
Signed-off-by: Joaquin Anton <janton@nvidia.com>
@jantonguirao jantonguirao marked this pull request as draft May 13, 2022 12:55
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
@jantonguirao jantonguirao changed the title Introduce CenterCropPad Introduce CenterCropPad, add axes to Pad and Shape May 17, 2022
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
@gramalingam gramalingam added the topic: operator Issues related to ONNX operators label May 25, 2022
.SetContextDependentFunctionBodyBuilder(
[](const FunctionBodyBuildContext& ctx, const OpSchema& schema, FunctionProto& functionProto) {
const auto& input_shape = ctx.getInputType(0)->tensor_type().shape();
const int64_t input_rank = input_shape.dim_size();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is going to be a problem. The shape we can get is only a static-shape, not a dynamic-shape. Statically, we may not know the rank of the input.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, one alternative is to get the (full) shape, convert the axes attribute into a tensor, and use it to index into the full-shape using some form of gather op.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with a separate code path when axes are not provided. Also, I took your suggestion and used Gather instead of extending Shape.

Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
@jantonguirao jantonguirao marked this pull request as ready for review June 14, 2022 07:29
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
@jantonguirao jantonguirao changed the title Introduce CenterCropPad, add axes to Pad and Shape Introduce CenterCropPad, add axes to Pad Jun 14, 2022
Signed-off-by: Joaquin Anton <janton@nvidia.com>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
@gramalingam
Copy link
Contributor

LGTM, thanks. Have the test-cases been validated with ORT? Unfortunately, there is no other way to validate function definitions.

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>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
@jantonguirao
Copy link
Contributor Author

LGTM, thanks. Have the test-cases been validated with ORT? Unfortunately, there is no other way to validate function definitions.

Yes. I have validated the test cases that don't depend on Pad-18 having support for axes

Copy link
Contributor

@p-wysocki p-wysocki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left one comment, LGTM otherwise.

3,
"axes",
"1-D tensor of axes that `pads` apply to. Negative value means counting dimensions "
"from the back. Accepted range is [-r, r-1] where r = rank(data). Behavior is undefined if an "
Copy link
Contributor

@p-wysocki p-wysocki Jun 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a warning/exception when an axis is repeated, like in case of Resize-18?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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>
Signed-off-by: Joaquin Anton <janton@nvidia.com>
@gramalingam gramalingam merged commit 1400c98 into onnx:main Jul 12, 2022
broune pushed a commit to broune/onnx that referenced this pull request May 6, 2023
* Introduce ai.onnx.image.CenterCropPad

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Remove ai.onnx.image domain

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Add Pad axes

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Add axes to Shape, update CenterCropPad to work with arbitrary axes

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Fix shape inference file

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Undo Shape-17

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Update doc

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Linter fix

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Create Const directly from attribute

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Move to opset 18

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Apply clang-format

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Update opset in shape_inference_tests

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Fix shape inference tests

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Add automatic upgrade test

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Update CenterCropPad tests

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Refactor tests

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Update tests

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Check for repeated axes

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Fix flake8 issues

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Fix clang-format issue

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Fix CenterCropPad tests with axes

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Update doc

Signed-off-by: Joaquin Anton <janton@nvidia.com>
@fdwr
Copy link
Contributor

fdwr commented Sep 20, 2024

  • What is the behavior for odd input sizes? Floor? Ceil?
  • When padded, what is the fill value? Zero? Edge repetition?

The documentation currently lacks these salient details - https://onnx.ai/onnx/operators/onnx__CenterCropPad.html#l-onnx-doc-centercroppad. Is the following correct?

If the input dimensions are bigger than the crop shape, a centered cropping window is extracted from the input. If the input dimensions are smaller than the crop shape, the input is padded on each side equally <with zero values>, so that the input is centered in the output. <If the input dimensions are odd relative to the cropping window, the offset is floored>.

@jantonguirao
Copy link
Contributor Author

jantonguirao commented Oct 16, 2024

  • What is the behavior for odd input sizes? Floor? Ceil?
  • When padded, what is the fill value? Zero? Edge repetition?

The documentation currently lacks these salient details - https://onnx.ai/onnx/operators/onnx__CenterCropPad.html#l-onnx-doc-centercroppad. Is the following correct?

If the input dimensions are bigger than the crop shape, a centered cropping window is extracted from the input. If the input dimensions are smaller than the crop shape, the input is padded on each side equally <with zero values>, so that the input is centered in the output. <If the input dimensions are odd relative to the cropping window, the offset is floored>.

You are right, the documentation is ambiguous in those cases.
I've created a PR to enhance the documentation: #6464

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: operator Issues related to ONNX operators
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants