FIX reconstruct_from_patches_2d when patch size equals image size#33643
Merged
jeremiedbb merged 3 commits intoscikit-learn:mainfrom Apr 9, 2026
Merged
Conversation
added 2 commits
March 26, 2026 21:22
The overlap counting formula used p_h and p_w (patch dimensions) as upper bounds, but when a patch dimension equals the image dimension there is only one patch along that axis (n_h=1 or n_w=1). The correct upper bound must also include n_h and n_w (the number of patches along each axis), otherwise pixels are divided by inflated overlap counts. Add n_h and n_w to the min() call in the overlap computation. Fixes scikit-learn#10910
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.
Summary
Fixes #10910
reconstruct_from_patches_2dproduces incorrect results when one patchdimension equals the corresponding image dimension (e.g. patches of size
128x128 from a 128x256 image). The reconstruction error is ~1.0 instead of
~0.0.
Root cause
The overlap counting formula on line 524:
uses
p_handp_w(patch dimensions) as upper bounds. But whenp_h == i_h, there is onlyn_h = 1patch along that axis, so every pixelhas an overlap count of 1. The formula incorrectly computes values up to
p_h(128), dividing pixel values by 128 instead of 1.Fix
Add
n_handn_w(number of patches per axis) to themin()call:This correctly constrains the overlap count when the number of patches is
smaller than the patch size. For the normal case (
p_h << i_h),n_hislarge and doesn't affect the result.
Changes
sklearn/feature_extraction/image.py: Addedn_handn_wto theoverlap count formula.
sklearn/feature_extraction/tests/test_image.py: Added parametrizednon-regression test covering
p_h == i_h,p_w == i_w,p == img, andthe RGB variant.