Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transformers.image_transforms.normalize wrong types #35773

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/transformers/image_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import warnings
from math import ceil
from typing import Iterable, List, Optional, Tuple, Union
from typing import Iterable, List, Optional, Sequence, Tuple, Union

import numpy as np

Expand Down Expand Up @@ -357,8 +357,8 @@ def resize(

def normalize(
image: np.ndarray,
mean: Union[float, Iterable[float]],
std: Union[float, Iterable[float]],
mean: Union[float, Sequence[float]],
std: Union[float, Sequence[float]],
data_format: Optional[ChannelDimension] = None,
input_data_format: Optional[Union[str, ChannelDimension]] = None,
) -> np.ndarray:
Expand All @@ -370,9 +370,9 @@ def normalize(
Args:
image (`np.ndarray`):
The image to normalize.
mean (`float` or `Iterable[float]`):
mean (`float` or `Sequence[float]`):
The mean to use for normalization.
std (`float` or `Iterable[float]`):
std (`float` or `Sequence[float]`):
The standard deviation to use for normalization.
data_format (`ChannelDimension`, *optional*):
The channel dimension format of the output image. If unset, will use the inferred format from the input.
Expand All @@ -393,14 +393,14 @@ def normalize(
if not np.issubdtype(image.dtype, np.floating):
image = image.astype(np.float32)

if isinstance(mean, Iterable):
if isinstance(mean, Sequence):
if len(mean) != num_channels:
raise ValueError(f"mean must have {num_channels} elements if it is an iterable, got {len(mean)}")
else:
mean = [mean] * num_channels
mean = np.array(mean, dtype=image.dtype)

if isinstance(std, Iterable):
if isinstance(std, Sequence):
if len(std) != num_channels:
raise ValueError(f"std must have {num_channels} elements if it is an iterable, got {len(std)}")
else:
Expand Down