[bug] Fix interpolation of positional embeddings #378
Merged
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.
Use size instead of scale factor to specify the output size of
nn.interpolate()
: this avoids any rounding issue leading to mismatching output size and consistently generate the same output size as with the previous kludge (from facebookresearch/dino#8).Test:
Before (using
scale_factor
without interpolate offset):Simulating the computation of the output size: each mismatching line reports
[N_image_pixels] N_image_patches / sqrt(N_total_model_patches) * sqrt(N_total_model_patches != interpolate_output_size
(i.e. 3 failing image sizes for 224/14 models, 6 failing images size for 518/14)
Forward (with DINOv2 ViT-S/14 w/ registers) on a 1x3xHxW tensor for H = W = one of the offending sizes:
After (using
size
without interpolate offset):Simulating the computation of the output size: no reported mismatch.
(i.e. no failing image size)
Also checked that the output size is always matching the one with the kludge
Forward (with DINOv2 ViT-S/14 w/ registers) on a 1x3xHxW tensor for H = W = one of the offending sizes for the before case: no error.
Forward (with DINOv2 ViT-S/14 w/ registers) on a 1x3xHxW tensor for H = W = some size: same result as with the kludge.