Reject an empty mask in get_crop_region instead of returning an inverted box - #14322
Open
ErenAta16 wants to merge 1 commit into
Open
Reject an empty mask in get_crop_region instead of returning an inverted box#14322ErenAta16 wants to merge 1 commit into
get_crop_region instead of returning an inverted box#14322ErenAta16 wants to merge 1 commit into
Conversation
…ted box
`get_crop_region` scans in from each edge to find the masked area. For an
all-zero mask nothing ever breaks those loops, so `crop_left` and `crop_right`
both reach `w` (and `crop_top`/`crop_bottom` reach `h`) and the box comes out
inverted:
normal mask -> (10, 20, 30, 40) valid=True size=20x20
empty mask (all zeros) -> (64, 64, 0, 0) valid=False size=-64x-64
empty mask, pad=8 -> (56, 56, 8, 8) valid=False size=-48x-48
The aspect-ratio expansion that follows happily operates on the inverted box,
and the result travels all the way to the caller:
PIL.Image.crop((64, 64, 0, 0))
ValueError: Coordinate 'right' is less than 'left'
which says nothing about the mask. Eight inpaint pipelines call this whenever
`padding_mask_crop` is set, so an empty mask is a plausible input (a threshold
that removed everything, or a programmatically built mask with no region
selected).
Check for it up front and say so. The empty case already failed; this just
fails at the point that knows why.
Single-pixel and fully-masked inputs are the cases either side of it and both
still return a usable box, so the new check is added with tests pinning all
three.
Contributor
|
Hi @ErenAta16, thanks for the PR! It does not appear to link an issue it fixes. If this PR addresses an existing issue, please add a closing keyword (e.g. |
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.
What does this PR do?
VaeImageProcessor.get_crop_regionscans in from each edge to find the masked area. For an all-zero mask nothing ever breaks those loops, socrop_leftandcrop_rightboth reachw(andcrop_top/crop_bottomreachh) and the returned box is inverted:The aspect-ratio expansion below operates on the inverted box without noticing, and the result reaches the caller:
Nothing in that message points back at the mask.
This is reachable from every inpaint pipeline that takes
padding_mask_crop:An empty mask is a plausible input there: a threshold that removed everything, or a programmatically built mask where no region ended up selected.
The change
Check
mask.any()before the scan and raise with a message that names the cause and the two ways out. The empty case already failed; this makes it fail where the reason is known, and it does not change any path that previously worked.Tests
Added to
tests/others/test_image_processor.py:test_get_crop_region_normal_mask— pins the ordinary result(10, 20, 30, 40)test_get_crop_region_rejects_empty_mask— withpad=0andpad=8, since padding shifts the inverted box but does not fix ittest_get_crop_region_single_pixel_and_full_mask_still_work— the two cases either side of empty, so the new check cannot be widened into rejecting sparse or full masksReverting only
image_processor.py:and with the change the whole file passes:
make quality(ruff check+ruff format --check) clean on both files.Who can review?
Anyone in the community is free to review the PR once the tests have passed.