Fix Upsample align_corners=False for fractional scale factors#3908
Open
v-code01 wants to merge 1 commit into
Open
Fix Upsample align_corners=False for fractional scale factors#3908v-code01 wants to merge 1 commit into
v-code01 wants to merge 1 commit into
Conversation
_scaled_indices computed the align_corners=False start offset as ((M - 1) * step - N + 1) / 2, which equals the half-pixel offset (1 - step) / 2 only when M == scale * N exactly. For a fractional scale factor M = int(scale * N) is floored, so the sampling grid was re-centered to the corner-aligned (align_corners=True) grid: the output became bit-identical to align_corners=True and the flag had no effect (e.g. scale=1.5, mode=linear/cubic). Use the constant half-pixel offset (1 - step) / 2 so the sampling positions are (i + 0.5) / scale - 0.5 for every scale. Integer scale factors are unchanged (the two expressions already coincide there), so the existing tests still pass. Adds a regression test at a fractional scale that pins the half-pixel positions and checks align_corners False differs from True.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
_scaled_indicescomputes thealign_corners=Falsestart offset asThis equals the half-pixel offset
(1 - step) / 2only whenM == scale * Nexactly. For a fractional scale factorM = int(scale * N)is floored, so the grid is re-centered to the corner-aligned (align_corners=True) grid. The result:align_corners=Falseoutput becomes bit-identical toalign_corners=True, i.e. the flag is silently ignored.This affects
linearandcubicmodes (both route through_scaled_indices) at any fractional scale (1.5, 1.25, ...). All existing upsample tests use integer scale factors, where the two expressions coincide, so the regime was untested.Fix
Use the constant half-pixel offset
start = (1 - step) / 2, so the sampling positions are(i + 0.5) / scale - 0.5for every scale. Integer scale factors are unchanged.Test
Added
test_upsample_align_corners_false_fractional_scalepinning the half-pixel positions atscale=1.5and assertingalign_corners=Falsenow differs fromTrue. It fails on the old code and passes with the fix; the existing upsample tests stay green.