Skip to content

Clarify interpolation algorithms for resample2d - #816

Merged
fdwr merged 9 commits into
webmachinelearning:mainfrom
inexorabletash:resample-algos
Feb 13, 2025
Merged

Clarify interpolation algorithms for resample2d#816
fdwr merged 9 commits into
webmachinelearning:mainfrom
inexorabletash:resample-algos

Conversation

@inexorabletash

@inexorabletash inexorabletash commented Feb 12, 2025

Copy link
Copy Markdown
Contributor

This gives formal definitions for the nearest-neighbor and linear interpolation modes. The definitions are based on text given by @fdwr and baseline implementation by @BruceDai and independently verified.

Resolves #358


Preview | Diff

@fdwr fdwr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice. Thanks for improving the spec. 🙏

Comment thread index.bs Outdated
Comment thread index.bs Outdated
Comment thread index.bs Outdated
Comment thread index.bs


<div class="note">
The specific sampling algorithms are based on those widely used in existing Machine Learning frameworks. For example, when performing {{MLInterpolationMode/linear}} resampling from the following *[4, 4]* input tensor (considering only spatial dimensions):

@fdwr fdwr Feb 13, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The specific sampling algorithms are based on those widely used in existing Machine Learning frameworks.

Note

Some ML libraries got this wrong historically and did things like stretch the centers of the input corner pixels to the centers of the output corner pixels (rather than the corner extents, including the whole pixel box rectangle rather than just a point sample), which graphics experts know is incorrect 😉 and gives you poor results. Imaging libraries like OpenCV though do the right thing, and thankfully newer versions of TF and PyTorch have fixed this behavior by default. e.g. #1 #2.

Wrong:
image
Right:
image

(no action - resolve me)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If we want to say more in the spec we can!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Updated comment with visualization - think it would help? I should probably recreate it from scratch to avoid directly reusing Jacob Richeimer's figure https://jricheimer.github.io/tensorflow/2019/02/11/resize-confusion/.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm thinking it will be hard to capture more of the history here without it turning into an essay equivalent to these linked resources. Maybe we should just link to these blog posts? @anssiko - any thoughts on non-normative links to potentially ephemeral resources?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

No need to stall on this aspect. Happy to merge if you say go.

Comment thread index.bs Outdated

@huningxin huningxin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM with nits, thanks much!

Comment thread index.bs Outdated
Comment thread index.bs Outdated
Comment thread index.bs Outdated
Comment thread index.bs Outdated
Comment thread index.bs
Comment thread index.bs Outdated
Comment thread index.bs Outdated

@fdwr fdwr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

👍

inexorabletash and others added 9 commits February 13, 2025 15:20
This gives formal definitions for the `nearest-neighbor` and `linear`
interpolation modes. The definitions are based on text given by @fdwr
and baseline implementation by @BruceDai and independently verified.

Resolves #358
Co-authored-by: Dwayne Robinson <dwayner@microsoft.com>
Co-authored-by: Dwayne Robinson <dwayner@microsoft.com>
Co-authored-by: Dwayne Robinson <dwayner@microsoft.com>
Co-authored-by: Ningxin Hu <ningxin.hu@intel.com>
Co-authored-by: Ningxin Hu <ningxin.hu@intel.com>
Co-authored-by: Ningxin Hu <ningxin.hu@intel.com>
@inexorabletash

Copy link
Copy Markdown
Contributor Author

If it still looks good @fdwr can you squash-merge ?

@fdwr
fdwr merged commit 4c34b9e into webmachinelearning:main Feb 13, 2025
github-actions Bot added a commit that referenced this pull request Feb 13, 2025
SHA: 4c34b9e
Reason: push, by fdwr

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@huningxin

Copy link
Copy Markdown
Contributor

👍

@vmukhachev

vmukhachev commented Jan 23, 2026

Copy link
Copy Markdown

it seems, it is not what Chrome on Windows does:

nearest-neighbor of [0, 1, 2, 3, 4, 5] to the shape 21 gives [0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5]
the spec says to return
[0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5]

and for nearest-neighbor of [0, 1, 2, 3, 4, 5] to the shape 3:
[1, 3, 5]
vs
[0, 2, 4]

Some testing of all inputsizes and outputsizes between 1 and 51 shows that Chrome does something like
x = floor(inputCoordinate.x + 0.5)
instead of
x = ceil(inputCoordinate.x - 0.5)
as specified
It floor(z+0.5) fails 22 of those tests althought

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Please clarify interpolation algorithm for resample2d

4 participants