Skip to content

Commit

Permalink
Update kwargs validation for preprocess with decorator (#32024)
Browse files Browse the repository at this point in the history
* BLIP preprocess

* BIT preprocess

* BRIDGETOWER preprocess

* CHAMELEON preprocess

* CHINESE_CLIP preprocess

* CONVNEXT preprocess

* DEIT preprocess

* DONUT preprocess

* DPT preprocess

* FLAVA preprocess

* EFFICIENTNET preprocess

* FUYU preprocess

* GLPN preprocess

* IMAGEGPT preprocess

* INTRUCTBLIPVIDEO preprocess

* VIVIT preprocess

* ZOEDEPTH preprocess

* VITMATTE preprocess

* VIT preprocess

* VILT preprocess

* VIDEOMAE preprocess

* VIDEOLLAVA

* TVP processing

* TVP fixup

* SWIN2SR preprocess

* SIGLIP preprocess

* SAM preprocess

* RT-DETR preprocess

* PVT preprocess

* POOLFORMER preprocess

* PERCEIVER preprocess

* OWLVIT preprocess

* OWLV2 preprocess

* NOUGAT preprocess

* MOBILEVIT preprocess

* MOBILENETV2 preprocess

* MOBILENETV1 preprocess

* LEVIT preprocess

* LAYOUTLMV2 preprocess

* LAYOUTLMV3 preprocess

* Add test

* Update tests
  • Loading branch information
qubvel authored Aug 6, 2024
1 parent e85d863 commit fb66ef8
Show file tree
Hide file tree
Showing 76 changed files with 189 additions and 826 deletions.
24 changes: 2 additions & 22 deletions src/transformers/models/bit/image_processing_bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@
make_list_of_images,
to_numpy_array,
valid_images,
validate_kwargs,
validate_preprocess_arguments,
)
from ...utils import TensorType, is_vision_available, logging
from ...utils import TensorType, filter_out_non_signature_kwargs, is_vision_available, logging


