VaeImageProcessor.denormalize() raises AttributeError for NumPy inputs
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- machine-learning
Research direction
Read src/diffusers/image_processor.py at VaeImageProcessor.denormalize() and check its documented input types. Reproduce the failure with the NumPy example in the issue, then inspect nearby image processor tests for coverage of NumPy and PyTorch inputs. Done means NumPy input returns clipped values in [0, 1] with its type preserved, while existing PyTorch behavior still works.
Written by the indexing model from the issue text.
Description
Describe the bug
VaeImageProcessor.denormalize() is documented to accept both NumPy arrays (np.ndarray) and PyTorch tensors (torch.Tensor). However, passing a NumPy array raises an AttributeError because the implementation uses the PyTorch-specific .clamp() method.
Root cause
In src/diffusers/image_processor.py, line 234:
return (images * 0.5 + 0.5).clamp(0, 1)
NumPy arrays do not implement .clamp(), despite being included in the function's type annotations and documentation.
Expected behavior
The function should denormalize NumPy inputs to [0, 1], preserving the input type and existing PyTorch behavior.
Proposed direction
Use input-type-appropriate clipping, such as np.clip() for NumPy and .clamp() for PyTorch, or clarify the supported types if NumPy support is not intended.
I'd be happy to contribute a fix after discussing the preferred approach with maintainers.
Reproduction
import numpy as np
import torch
from diffusers.image_processor import VaeImageProcessor
values = [-2.0, -1.0, 0.0, 1.0, 2.0]
numpy_input = np.array(values, dtype=np.float32)
torch_input = torch.tensor(values, dtype=torch.float32)
# PyTorch succeeds
print("PyTorch:", VaeImageProcessor.denormalize(torch_input))
# NumPy raises AttributeError
print("NumPy:", VaeImageProcessor.denormalize(numpy_input))
The issue is reproducible on CPU without pretrained models or external downloads.
Logs
File "E:\diffusers\src\diffusers\image_processor.py", line 234, in denormalize
return (images * 0.5 + 0.5).clamp(0, 1)
AttributeError: 'numpy.ndarray' object has no attribute 'clamp'. Did you mean: 'clip'?
Additional validation:
PyTorch actual: [0.0, 0.0, 0.5, 1.0, 1.0]
NumPy expected: [0.0, 0.0, 0.5, 1.0, 1.0]
Outputs match: True
System Info
- 🤗 Diffusers version: 0.41.0.dev0
- Platform: Windows-10-10.0.22631-SP0
- Running on Google Colab?: No
- Python version: 3.11.16
- PyTorch version (GPU?): 2.14.1+cpu (False)
- NumPy version: 2.4.6
- Huggingface_hub version: 1.33.0
- Transformers version: 5.18.0
- Accelerate version: 1.15.0
- PEFT version: 0.21.2
- Safetensors version: 0.8.0
Source revision: 899c9f3fd0e64f3206781c00c71c28249eff9ca5
Reproduction environment: Windows, CPU only. No pretrained models or external downloads required.
Who can help?
@DN6 @sayakpaul
I'd appreciate your feedback on the expected NumPy support and the preferred approach to resolving this issue. I'm happy to contribute a fix once the implementation approach is agreed upon.
- Dominant language
- Python
- Stars
- 34.8k
- Forks
- 7.4k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 58
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from huggingface/diffusers
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
huggingface/diffusers#15170 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
huggingface/diffusers#15163 · 3 comments ·
Maintainers usually reply within 1 day
-
Clarify set_adapters() adapter_weights documentation and type hintsPossibly taken @gorkadabo1 claimed this 4 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
huggingface/diffusers#15146 · 1 comment · 1 assignee ·
Maintainers usually reply within 1 day
-
`encode_video` truncates a fractional `fps`, so the video runs longer than its audioPossibly taken @guptaishaan claimed this 12 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
huggingface/diffusers#15104 · 1 comment ·
Maintainers usually reply within 1 day
-
`ModelMixin.from_pretrained` on a sharded checkpoint fails under `HF_HUB_OFFLINE=1` even when fully cached (`_get_checkpoint_shard_files` still calls `model_info`)Possibly taken @Subodh-17 claimed this 28 days ago. Openbug models
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
huggingface/diffusers#14952 · 1 comment ·
Maintainers usually reply within 1 day
All issues in huggingface/diffusers
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
unionai-oss/pandera#2640 ·
Maintainers usually reply within 2 days
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
JdeRobot/RoboticsInfrastructure#843 ·
Maintainers usually reply within 1 day
-
automated endpoint-dead needs-curation
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
almanac-data/climate-almanac#122 ·
Maintainers usually reply within 1 day
-
area:harness enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
evarness-ai/iris-harness#420 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
suitenumerique/find#206 ·
Maintainers usually reply within 2 days