Skip to content

fix: preserve singleton dimensions in numpy_to_pil - #14984

Open
Subodh-17 wants to merge 1 commit into
huggingface:mainfrom
Subodh-17:fix/numpy-to-pil-singleton-dimensions
Open

Subodh-17 wants to merge 1 commit into
huggingface:mainfrom
Subodh-17:fix/numpy-to-pil-singleton-dimensions

Conversation

@Subodh-17

Copy link
Copy Markdown

What does this PR do?

Fixes #14901.

numpy_to_pil incorrectly uses an unrestricted squeeze() for grayscale images. When a valid spatial dimension has size 1, that dimension is removed together with the singleton channel dimension.

For example, an image with shape (1, 7, 1) is converted incorrectly because both singleton spatial/channel dimensions are squeezed.

This PR makes the squeeze operation explicit so that only the intended channel dimension is removed.

What changed?

  • Use squeeze(-1) for HWC grayscale image paths.
  • Use squeeze(0) for CHW grayscale image paths.
  • Add regression coverage for singleton spatial dimensions.

Affected production files:

  • src/diffusers/utils/pil_utils.py
  • src/diffusers/image_processor.py
  • examples/dreambooth/train_dreambooth_lora_flux2_img2img.py
  • examples/dreambooth/train_dreambooth_lora_flux2_klein_img2img.py

Regression test:

  • (1, 7, 1) -> (7, 1)
  • (7, 1, 1) -> (1, 7)
  • (1, 1, 1) -> (1, 1)

Tests

pytest tests/others/test_image_processor.py

Result:
11 passed, 2 warnings.

Also verified normal RGB inputs remain unaffected.

The commit is DCO signed.

Signed-off-by: Subodh Bansode <subodh171011@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

numpy_to_pil removes singleton spatial dimensions from grayscale images

1 participant