logger = logging.get_logger(__name__)
Expand Down Expand Up @@ -122,23 +121,6 @@ def __init__(
self.image_mean = image_mean if image_mean is not None else OPENAI_CLIP_MEAN
self.image_std = image_std if image_std is not None else OPENAI_CLIP_STD
self.do_convert_rgb = do_convert_rgb
self._valid_processor_keys = [
"images",
"do_resize",
"size",
"resample",
"do_center_crop",
"crop_size",
"do_rescale",
"rescale_factor",
"do_normalize",
"image_mean",
"image_std",
"do_convert_rgb",
"return_tensors",
"data_format",
"input_data_format",
]

# Copied from transformers.models.clip.image_processing_clip.CLIPImageProcessor.resize
def resize(
Expand Down Expand Up @@ -190,6 +172,7 @@ def resize(
**kwargs,
)

@filter_out_non_signature_kwargs()
def preprocess(
self,
images: ImageInput,
Expand All @@ -207,7 +190,6 @@ def preprocess(
return_tensors: Optional[Union[str, TensorType]] = None,
data_format: Optional[ChannelDimension] = ChannelDimension.FIRST,
input_data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> PIL.Image.Image:
"""
Preprocess an image or batch of images.
Expand Down Expand Up @@ -274,8 +256,6 @@ def preprocess(
image_std = image_std if image_std is not None else self.image_std
do_convert_rgb = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

validate_kwargs(captured_kwargs=kwargs.keys(), valid_processor_keys=self._valid_processor_keys)

images = make_list_of_images(images)

if not valid_images(images):
Expand Down
22 changes: 2 additions & 20 deletions src/transformers/models/blip/image_processing_blip.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@
make_list_of_images,
to_numpy_array,
valid_images,
validate_kwargs,
validate_preprocess_arguments,
)
from ...utils import TensorType, is_vision_available, logging
from ...utils import TensorType, filter_out_non_signature_kwargs, is_vision_available, logging


if is_vision_available():
Expand Down Expand Up @@ -107,21 +106,6 @@ def __init__(
self.image_mean = image_mean if image_mean is not None else OPENAI_CLIP_MEAN
self.image_std = image_std if image_std is not None else OPENAI_CLIP_STD
self.do_convert_rgb = do_convert_rgb
self._valid_processor_keys = [
"images",
"do_resize",
"size",
"resample",
"do_rescale",
"rescale_factor",
"do_normalize",
"image_mean",
"image_std",
"do_convert_rgb",
"return_tensors",
"data_format",
"input_data_format",
]

# Copied from transformers.models.vit.image_processing_vit.ViTImageProcessor.resize with PILImageResampling.BILINEAR->PILImageResampling.BICUBIC
def resize(
Expand Down Expand Up @@ -172,6 +156,7 @@ def resize(
**kwargs,
)

@filter_out_non_signature_kwargs()
def preprocess(
self,
images: ImageInput,
Expand All @@ -187,7 +172,6 @@ def preprocess(
do_convert_rgb: bool = None,
data_format: ChannelDimension = ChannelDimension.FIRST,
input_data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> PIL.Image.Image:
"""
Preprocess an image or batch of images.
Expand Down Expand Up @@ -250,8 +234,6 @@ def preprocess(

images = make_list_of_images(images)

validate_kwargs(captured_kwargs=kwargs.keys(), valid_processor_keys=self._valid_processor_keys)

if not valid_images(images):
raise ValueError(
"Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@
is_scaled_image,
to_numpy_array,
valid_images,
validate_kwargs,
validate_preprocess_arguments,
)
from ...utils import TensorType, is_vision_available, logging
from ...utils import TensorType, filter_out_non_signature_kwargs, is_vision_available, logging


if is_vision_available():
Expand Down Expand Up @@ -205,24 +204,6 @@ def __init__(
self.do_pad = do_pad
self.do_center_crop = do_center_crop
self.crop_size = crop_size
self._valid_processor_keys = [
"images",
"do_resize",
"size",
"size_divisor",
"resample",
"do_rescale",
"rescale_factor",
"do_normalize",
"image_mean",
"image_std",
"do_pad",
"do_center_crop",
"crop_size",
"return_tensors",
"data_format",
"input_data_format",
]

# Copied from transformers.models.vilt.image_processing_vilt.ViltImageProcessor.resize
def resize(
Expand Down Expand Up @@ -389,6 +370,7 @@ def pad(

return BatchFeature(data=data, tensor_type=return_tensors)

@filter_out_non_signature_kwargs()
def preprocess(
self,
images: ImageInput,
Expand All @@ -407,7 +389,6 @@ def preprocess(
return_tensors: Optional[Union[str, TensorType]] = None,
data_format: ChannelDimension = ChannelDimension.FIRST,
input_data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> PIL.Image.Image:
"""
Preprocess an image or batch of images.
Expand Down Expand Up @@ -484,8 +465,6 @@ def preprocess(
size = size if size is not None else self.size
size = get_size_dict(size, default_to_square=False)

validate_kwargs(captured_kwargs=kwargs.keys(), valid_processor_keys=self._valid_processor_keys)

if not is_batched(images):
images = [images]

Expand Down
24 changes: 2 additions & 22 deletions src/transformers/models/chameleon/image_processing_chameleon.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@
is_valid_image,
to_numpy_array,
valid_images,
validate_kwargs,
validate_preprocess_arguments,
)
from ...utils import TensorType, is_vision_available, logging
from ...utils import TensorType, filter_out_non_signature_kwargs, is_vision_available, logging


logger = logging.get_logger(__name__)
Expand Down Expand Up @@ -141,23 +140,6 @@ def __init__(
self.image_mean = image_mean if image_mean is not None else [1.0, 1.0, 1.0]
self.image_std = image_std if image_std is not None else [1.0, 1.0, 1.0]
self.do_convert_rgb = do_convert_rgb
self._valid_processor_keys = [
"images",
"do_resize",
"size",
"resample",
"do_center_crop",
"crop_size",
"do_rescale",
"rescale_factor",
"do_normalize",
"image_mean",
"image_std",
"do_convert_rgb",
"return_tensors",
"data_format",
"input_data_format",
]

# Copied from transformers.models.clip.image_processing_clip.CLIPImageProcessor.resize
def resize(
Expand Down Expand Up @@ -209,6 +191,7 @@ def resize(
**kwargs,
)

@filter_out_non_signature_kwargs()
def preprocess(
self,
images: ImageInput,
Expand All @@ -226,7 +209,6 @@ def preprocess(
return_tensors: Optional[Union[str, TensorType]] = None,
data_format: Optional[ChannelDimension] = ChannelDimension.FIRST,
input_data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> PIL.Image.Image:
"""
Preprocess an image or batch of images.
Expand Down Expand Up @@ -293,8 +275,6 @@ def preprocess(
image_std = image_std if image_std is not None else self.image_std
do_convert_rgb = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

validate_kwargs(captured_kwargs=kwargs.keys(), valid_processor_keys=self._valid_processor_keys)

images = make_batched_images(images)

if not valid_images(images):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@
make_list_of_images,
to_numpy_array,
valid_images,
validate_kwargs,
validate_preprocess_arguments,
)
from ...utils import TensorType, is_vision_available, logging
from ...utils import TensorType, filter_out_non_signature_kwargs, is_vision_available, logging


logger = logging.get_logger(__name__)
Expand Down Expand Up @@ -122,23 +121,6 @@ def __init__(
self.image_mean = image_mean if image_mean is not None else OPENAI_CLIP_MEAN
self.image_std = image_std if image_std is not None else OPENAI_CLIP_STD
self.do_convert_rgb = do_convert_rgb
self._valid_processor_keys = [
"images",
"do_resize",
"size",
"resample",
"do_center_crop",
"crop_size",
"do_rescale",
"rescale_factor",
"do_normalize",
"image_mean",
"image_std",
"do_convert_rgb",
"return_tensors",
"data_format",
"input_data_format",
]

def resize(
self,
Expand Down Expand Up @@ -179,6 +161,7 @@ def resize(
**kwargs,
)

@filter_out_non_signature_kwargs()
def preprocess(
self,
images: ImageInput,
Expand All @@ -196,7 +179,6 @@ def preprocess(
return_tensors: Optional[Union[str, TensorType]] = None,
data_format: Optional[ChannelDimension] = ChannelDimension.FIRST,
input_data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> PIL.Image.Image:
"""
Preprocess an image or batch of images.
Expand Down Expand Up @@ -265,8 +247,6 @@ def preprocess(

images = make_list_of_images(images)

validate_kwargs(captured_kwargs=kwargs.keys(), valid_processor_keys=self._valid_processor_keys)

if not valid_images(images):
raise ValueError(
"Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
Expand Down
22 changes: 2 additions & 20 deletions src/transformers/models/convnext/image_processing_convnext.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@
make_list_of_images,
to_numpy_array,
valid_images,
validate_kwargs,
validate_preprocess_arguments,
)
from ...utils import TensorType, is_vision_available, logging
from ...utils import TensorType, filter_out_non_signature_kwargs, is_vision_available, logging


if is_vision_available():
Expand Down Expand Up @@ -114,21 +113,6 @@ def __init__(
self.do_normalize = do_normalize
self.image_mean = image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
self.image_std = image_std if image_std is not None else IMAGENET_STANDARD_STD
self._valid_processor_keys = [
"images",
"do_resize",
"size",
"crop_pct",
"resample",
"do_rescale",
"rescale_factor",
"do_normalize",
"image_mean",
"image_std",
"return_tensors",
"data_format",
"input_data_format",
]

def resize(
self,
Expand Down Expand Up @@ -199,6 +183,7 @@ def resize(
**kwargs,
)

@filter_out_non_signature_kwargs()
def preprocess(
self,
images: ImageInput,
Expand All @@ -214,7 +199,6 @@ def preprocess(
return_tensors: Optional[Union[str, TensorType]] = None,
data_format: ChannelDimension = ChannelDimension.FIRST,
input_data_format: Optional[Union[str, ChannelDimension]] = None,
**kwargs,
) -> PIL.Image.Image:
"""
Preprocess an image or batch of images.
Expand Down Expand Up @@ -276,8 +260,6 @@ def preprocess(
size = size if size is not None else self.size
size = get_size_dict(size, default_to_square=False)

validate_kwargs(captured_kwargs=kwargs.keys(), valid_processor_keys=self._valid_processor_keys)

images = make_list_of_images(images)

if not valid_images(images):
Expand Down
Loading

0 comments on commit fb66ef8

Please sign in to comment.