Repository navigation
fix(export_utils): avoid rescaling integer uint8 arrays in encode_video - #15505
Open
2422725977 wants to merge 1 commit into
Open
2422725977 wants to merge 1 commit into
2422725977 wants to merge 1 commit into
Conversation
This branch has not been deployed
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?
Fixes a bug in
encode_video(src/diffusers/utils/export_utils.py) where the[0, 1]normalization check (0 <= video <= 1) was applied to everynp.ndarrayregardless of dtype:uint8(or other integer) arrays containing only{0, 1}(e.g., binary masks or very dark frames) matched0 <= video <= 1and were silently multiplied by255.uint8[0, 255]arrays triggered a spurious"Supplied numpy.ndarray does not have values in [0, 1]"warning whenever any pixel exceeded1.uint8integer arrays were passed through toav.VideoFrame.from_ndarray(..., format="rgb24")without casting touint8.Summary of changes
[0, 1]normalization check and* 255rescaling behindnp.issubdtype(video.dtype, np.floating).[0, 1], keep the warning and convert via.round().clip(0, 255).astype("uint8").uint8integer arrays, convert directly via.clip(0, 255).astype("uint8")without rescaling; keepuint8arrays untouched.TestEncodeVideo(4 unit tests) intests/others/test_utils.pycoveringuint8{0, 1}preservation, float[0, 1]scaling, out-of-range float warning + clamping/casting, and non-uint8integer casting.Verification
PYTHONPATH=src pytest -v tests/others/test_utils.py -k encode_video— 4 passedruff check --no-cache src/diffusers/utils/export_utils.py tests/others/test_utils.py— passedruff format --check --no-cache src/diffusers/utils/export_utils.py tests/others/test_utils.py— passedBefore submitting
self-reviewskill on the diff?Self-review notes
encode_videodocstring (src/diffusers/utils/export_utils.py:318-321) currently notes thatnp.ndarrayinputs are expected to be float arrays in[0, 1]; left unchanged to keep the diff focused, happy to expand it to documentuint8[0, 255]arrays as well if maintainers prefer.TestEncodeVideomocks_import_avbecauseav(PyAV) is an optional dependency not required bytests/others/test_utils.py, verifying the exactuint8rgb24frames handed toav.VideoFrame.from_ndarray.src/diffusers/utils/export_utils.py:341-356is executed directly on thenp.ndarraybranch ofencode_video).