From 1abefc9cd4fef604ec60eb5211ba971cca2ab35f Mon Sep 17 00:00:00 2001 From: Lidang-Jiang Date: Fri, 27 Mar 2026 16:17:09 +0800 Subject: [PATCH 01/30] [Bugfix] Remove incorrect torchvision requirement from PIL backend image processors PR #45029 added @requires(backends=("vision", "torch", "torchvision")) to 67 PIL backend image_processing_pil_*.py files. This causes PIL backend classes to become dummy objects when torchvision is not installed, making AutoImageProcessor unable to find any working processor. Fix: set @requires to ("vision",) for files that only need PIL, and ("vision", "torch") for files that also use torch directly. Also fix 5 modular source files so make fix-repo preserves the correct backends. Fixes #45042 Co-Authored-By: Claude Opus 4.6 --- src/transformers/models/aria/image_processing_pil_aria.py | 2 +- src/transformers/models/beit/image_processing_pil_beit.py | 2 +- .../models/bridgetower/image_processing_pil_bridgetower.py | 2 +- .../conditional_detr/image_processing_pil_conditional_detr.py | 2 +- .../models/convnext/image_processing_pil_convnext.py | 2 +- .../models/deepseek_vl/image_processing_pil_deepseek_vl.py | 2 +- .../image_processing_pil_deepseek_vl_hybrid.py | 2 +- .../deformable_detr/image_processing_pil_deformable_detr.py | 2 +- src/transformers/models/detr/image_processing_pil_detr.py | 2 +- src/transformers/models/donut/image_processing_pil_donut.py | 2 +- src/transformers/models/dpt/image_processing_pil_dpt.py | 2 +- .../efficientloftr/image_processing_pil_efficientloftr.py | 2 +- .../models/efficientloftr/modular_efficientloftr.py | 2 +- .../models/efficientnet/image_processing_pil_efficientnet.py | 2 +- src/transformers/models/eomt/image_processing_pil_eomt.py | 2 +- .../ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py | 2 +- src/transformers/models/flava/image_processing_pil_flava.py | 2 +- src/transformers/models/fuyu/image_processing_pil_fuyu.py | 2 +- src/transformers/models/gemma3/image_processing_pil_gemma3.py | 2 +- src/transformers/models/glm46v/image_processing_pil_glm46v.py | 2 +- src/transformers/models/glm4v/image_processing_pil_glm4v.py | 2 +- .../models/glm_image/image_processing_pil_glm_image.py | 2 +- src/transformers/models/glpn/image_processing_pil_glpn.py | 2 +- .../models/got_ocr2/image_processing_pil_got_ocr2.py | 2 +- .../grounding_dino/image_processing_pil_grounding_dino.py | 2 +- src/transformers/models/idefics/image_processing_pil_idefics.py | 2 +- .../models/idefics2/image_processing_pil_idefics2.py | 2 +- .../models/idefics3/image_processing_pil_idefics3.py | 2 +- .../models/imagegpt/image_processing_pil_imagegpt.py | 2 +- src/transformers/models/janus/image_processing_pil_janus.py | 2 +- .../models/kosmos2_5/image_processing_pil_kosmos2_5.py | 2 +- .../models/layoutlmv2/image_processing_pil_layoutlmv2.py | 2 +- .../models/layoutlmv3/image_processing_pil_layoutlmv3.py | 2 +- .../models/lightglue/image_processing_pil_lightglue.py | 2 +- src/transformers/models/lightglue/modular_lightglue.py | 2 +- .../models/llava_next/image_processing_pil_llava_next.py | 2 +- .../llava_onevision/image_processing_pil_llava_onevision.py | 2 +- .../models/mask2former/image_processing_pil_mask2former.py | 2 +- src/transformers/models/mask2former/modular_mask2former.py | 2 +- .../models/maskformer/image_processing_pil_maskformer.py | 2 +- src/transformers/models/mllama/image_processing_pil_mllama.py | 2 +- .../models/mobilenet_v2/image_processing_pil_mobilenet_v2.py | 2 +- .../models/mobilevit/image_processing_pil_mobilevit.py | 2 +- src/transformers/models/nougat/image_processing_pil_nougat.py | 2 +- .../models/oneformer/image_processing_pil_oneformer.py | 2 +- src/transformers/models/ovis2/image_processing_pil_ovis2.py | 2 +- src/transformers/models/owlv2/image_processing_pil_owlv2.py | 2 +- src/transformers/models/owlv2/modular_owlv2.py | 2 +- src/transformers/models/owlvit/image_processing_pil_owlvit.py | 2 +- .../models/paddleocr_vl/image_processing_pil_paddleocr_vl.py | 2 +- .../models/pix2struct/image_processing_pil_pix2struct.py | 2 +- src/transformers/models/pixtral/image_processing_pil_pixtral.py | 2 +- .../models/poolformer/image_processing_pil_poolformer.py | 2 +- .../image_processing_pil_prompt_depth_anything.py | 2 +- .../models/qwen2_vl/image_processing_pil_qwen2_vl.py | 2 +- src/transformers/models/rt_detr/image_processing_pil_rt_detr.py | 2 +- src/transformers/models/rt_detr/modular_rt_detr.py | 2 +- src/transformers/models/sam/image_processing_pil_sam.py | 2 +- src/transformers/models/seggpt/image_processing_pil_seggpt.py | 2 +- src/transformers/models/siglip2/image_processing_pil_siglip2.py | 2 +- src/transformers/models/smolvlm/image_processing_pil_smolvlm.py | 2 +- .../models/superglue/image_processing_pil_superglue.py | 2 +- .../models/superpoint/image_processing_pil_superpoint.py | 2 +- src/transformers/models/swin2sr/image_processing_pil_swin2sr.py | 2 +- src/transformers/models/textnet/image_processing_pil_textnet.py | 2 +- src/transformers/models/tvp/image_processing_pil_tvp.py | 2 +- .../models/video_llama_3/image_processing_pil_video_llama_3.py | 2 +- src/transformers/models/vilt/image_processing_pil_vilt.py | 2 +- .../models/vitmatte/image_processing_pil_vitmatte.py | 2 +- src/transformers/models/yolos/image_processing_pil_yolos.py | 2 +- .../models/zoedepth/image_processing_pil_zoedepth.py | 2 +- 71 files changed, 71 insertions(+), 71 deletions(-) diff --git a/src/transformers/models/aria/image_processing_pil_aria.py b/src/transformers/models/aria/image_processing_pil_aria.py index f2209b8dcfd6..b4a31e30f802 100644 --- a/src/transformers/models/aria/image_processing_pil_aria.py +++ b/src/transformers/models/aria/image_processing_pil_aria.py @@ -30,7 +30,7 @@ from .image_processing_aria import AriaImageProcessorKwargs -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class AriaImageProcessorPil(PilBackend): model_input_names = ["pixel_values", "pixel_mask", "num_crops"] diff --git a/src/transformers/models/beit/image_processing_pil_beit.py b/src/transformers/models/beit/image_processing_pil_beit.py index 976e3dfdee33..0b0405d64fcd 100644 --- a/src/transformers/models/beit/image_processing_pil_beit.py +++ b/src/transformers/models/beit/image_processing_pil_beit.py @@ -35,7 +35,7 @@ @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class BeitImageProcessorPil(PilBackend): """PIL backend for BEiT with reduce_label support.""" diff --git a/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py b/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py index c9f359f9e8b8..1ee53b0d8e0e 100644 --- a/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py +++ b/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py @@ -29,7 +29,7 @@ from .image_processing_bridgetower import BridgeTowerImageProcessorKwargs, get_resize_output_image_size -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class BridgeTowerImageProcessorPil(PilBackend): """PIL backend for BridgeTower with custom resize and center_crop.""" diff --git a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py index 423b5885f301..a8602e1fea7d 100644 --- a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py +++ b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py @@ -263,7 +263,7 @@ def prepare_coco_panoptic_annotation( return new_target -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class ConditionalDetrImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/convnext/image_processing_pil_convnext.py b/src/transformers/models/convnext/image_processing_pil_convnext.py index 4dc2b9428ac5..6edd51c8484b 100644 --- a/src/transformers/models/convnext/image_processing_pil_convnext.py +++ b/src/transformers/models/convnext/image_processing_pil_convnext.py @@ -31,7 +31,7 @@ from .image_processing_convnext import ConvNextImageProcessorKwargs -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class ConvNextImageProcessorPil(PilBackend): """PIL backend for ConvNeXT with custom resize.""" diff --git a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py index dd8750cc0690..e14256c3de5e 100644 --- a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py +++ b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py @@ -38,7 +38,7 @@ from .image_processing_deepseek_vl import DeepseekVLImageProcessorKwargs -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class DeepseekVLImageProcessorPil(PilBackend): resample = PILImageResampling.BICUBIC diff --git a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py index 9365a1a82a02..1c0e5bbfbd06 100644 --- a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py +++ b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py @@ -43,7 +43,7 @@ import torch -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class DeepseekVLHybridImageProcessorPil(PilBackend): resample = PILImageResampling.BICUBIC diff --git a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py index 87fe051d8a37..3532bbe96bd6 100644 --- a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py +++ b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py @@ -246,7 +246,7 @@ def prepare_coco_panoptic_annotation( return new_target -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class DeformableDetrImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/detr/image_processing_pil_detr.py b/src/transformers/models/detr/image_processing_pil_detr.py index d45a34a6cee3..fe406e888c2b 100644 --- a/src/transformers/models/detr/image_processing_pil_detr.py +++ b/src/transformers/models/detr/image_processing_pil_detr.py @@ -254,7 +254,7 @@ def prepare_coco_panoptic_annotation( return new_target -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class DetrImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/donut/image_processing_pil_donut.py b/src/transformers/models/donut/image_processing_pil_donut.py index 15cdf5ac0d46..7c5c5cc9d7f7 100644 --- a/src/transformers/models/donut/image_processing_pil_donut.py +++ b/src/transformers/models/donut/image_processing_pil_donut.py @@ -31,7 +31,7 @@ from .image_processing_donut import DonutImageProcessorKwargs -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class DonutImageProcessorPil(PilBackend): """PIL backend for Donut with align_long_axis, thumbnail, and pad_image.""" diff --git a/src/transformers/models/dpt/image_processing_pil_dpt.py b/src/transformers/models/dpt/image_processing_pil_dpt.py index d2d1afe92996..80cb618370cd 100644 --- a/src/transformers/models/dpt/image_processing_pil_dpt.py +++ b/src/transformers/models/dpt/image_processing_pil_dpt.py @@ -46,7 +46,7 @@ @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class DPTImageProcessorPil(PilBackend): """PIL backend for DPT with custom resize and pad.""" diff --git a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py index b7c6da2e648c..fe266a8c0a10 100644 --- a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py @@ -61,7 +61,7 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: return image -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class EfficientLoFTRImageProcessorPil(PilBackend): valid_kwargs = EfficientLoFTRImageProcessorKwargs resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/efficientloftr/modular_efficientloftr.py b/src/transformers/models/efficientloftr/modular_efficientloftr.py index 600620806d64..b19028687f69 100644 --- a/src/transformers/models/efficientloftr/modular_efficientloftr.py +++ b/src/transformers/models/efficientloftr/modular_efficientloftr.py @@ -68,7 +68,7 @@ def post_process_keypoint_matching( return results -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class EfficientLoFTRImageProcessorPil(SuperGlueImageProcessorPil): def post_process_keypoint_matching( self, diff --git a/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py b/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py index 3b23a8ef6e3d..d5f76e1c1d83 100644 --- a/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py +++ b/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py @@ -29,7 +29,7 @@ from .image_processing_efficientnet import EfficientNetImageProcessorKwargs -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class EfficientNetImageProcessorPil(PilBackend): """PIL backend for EfficientNet with rescale offset and include_top.""" diff --git a/src/transformers/models/eomt/image_processing_pil_eomt.py b/src/transformers/models/eomt/image_processing_pil_eomt.py index e4bb98690527..8a8862cef95b 100644 --- a/src/transformers/models/eomt/image_processing_pil_eomt.py +++ b/src/transformers/models/eomt/image_processing_pil_eomt.py @@ -82,7 +82,7 @@ def convert_segmentation_map_to_binary_masks( @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class EomtImageProcessorPil(PilBackend): valid_kwargs = EomtImageProcessorKwargs resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py index e408d5e1be26..11574f7951d2 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py @@ -38,7 +38,7 @@ logger = logging.get_logger(__name__) -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class Ernie4_5_VLMoeImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/flava/image_processing_pil_flava.py b/src/transformers/models/flava/image_processing_pil_flava.py index 1f55fa338e7e..17b9ce38fd9f 100644 --- a/src/transformers/models/flava/image_processing_pil_flava.py +++ b/src/transformers/models/flava/image_processing_pil_flava.py @@ -44,7 +44,7 @@ ) -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class FlavaImageProcessorPil(PilBackend): valid_kwargs = FlavaImageProcessorKwargs diff --git a/src/transformers/models/fuyu/image_processing_pil_fuyu.py b/src/transformers/models/fuyu/image_processing_pil_fuyu.py index 4992f4073dfb..58ac2804aeb0 100644 --- a/src/transformers/models/fuyu/image_processing_pil_fuyu.py +++ b/src/transformers/models/fuyu/image_processing_pil_fuyu.py @@ -29,7 +29,7 @@ @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class FuyuImageProcessorPil(PilBackend): do_resize = True size = {"height": 1080, "width": 1920} diff --git a/src/transformers/models/gemma3/image_processing_pil_gemma3.py b/src/transformers/models/gemma3/image_processing_pil_gemma3.py index 54414cf12736..46f622a6b279 100644 --- a/src/transformers/models/gemma3/image_processing_pil_gemma3.py +++ b/src/transformers/models/gemma3/image_processing_pil_gemma3.py @@ -37,7 +37,7 @@ from .image_processing_gemma3 import Gemma3ImageProcessorKwargs -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class Gemma3ImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/glm46v/image_processing_pil_glm46v.py b/src/transformers/models/glm46v/image_processing_pil_glm46v.py index 08ffe8abbfd2..609d1ae2e3cf 100644 --- a/src/transformers/models/glm46v/image_processing_pil_glm46v.py +++ b/src/transformers/models/glm46v/image_processing_pil_glm46v.py @@ -30,7 +30,7 @@ from .image_processing_glm46v import Glm46VImageProcessorKwargs, smart_resize -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class Glm46VImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/glm4v/image_processing_pil_glm4v.py b/src/transformers/models/glm4v/image_processing_pil_glm4v.py index 327a64c43746..6e85edd39330 100644 --- a/src/transformers/models/glm4v/image_processing_pil_glm4v.py +++ b/src/transformers/models/glm4v/image_processing_pil_glm4v.py @@ -48,7 +48,7 @@ class Glm4vImageProcessorKwargs(ImagesKwargs, total=False): merge_size: int -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class Glm4vImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/glm_image/image_processing_pil_glm_image.py b/src/transformers/models/glm_image/image_processing_pil_glm_image.py index 68caf452a7a2..263c47da70af 100644 --- a/src/transformers/models/glm_image/image_processing_pil_glm_image.py +++ b/src/transformers/models/glm_image/image_processing_pil_glm_image.py @@ -31,7 +31,7 @@ from .image_processing_glm_image import GlmImageImageProcessorKwargs, smart_resize -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class GlmImageImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/glpn/image_processing_pil_glpn.py b/src/transformers/models/glpn/image_processing_pil_glpn.py index a98af6587ac9..84468a88ab4d 100644 --- a/src/transformers/models/glpn/image_processing_pil_glpn.py +++ b/src/transformers/models/glpn/image_processing_pil_glpn.py @@ -35,7 +35,7 @@ @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class GLPNImageProcessorPil(PilBackend): """PIL backend for GLPN with size_divisor resize.""" diff --git a/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py b/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py index 2ffb5e199feb..97abffe7dd6d 100644 --- a/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py +++ b/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py @@ -36,7 +36,7 @@ from .image_processing_got_ocr2 import GotOcr2ImageProcessorKwargs, get_optimal_tiled_canvas -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class GotOcr2ImageProcessorPil(PilBackend): valid_kwargs = GotOcr2ImageProcessorKwargs diff --git a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py index 86133985b67b..e1bc88b60e71 100644 --- a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py +++ b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py @@ -290,7 +290,7 @@ def _scale_boxes(boxes, target_sizes): return boxes -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class GroundingDinoImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/idefics/image_processing_pil_idefics.py b/src/transformers/models/idefics/image_processing_pil_idefics.py index 9d4cf14f85c0..c3c991152922 100644 --- a/src/transformers/models/idefics/image_processing_pil_idefics.py +++ b/src/transformers/models/idefics/image_processing_pil_idefics.py @@ -21,7 +21,7 @@ from .image_processing_idefics import IDEFICS_STANDARD_MEAN, IDEFICS_STANDARD_STD, IdeficsImageProcessorKwargs -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class IdeficsImageProcessorPil(PilBackend): valid_kwargs = IdeficsImageProcessorKwargs diff --git a/src/transformers/models/idefics2/image_processing_pil_idefics2.py b/src/transformers/models/idefics2/image_processing_pil_idefics2.py index c04bd45cebce..f3213b1ed6b7 100644 --- a/src/transformers/models/idefics2/image_processing_pil_idefics2.py +++ b/src/transformers/models/idefics2/image_processing_pil_idefics2.py @@ -45,7 +45,7 @@ def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndar return mask -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class Idefics2ImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/idefics3/image_processing_pil_idefics3.py b/src/transformers/models/idefics3/image_processing_pil_idefics3.py index 2fdb99e39e2f..32e5db1ffdaf 100644 --- a/src/transformers/models/idefics3/image_processing_pil_idefics3.py +++ b/src/transformers/models/idefics3/image_processing_pil_idefics3.py @@ -50,7 +50,7 @@ def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndar return mask -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class Idefics3ImageProcessorPil(PilBackend): resample = PILImageResampling.LANCZOS diff --git a/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py b/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py index 772d1ba5d5cb..869187b8555f 100644 --- a/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py +++ b/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py @@ -51,7 +51,7 @@ def color_quantize(x, clusters): return np.argmin(d, axis=1) -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class ImageGPTImageProcessorPil(PilBackend): model_input_names = ["input_ids"] diff --git a/src/transformers/models/janus/image_processing_pil_janus.py b/src/transformers/models/janus/image_processing_pil_janus.py index 01bcb298f8bb..da3b485a0b3f 100644 --- a/src/transformers/models/janus/image_processing_pil_janus.py +++ b/src/transformers/models/janus/image_processing_pil_janus.py @@ -42,7 +42,7 @@ from .image_processing_janus import JanusImageProcessorKwargs -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class JanusImageProcessorPil(PilBackend): resample = PILImageResampling.BICUBIC diff --git a/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py b/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py index c47a0bc724a0..bfa96178a309 100644 --- a/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py +++ b/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py @@ -28,7 +28,7 @@ @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class Kosmos2_5ImageProcessorPil(PilBackend): do_normalize = True do_convert_rgb = True diff --git a/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py b/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py index 78a77eae63b2..e5c47eb61c5a 100644 --- a/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py +++ b/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py @@ -30,7 +30,7 @@ from .image_processing_layoutlmv2 import LayoutLMv2ImageProcessorKwargs, apply_tesseract -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class LayoutLMv2ImageProcessorPil(PilBackend): valid_kwargs = LayoutLMv2ImageProcessorKwargs diff --git a/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py b/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py index d03d62c722de..9180030ae760 100644 --- a/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py +++ b/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py @@ -31,7 +31,7 @@ from .image_processing_layoutlmv3 import LayoutLMv3ImageProcessorKwargs, apply_tesseract -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class LayoutLMv3ImageProcessorPil(PilBackend): valid_kwargs = LayoutLMv3ImageProcessorKwargs diff --git a/src/transformers/models/lightglue/image_processing_pil_lightglue.py b/src/transformers/models/lightglue/image_processing_pil_lightglue.py index 66c85ba46efc..d74caec003ae 100644 --- a/src/transformers/models/lightglue/image_processing_pil_lightglue.py +++ b/src/transformers/models/lightglue/image_processing_pil_lightglue.py @@ -84,7 +84,7 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: return image -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class LightGlueImageProcessorPil(PilBackend): valid_kwargs = LightGlueImageProcessorKwargs resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/lightglue/modular_lightglue.py b/src/transformers/models/lightglue/modular_lightglue.py index 71255c8d3dd0..b72d48c2cc87 100644 --- a/src/transformers/models/lightglue/modular_lightglue.py +++ b/src/transformers/models/lightglue/modular_lightglue.py @@ -179,7 +179,7 @@ def post_process_keypoint_matching( return super().post_process_keypoint_matching(outputs, target_sizes, threshold) -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class LightGlueImageProcessorPil(SuperGlueImageProcessorPil): def post_process_keypoint_matching( self, diff --git a/src/transformers/models/llava_next/image_processing_pil_llava_next.py b/src/transformers/models/llava_next/image_processing_pil_llava_next.py index 5c520095bd6e..eed01006d6d0 100644 --- a/src/transformers/models/llava_next/image_processing_pil_llava_next.py +++ b/src/transformers/models/llava_next/image_processing_pil_llava_next.py @@ -36,7 +36,7 @@ from .image_processing_llava_next import LlavaNextImageProcessorKwargs -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class LlavaNextImageProcessorPil(PilBackend): model_input_names = ["pixel_values", "image_sizes"] diff --git a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py index c8e3fccd2eb4..b1975f80a66e 100644 --- a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py +++ b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py @@ -38,7 +38,7 @@ from .image_processing_llava_onevision import LlavaOnevisionImageProcessorKwargs -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class LlavaOnevisionImageProcessorPil(PilBackend): model_input_names = ["pixel_values", "image_sizes", "batch_num_images"] diff --git a/src/transformers/models/mask2former/image_processing_pil_mask2former.py b/src/transformers/models/mask2former/image_processing_pil_mask2former.py index ad926a52309f..11114008297c 100644 --- a/src/transformers/models/mask2former/image_processing_pil_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_pil_mask2former.py @@ -103,7 +103,7 @@ def convert_segmentation_map_to_binary_masks( return binary_masks.astype(np.float32), labels -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class Mask2FormerImageProcessorPil(PilBackend): valid_kwargs = Mask2FormerImageProcessorKwargs resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/mask2former/modular_mask2former.py b/src/transformers/models/mask2former/modular_mask2former.py index 9fb3c0639554..8885a0fd38a1 100644 --- a/src/transformers/models/mask2former/modular_mask2former.py +++ b/src/transformers/models/mask2former/modular_mask2former.py @@ -306,7 +306,7 @@ def post_process_panoptic_segmentation( return results -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class Mask2FormerImageProcessorPil(MaskFormerImageProcessorPil): def post_process_semantic_segmentation( self, outputs, target_sizes: list[tuple[int, int]] | None = None diff --git a/src/transformers/models/maskformer/image_processing_pil_maskformer.py b/src/transformers/models/maskformer/image_processing_pil_maskformer.py index 5ef4fc1d1f1c..1c2189a78062 100644 --- a/src/transformers/models/maskformer/image_processing_pil_maskformer.py +++ b/src/transformers/models/maskformer/image_processing_pil_maskformer.py @@ -87,7 +87,7 @@ def convert_segmentation_map_to_binary_masks( @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class MaskFormerImageProcessorPil(PilBackend): valid_kwargs = MaskFormerImageProcessorKwargs resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/mllama/image_processing_pil_mllama.py b/src/transformers/models/mllama/image_processing_pil_mllama.py index a7ae5ce21a9a..3ac899e82efd 100644 --- a/src/transformers/models/mllama/image_processing_pil_mllama.py +++ b/src/transformers/models/mllama/image_processing_pil_mllama.py @@ -168,7 +168,7 @@ def convert_aspect_ratios_to_ids_np(aspect_ratios: list[list[tuple[int, int]]], return aspect_ratios_ids -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class MllamaImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py index 48d87af8a807..6dd56ce4f950 100644 --- a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py +++ b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py @@ -36,7 +36,7 @@ @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class MobileNetV2ImageProcessorPil(PilBackend): """PIL backend for MobileNetV2 with reduce_label support.""" diff --git a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py index 714800919eca..be47ec11cccd 100644 --- a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py +++ b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py @@ -40,7 +40,7 @@ @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class MobileViTImageProcessorPil(PilBackend): """PIL backend for MobileViT with flip_channel_order and reduce_label support.""" diff --git a/src/transformers/models/nougat/image_processing_pil_nougat.py b/src/transformers/models/nougat/image_processing_pil_nougat.py index 5214dcd7a3e3..b69f069753a9 100644 --- a/src/transformers/models/nougat/image_processing_pil_nougat.py +++ b/src/transformers/models/nougat/image_processing_pil_nougat.py @@ -41,7 +41,7 @@ from .image_processing_nougat import NougatImageProcessorKwargs -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class NougatImageProcessorPil(PilBackend): valid_kwargs = NougatImageProcessorKwargs diff --git a/src/transformers/models/oneformer/image_processing_pil_oneformer.py b/src/transformers/models/oneformer/image_processing_pil_oneformer.py index 2a14eb51bea8..bb93ab247842 100644 --- a/src/transformers/models/oneformer/image_processing_pil_oneformer.py +++ b/src/transformers/models/oneformer/image_processing_pil_oneformer.py @@ -67,7 +67,7 @@ def make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarr @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class OneFormerImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR image_mean = IMAGENET_DEFAULT_MEAN diff --git a/src/transformers/models/ovis2/image_processing_pil_ovis2.py b/src/transformers/models/ovis2/image_processing_pil_ovis2.py index 5ecc453f7c6a..395c90b6e273 100644 --- a/src/transformers/models/ovis2/image_processing_pil_ovis2.py +++ b/src/transformers/models/ovis2/image_processing_pil_ovis2.py @@ -37,7 +37,7 @@ ) -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class Ovis2ImageProcessorPil(PilBackend): resample = PILImageResampling.BICUBIC diff --git a/src/transformers/models/owlv2/image_processing_pil_owlv2.py b/src/transformers/models/owlv2/image_processing_pil_owlv2.py index 9fe163797f25..1fe7430e1497 100644 --- a/src/transformers/models/owlv2/image_processing_pil_owlv2.py +++ b/src/transformers/models/owlv2/image_processing_pil_owlv2.py @@ -153,7 +153,7 @@ def _scale_boxes(boxes, target_sizes): @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class Owlv2ImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR image_mean = OPENAI_CLIP_MEAN diff --git a/src/transformers/models/owlv2/modular_owlv2.py b/src/transformers/models/owlv2/modular_owlv2.py index 9d5d1d873ba6..8cba8e0ccecb 100644 --- a/src/transformers/models/owlv2/modular_owlv2.py +++ b/src/transformers/models/owlv2/modular_owlv2.py @@ -312,7 +312,7 @@ def _preprocess( @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class Owlv2ImageProcessorPil(OwlViTImageProcessorPil): resample = PILImageResampling.BILINEAR image_mean = OPENAI_CLIP_MEAN diff --git a/src/transformers/models/owlvit/image_processing_pil_owlvit.py b/src/transformers/models/owlvit/image_processing_pil_owlvit.py index 2afdea5eb809..84d016c22f15 100644 --- a/src/transformers/models/owlvit/image_processing_pil_owlvit.py +++ b/src/transformers/models/owlvit/image_processing_pil_owlvit.py @@ -33,7 +33,7 @@ logger = logging.get_logger(__name__) -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) @auto_docstring class OwlViTImageProcessorPil(PilBackend): resample = PILImageResampling.BICUBIC diff --git a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py index 50b403d270e6..110875403563 100644 --- a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py @@ -40,7 +40,7 @@ from torchvision.transforms.v2 import functional as tvF -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class PaddleOCRVLImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py index 72fb9c9bbe7e..43c63c313549 100644 --- a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py +++ b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py @@ -30,7 +30,7 @@ @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class Pix2StructImageProcessorPil(PilBackend): rescale_factor = None do_normalize = True diff --git a/src/transformers/models/pixtral/image_processing_pil_pixtral.py b/src/transformers/models/pixtral/image_processing_pil_pixtral.py index a0cc0cfa1364..105601cd1dda 100644 --- a/src/transformers/models/pixtral/image_processing_pil_pixtral.py +++ b/src/transformers/models/pixtral/image_processing_pil_pixtral.py @@ -31,7 +31,7 @@ from .image_processing_pixtral import PixtralImageProcessorKwargs, get_resize_output_image_size -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class PixtralImageProcessorPil(PilBackend): resample = PILImageResampling.BICUBIC diff --git a/src/transformers/models/poolformer/image_processing_pil_poolformer.py b/src/transformers/models/poolformer/image_processing_pil_poolformer.py index 635635306ff5..0557818192d9 100644 --- a/src/transformers/models/poolformer/image_processing_pil_poolformer.py +++ b/src/transformers/models/poolformer/image_processing_pil_poolformer.py @@ -31,7 +31,7 @@ from .image_processing_poolformer import PoolFormerImageProcessorKwargs -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class PoolFormerImageProcessorPil(PilBackend): """PIL backend for PoolFormer with custom resize (crop_pct).""" diff --git a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py index 522068abeba0..3d5c5b4d2602 100644 --- a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py @@ -85,7 +85,7 @@ def _get_resize_output_image_size( @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class PromptDepthAnythingImageProcessorPil(PilBackend): model_input_names = ["pixel_values", "prompt_depth"] diff --git a/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py b/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py index e3707edf5467..30b8d703d3ba 100644 --- a/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py +++ b/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py @@ -32,7 +32,7 @@ from .image_processing_qwen2_vl import Qwen2VLImageProcessorKwargs, smart_resize -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class Qwen2VLImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py index 848de988db74..62e12da0bbf9 100644 --- a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py @@ -115,7 +115,7 @@ def prepare_coco_detection_annotation_pil( return new_target -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class RTDetrImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR image_mean = IMAGENET_DEFAULT_MEAN diff --git a/src/transformers/models/rt_detr/modular_rt_detr.py b/src/transformers/models/rt_detr/modular_rt_detr.py index a299925a348d..03c17977db62 100644 --- a/src/transformers/models/rt_detr/modular_rt_detr.py +++ b/src/transformers/models/rt_detr/modular_rt_detr.py @@ -427,7 +427,7 @@ def post_process_panoptic_segmentation(self): raise NotImplementedError("Panoptic segmentation post-processing is not implemented for RT-DETR yet.") -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class RTDetrImageProcessorPil(DetrImageProcessorPil): resample = PILImageResampling.BILINEAR image_mean = IMAGENET_DEFAULT_MEAN diff --git a/src/transformers/models/sam/image_processing_pil_sam.py b/src/transformers/models/sam/image_processing_pil_sam.py index ffc745e02ac3..8d15dcd9e309 100644 --- a/src/transformers/models/sam/image_processing_pil_sam.py +++ b/src/transformers/models/sam/image_processing_pil_sam.py @@ -63,7 +63,7 @@ def get_resize_output_image_size( @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class SamImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR image_mean = IMAGENET_DEFAULT_MEAN diff --git a/src/transformers/models/seggpt/image_processing_pil_seggpt.py b/src/transformers/models/seggpt/image_processing_pil_seggpt.py index 33039503d802..ede14b211d95 100644 --- a/src/transformers/models/seggpt/image_processing_pil_seggpt.py +++ b/src/transformers/models/seggpt/image_processing_pil_seggpt.py @@ -34,7 +34,7 @@ @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class SegGptImageProcessorPil(PilBackend): valid_kwargs = SegGptImageProcessorKwargs diff --git a/src/transformers/models/siglip2/image_processing_pil_siglip2.py b/src/transformers/models/siglip2/image_processing_pil_siglip2.py index efca87b53635..02c0a892a38b 100644 --- a/src/transformers/models/siglip2/image_processing_pil_siglip2.py +++ b/src/transformers/models/siglip2/image_processing_pil_siglip2.py @@ -58,7 +58,7 @@ def pad_along_first_dim(array: np.ndarray, target_length: int, pad_value: int = return array, mask -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class Siglip2ImageProcessorPil(PilBackend): valid_kwargs = Siglip2ImageProcessorKwargs diff --git a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py index 0f0711dcee7d..0a4e8476c1d3 100644 --- a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py +++ b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py @@ -57,7 +57,7 @@ def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndar return mask -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class SmolVLMImageProcessorPil(PilBackend): resample = PILImageResampling.LANCZOS diff --git a/src/transformers/models/superglue/image_processing_pil_superglue.py b/src/transformers/models/superglue/image_processing_pil_superglue.py index bb2837a33eea..ab0f2b37814c 100644 --- a/src/transformers/models/superglue/image_processing_pil_superglue.py +++ b/src/transformers/models/superglue/image_processing_pil_superglue.py @@ -81,7 +81,7 @@ class SuperGlueImageProcessorKwargs(ImagesKwargs, total=False): @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class SuperGlueImageProcessorPil(PilBackend): valid_kwargs = SuperGlueImageProcessorKwargs resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/superpoint/image_processing_pil_superpoint.py b/src/transformers/models/superpoint/image_processing_pil_superpoint.py index c428241b5840..62ad6272c1cd 100644 --- a/src/transformers/models/superpoint/image_processing_pil_superpoint.py +++ b/src/transformers/models/superpoint/image_processing_pil_superpoint.py @@ -61,7 +61,7 @@ def convert_to_grayscale(image: np.ndarray) -> np.ndarray: @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class SuperPointImageProcessorPil(PilBackend): valid_kwargs = SuperPointImageProcessorKwargs resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py b/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py index 8a8e4058af70..03cc1533c690 100644 --- a/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py +++ b/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py @@ -30,7 +30,7 @@ from .image_processing_swin2sr import Swin2SRImageProcessorKwargs -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class Swin2SRImageProcessorPil(PilBackend): """PIL backend for Swin2SR with custom pad.""" diff --git a/src/transformers/models/textnet/image_processing_pil_textnet.py b/src/transformers/models/textnet/image_processing_pil_textnet.py index 394693c417a2..084d656f85d2 100644 --- a/src/transformers/models/textnet/image_processing_pil_textnet.py +++ b/src/transformers/models/textnet/image_processing_pil_textnet.py @@ -33,7 +33,7 @@ from .image_processing_textnet import TextNetImageProcessorKwargs -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class TextNetImageProcessorPil(PilBackend): """PIL backend for TextNet with size_divisor resize.""" diff --git a/src/transformers/models/tvp/image_processing_pil_tvp.py b/src/transformers/models/tvp/image_processing_pil_tvp.py index 7a15222ba975..c9df38012e0d 100644 --- a/src/transformers/models/tvp/image_processing_pil_tvp.py +++ b/src/transformers/models/tvp/image_processing_pil_tvp.py @@ -59,7 +59,7 @@ def get_resize_output_image_size( @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) class TvpImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR image_mean = IMAGENET_STANDARD_MEAN diff --git a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py index 53c3e3c03d26..5db04e705f30 100644 --- a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py +++ b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py @@ -34,7 +34,7 @@ from torchvision.transforms.v2 import functional as tvF -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class VideoLlama3ImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/vilt/image_processing_pil_vilt.py b/src/transformers/models/vilt/image_processing_pil_vilt.py index a5c4ecd9de51..6de4f0fffb93 100644 --- a/src/transformers/models/vilt/image_processing_pil_vilt.py +++ b/src/transformers/models/vilt/image_processing_pil_vilt.py @@ -99,7 +99,7 @@ def get_resize_output_image_size( return new_height, new_width -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class ViltImageProcessorPil(PilBackend): valid_kwargs = ViltImageProcessorKwargs diff --git a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py index 7438d2aa9168..3033c4930129 100644 --- a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py +++ b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py @@ -38,7 +38,7 @@ import torch -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class VitMatteImageProcessorPil(PilBackend): do_rescale = True diff --git a/src/transformers/models/yolos/image_processing_pil_yolos.py b/src/transformers/models/yolos/image_processing_pil_yolos.py index 943aee1cf6be..aafe3d4ec6b6 100644 --- a/src/transformers/models/yolos/image_processing_pil_yolos.py +++ b/src/transformers/models/yolos/image_processing_pil_yolos.py @@ -280,7 +280,7 @@ def get_size_with_aspect_ratio_yolos( return (oh, ow) -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision",)) @auto_docstring class YolosImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py index 3c446bbfdbbd..1d017046079b 100644 --- a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py +++ b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py @@ -48,7 +48,7 @@ @auto_docstring -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("vision", "torch")) class ZoeDepthImageProcessorPil(PilBackend): valid_kwargs = ZoeDepthImageProcessorKwargs do_pad = True From 035abaa1d4d23c925d3a4f7228c29f83d96d95f5 Mon Sep 17 00:00:00 2001 From: Lidang-Jiang Date: Fri, 27 Mar 2026 18:24:59 +0800 Subject: [PATCH 02/30] [Bugfix] Remove redundant @requires(backends=("vision",)) from PIL backends Per reviewer feedback: the vision-only @requires decorator is redundant for PIL backend classes since PilBackend base class already handles this. - Remove @requires(backends=("vision",)) from 43 PIL backend files - Remove unused `requires` import from 38 files (Category A) - Keep @requires(backends=("vision", "torch")) on method-level decorators (Category B: 5 files) --- src/transformers/models/aria/image_processing_pil_aria.py | 2 -- .../models/bridgetower/image_processing_pil_bridgetower.py | 2 -- .../conditional_detr/image_processing_pil_conditional_detr.py | 1 - .../models/convnext/image_processing_pil_convnext.py | 2 -- .../models/deepseek_vl/image_processing_pil_deepseek_vl.py | 2 -- .../image_processing_pil_deepseek_vl_hybrid.py | 2 -- .../deformable_detr/image_processing_pil_deformable_detr.py | 1 - src/transformers/models/detr/image_processing_pil_detr.py | 1 - src/transformers/models/donut/image_processing_pil_donut.py | 2 -- .../models/efficientnet/image_processing_pil_efficientnet.py | 2 -- .../ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py | 2 -- src/transformers/models/flava/image_processing_pil_flava.py | 2 -- src/transformers/models/gemma3/image_processing_pil_gemma3.py | 2 -- src/transformers/models/glm46v/image_processing_pil_glm46v.py | 2 -- src/transformers/models/glm4v/image_processing_pil_glm4v.py | 2 -- .../models/glm_image/image_processing_pil_glm_image.py | 2 -- .../models/got_ocr2/image_processing_pil_got_ocr2.py | 2 -- .../grounding_dino/image_processing_pil_grounding_dino.py | 1 - src/transformers/models/idefics/image_processing_pil_idefics.py | 2 -- .../models/idefics2/image_processing_pil_idefics2.py | 2 -- .../models/idefics3/image_processing_pil_idefics3.py | 2 -- .../models/imagegpt/image_processing_pil_imagegpt.py | 2 -- src/transformers/models/janus/image_processing_pil_janus.py | 2 -- .../models/layoutlmv2/image_processing_pil_layoutlmv2.py | 2 -- .../models/layoutlmv3/image_processing_pil_layoutlmv3.py | 2 -- .../models/llava_next/image_processing_pil_llava_next.py | 2 -- .../llava_onevision/image_processing_pil_llava_onevision.py | 2 -- src/transformers/models/mllama/image_processing_pil_mllama.py | 2 -- src/transformers/models/nougat/image_processing_pil_nougat.py | 2 -- src/transformers/models/ovis2/image_processing_pil_ovis2.py | 2 -- .../models/paddleocr_vl/image_processing_pil_paddleocr_vl.py | 2 -- src/transformers/models/pixtral/image_processing_pil_pixtral.py | 2 -- .../models/poolformer/image_processing_pil_poolformer.py | 2 -- .../models/qwen2_vl/image_processing_pil_qwen2_vl.py | 2 -- src/transformers/models/siglip2/image_processing_pil_siglip2.py | 2 -- src/transformers/models/smolvlm/image_processing_pil_smolvlm.py | 2 -- src/transformers/models/swin2sr/image_processing_pil_swin2sr.py | 2 -- src/transformers/models/textnet/image_processing_pil_textnet.py | 2 -- src/transformers/models/tvp/image_processing_pil_tvp.py | 2 -- .../models/video_llama_3/image_processing_pil_video_llama_3.py | 2 -- src/transformers/models/vilt/image_processing_pil_vilt.py | 2 -- .../models/vitmatte/image_processing_pil_vitmatte.py | 2 -- src/transformers/models/yolos/image_processing_pil_yolos.py | 1 - 43 files changed, 81 deletions(-) diff --git a/src/transformers/models/aria/image_processing_pil_aria.py b/src/transformers/models/aria/image_processing_pil_aria.py index b4a31e30f802..7909809b8064 100644 --- a/src/transformers/models/aria/image_processing_pil_aria.py +++ b/src/transformers/models/aria/image_processing_pil_aria.py @@ -26,11 +26,9 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_aria import AriaImageProcessorKwargs -@requires(backends=("vision",)) @auto_docstring class AriaImageProcessorPil(PilBackend): model_input_names = ["pixel_values", "pixel_mask", "num_crops"] diff --git a/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py b/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py index 1ee53b0d8e0e..7cbc779930e1 100644 --- a/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py +++ b/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py @@ -25,11 +25,9 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_bridgetower import BridgeTowerImageProcessorKwargs, get_resize_output_image_size -@requires(backends=("vision",)) @auto_docstring class BridgeTowerImageProcessorPil(PilBackend): """PIL backend for BridgeTower with custom resize and center_crop.""" diff --git a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py index a8602e1fea7d..7f6bddce6dce 100644 --- a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py +++ b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py @@ -263,7 +263,6 @@ def prepare_coco_panoptic_annotation( return new_target -@requires(backends=("vision",)) @auto_docstring class ConditionalDetrImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/convnext/image_processing_pil_convnext.py b/src/transformers/models/convnext/image_processing_pil_convnext.py index 6edd51c8484b..78972e98e963 100644 --- a/src/transformers/models/convnext/image_processing_pil_convnext.py +++ b/src/transformers/models/convnext/image_processing_pil_convnext.py @@ -27,11 +27,9 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_convnext import ConvNextImageProcessorKwargs -@requires(backends=("vision",)) @auto_docstring class ConvNextImageProcessorPil(PilBackend): """PIL backend for ConvNeXT with custom resize.""" diff --git a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py index e14256c3de5e..d29296535277 100644 --- a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py +++ b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py @@ -34,11 +34,9 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_deepseek_vl import DeepseekVLImageProcessorKwargs -@requires(backends=("vision",)) @auto_docstring class DeepseekVLImageProcessorPil(PilBackend): resample = PILImageResampling.BICUBIC diff --git a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py index 1c0e5bbfbd06..3c9bc6b7436a 100644 --- a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py +++ b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py @@ -35,7 +35,6 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, is_torch_available -from ...utils.import_utils import requires from .image_processing_deepseek_vl_hybrid import DeepseekVLHybridImageProcessorKwargs @@ -43,7 +42,6 @@ import torch -@requires(backends=("vision",)) @auto_docstring class DeepseekVLHybridImageProcessorPil(PilBackend): resample = PILImageResampling.BICUBIC diff --git a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py index 3532bbe96bd6..1c82253d367e 100644 --- a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py +++ b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py @@ -246,7 +246,6 @@ def prepare_coco_panoptic_annotation( return new_target -@requires(backends=("vision",)) @auto_docstring class DeformableDetrImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/detr/image_processing_pil_detr.py b/src/transformers/models/detr/image_processing_pil_detr.py index fe406e888c2b..8f4b2f47068b 100644 --- a/src/transformers/models/detr/image_processing_pil_detr.py +++ b/src/transformers/models/detr/image_processing_pil_detr.py @@ -254,7 +254,6 @@ def prepare_coco_panoptic_annotation( return new_target -@requires(backends=("vision",)) @auto_docstring class DetrImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/donut/image_processing_pil_donut.py b/src/transformers/models/donut/image_processing_pil_donut.py index 7c5c5cc9d7f7..ba73d9f24b23 100644 --- a/src/transformers/models/donut/image_processing_pil_donut.py +++ b/src/transformers/models/donut/image_processing_pil_donut.py @@ -27,11 +27,9 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_donut import DonutImageProcessorKwargs -@requires(backends=("vision",)) @auto_docstring class DonutImageProcessorPil(PilBackend): """PIL backend for Donut with align_long_axis, thumbnail, and pad_image.""" diff --git a/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py b/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py index d5f76e1c1d83..a9469f357fd0 100644 --- a/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py +++ b/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py @@ -25,11 +25,9 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_efficientnet import EfficientNetImageProcessorKwargs -@requires(backends=("vision",)) @auto_docstring class EfficientNetImageProcessorPil(PilBackend): """PIL backend for EfficientNet with rescale offset and include_top.""" diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py index 11574f7951d2..1bbff5d029c9 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py @@ -25,7 +25,6 @@ from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available, logging -from ...utils.import_utils import requires from .image_processing_ernie4_5_vl_moe import Ernie4_5_VLMoeImageProcessorKwargs, smart_resize @@ -38,7 +37,6 @@ logger = logging.get_logger(__name__) -@requires(backends=("vision",)) @auto_docstring class Ernie4_5_VLMoeImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/flava/image_processing_pil_flava.py b/src/transformers/models/flava/image_processing_pil_flava.py index 17b9ce38fd9f..424f97928723 100644 --- a/src/transformers/models/flava/image_processing_pil_flava.py +++ b/src/transformers/models/flava/image_processing_pil_flava.py @@ -32,7 +32,6 @@ TensorType, auto_docstring, ) -from ...utils.import_utils import requires from .image_processing_flava import ( FLAVA_CODEBOOK_MEAN, FLAVA_CODEBOOK_STD, @@ -44,7 +43,6 @@ ) -@requires(backends=("vision",)) @auto_docstring class FlavaImageProcessorPil(PilBackend): valid_kwargs = FlavaImageProcessorKwargs diff --git a/src/transformers/models/gemma3/image_processing_pil_gemma3.py b/src/transformers/models/gemma3/image_processing_pil_gemma3.py index 46f622a6b279..a849e05aead0 100644 --- a/src/transformers/models/gemma3/image_processing_pil_gemma3.py +++ b/src/transformers/models/gemma3/image_processing_pil_gemma3.py @@ -33,11 +33,9 @@ TensorType, auto_docstring, ) -from ...utils.import_utils import requires from .image_processing_gemma3 import Gemma3ImageProcessorKwargs -@requires(backends=("vision",)) @auto_docstring class Gemma3ImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/glm46v/image_processing_pil_glm46v.py b/src/transformers/models/glm46v/image_processing_pil_glm46v.py index 609d1ae2e3cf..58071c11fa3b 100644 --- a/src/transformers/models/glm46v/image_processing_pil_glm46v.py +++ b/src/transformers/models/glm46v/image_processing_pil_glm46v.py @@ -26,11 +26,9 @@ from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_glm46v import Glm46VImageProcessorKwargs, smart_resize -@requires(backends=("vision",)) @auto_docstring class Glm46VImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/glm4v/image_processing_pil_glm4v.py b/src/transformers/models/glm4v/image_processing_pil_glm4v.py index 6e85edd39330..dcd5a127b934 100644 --- a/src/transformers/models/glm4v/image_processing_pil_glm4v.py +++ b/src/transformers/models/glm4v/image_processing_pil_glm4v.py @@ -26,7 +26,6 @@ ) from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, logging -from ...utils.import_utils import requires from .image_processing_glm4v import smart_resize @@ -48,7 +47,6 @@ class Glm4vImageProcessorKwargs(ImagesKwargs, total=False): merge_size: int -@requires(backends=("vision",)) @auto_docstring class Glm4vImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/glm_image/image_processing_pil_glm_image.py b/src/transformers/models/glm_image/image_processing_pil_glm_image.py index 263c47da70af..1d36a1b095de 100644 --- a/src/transformers/models/glm_image/image_processing_pil_glm_image.py +++ b/src/transformers/models/glm_image/image_processing_pil_glm_image.py @@ -27,11 +27,9 @@ from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_glm_image import GlmImageImageProcessorKwargs, smart_resize -@requires(backends=("vision",)) @auto_docstring class GlmImageImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py b/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py index 97abffe7dd6d..c238d2625b2e 100644 --- a/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py +++ b/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py @@ -32,11 +32,9 @@ TensorType, auto_docstring, ) -from ...utils.import_utils import requires from .image_processing_got_ocr2 import GotOcr2ImageProcessorKwargs, get_optimal_tiled_canvas -@requires(backends=("vision",)) @auto_docstring class GotOcr2ImageProcessorPil(PilBackend): valid_kwargs = GotOcr2ImageProcessorKwargs diff --git a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py index e1bc88b60e71..e18e0be190a7 100644 --- a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py +++ b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py @@ -290,7 +290,6 @@ def _scale_boxes(boxes, target_sizes): return boxes -@requires(backends=("vision",)) @auto_docstring class GroundingDinoImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/idefics/image_processing_pil_idefics.py b/src/transformers/models/idefics/image_processing_pil_idefics.py index c3c991152922..57905fd6d7d4 100644 --- a/src/transformers/models/idefics/image_processing_pil_idefics.py +++ b/src/transformers/models/idefics/image_processing_pil_idefics.py @@ -17,11 +17,9 @@ from ...image_utils import ImageInput, PILImageResampling, make_flat_list_of_images from ...processing_utils import Unpack from ...utils import auto_docstring, is_torch_available -from ...utils.import_utils import requires from .image_processing_idefics import IDEFICS_STANDARD_MEAN, IDEFICS_STANDARD_STD, IdeficsImageProcessorKwargs -@requires(backends=("vision",)) @auto_docstring class IdeficsImageProcessorPil(PilBackend): valid_kwargs = IdeficsImageProcessorKwargs diff --git a/src/transformers/models/idefics2/image_processing_pil_idefics2.py b/src/transformers/models/idefics2/image_processing_pil_idefics2.py index f3213b1ed6b7..8b6373564aca 100644 --- a/src/transformers/models/idefics2/image_processing_pil_idefics2.py +++ b/src/transformers/models/idefics2/image_processing_pil_idefics2.py @@ -28,7 +28,6 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_idefics2 import ( Idefics2ImageProcessorKwargs, convert_to_rgb, @@ -45,7 +44,6 @@ def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndar return mask -@requires(backends=("vision",)) @auto_docstring class Idefics2ImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/idefics3/image_processing_pil_idefics3.py b/src/transformers/models/idefics3/image_processing_pil_idefics3.py index 32e5db1ffdaf..2430590b8e07 100644 --- a/src/transformers/models/idefics3/image_processing_pil_idefics3.py +++ b/src/transformers/models/idefics3/image_processing_pil_idefics3.py @@ -30,7 +30,6 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_idefics3 import ( MAX_IMAGE_SIZE, Idefics3ImageProcessorKwargs, @@ -50,7 +49,6 @@ def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndar return mask -@requires(backends=("vision",)) @auto_docstring class Idefics3ImageProcessorPil(PilBackend): resample = PILImageResampling.LANCZOS diff --git a/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py b/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py index 869187b8555f..fc9c9e6a7337 100644 --- a/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py +++ b/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py @@ -27,7 +27,6 @@ auto_docstring, is_torch_available, ) -from ...utils.import_utils import requires if is_torch_available(): @@ -51,7 +50,6 @@ def color_quantize(x, clusters): return np.argmin(d, axis=1) -@requires(backends=("vision",)) @auto_docstring class ImageGPTImageProcessorPil(PilBackend): model_input_names = ["input_ids"] diff --git a/src/transformers/models/janus/image_processing_pil_janus.py b/src/transformers/models/janus/image_processing_pil_janus.py index da3b485a0b3f..7cc38a538e70 100644 --- a/src/transformers/models/janus/image_processing_pil_janus.py +++ b/src/transformers/models/janus/image_processing_pil_janus.py @@ -38,11 +38,9 @@ TensorType, auto_docstring, ) -from ...utils.import_utils import requires from .image_processing_janus import JanusImageProcessorKwargs -@requires(backends=("vision",)) @auto_docstring class JanusImageProcessorPil(PilBackend): resample = PILImageResampling.BICUBIC diff --git a/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py b/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py index e5c47eb61c5a..6a2884f7d899 100644 --- a/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py +++ b/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py @@ -26,11 +26,9 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, requires_backends -from ...utils.import_utils import requires from .image_processing_layoutlmv2 import LayoutLMv2ImageProcessorKwargs, apply_tesseract -@requires(backends=("vision",)) @auto_docstring class LayoutLMv2ImageProcessorPil(PilBackend): valid_kwargs = LayoutLMv2ImageProcessorKwargs diff --git a/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py b/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py index 9180030ae760..031c6332049b 100644 --- a/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py +++ b/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py @@ -27,11 +27,9 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, requires_backends -from ...utils.import_utils import requires from .image_processing_layoutlmv3 import LayoutLMv3ImageProcessorKwargs, apply_tesseract -@requires(backends=("vision",)) @auto_docstring class LayoutLMv3ImageProcessorPil(PilBackend): valid_kwargs = LayoutLMv3ImageProcessorKwargs diff --git a/src/transformers/models/llava_next/image_processing_pil_llava_next.py b/src/transformers/models/llava_next/image_processing_pil_llava_next.py index eed01006d6d0..1dfda3142a44 100644 --- a/src/transformers/models/llava_next/image_processing_pil_llava_next.py +++ b/src/transformers/models/llava_next/image_processing_pil_llava_next.py @@ -32,11 +32,9 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_llava_next import LlavaNextImageProcessorKwargs -@requires(backends=("vision",)) @auto_docstring class LlavaNextImageProcessorPil(PilBackend): model_input_names = ["pixel_values", "image_sizes"] diff --git a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py index b1975f80a66e..1a1025a923c8 100644 --- a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py +++ b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py @@ -34,11 +34,9 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_llava_onevision import LlavaOnevisionImageProcessorKwargs -@requires(backends=("vision",)) @auto_docstring class LlavaOnevisionImageProcessorPil(PilBackend): model_input_names = ["pixel_values", "image_sizes", "batch_num_images"] diff --git a/src/transformers/models/mllama/image_processing_pil_mllama.py b/src/transformers/models/mllama/image_processing_pil_mllama.py index 3ac899e82efd..3697a2632415 100644 --- a/src/transformers/models/mllama/image_processing_pil_mllama.py +++ b/src/transformers/models/mllama/image_processing_pil_mllama.py @@ -30,7 +30,6 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_mllama import ( MllamaImageProcessorKwargs, _validate_mllama_preprocess_arguments, @@ -168,7 +167,6 @@ def convert_aspect_ratios_to_ids_np(aspect_ratios: list[list[tuple[int, int]]], return aspect_ratios_ids -@requires(backends=("vision",)) @auto_docstring class MllamaImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/nougat/image_processing_pil_nougat.py b/src/transformers/models/nougat/image_processing_pil_nougat.py index b69f069753a9..10a673f50c51 100644 --- a/src/transformers/models/nougat/image_processing_pil_nougat.py +++ b/src/transformers/models/nougat/image_processing_pil_nougat.py @@ -37,11 +37,9 @@ TensorType, auto_docstring, ) -from ...utils.import_utils import requires from .image_processing_nougat import NougatImageProcessorKwargs -@requires(backends=("vision",)) @auto_docstring class NougatImageProcessorPil(PilBackend): valid_kwargs = NougatImageProcessorKwargs diff --git a/src/transformers/models/ovis2/image_processing_pil_ovis2.py b/src/transformers/models/ovis2/image_processing_pil_ovis2.py index 395c90b6e273..ce047b7b372d 100644 --- a/src/transformers/models/ovis2/image_processing_pil_ovis2.py +++ b/src/transformers/models/ovis2/image_processing_pil_ovis2.py @@ -29,7 +29,6 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_ovis2 import ( Ovis2ImageProcessorKwargs, get_min_tile_covering_grid, @@ -37,7 +36,6 @@ ) -@requires(backends=("vision",)) @auto_docstring class Ovis2ImageProcessorPil(PilBackend): resample = PILImageResampling.BICUBIC diff --git a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py index 110875403563..20e0887400e1 100644 --- a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py @@ -32,7 +32,6 @@ from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, is_torchvision_available -from ...utils.import_utils import requires from .image_processing_paddleocr_vl import PaddleOCRVLImageProcessorKwargs, smart_resize @@ -40,7 +39,6 @@ from torchvision.transforms.v2 import functional as tvF -@requires(backends=("vision",)) @auto_docstring class PaddleOCRVLImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/pixtral/image_processing_pil_pixtral.py b/src/transformers/models/pixtral/image_processing_pil_pixtral.py index 105601cd1dda..cd1759efacbf 100644 --- a/src/transformers/models/pixtral/image_processing_pil_pixtral.py +++ b/src/transformers/models/pixtral/image_processing_pil_pixtral.py @@ -27,11 +27,9 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_pixtral import PixtralImageProcessorKwargs, get_resize_output_image_size -@requires(backends=("vision",)) @auto_docstring class PixtralImageProcessorPil(PilBackend): resample = PILImageResampling.BICUBIC diff --git a/src/transformers/models/poolformer/image_processing_pil_poolformer.py b/src/transformers/models/poolformer/image_processing_pil_poolformer.py index 0557818192d9..8208a9c79b5c 100644 --- a/src/transformers/models/poolformer/image_processing_pil_poolformer.py +++ b/src/transformers/models/poolformer/image_processing_pil_poolformer.py @@ -27,11 +27,9 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_poolformer import PoolFormerImageProcessorKwargs -@requires(backends=("vision",)) @auto_docstring class PoolFormerImageProcessorPil(PilBackend): """PIL backend for PoolFormer with custom resize (crop_pct).""" diff --git a/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py b/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py index 30b8d703d3ba..92cec311be12 100644 --- a/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py +++ b/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py @@ -28,11 +28,9 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_qwen2_vl import Qwen2VLImageProcessorKwargs, smart_resize -@requires(backends=("vision",)) @auto_docstring class Qwen2VLImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/siglip2/image_processing_pil_siglip2.py b/src/transformers/models/siglip2/image_processing_pil_siglip2.py index 02c0a892a38b..c90a3a2f9e90 100644 --- a/src/transformers/models/siglip2/image_processing_pil_siglip2.py +++ b/src/transformers/models/siglip2/image_processing_pil_siglip2.py @@ -23,7 +23,6 @@ TensorType, auto_docstring, ) -from ...utils.import_utils import requires from .image_processing_siglip2 import ( Siglip2ImageProcessorKwargs, get_image_size_for_max_num_patches, @@ -58,7 +57,6 @@ def pad_along_first_dim(array: np.ndarray, target_length: int, pad_value: int = return array, mask -@requires(backends=("vision",)) @auto_docstring class Siglip2ImageProcessorPil(PilBackend): valid_kwargs = Siglip2ImageProcessorKwargs diff --git a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py index 0a4e8476c1d3..35edc052a533 100644 --- a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py +++ b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py @@ -37,7 +37,6 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_smolvlm import ( MAX_IMAGE_SIZE, SmolVLMImageProcessorKwargs, @@ -57,7 +56,6 @@ def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndar return mask -@requires(backends=("vision",)) @auto_docstring class SmolVLMImageProcessorPil(PilBackend): resample = PILImageResampling.LANCZOS diff --git a/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py b/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py index 03cc1533c690..d81311024510 100644 --- a/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py +++ b/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py @@ -26,11 +26,9 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_swin2sr import Swin2SRImageProcessorKwargs -@requires(backends=("vision",)) @auto_docstring class Swin2SRImageProcessorPil(PilBackend): """PIL backend for Swin2SR with custom pad.""" diff --git a/src/transformers/models/textnet/image_processing_pil_textnet.py b/src/transformers/models/textnet/image_processing_pil_textnet.py index 084d656f85d2..29b4059aaa7b 100644 --- a/src/transformers/models/textnet/image_processing_pil_textnet.py +++ b/src/transformers/models/textnet/image_processing_pil_textnet.py @@ -29,11 +29,9 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring -from ...utils.import_utils import requires from .image_processing_textnet import TextNetImageProcessorKwargs -@requires(backends=("vision",)) @auto_docstring class TextNetImageProcessorPil(PilBackend): """PIL backend for TextNet with size_divisor resize.""" diff --git a/src/transformers/models/tvp/image_processing_pil_tvp.py b/src/transformers/models/tvp/image_processing_pil_tvp.py index c9df38012e0d..48796df9b4ae 100644 --- a/src/transformers/models/tvp/image_processing_pil_tvp.py +++ b/src/transformers/models/tvp/image_processing_pil_tvp.py @@ -31,7 +31,6 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, is_torchvision_available -from ...utils.import_utils import requires from .image_processing_tvp import TvpImageProcessorKwargs @@ -59,7 +58,6 @@ def get_resize_output_image_size( @auto_docstring -@requires(backends=("vision",)) class TvpImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR image_mean = IMAGENET_STANDARD_MEAN diff --git a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py index 5db04e705f30..90fbf0efbe86 100644 --- a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py +++ b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py @@ -26,7 +26,6 @@ from ...image_utils import IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, is_torchvision_available -from ...utils.import_utils import requires from .image_processing_video_llama_3 import VideoLlama3ImageProcessorKwargs, smart_resize @@ -34,7 +33,6 @@ from torchvision.transforms.v2 import functional as tvF -@requires(backends=("vision",)) @auto_docstring class VideoLlama3ImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/vilt/image_processing_pil_vilt.py b/src/transformers/models/vilt/image_processing_pil_vilt.py index 6de4f0fffb93..529d3ec0fc6f 100644 --- a/src/transformers/models/vilt/image_processing_pil_vilt.py +++ b/src/transformers/models/vilt/image_processing_pil_vilt.py @@ -34,7 +34,6 @@ TensorType, auto_docstring, ) -from ...utils.import_utils import requires from .image_processing_vilt import ViltImageProcessorKwargs @@ -99,7 +98,6 @@ def get_resize_output_image_size( return new_height, new_width -@requires(backends=("vision",)) @auto_docstring class ViltImageProcessorPil(PilBackend): valid_kwargs = ViltImageProcessorKwargs diff --git a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py index 3033c4930129..b83a773b5ac5 100644 --- a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py +++ b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py @@ -30,7 +30,6 @@ ) from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, is_torch_available -from ...utils.import_utils import requires from .image_processing_vitmatte import VitMatteImageProcessorKwargs @@ -38,7 +37,6 @@ import torch -@requires(backends=("vision",)) @auto_docstring class VitMatteImageProcessorPil(PilBackend): do_rescale = True diff --git a/src/transformers/models/yolos/image_processing_pil_yolos.py b/src/transformers/models/yolos/image_processing_pil_yolos.py index aafe3d4ec6b6..0962d02caaa9 100644 --- a/src/transformers/models/yolos/image_processing_pil_yolos.py +++ b/src/transformers/models/yolos/image_processing_pil_yolos.py @@ -280,7 +280,6 @@ def get_size_with_aspect_ratio_yolos( return (oh, ow) -@requires(backends=("vision",)) @auto_docstring class YolosImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR From 1417bc1917e78683d83d80d8c0386481efcfd231 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 12:14:11 +0100 Subject: [PATCH 03/30] update --- .../models/beit/image_processing_beit.py | 6 +++--- .../models/beit/image_processing_pil_beit.py | 19 +++++++++++-------- .../image_processing_pil_conditional_detr.py | 8 ++++---- .../modular_conditional_detr.py | 4 ++-- .../image_processing_pil_deformable_detr.py | 2 +- .../modular_deformable_detr.py | 2 +- .../models/detr/image_processing_pil_detr.py | 8 ++++---- .../models/dpt/image_processing_pil_dpt.py | 17 ++++++++++------- .../image_processing_pil_efficientloftr.py | 2 +- .../efficientloftr/modular_efficientloftr.py | 2 +- .../models/eomt/image_processing_pil_eomt.py | 8 ++++++-- .../models/fuyu/image_processing_pil_fuyu.py | 2 +- .../models/glpn/image_processing_pil_glpn.py | 13 +++++++------ .../image_processing_pil_grounding_dino.py | 2 +- .../grounding_dino/modular_grounding_dino.py | 2 +- .../image_processing_pil_kosmos2_5.py | 2 +- .../image_processing_pil_lightglue.py | 2 +- .../models/lightglue/modular_lightglue.py | 2 +- .../image_processing_pil_mask2former.py | 2 +- .../models/mask2former/modular_mask2former.py | 2 +- .../image_processing_pil_maskformer.py | 2 +- .../image_processing_pil_mobilenet_v2.py | 17 ++++++++++------- .../image_processing_pil_mobilevit.py | 16 ++++++++++------ .../image_processing_pil_oneformer.py | 2 +- .../owlv2/image_processing_pil_owlv2.py | 6 +++--- .../models/owlv2/modular_owlv2.py | 2 +- .../owlvit/image_processing_pil_owlvit.py | 15 +++++++-------- .../image_processing_pil_pix2struct.py | 2 +- ...ge_processing_pil_prompt_depth_anything.py | 12 ++++++------ .../rt_detr/image_processing_pil_rt_detr.py | 4 ++-- .../models/rt_detr/modular_rt_detr.py | 2 +- .../models/sam/image_processing_pil_sam.py | 2 +- .../image_processing_pil_segformer.py | 2 +- .../models/segformer/modular_segformer.py | 2 +- .../seggpt/image_processing_pil_seggpt.py | 14 +++++++++----- .../image_processing_pil_superglue.py | 14 ++++++++------ .../image_processing_pil_superpoint.py | 12 ++++++------ .../vitpose/image_processing_pil_vitpose.py | 12 +++++++----- .../yolos/image_processing_pil_yolos.py | 2 +- .../zoedepth/image_processing_pil_zoedepth.py | 2 +- 40 files changed, 137 insertions(+), 112 deletions(-) diff --git a/src/transformers/models/beit/image_processing_beit.py b/src/transformers/models/beit/image_processing_beit.py index 9180d4fc9c4e..0392914ba32f 100644 --- a/src/transformers/models/beit/image_processing_beit.py +++ b/src/transformers/models/beit/image_processing_beit.py @@ -27,7 +27,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available +from ...utils import TensorType, is_torch_available, is_torchvision_available if is_torch_available(): @@ -49,7 +49,7 @@ class BeitImageProcessorKwargs(ImagesKwargs, total=False): do_reduce_labels: bool -@auto_docstring +# @auto_docstring class BeitImageProcessor(TorchvisionBackend): """PIL backend for BEiT with reduce_label support.""" @@ -70,7 +70,7 @@ class BeitImageProcessor(TorchvisionBackend): def __init__(self, **kwargs: Unpack[BeitImageProcessorKwargs]): super().__init__(**kwargs) - @auto_docstring + # @auto_docstring def preprocess( self, images: ImageInput, diff --git a/src/transformers/models/beit/image_processing_pil_beit.py b/src/transformers/models/beit/image_processing_pil_beit.py index 0b0405d64fcd..401344dda1c7 100644 --- a/src/transformers/models/beit/image_processing_pil_beit.py +++ b/src/transformers/models/beit/image_processing_pil_beit.py @@ -13,10 +13,13 @@ # limitations under the License. """Image processor class for BEiT.""" +from typing import TYPE_CHECKING + import numpy as np -import torch -import torch.nn.functional as F -from torchvision.transforms.v2 import functional as tvF + + +if TYPE_CHECKING: + from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature @@ -29,13 +32,12 @@ SizeDict, ) from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, is_torch_available +from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires from .image_processing_beit import BeitImageProcessorKwargs @auto_docstring -@requires(backends=("vision", "torch")) class BeitImageProcessorPil(PilBackend): """PIL backend for BEiT with reduce_label support.""" @@ -56,7 +58,7 @@ class BeitImageProcessorPil(PilBackend): def __init__(self, **kwargs: Unpack[BeitImageProcessorKwargs]): super().__init__(**kwargs) - @auto_docstring + # @auto_docstring def preprocess( self, images: ImageInput, @@ -152,6 +154,7 @@ def _preprocess( return processed_images + @requires(backends=("torch",)) def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] | None = None): """ Converts the output of [`BeitForSemanticSegmentation`] into semantic segmentation maps. @@ -168,8 +171,8 @@ def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. """ - if not is_torch_available(): - raise ImportError("PyTorch is required for post_process_semantic_segmentation") + import torch + import torch.nn.functional as F logits = outputs.logits diff --git a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py index 7f6bddce6dce..94fc944123ba 100644 --- a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py +++ b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py @@ -677,7 +677,7 @@ def _preprocess( ] return encoded_inputs - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_object_detection( self, outputs, threshold: float = 0.5, target_sizes: TensorType | list[tuple] = None, top_k: int = 100 ): @@ -738,7 +738,7 @@ def post_process_object_detection( return results - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple[int, int]] | None = None): """ Converts the output of [`ConditionalDetrForSegmentation`] into semantic segmentation maps. Only supports PyTorch. @@ -787,7 +787,7 @@ def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple[i return semantic_segmentation - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_instance_segmentation( self, outputs, @@ -876,7 +876,7 @@ def post_process_instance_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_panoptic_segmentation( self, outputs, diff --git a/src/transformers/models/conditional_detr/modular_conditional_detr.py b/src/transformers/models/conditional_detr/modular_conditional_detr.py index 5a0956cc85ed..2205b85c5547 100644 --- a/src/transformers/models/conditional_detr/modular_conditional_detr.py +++ b/src/transformers/models/conditional_detr/modular_conditional_detr.py @@ -174,7 +174,7 @@ def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple[i class ConditionalDetrImageProcessorPil(DetrImageProcessorPil): - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_object_detection( self, outputs, threshold: float = 0.5, target_sizes: TensorType | list[tuple] = None, top_k: int = 100 ): @@ -235,7 +235,7 @@ def post_process_object_detection( return results - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple[int, int]] | None = None): """ Converts the output of [`ConditionalDetrForSegmentation`] into semantic segmentation maps. Only supports PyTorch. diff --git a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py index 1c82253d367e..dc38be54c3df 100644 --- a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py +++ b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py @@ -660,7 +660,7 @@ def _preprocess( ] return encoded_inputs - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_object_detection( self, outputs, threshold: float = 0.5, target_sizes: TensorType | list[tuple] = None, top_k: int = 100 ): diff --git a/src/transformers/models/deformable_detr/modular_deformable_detr.py b/src/transformers/models/deformable_detr/modular_deformable_detr.py index 7af635a9ab30..a4a5b4acd95a 100644 --- a/src/transformers/models/deformable_detr/modular_deformable_detr.py +++ b/src/transformers/models/deformable_detr/modular_deformable_detr.py @@ -131,7 +131,7 @@ def post_process_panoptic_segmentation(self): class DeformableDetrImageProcessorPil(DetrImageProcessorPil): - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_object_detection( self, outputs, threshold: float = 0.5, target_sizes: TensorType | list[tuple] = None, top_k: int = 100 ): diff --git a/src/transformers/models/detr/image_processing_pil_detr.py b/src/transformers/models/detr/image_processing_pil_detr.py index 8f4b2f47068b..cbf5a157526e 100644 --- a/src/transformers/models/detr/image_processing_pil_detr.py +++ b/src/transformers/models/detr/image_processing_pil_detr.py @@ -668,7 +668,7 @@ def _preprocess( ] return encoded_inputs - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_object_detection( self, outputs, threshold: float = 0.5, target_sizes: TensorType | list[tuple] | None = None ): @@ -727,7 +727,7 @@ def post_process_object_detection( return results - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple[int, int]] | None = None): """ Converts the output of [`DetrForSegmentation`] into semantic segmentation maps. Only supports PyTorch. @@ -780,7 +780,7 @@ def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple[i return semantic_segmentation - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_instance_segmentation( self, outputs, @@ -869,7 +869,7 @@ def post_process_instance_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_panoptic_segmentation( self, outputs, diff --git a/src/transformers/models/dpt/image_processing_pil_dpt.py b/src/transformers/models/dpt/image_processing_pil_dpt.py index 80cb618370cd..184f152b9be1 100644 --- a/src/transformers/models/dpt/image_processing_pil_dpt.py +++ b/src/transformers/models/dpt/image_processing_pil_dpt.py @@ -30,23 +30,22 @@ SizeDict, ) from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, logging, requires_backends +from ...utils import TensorType, auto_docstring, logging from ...utils.import_utils import requires from .image_processing_dpt import DPTImageProcessorKwargs, get_resize_output_image_size if TYPE_CHECKING: - from ...modeling_outputs import DepthEstimatorOutput + import torch + from torchvision.transforms.v2 import functional as tvF -import torch -from torchvision.transforms.v2 import functional as tvF + from ...modeling_outputs import DepthEstimatorOutput logger = logging.get_logger(__name__) @auto_docstring -@requires(backends=("vision", "torch")) class DPTImageProcessorPil(PilBackend): """PIL backend for DPT with custom resize and pad.""" @@ -205,9 +204,11 @@ def _preprocess( processed_images.append(image) return processed_images + @requires(backends=("torch",)) def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] | None = None): """Converts the output of [`DPTForSemanticSegmentation`] into semantic segmentation maps.""" - requires_backends(self, "torch") + import torch + logits = outputs.logits if target_sizes is not None: if len(logits) != len(target_sizes): @@ -228,11 +229,13 @@ def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] semantic_segmentation = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])] return semantic_segmentation + @requires(backends=("torch",)) def post_process_depth_estimation( self, outputs: "DepthEstimatorOutput", target_sizes: TensorType | list[tuple[int, int]] | None = None ) -> list[dict[str, TensorType]]: """Converts the raw output of [`DepthEstimatorOutput`] into final depth predictions.""" - requires_backends(self, "torch") + import torch + predicted_depth = outputs.predicted_depth if (target_sizes is not None) and (len(predicted_depth) != len(target_sizes)): raise ValueError( diff --git a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py index fe266a8c0a10..90aa9b158663 100644 --- a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py @@ -61,7 +61,7 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: return image -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class EfficientLoFTRImageProcessorPil(PilBackend): valid_kwargs = EfficientLoFTRImageProcessorKwargs resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/efficientloftr/modular_efficientloftr.py b/src/transformers/models/efficientloftr/modular_efficientloftr.py index b19028687f69..f5bcdbdde644 100644 --- a/src/transformers/models/efficientloftr/modular_efficientloftr.py +++ b/src/transformers/models/efficientloftr/modular_efficientloftr.py @@ -68,7 +68,7 @@ def post_process_keypoint_matching( return results -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class EfficientLoFTRImageProcessorPil(SuperGlueImageProcessorPil): def post_process_keypoint_matching( self, diff --git a/src/transformers/models/eomt/image_processing_pil_eomt.py b/src/transformers/models/eomt/image_processing_pil_eomt.py index 8a8862cef95b..df11adf69d64 100644 --- a/src/transformers/models/eomt/image_processing_pil_eomt.py +++ b/src/transformers/models/eomt/image_processing_pil_eomt.py @@ -14,10 +14,14 @@ """Image processor class for EoMT.""" import math +from typing import TYPE_CHECKING import numpy as np import torch -from torchvision.transforms.v2 import functional as tvF + + +if TYPE_CHECKING: + from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature @@ -82,7 +86,7 @@ def convert_segmentation_map_to_binary_masks( @auto_docstring -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class EomtImageProcessorPil(PilBackend): valid_kwargs = EomtImageProcessorKwargs resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/fuyu/image_processing_pil_fuyu.py b/src/transformers/models/fuyu/image_processing_pil_fuyu.py index 58ac2804aeb0..c2d22cea6b9e 100644 --- a/src/transformers/models/fuyu/image_processing_pil_fuyu.py +++ b/src/transformers/models/fuyu/image_processing_pil_fuyu.py @@ -29,7 +29,7 @@ @auto_docstring -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class FuyuImageProcessorPil(PilBackend): do_resize = True size = {"height": 1080, "width": 1920} diff --git a/src/transformers/models/glpn/image_processing_pil_glpn.py b/src/transformers/models/glpn/image_processing_pil_glpn.py index 84468a88ab4d..382418d25bfc 100644 --- a/src/transformers/models/glpn/image_processing_pil_glpn.py +++ b/src/transformers/models/glpn/image_processing_pil_glpn.py @@ -16,26 +16,25 @@ from typing import TYPE_CHECKING import numpy as np -import torch -from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput, PILImageResampling, SizeDict from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, logging, requires_backends +from ...utils import TensorType, auto_docstring, logging from ...utils.import_utils import requires from .image_processing_glpn import GLPNImageProcessorKwargs if TYPE_CHECKING: + from torchvision.transforms.v2 import functional as tvF + from ...modeling_outputs import DepthEstimatorOutput logger = logging.get_logger(__name__) @auto_docstring -@requires(backends=("vision", "torch")) class GLPNImageProcessorPil(PilBackend): """PIL backend for GLPN with size_divisor resize.""" @@ -104,6 +103,7 @@ def _preprocess( processed_images.append(image) return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) + @requires(backends=("torch",)) def post_process_depth_estimation( self, outputs: "DepthEstimatorOutput", target_sizes: TensorType | list[tuple[int, int]] | None = None ) -> list[dict[str, TensorType]]: @@ -111,7 +111,8 @@ def post_process_depth_estimation( Convert raw model outputs to final depth predictions. Only supports PyTorch. """ - requires_backends(self, "torch") + import torch.nn.functional as F + predicted_depth = outputs.predicted_depth if target_sizes is not None and len(predicted_depth) != len(target_sizes): raise ValueError( @@ -122,7 +123,7 @@ def post_process_depth_estimation( for depth, target_size in zip(predicted_depth, target_sizes): if target_size is not None: depth = depth[None, None, ...] - depth = torch.nn.functional.interpolate(depth, size=target_size, mode="bicubic", align_corners=False) + depth = F.interpolate(depth, size=target_size, mode="bicubic", align_corners=False) depth = depth.squeeze(0).squeeze(0) results.append({"predicted_depth": depth}) return results diff --git a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py index e18e0be190a7..da1c57930f59 100644 --- a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py +++ b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py @@ -704,7 +704,7 @@ def _preprocess( ] return encoded_inputs - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_object_detection( self, outputs: "GroundingDinoObjectDetectionOutput", diff --git a/src/transformers/models/grounding_dino/modular_grounding_dino.py b/src/transformers/models/grounding_dino/modular_grounding_dino.py index d8ae692b0890..bd35fd512ffe 100644 --- a/src/transformers/models/grounding_dino/modular_grounding_dino.py +++ b/src/transformers/models/grounding_dino/modular_grounding_dino.py @@ -133,7 +133,7 @@ def post_process_panoptic_segmentation(self): class GroundingDinoImageProcessorPil(DetrImageProcessorPil): - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_object_detection( self, outputs: "GroundingDinoObjectDetectionOutput", diff --git a/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py b/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py index bfa96178a309..4a8d7ad9396c 100644 --- a/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py +++ b/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py @@ -28,7 +28,7 @@ @auto_docstring -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class Kosmos2_5ImageProcessorPil(PilBackend): do_normalize = True do_convert_rgb = True diff --git a/src/transformers/models/lightglue/image_processing_pil_lightglue.py b/src/transformers/models/lightglue/image_processing_pil_lightglue.py index d74caec003ae..2f8546690244 100644 --- a/src/transformers/models/lightglue/image_processing_pil_lightglue.py +++ b/src/transformers/models/lightglue/image_processing_pil_lightglue.py @@ -84,7 +84,7 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: return image -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class LightGlueImageProcessorPil(PilBackend): valid_kwargs = LightGlueImageProcessorKwargs resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/lightglue/modular_lightglue.py b/src/transformers/models/lightglue/modular_lightglue.py index b72d48c2cc87..a94bee279c3f 100644 --- a/src/transformers/models/lightglue/modular_lightglue.py +++ b/src/transformers/models/lightglue/modular_lightglue.py @@ -179,7 +179,7 @@ def post_process_keypoint_matching( return super().post_process_keypoint_matching(outputs, target_sizes, threshold) -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class LightGlueImageProcessorPil(SuperGlueImageProcessorPil): def post_process_keypoint_matching( self, diff --git a/src/transformers/models/mask2former/image_processing_pil_mask2former.py b/src/transformers/models/mask2former/image_processing_pil_mask2former.py index 11114008297c..c1d76b853401 100644 --- a/src/transformers/models/mask2former/image_processing_pil_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_pil_mask2former.py @@ -103,7 +103,7 @@ def convert_segmentation_map_to_binary_masks( return binary_masks.astype(np.float32), labels -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class Mask2FormerImageProcessorPil(PilBackend): valid_kwargs = Mask2FormerImageProcessorKwargs resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/mask2former/modular_mask2former.py b/src/transformers/models/mask2former/modular_mask2former.py index 8885a0fd38a1..089baffe5df7 100644 --- a/src/transformers/models/mask2former/modular_mask2former.py +++ b/src/transformers/models/mask2former/modular_mask2former.py @@ -306,7 +306,7 @@ def post_process_panoptic_segmentation( return results -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class Mask2FormerImageProcessorPil(MaskFormerImageProcessorPil): def post_process_semantic_segmentation( self, outputs, target_sizes: list[tuple[int, int]] | None = None diff --git a/src/transformers/models/maskformer/image_processing_pil_maskformer.py b/src/transformers/models/maskformer/image_processing_pil_maskformer.py index 1c2189a78062..2666e14a7c0d 100644 --- a/src/transformers/models/maskformer/image_processing_pil_maskformer.py +++ b/src/transformers/models/maskformer/image_processing_pil_maskformer.py @@ -87,7 +87,7 @@ def convert_segmentation_map_to_binary_masks( @auto_docstring -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class MaskFormerImageProcessorPil(PilBackend): valid_kwargs = MaskFormerImageProcessorKwargs resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py index 6dd56ce4f950..6b0e5ef6cc3b 100644 --- a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py +++ b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py @@ -13,11 +13,14 @@ # limitations under the License. """Image processor class for MobileNetV2.""" -from typing import Union +from typing import TYPE_CHECKING, Union import numpy as np -import torch -from torchvision.transforms.v2 import functional as tvF + + +if TYPE_CHECKING: + import torch + from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature @@ -30,13 +33,12 @@ SizeDict, ) from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, is_torch_available +from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires from .image_processing_mobilenet_v2 import MobileNetV2ImageProcessorKwargs @auto_docstring -@requires(backends=("vision", "torch")) class MobileNetV2ImageProcessorPil(PilBackend): """PIL backend for MobileNetV2 with reduce_label support.""" @@ -160,12 +162,13 @@ def _preprocess( processed_images.append(image) return processed_images + @requires(backends=("torch",)) def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] | None = None): """ Converts the output of [`MobileNetV2ForSemanticSegmentation`] into semantic segmentation maps. """ - if not is_torch_available(): - raise ImportError("PyTorch is required for post_process_semantic_segmentation") + import torch + logits = outputs.logits if target_sizes is not None: if len(logits) != len(target_sizes): diff --git a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py index be47ec11cccd..fa95d8ac65a1 100644 --- a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py +++ b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py @@ -13,11 +13,14 @@ # limitations under the License. """Image processor class for MobileViT.""" -from typing import Union +from typing import TYPE_CHECKING, Union import numpy as np -import torch -from torchvision.transforms.v2 import functional as tvF + + +if TYPE_CHECKING: + import torch + from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature @@ -31,7 +34,7 @@ SizeDict, ) from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, logging, requires_backends +from ...utils import TensorType, auto_docstring, logging from ...utils.import_utils import requires from .image_processing_mobilevit import MobileVitImageProcessorKwargs @@ -40,7 +43,6 @@ @auto_docstring -@requires(backends=("vision", "torch")) class MobileViTImageProcessorPil(PilBackend): """PIL backend for MobileViT with flip_channel_order and reduce_label support.""" @@ -168,9 +170,11 @@ def _preprocess( processed_images.append(image) return processed_images + @requires(backends=("torch",)) def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] | None = None): """Converts the output of [`MobileViTForSemanticSegmentation`] into semantic segmentation maps.""" - requires_backends(self, "torch") + import torch + logits = outputs.logits if target_sizes is not None: if len(logits) != len(target_sizes): diff --git a/src/transformers/models/oneformer/image_processing_pil_oneformer.py b/src/transformers/models/oneformer/image_processing_pil_oneformer.py index bb93ab247842..e36c930f1be9 100644 --- a/src/transformers/models/oneformer/image_processing_pil_oneformer.py +++ b/src/transformers/models/oneformer/image_processing_pil_oneformer.py @@ -67,7 +67,7 @@ def make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarr @auto_docstring -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class OneFormerImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR image_mean = IMAGENET_DEFAULT_MEAN diff --git a/src/transformers/models/owlv2/image_processing_pil_owlv2.py b/src/transformers/models/owlv2/image_processing_pil_owlv2.py index 1fe7430e1497..7d93785d788d 100644 --- a/src/transformers/models/owlv2/image_processing_pil_owlv2.py +++ b/src/transformers/models/owlv2/image_processing_pil_owlv2.py @@ -153,7 +153,7 @@ def _scale_boxes(boxes, target_sizes): @auto_docstring -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class Owlv2ImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR image_mean = OPENAI_CLIP_MEAN @@ -180,7 +180,7 @@ def __init__(self, **kwargs: Unpack[ImagesKwargs]): super().__init__(**kwargs) - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_object_detection( self, outputs: "Owlv2ObjectDetectionOutput", @@ -235,7 +235,7 @@ def post_process_object_detection( return results - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_image_guided_detection(self, outputs, threshold=0.0, nms_threshold=0.3, target_sizes=None): """ Converts the output of [`Owlv2ForObjectDetection.image_guided_detection`] into the format expected by the COCO diff --git a/src/transformers/models/owlv2/modular_owlv2.py b/src/transformers/models/owlv2/modular_owlv2.py index 8cba8e0ccecb..b79b14d6c843 100644 --- a/src/transformers/models/owlv2/modular_owlv2.py +++ b/src/transformers/models/owlv2/modular_owlv2.py @@ -312,7 +312,7 @@ def _preprocess( @auto_docstring -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class Owlv2ImageProcessorPil(OwlViTImageProcessorPil): resample = PILImageResampling.BILINEAR image_mean = OPENAI_CLIP_MEAN diff --git a/src/transformers/models/owlvit/image_processing_pil_owlvit.py b/src/transformers/models/owlvit/image_processing_pil_owlvit.py index 84d016c22f15..a130516ab5a7 100644 --- a/src/transformers/models/owlvit/image_processing_pil_owlvit.py +++ b/src/transformers/models/owlvit/image_processing_pil_owlvit.py @@ -15,13 +15,11 @@ from typing import TYPE_CHECKING -import torch - from ...image_processing_backends import PilBackend from ...image_transforms import center_to_corners_format from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, PILImageResampling from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, logging, requires_backends +from ...utils import TensorType, auto_docstring, logging from ...utils.import_utils import requires from ..owlvit.image_processing_owlvit import _scale_boxes, box_iou @@ -33,7 +31,6 @@ logger = logging.get_logger(__name__) -@requires(backends=("vision", "torch")) @auto_docstring class OwlViTImageProcessorPil(PilBackend): resample = PILImageResampling.BICUBIC @@ -59,7 +56,7 @@ def __init__(self, **kwargs: Unpack[ImagesKwargs]): super().__init__(**kwargs) - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_object_detection( self, outputs: "OwlViTObjectDetectionOutput", @@ -85,7 +82,8 @@ def post_process_object_detection( - "labels": Indexes of the classes predicted by the model on the image. - "boxes": Image bounding boxes in (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format. """ - requires_backends(self, "torch") + import torch + batch_logits, batch_boxes = outputs.logits, outputs.pred_boxes batch_size = len(batch_logits) @@ -114,7 +112,7 @@ def post_process_object_detection( return results - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_image_guided_detection(self, outputs, threshold=0.0, nms_threshold=0.3, target_sizes=None): """ Converts the output of [`OwlViTForObjectDetection.image_guided_detection`] into the format expected by the COCO @@ -137,7 +135,8 @@ def post_process_image_guided_detection(self, outputs, threshold=0.0, nms_thresh in the batch as predicted by the model. All labels are set to None as `OwlViTForObjectDetection.image_guided_detection` perform one-shot object detection. """ - requires_backends(self, "torch") + import torch + logits, target_boxes = outputs.logits, outputs.target_pred_boxes if target_sizes is not None and len(logits) != len(target_sizes): diff --git a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py index 43c63c313549..54b21d47c3f4 100644 --- a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py +++ b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py @@ -30,7 +30,7 @@ @auto_docstring -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class Pix2StructImageProcessorPil(PilBackend): rescale_factor = None do_normalize = True diff --git a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py index 3d5c5b4d2602..7f92c4fc5ece 100644 --- a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py @@ -32,15 +32,15 @@ get_image_size, ) from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, requires_backends +from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires if TYPE_CHECKING: - from ...modeling_outputs import DepthEstimatorOutput + import torch + from torchvision.transforms.v2 import functional as tvF -import torch -from torchvision.transforms.v2 import functional as tvF + from ...modeling_outputs import DepthEstimatorOutput from .image_processing_prompt_depth_anything import PromptDepthAnythingImageProcessorKwargs @@ -85,7 +85,6 @@ def _get_resize_output_image_size( @auto_docstring -@requires(backends=("vision", "torch")) class PromptDepthAnythingImageProcessorPil(PilBackend): model_input_names = ["pixel_values", "prompt_depth"] @@ -276,6 +275,7 @@ def _preprocess( return processed_images + @requires(backends=("torch",)) def post_process_depth_estimation( self, outputs: "DepthEstimatorOutput", target_sizes: TensorType | list[tuple[int, int]] | None | None = None ) -> list[dict[str, TensorType]]: @@ -294,7 +294,7 @@ def post_process_depth_estimation( `list[dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth predictions. """ - requires_backends(self, "torch") + import torch predicted_depth = outputs.predicted_depth diff --git a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py index 62e12da0bbf9..fd290d22940a 100644 --- a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py @@ -115,7 +115,7 @@ def prepare_coco_detection_annotation_pil( return new_target -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class RTDetrImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR image_mean = IMAGENET_DEFAULT_MEAN @@ -493,7 +493,7 @@ def _preprocess( ] return encoded_inputs - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_object_detection( self, outputs, diff --git a/src/transformers/models/rt_detr/modular_rt_detr.py b/src/transformers/models/rt_detr/modular_rt_detr.py index 03c17977db62..d5783a83f3f9 100644 --- a/src/transformers/models/rt_detr/modular_rt_detr.py +++ b/src/transformers/models/rt_detr/modular_rt_detr.py @@ -427,7 +427,7 @@ def post_process_panoptic_segmentation(self): raise NotImplementedError("Panoptic segmentation post-processing is not implemented for RT-DETR yet.") -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class RTDetrImageProcessorPil(DetrImageProcessorPil): resample = PILImageResampling.BILINEAR image_mean = IMAGENET_DEFAULT_MEAN diff --git a/src/transformers/models/sam/image_processing_pil_sam.py b/src/transformers/models/sam/image_processing_pil_sam.py index 8d15dcd9e309..88c3ff534a9a 100644 --- a/src/transformers/models/sam/image_processing_pil_sam.py +++ b/src/transformers/models/sam/image_processing_pil_sam.py @@ -63,7 +63,7 @@ def get_resize_output_image_size( @auto_docstring -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class SamImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR image_mean = IMAGENET_DEFAULT_MEAN diff --git a/src/transformers/models/segformer/image_processing_pil_segformer.py b/src/transformers/models/segformer/image_processing_pil_segformer.py index bd517cfb9c56..c63ecd697972 100644 --- a/src/transformers/models/segformer/image_processing_pil_segformer.py +++ b/src/transformers/models/segformer/image_processing_pil_segformer.py @@ -44,7 +44,7 @@ import torchvision.transforms.v2.functional as tvF -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("torch", "torchvision")) class SegformerImageProcessorPil(PilBackend): """PIL backend for Segformer with reduce_label support.""" diff --git a/src/transformers/models/segformer/modular_segformer.py b/src/transformers/models/segformer/modular_segformer.py index a8303bd9ec05..1f2e80a7cc00 100644 --- a/src/transformers/models/segformer/modular_segformer.py +++ b/src/transformers/models/segformer/modular_segformer.py @@ -142,7 +142,7 @@ def _preprocess( return processed_images -@requires(backends=("vision", "torch", "torchvision")) +@requires(backends=("torch", "torchvision")) class SegformerImageProcessorPil(BeitImageProcessorPil): resample = PILImageResampling.BILINEAR image_mean = IMAGENET_DEFAULT_MEAN diff --git a/src/transformers/models/seggpt/image_processing_pil_seggpt.py b/src/transformers/models/seggpt/image_processing_pil_seggpt.py index ede14b211d95..141e47e3611a 100644 --- a/src/transformers/models/seggpt/image_processing_pil_seggpt.py +++ b/src/transformers/models/seggpt/image_processing_pil_seggpt.py @@ -13,9 +13,13 @@ # limitations under the License. """Image processor class for SegGPT.""" +from typing import TYPE_CHECKING + import numpy as np -import torch -from torchvision.transforms.v2 import functional as tvF + + +if TYPE_CHECKING: + from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature @@ -28,13 +32,12 @@ SizeDict, ) from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, requires_backends +from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires from .image_processing_seggpt import SegGptImageProcessorKwargs, build_palette @auto_docstring -@requires(backends=("vision", "torch")) class SegGptImageProcessorPil(PilBackend): valid_kwargs = SegGptImageProcessorKwargs @@ -198,6 +201,7 @@ def _preprocess( return processed_images + @requires(backends=("torch",)) def post_process_semantic_segmentation( self, outputs, target_sizes: list[tuple[int, int]] | None = None, num_labels: int | None = None ): @@ -220,7 +224,7 @@ def post_process_semantic_segmentation( `(height, width)`. Each entry corresponds to a semantic class id. """ - requires_backends(self, ["torch"]) + import torch masks = outputs.pred_masks masks = masks[:, :, masks.shape[2] // 2 :, :] diff --git a/src/transformers/models/superglue/image_processing_pil_superglue.py b/src/transformers/models/superglue/image_processing_pil_superglue.py index ab0f2b37814c..176297a1b5f1 100644 --- a/src/transformers/models/superglue/image_processing_pil_superglue.py +++ b/src/transformers/models/superglue/image_processing_pil_superglue.py @@ -27,15 +27,15 @@ if TYPE_CHECKING: + import torch + from torchvision.transforms.v2 import functional as tvF + from .modeling_superglue import SuperGlueKeypointMatchingOutput if is_vision_available(): import PIL from PIL import Image, ImageDraw -import torch -from torchvision.transforms.v2 import functional as tvF - def is_grayscale(image: np.ndarray): if image.shape[0] == 1: @@ -81,7 +81,6 @@ class SuperGlueImageProcessorKwargs(ImagesKwargs, total=False): @auto_docstring -@requires(backends=("vision", "torch")) class SuperGlueImageProcessorPil(PilBackend): valid_kwargs = SuperGlueImageProcessorKwargs resample = PILImageResampling.BILINEAR @@ -137,12 +136,13 @@ def _preprocess( return BatchFeature(data=data, tensor_type=return_tensors) + @requires(backends=("torch",)) def post_process_keypoint_matching( self, outputs: "SuperGlueKeypointMatchingOutput", target_sizes: TensorType | list[tuple], threshold: float = 0.0, - ) -> list[dict[str, torch.Tensor]]: + ) -> list[dict[str, "torch.Tensor"]]: """ Converts the raw output of [`SuperGlueKeypointMatchingOutput`] into lists of keypoints, scores and descriptors with coordinates absolute to the original image sizes. @@ -159,6 +159,8 @@ def post_process_keypoint_matching( `list[Dict]`: A list of dictionaries, each dictionary containing the keypoints in the first and second image of the pair, the matching scores and the matching indices. """ + import torch + if outputs.mask.shape[0] != len(target_sizes): raise ValueError("Make sure that you pass in as many target sizes as the batch dimension of the mask") if not all(len(target_size) == 2 for target_size in target_sizes): @@ -206,7 +208,7 @@ def post_process_keypoint_matching( return results def visualize_keypoint_matching( - self, images: ImageInput, keypoint_matching_output: list[dict[str, torch.Tensor]] + self, images: ImageInput, keypoint_matching_output: list[dict[str, "torch.Tensor"]] ) -> list["Image.Image"]: """ Plots the image pairs side by side with the detected keypoints as well as the matching between them. diff --git a/src/transformers/models/superpoint/image_processing_pil_superpoint.py b/src/transformers/models/superpoint/image_processing_pil_superpoint.py index 62ad6272c1cd..f999ecb86e02 100644 --- a/src/transformers/models/superpoint/image_processing_pil_superpoint.py +++ b/src/transformers/models/superpoint/image_processing_pil_superpoint.py @@ -21,16 +21,16 @@ from ...image_processing_utils import BatchFeature from ...image_utils import PILImageResampling, SizeDict from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, requires_backends +from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires from .image_processing_superpoint import SuperPointImageProcessorKwargs if TYPE_CHECKING: - from .modeling_superpoint import SuperPointKeypointDescriptionOutput + import torch + from torchvision.transforms.v2 import functional as tvF -import torch -from torchvision.transforms.v2 import functional as tvF + from .modeling_superpoint import SuperPointKeypointDescriptionOutput def is_grayscale(image: np.ndarray) -> bool: @@ -61,7 +61,6 @@ def convert_to_grayscale(image: np.ndarray) -> np.ndarray: @auto_docstring -@requires(backends=("vision", "torch")) class SuperPointImageProcessorPil(PilBackend): valid_kwargs = SuperPointImageProcessorKwargs resample = PILImageResampling.BILINEAR @@ -103,6 +102,7 @@ def _preprocess( return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) + @requires(backends=("torch",)) def post_process_keypoint_detection( self, outputs: "SuperPointKeypointDescriptionOutput", target_sizes: TensorType | list[tuple] ) -> list[dict[str, "torch.Tensor"]]: @@ -121,7 +121,7 @@ def post_process_keypoint_detection( `list[Dict]`: A list of dictionaries, each dictionary containing the keypoints in absolute format according to target_sizes, scores and descriptors for an image in the batch as predicted by the model. """ - requires_backends(self, "torch") + import torch if len(outputs.mask) != len(target_sizes): raise ValueError("Make sure that you pass in as many target sizes as the batch dimension of the mask") diff --git a/src/transformers/models/vitpose/image_processing_pil_vitpose.py b/src/transformers/models/vitpose/image_processing_pil_vitpose.py index fb246f8dcc3d..4490b53c38fc 100644 --- a/src/transformers/models/vitpose/image_processing_pil_vitpose.py +++ b/src/transformers/models/vitpose/image_processing_pil_vitpose.py @@ -17,8 +17,6 @@ from typing import TYPE_CHECKING, Union import numpy as np -import torch -from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature @@ -31,7 +29,7 @@ SizeDict, ) from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, is_scipy_available, logging, requires_backends +from ...utils import TensorType, auto_docstring, is_scipy_available, logging from ...utils.import_utils import requires from .image_processing_vitpose import ( VitPoseImageProcessorKwargs, @@ -49,13 +47,15 @@ pass if TYPE_CHECKING: + import torch + from torchvision.transforms.v2 import functional as tvF + from .modeling_vitpose import VitPoseEstimatorOutput logger = logging.get_logger(__name__) @auto_docstring -@requires(backends=("torch", "torchvision")) class VitPoseImageProcessorPil(PilBackend): """PIL backend for VitPose with affine transform.""" @@ -170,6 +170,7 @@ def keypoints_from_heatmaps(self, heatmaps: np.ndarray, center: np.ndarray, scal preds[i] = transform_preds(preds[i], center=center[i], scale=scale[i], output_size=[height, width]) return preds, scores + @requires(backends=("torch",)) def post_process_pose_estimation( self, outputs: "VitPoseEstimatorOutput", @@ -198,7 +199,8 @@ def post_process_pose_estimation( `list[list[Dict]]`: A list of dictionaries, each dictionary containing the keypoints and boxes for an image in the batch as predicted by the model. """ - requires_backends(self, "torch") + import torch + batch_size, num_keypoints, _, _ = outputs.heatmaps.shape if target_sizes is not None and batch_size != len(target_sizes): raise ValueError("Make sure that you pass in as many target sizes as the batch dimension of the logits") diff --git a/src/transformers/models/yolos/image_processing_pil_yolos.py b/src/transformers/models/yolos/image_processing_pil_yolos.py index 0962d02caaa9..9616ca3a85b9 100644 --- a/src/transformers/models/yolos/image_processing_pil_yolos.py +++ b/src/transformers/models/yolos/image_processing_pil_yolos.py @@ -694,7 +694,7 @@ def _preprocess( ] return encoded_inputs - @requires(backends=("vision", "torch")) + @requires(backends=("torch",)) def post_process_object_detection( self, outputs, threshold: float = 0.5, target_sizes: TensorType | list[tuple] = None ): diff --git a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py index 1d017046079b..d36e2afb6e20 100644 --- a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py +++ b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py @@ -48,7 +48,7 @@ @auto_docstring -@requires(backends=("vision", "torch")) +@requires(backends=("torch",)) class ZoeDepthImageProcessorPil(PilBackend): valid_kwargs = ZoeDepthImageProcessorKwargs do_pad = True From be82ad82ba663a1a03688e3a77e591d6cd2d87bf Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 12:16:40 +0100 Subject: [PATCH 04/30] remove torch when its not necessary --- src/transformers/models/beit/image_processing_pil_beit.py | 2 +- src/transformers/models/dpt/image_processing_pil_dpt.py | 6 +++--- .../efficientloftr/image_processing_pil_efficientloftr.py | 2 +- src/transformers/models/eomt/image_processing_pil_eomt.py | 2 +- .../ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py | 2 +- src/transformers/models/fuyu/image_processing_pil_fuyu.py | 4 ++-- src/transformers/models/glpn/image_processing_pil_glpn.py | 4 ++-- .../models/lightglue/image_processing_pil_lightglue.py | 2 +- .../models/mask2former/image_processing_pil_mask2former.py | 4 ++-- .../models/maskformer/image_processing_pil_maskformer.py | 4 ++-- .../mobilenet_v2/image_processing_pil_mobilenet_v2.py | 4 ++-- .../models/mobilevit/image_processing_pil_mobilevit.py | 4 ++-- .../models/oneformer/image_processing_pil_oneformer.py | 2 +- src/transformers/models/owlv2/image_processing_pil_owlv2.py | 2 +- .../paddleocr_vl/image_processing_pil_paddleocr_vl.py | 2 +- .../image_processing_pil_prompt_depth_anything.py | 6 +++--- .../models/rt_detr/image_processing_pil_rt_detr.py | 2 +- src/transformers/models/sam/image_processing_pil_sam.py | 6 +++--- .../models/segformer/image_processing_pil_segformer.py | 2 +- .../models/seggpt/image_processing_pil_seggpt.py | 2 +- .../models/superglue/image_processing_pil_superglue.py | 2 +- .../models/superpoint/image_processing_pil_superpoint.py | 2 +- src/transformers/models/tvp/image_processing_pil_tvp.py | 2 +- .../video_llama_3/image_processing_pil_video_llama_3.py | 2 +- .../models/vitmatte/image_processing_pil_vitmatte.py | 2 +- .../models/vitpose/image_processing_pil_vitpose.py | 4 ++-- .../models/zoedepth/image_processing_pil_zoedepth.py | 2 +- 27 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/transformers/models/beit/image_processing_pil_beit.py b/src/transformers/models/beit/image_processing_pil_beit.py index 401344dda1c7..708333e578bb 100644 --- a/src/transformers/models/beit/image_processing_pil_beit.py +++ b/src/transformers/models/beit/image_processing_pil_beit.py @@ -126,7 +126,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/dpt/image_processing_pil_dpt.py b/src/transformers/models/dpt/image_processing_pil_dpt.py index 184f152b9be1..14e7b44978b3 100644 --- a/src/transformers/models/dpt/image_processing_pil_dpt.py +++ b/src/transformers/models/dpt/image_processing_pil_dpt.py @@ -87,7 +87,7 @@ def _preprocess_image_like_inputs( do_convert_rgb: bool, input_data_format: ChannelDimension, return_tensors: str | TensorType | None, - device: Union[str, "torch.device"] | None = None, + device: str | None = None, **kwargs, ) -> BatchFeature: """Handle extra inputs beyond images.""" @@ -132,7 +132,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, ensure_multiple_of: int = 1, keep_aspect_ratio: bool = False, **kwargs, @@ -173,7 +173,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py index 90aa9b158663..6d48d89bdb55 100644 --- a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py @@ -90,7 +90,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool, rescale_factor: float, return_tensors: str | TensorType | None, diff --git a/src/transformers/models/eomt/image_processing_pil_eomt.py b/src/transformers/models/eomt/image_processing_pil_eomt.py index df11adf69d64..89de193098c7 100644 --- a/src/transformers/models/eomt/image_processing_pil_eomt.py +++ b/src/transformers/models/eomt/image_processing_pil_eomt.py @@ -250,7 +250,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py index 1bbff5d029c9..93c20ad57f89 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py @@ -82,7 +82,7 @@ def _preprocess( images: list["torch.Tensor"], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/fuyu/image_processing_pil_fuyu.py b/src/transformers/models/fuyu/image_processing_pil_fuyu.py index c2d22cea6b9e..7eeec9da2010 100644 --- a/src/transformers/models/fuyu/image_processing_pil_fuyu.py +++ b/src/transformers/models/fuyu/image_processing_pil_fuyu.py @@ -63,7 +63,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None" = None, + resample: PILImageResampling | int | None = None, **kwargs, ) -> np.ndarray: """ @@ -98,7 +98,7 @@ def _preprocess( images: list[list[np.ndarray]], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/glpn/image_processing_pil_glpn.py b/src/transformers/models/glpn/image_processing_pil_glpn.py index 382418d25bfc..062a5f42f262 100644 --- a/src/transformers/models/glpn/image_processing_pil_glpn.py +++ b/src/transformers/models/glpn/image_processing_pil_glpn.py @@ -62,7 +62,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, size_divisor: int = 32, **kwargs, ) -> np.ndarray: @@ -77,7 +77,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/lightglue/image_processing_pil_lightglue.py b/src/transformers/models/lightglue/image_processing_pil_lightglue.py index 2f8546690244..63d963d79411 100644 --- a/src/transformers/models/lightglue/image_processing_pil_lightglue.py +++ b/src/transformers/models/lightglue/image_processing_pil_lightglue.py @@ -113,7 +113,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool, rescale_factor: float, return_tensors: str | TensorType | None, diff --git a/src/transformers/models/mask2former/image_processing_pil_mask2former.py b/src/transformers/models/mask2former/image_processing_pil_mask2former.py index c1d76b853401..a78182a709d1 100644 --- a/src/transformers/models/mask2former/image_processing_pil_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_pil_mask2former.py @@ -159,7 +159,7 @@ def resize( image: np.ndarray, size: SizeDict, size_divisor: int = 0, - resample: "PILImageResampling | tvF.InterpolationMode | int | None" = None, + resample: PILImageResampling | int | None = None, **kwargs, ) -> np.ndarray: """ @@ -325,7 +325,7 @@ def _preprocess( size: SizeDict | None, pad_size: SizeDict | None, size_divisor: int | None, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool | None, rescale_factor: float | None, do_normalize: bool | None, diff --git a/src/transformers/models/maskformer/image_processing_pil_maskformer.py b/src/transformers/models/maskformer/image_processing_pil_maskformer.py index 2666e14a7c0d..3bd2237a0539 100644 --- a/src/transformers/models/maskformer/image_processing_pil_maskformer.py +++ b/src/transformers/models/maskformer/image_processing_pil_maskformer.py @@ -143,7 +143,7 @@ def resize( image: np.ndarray, size: SizeDict, size_divisor: int = 0, - resample: "PILImageResampling | tvF.InterpolationMode | int | None" = None, + resample: PILImageResampling | int | None = None, **kwargs, ) -> np.ndarray: """ @@ -309,7 +309,7 @@ def _preprocess( size: SizeDict | None, pad_size: SizeDict | None, size_divisor: int | None, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool | None, rescale_factor: float | None, do_normalize: bool | None, diff --git a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py index 6b0e5ef6cc3b..7ae1c7cca0a5 100644 --- a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py +++ b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py @@ -79,7 +79,7 @@ def _preprocess_image_like_inputs( do_convert_rgb: bool, input_data_format: ChannelDimension, return_tensors: str | TensorType | None, - device: Union[str, "torch.device"] | None = None, + device: str | None = None, **kwargs, ) -> BatchFeature: """Handle extra inputs beyond images.""" @@ -135,7 +135,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py index fa95d8ac65a1..ac4499f1c70b 100644 --- a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py +++ b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py @@ -85,7 +85,7 @@ def _preprocess_image_like_inputs( do_convert_rgb: bool, input_data_format: ChannelDimension, return_tensors: str | TensorType | None, - device: Union[str, "torch.device"] | None = None, + device: str | None = None, **kwargs, ) -> BatchFeature: """Handle extra inputs beyond images.""" @@ -145,7 +145,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/oneformer/image_processing_pil_oneformer.py b/src/transformers/models/oneformer/image_processing_pil_oneformer.py index e36c930f1be9..1b879385bac0 100644 --- a/src/transformers/models/oneformer/image_processing_pil_oneformer.py +++ b/src/transformers/models/oneformer/image_processing_pil_oneformer.py @@ -150,7 +150,7 @@ def _preprocess( instance_id_to_semantic_id: list[dict[int, int]] | dict[int, int] | None, do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/owlv2/image_processing_pil_owlv2.py b/src/transformers/models/owlv2/image_processing_pil_owlv2.py index 7d93785d788d..bc02c8aaec10 100644 --- a/src/transformers/models/owlv2/image_processing_pil_owlv2.py +++ b/src/transformers/models/owlv2/image_processing_pil_owlv2.py @@ -391,7 +391,7 @@ def _preprocess( images: list["torch.Tensor"], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_pad: bool, do_rescale: bool, rescale_factor: float, diff --git a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py index 20e0887400e1..b782a3cfb190 100644 --- a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py @@ -101,7 +101,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py index 7f92c4fc5ece..f595ee60d1df 100644 --- a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py @@ -124,7 +124,7 @@ def resize_with_aspect_ratio( size: SizeDict, keep_aspect_ratio: bool = False, ensure_multiple_of: int = 1, - resample: "PILImageResampling | tvF.InterpolationMode | int | None" = None, + resample: PILImageResampling | int | None = None, ) -> np.ndarray: """ Resize an image to target size while optionally maintaining aspect ratio and ensuring dimensions are multiples. @@ -180,7 +180,7 @@ def _preprocess_image_like_inputs( prompt_depth: ImageInput | None, do_convert_rgb: bool, input_data_format: ChannelDimension, - device: Union[str, "torch.device"] | None = None, + device: str | None = None, return_tensors: str | TensorType | None = None, prompt_scale_to_meter: float | None = None, **kwargs, @@ -239,7 +239,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py index fd290d22940a..d5ad289b06c5 100644 --- a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py @@ -392,7 +392,7 @@ def _preprocess( masks_path: str | pathlib.Path | None, do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/sam/image_processing_pil_sam.py b/src/transformers/models/sam/image_processing_pil_sam.py index 88c3ff534a9a..7425806334bb 100644 --- a/src/transformers/models/sam/image_processing_pil_sam.py +++ b/src/transformers/models/sam/image_processing_pil_sam.py @@ -131,7 +131,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None" = None, + resample: PILImageResampling | int | None = None, **kwargs, ) -> np.ndarray: """ @@ -165,7 +165,7 @@ def _preprocess_image_like_inputs( do_convert_rgb: bool, input_data_format: ChannelDimension, return_tensors: str | TensorType | None, - device: Union[str, "torch.device"] | None = None, + device: str | None = None, **kwargs, ) -> BatchFeature: """ @@ -213,7 +213,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/segformer/image_processing_pil_segformer.py b/src/transformers/models/segformer/image_processing_pil_segformer.py index c63ecd697972..f7774fd8fb9c 100644 --- a/src/transformers/models/segformer/image_processing_pil_segformer.py +++ b/src/transformers/models/segformer/image_processing_pil_segformer.py @@ -139,7 +139,7 @@ def _preprocess( self, images: list["np.ndarray"], do_reduce_labels: bool, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_resize: bool, do_rescale: bool, do_normalize: bool, diff --git a/src/transformers/models/seggpt/image_processing_pil_seggpt.py b/src/transformers/models/seggpt/image_processing_pil_seggpt.py index 141e47e3611a..e12b9a77ef8c 100644 --- a/src/transformers/models/seggpt/image_processing_pil_seggpt.py +++ b/src/transformers/models/seggpt/image_processing_pil_seggpt.py @@ -181,7 +181,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/superglue/image_processing_pil_superglue.py b/src/transformers/models/superglue/image_processing_pil_superglue.py index 176297a1b5f1..057a26403ff4 100644 --- a/src/transformers/models/superglue/image_processing_pil_superglue.py +++ b/src/transformers/models/superglue/image_processing_pil_superglue.py @@ -109,7 +109,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool, rescale_factor: float, return_tensors: str | TensorType | None, diff --git a/src/transformers/models/superpoint/image_processing_pil_superpoint.py b/src/transformers/models/superpoint/image_processing_pil_superpoint.py index f999ecb86e02..85501dc0a09b 100644 --- a/src/transformers/models/superpoint/image_processing_pil_superpoint.py +++ b/src/transformers/models/superpoint/image_processing_pil_superpoint.py @@ -80,7 +80,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool, rescale_factor: float, return_tensors: str | TensorType | None, diff --git a/src/transformers/models/tvp/image_processing_pil_tvp.py b/src/transformers/models/tvp/image_processing_pil_tvp.py index 48796df9b4ae..6a1c9eab8d9f 100644 --- a/src/transformers/models/tvp/image_processing_pil_tvp.py +++ b/src/transformers/models/tvp/image_processing_pil_tvp.py @@ -189,7 +189,7 @@ def _preprocess( images: list[list[np.ndarray]], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py index 90fbf0efbe86..5b43c6135dbe 100644 --- a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py +++ b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py @@ -99,7 +99,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py index b83a773b5ac5..fe11dc5080ed 100644 --- a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py +++ b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py @@ -102,7 +102,7 @@ def _preprocess_image_like_inputs( trimaps: ImageInput, do_convert_rgb: bool, input_data_format: ChannelDimension, - device: Union[str, "torch.device"] | None = None, + device: str | None = None, **kwargs: Unpack[VitMatteImageProcessorKwargs], ) -> BatchFeature: """ diff --git a/src/transformers/models/vitpose/image_processing_pil_vitpose.py b/src/transformers/models/vitpose/image_processing_pil_vitpose.py index 4490b53c38fc..ed5287be2ce2 100644 --- a/src/transformers/models/vitpose/image_processing_pil_vitpose.py +++ b/src/transformers/models/vitpose/image_processing_pil_vitpose.py @@ -93,7 +93,7 @@ def _preprocess_image_like_inputs( boxes: list[list[list[float]]] | np.ndarray | None, do_convert_rgb: bool, input_data_format: ChannelDimension, - device: Union[str, "torch.device"] | None = None, + device: str | None = None, **kwargs, ) -> BatchFeature: """Handle extra inputs beyond images.""" @@ -124,7 +124,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py index d36e2afb6e20..305b39a9f08e 100644 --- a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py +++ b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py @@ -146,7 +146,7 @@ def _preprocess( size: SizeDict, keep_aspect_ratio: bool | None, ensure_multiple_of: int | None, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: PILImageResampling | int | None, do_pad: bool, do_rescale: bool, rescale_factor: float | None, From ed3111a6dbab616b181d3b17349b21c36679fde4 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 12:19:24 +0100 Subject: [PATCH 05/30] remove if typechecking --- src/transformers/models/beit/image_processing_pil_beit.py | 5 ----- src/transformers/models/dpt/image_processing_pil_dpt.py | 4 +--- .../efficientloftr/image_processing_pil_efficientloftr.py | 4 ---- src/transformers/models/eomt/image_processing_pil_eomt.py | 5 ----- .../ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py | 2 +- src/transformers/models/fuyu/image_processing_pil_fuyu.py | 1 - src/transformers/models/glpn/image_processing_pil_glpn.py | 2 -- .../models/lightglue/image_processing_pil_lightglue.py | 1 - .../models/mask2former/image_processing_pil_mask2former.py | 2 +- .../models/maskformer/image_processing_pil_maskformer.py | 1 - .../models/mobilenet_v2/image_processing_pil_mobilenet_v2.py | 5 ++--- .../models/mobilevit/image_processing_pil_mobilevit.py | 5 ++--- .../models/oneformer/image_processing_pil_oneformer.py | 1 - src/transformers/models/owlv2/image_processing_pil_owlv2.py | 2 +- .../models/paddleocr_vl/image_processing_pil_paddleocr_vl.py | 1 - .../image_processing_pil_prompt_depth_anything.py | 4 +--- .../models/rt_detr/image_processing_pil_rt_detr.py | 2 +- src/transformers/models/sam/image_processing_pil_sam.py | 3 +-- .../models/seggpt/image_processing_pil_seggpt.py | 5 ----- .../models/superglue/image_processing_pil_superglue.py | 1 - .../models/superpoint/image_processing_pil_superpoint.py | 1 - src/transformers/models/tvp/image_processing_pil_tvp.py | 2 +- .../video_llama_3/image_processing_pil_video_llama_3.py | 1 - .../models/vitmatte/image_processing_pil_vitmatte.py | 3 +-- .../models/vitpose/image_processing_pil_vitpose.py | 4 +--- 25 files changed, 14 insertions(+), 53 deletions(-) diff --git a/src/transformers/models/beit/image_processing_pil_beit.py b/src/transformers/models/beit/image_processing_pil_beit.py index 708333e578bb..32ad8d90d34d 100644 --- a/src/transformers/models/beit/image_processing_pil_beit.py +++ b/src/transformers/models/beit/image_processing_pil_beit.py @@ -13,14 +13,9 @@ # limitations under the License. """Image processor class for BEiT.""" -from typing import TYPE_CHECKING import numpy as np - -if TYPE_CHECKING: - from torchvision.transforms.v2 import functional as tvF - from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import ( diff --git a/src/transformers/models/dpt/image_processing_pil_dpt.py b/src/transformers/models/dpt/image_processing_pil_dpt.py index 14e7b44978b3..bed972553ffb 100644 --- a/src/transformers/models/dpt/image_processing_pil_dpt.py +++ b/src/transformers/models/dpt/image_processing_pil_dpt.py @@ -14,7 +14,7 @@ """Image processor class for DPT.""" import math -from typing import TYPE_CHECKING, Union +from typing import TYPE_CHECKING import numpy as np @@ -36,8 +36,6 @@ if TYPE_CHECKING: - import torch - from torchvision.transforms.v2 import functional as tvF from ...modeling_outputs import DepthEstimatorOutput diff --git a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py index 6d48d89bdb55..6b89bfd11c21 100644 --- a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py @@ -23,10 +23,6 @@ from PIL import Image, ImageDraw if is_torch_available(): import torch -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - def is_grayscale(image: np.ndarray): if image.shape[0] == 1: return True diff --git a/src/transformers/models/eomt/image_processing_pil_eomt.py b/src/transformers/models/eomt/image_processing_pil_eomt.py index 89de193098c7..0bc561ce3327 100644 --- a/src/transformers/models/eomt/image_processing_pil_eomt.py +++ b/src/transformers/models/eomt/image_processing_pil_eomt.py @@ -14,15 +14,10 @@ """Image processor class for EoMT.""" import math -from typing import TYPE_CHECKING import numpy as np import torch - -if TYPE_CHECKING: - from torchvision.transforms.v2 import functional as tvF - from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_transforms import PaddingMode, get_size_with_aspect_ratio diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py index 93c20ad57f89..b4421d72d7b3 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py @@ -31,7 +31,7 @@ if is_torch_available(): import torch if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF + pass logger = logging.get_logger(__name__) diff --git a/src/transformers/models/fuyu/image_processing_pil_fuyu.py b/src/transformers/models/fuyu/image_processing_pil_fuyu.py index 7eeec9da2010..e76c40ed518f 100644 --- a/src/transformers/models/fuyu/image_processing_pil_fuyu.py +++ b/src/transformers/models/fuyu/image_processing_pil_fuyu.py @@ -17,7 +17,6 @@ import numpy as np import torch -from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import PilBackend from ...image_processing_utils import get_size_dict diff --git a/src/transformers/models/glpn/image_processing_pil_glpn.py b/src/transformers/models/glpn/image_processing_pil_glpn.py index 062a5f42f262..6a70a1cd926e 100644 --- a/src/transformers/models/glpn/image_processing_pil_glpn.py +++ b/src/transformers/models/glpn/image_processing_pil_glpn.py @@ -27,8 +27,6 @@ if TYPE_CHECKING: - from torchvision.transforms.v2 import functional as tvF - from ...modeling_outputs import DepthEstimatorOutput logger = logging.get_logger(__name__) diff --git a/src/transformers/models/lightglue/image_processing_pil_lightglue.py b/src/transformers/models/lightglue/image_processing_pil_lightglue.py index 63d963d79411..4c1d59aad049 100644 --- a/src/transformers/models/lightglue/image_processing_pil_lightglue.py +++ b/src/transformers/models/lightglue/image_processing_pil_lightglue.py @@ -47,7 +47,6 @@ if is_torch_available(): import torch if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF def is_grayscale(image: np.ndarray): diff --git a/src/transformers/models/mask2former/image_processing_pil_mask2former.py b/src/transformers/models/mask2former/image_processing_pil_mask2former.py index a78182a709d1..662f7b40c8c9 100644 --- a/src/transformers/models/mask2former/image_processing_pil_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_pil_mask2former.py @@ -61,7 +61,7 @@ import torch from torch import nn if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF + pass logger = logging.get_logger(__name__) diff --git a/src/transformers/models/maskformer/image_processing_pil_maskformer.py b/src/transformers/models/maskformer/image_processing_pil_maskformer.py index 3bd2237a0539..cd06d34c8832 100644 --- a/src/transformers/models/maskformer/image_processing_pil_maskformer.py +++ b/src/transformers/models/maskformer/image_processing_pil_maskformer.py @@ -19,7 +19,6 @@ import numpy as np import torch from torch import nn -from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature, get_size_dict diff --git a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py index 7ae1c7cca0a5..fc28202598ff 100644 --- a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py +++ b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py @@ -13,14 +13,13 @@ # limitations under the License. """Image processor class for MobileNetV2.""" -from typing import TYPE_CHECKING, Union +from typing import TYPE_CHECKING import numpy as np if TYPE_CHECKING: - import torch - from torchvision.transforms.v2 import functional as tvF + pass from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature diff --git a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py index ac4499f1c70b..fc5ee2839508 100644 --- a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py +++ b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py @@ -13,14 +13,13 @@ # limitations under the License. """Image processor class for MobileViT.""" -from typing import TYPE_CHECKING, Union +from typing import TYPE_CHECKING import numpy as np if TYPE_CHECKING: - import torch - from torchvision.transforms.v2 import functional as tvF + pass from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature diff --git a/src/transformers/models/oneformer/image_processing_pil_oneformer.py b/src/transformers/models/oneformer/image_processing_pil_oneformer.py index 1b879385bac0..63ce0571a6c0 100644 --- a/src/transformers/models/oneformer/image_processing_pil_oneformer.py +++ b/src/transformers/models/oneformer/image_processing_pil_oneformer.py @@ -16,7 +16,6 @@ import numpy as np import torch from torch import nn -from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature diff --git a/src/transformers/models/owlv2/image_processing_pil_owlv2.py b/src/transformers/models/owlv2/image_processing_pil_owlv2.py index bc02c8aaec10..a2918840d288 100644 --- a/src/transformers/models/owlv2/image_processing_pil_owlv2.py +++ b/src/transformers/models/owlv2/image_processing_pil_owlv2.py @@ -49,7 +49,7 @@ if is_torch_available(): import torch if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF + pass def _preprocess_resize_output_shape(image, output_shape): diff --git a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py index b782a3cfb190..51ab51dbb536 100644 --- a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py @@ -36,7 +36,6 @@ if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF @auto_docstring diff --git a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py index f595ee60d1df..5ea50c33fe22 100644 --- a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py @@ -14,7 +14,7 @@ """Image processor class for PromptDepthAnything.""" import math -from typing import TYPE_CHECKING, Union +from typing import TYPE_CHECKING import numpy as np @@ -37,8 +37,6 @@ if TYPE_CHECKING: - import torch - from torchvision.transforms.v2 import functional as tvF from ...modeling_outputs import DepthEstimatorOutput diff --git a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py index d5ad289b06c5..8ea4102cd926 100644 --- a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py @@ -55,7 +55,7 @@ if is_torch_available(): import torch if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF + pass SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) diff --git a/src/transformers/models/sam/image_processing_pil_sam.py b/src/transformers/models/sam/image_processing_pil_sam.py index 7425806334bb..69141339556f 100644 --- a/src/transformers/models/sam/image_processing_pil_sam.py +++ b/src/transformers/models/sam/image_processing_pil_sam.py @@ -17,12 +17,11 @@ from collections.abc import Iterable from copy import deepcopy from itertools import product -from typing import Any, Optional, Union +from typing import Any, Optional import numpy as np import torch import torch.nn.functional as F -from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature, get_size_dict diff --git a/src/transformers/models/seggpt/image_processing_pil_seggpt.py b/src/transformers/models/seggpt/image_processing_pil_seggpt.py index e12b9a77ef8c..c32e2e469282 100644 --- a/src/transformers/models/seggpt/image_processing_pil_seggpt.py +++ b/src/transformers/models/seggpt/image_processing_pil_seggpt.py @@ -13,14 +13,9 @@ # limitations under the License. """Image processor class for SegGPT.""" -from typing import TYPE_CHECKING import numpy as np - -if TYPE_CHECKING: - from torchvision.transforms.v2 import functional as tvF - from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import ( diff --git a/src/transformers/models/superglue/image_processing_pil_superglue.py b/src/transformers/models/superglue/image_processing_pil_superglue.py index 057a26403ff4..e7e4761c6814 100644 --- a/src/transformers/models/superglue/image_processing_pil_superglue.py +++ b/src/transformers/models/superglue/image_processing_pil_superglue.py @@ -28,7 +28,6 @@ if TYPE_CHECKING: import torch - from torchvision.transforms.v2 import functional as tvF from .modeling_superglue import SuperGlueKeypointMatchingOutput diff --git a/src/transformers/models/superpoint/image_processing_pil_superpoint.py b/src/transformers/models/superpoint/image_processing_pil_superpoint.py index 85501dc0a09b..6dc427aa31e6 100644 --- a/src/transformers/models/superpoint/image_processing_pil_superpoint.py +++ b/src/transformers/models/superpoint/image_processing_pil_superpoint.py @@ -28,7 +28,6 @@ if TYPE_CHECKING: import torch - from torchvision.transforms.v2 import functional as tvF from .modeling_superpoint import SuperPointKeypointDescriptionOutput diff --git a/src/transformers/models/tvp/image_processing_pil_tvp.py b/src/transformers/models/tvp/image_processing_pil_tvp.py index 6a1c9eab8d9f..b85b85db4fdd 100644 --- a/src/transformers/models/tvp/image_processing_pil_tvp.py +++ b/src/transformers/models/tvp/image_processing_pil_tvp.py @@ -35,7 +35,7 @@ if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF + pass def get_resize_output_image_size( diff --git a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py index 5b43c6135dbe..344e24b560e9 100644 --- a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py +++ b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py @@ -30,7 +30,6 @@ if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF @auto_docstring diff --git a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py index fe11dc5080ed..59464395bedc 100644 --- a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py +++ b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py @@ -13,7 +13,6 @@ # limitations under the License. """Image processor class for ViTMatte.""" -from typing import Union import numpy as np @@ -34,7 +33,7 @@ if is_torch_available(): - import torch + pass @auto_docstring diff --git a/src/transformers/models/vitpose/image_processing_pil_vitpose.py b/src/transformers/models/vitpose/image_processing_pil_vitpose.py index ed5287be2ce2..010bbdf30106 100644 --- a/src/transformers/models/vitpose/image_processing_pil_vitpose.py +++ b/src/transformers/models/vitpose/image_processing_pil_vitpose.py @@ -14,7 +14,7 @@ """Image processor class for VitPose.""" import itertools -from typing import TYPE_CHECKING, Union +from typing import TYPE_CHECKING import numpy as np @@ -47,8 +47,6 @@ pass if TYPE_CHECKING: - import torch - from torchvision.transforms.v2 import functional as tvF from .modeling_vitpose import VitPoseEstimatorOutput From c07845591654a8dddb420da127232571a72ca546 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 12:29:10 +0100 Subject: [PATCH 06/30] fix import shinanigans --- .../models/beit/image_processing_pil_beit.py | 1 - .../models/dpt/image_processing_pil_dpt.py | 1 - .../image_processing_pil_efficientloftr.py | 18 ++++++----- .../image_processing_pil_lightglue.py | 31 +++++++------------ .../models/lightglue/modular_lightglue.py | 6 ++-- .../image_processing_pil_paddleocr_vl.py | 5 +-- .../paddleocr_vl/modular_paddleocr_vl.py | 9 ++---- ...ge_processing_pil_prompt_depth_anything.py | 1 - .../seggpt/image_processing_pil_seggpt.py | 1 - .../image_processing_pil_superglue.py | 10 +++--- .../image_processing_pil_video_llama_3.py | 5 +-- .../video_llama_3/modular_video_llama_3.py | 11 ++----- .../vitmatte/image_processing_pil_vitmatte.py | 1 - .../vitpose/image_processing_pil_vitpose.py | 1 - 14 files changed, 35 insertions(+), 66 deletions(-) diff --git a/src/transformers/models/beit/image_processing_pil_beit.py b/src/transformers/models/beit/image_processing_pil_beit.py index 32ad8d90d34d..9587fb3eca82 100644 --- a/src/transformers/models/beit/image_processing_pil_beit.py +++ b/src/transformers/models/beit/image_processing_pil_beit.py @@ -13,7 +13,6 @@ # limitations under the License. """Image processor class for BEiT.""" - import numpy as np from ...image_processing_backends import PilBackend diff --git a/src/transformers/models/dpt/image_processing_pil_dpt.py b/src/transformers/models/dpt/image_processing_pil_dpt.py index bed972553ffb..4c59c1557c5c 100644 --- a/src/transformers/models/dpt/image_processing_pil_dpt.py +++ b/src/transformers/models/dpt/image_processing_pil_dpt.py @@ -36,7 +36,6 @@ if TYPE_CHECKING: - from ...modeling_outputs import DepthEstimatorOutput diff --git a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py index 6b89bfd11c21..084b25bed08b 100644 --- a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py @@ -6,23 +6,25 @@ # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 +from typing import TYPE_CHECKING + import numpy as np +from PIL import Image, ImageDraw from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput, PILImageResampling, SizeDict, to_numpy_array from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available, is_vision_available -from ...utils.import_utils import requires, requires_backends +from ...utils import TensorType, auto_docstring +from ...utils.import_utils import requires from .image_processing_efficientloftr import EfficientLoFTRImageProcessorKwargs, validate_and_format_image_pairs from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput -if is_vision_available(): - import PIL - from PIL import Image, ImageDraw -if is_torch_available(): +if TYPE_CHECKING: import torch + + def is_grayscale(image: np.ndarray): if image.shape[0] == 1: return True @@ -50,14 +52,14 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: gray_image = np.stack([gray_image] * 3, axis=0) return gray_image - if not isinstance(image, PIL.Image.Image): + if not isinstance(image, Image.Image): return image image = image.convert("L") return image -@requires(backends=("torch",)) +@auto_docstring class EfficientLoFTRImageProcessorPil(PilBackend): valid_kwargs = EfficientLoFTRImageProcessorKwargs resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/lightglue/image_processing_pil_lightglue.py b/src/transformers/models/lightglue/image_processing_pil_lightglue.py index 4c1d59aad049..2b6fc3dfc480 100644 --- a/src/transformers/models/lightglue/image_processing_pil_lightglue.py +++ b/src/transformers/models/lightglue/image_processing_pil_lightglue.py @@ -21,32 +21,21 @@ from typing import TYPE_CHECKING import numpy as np +from PIL import Image, ImageDraw from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput, PILImageResampling, SizeDict, to_numpy_array from ...processing_utils import Unpack -from ...utils import ( - TensorType, - auto_docstring, - is_torch_available, - is_torchvision_available, - is_vision_available, - requires_backends, -) +from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires from .image_processing_lightglue import LightGlueImageProcessorKwargs, validate_and_format_image_pairs if TYPE_CHECKING: - from .modeling_lightglue import LightGlueKeypointMatchingOutput - -if is_vision_available(): - import PIL - from PIL import Image, ImageDraw -if is_torch_available(): import torch -if is_torchvision_available(): + + from .modeling_lightglue import LightGlueKeypointMatchingOutput def is_grayscale(image: np.ndarray): @@ -76,14 +65,14 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: gray_image = np.stack([gray_image] * 3, axis=0) return gray_image - if not isinstance(image, PIL.Image.Image): + if not isinstance(image, Image.Image): return image image = image.convert("L") return image -@requires(backends=("torch",)) +@auto_docstring class LightGlueImageProcessorPil(PilBackend): valid_kwargs = LightGlueImageProcessorKwargs resample = PILImageResampling.BILINEAR @@ -139,12 +128,13 @@ def _preprocess( return BatchFeature(data=data, tensor_type=return_tensors) + @requires(backends=("torch",)) def post_process_keypoint_matching( self, outputs: "LightGlueKeypointMatchingOutput", target_sizes: TensorType | list[tuple], threshold: float = 0.0, - ) -> list[dict[str, torch.Tensor]]: + ) -> list[dict[str, "torch.Tensor"]]: """ Converts the raw output of [`LightGlueKeypointMatchingOutput`] into lists of keypoints, scores and descriptors with coordinates absolute to the original image sizes. @@ -161,7 +151,8 @@ def post_process_keypoint_matching( `list[Dict]`: A list of dictionaries, each dictionary containing the keypoints in the first and second image of the pair, the matching scores and the matching indices. """ - requires_backends(self, "torch") + import torch + if outputs.mask.shape[0] != len(target_sizes): raise ValueError("Make sure that you pass in as many target sizes as the batch dimension of the mask") if not all(len(target_size) == 2 for target_size in target_sizes): @@ -209,7 +200,7 @@ def post_process_keypoint_matching( return results def visualize_keypoint_matching( - self, images: ImageInput, keypoint_matching_output: list[dict[str, torch.Tensor]] + self, images: ImageInput, keypoint_matching_output: list[dict[str, "torch.Tensor"]] ) -> list["Image.Image"]: """ Plots the image pairs side by side with the detected keypoints as well as the matching between them. diff --git a/src/transformers/models/lightglue/modular_lightglue.py b/src/transformers/models/lightglue/modular_lightglue.py index a94bee279c3f..c0b4812d76e7 100644 --- a/src/transformers/models/lightglue/modular_lightglue.py +++ b/src/transformers/models/lightglue/modular_lightglue.py @@ -24,7 +24,7 @@ from ...modeling_flash_attention_utils import FlashAttentionKwargs from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel from ...processing_utils import Unpack -from ...utils import ModelOutput, TensorType, auto_docstring, can_return_tuple, logging, requires_backends +from ...utils import ModelOutput, TensorType, auto_docstring, can_return_tuple, logging from ...utils.import_utils import requires from ..auto import CONFIG_MAPPING, AutoConfig from ..auto.modeling_auto import AutoModelForKeypointDetection @@ -181,13 +181,13 @@ def post_process_keypoint_matching( @requires(backends=("torch",)) class LightGlueImageProcessorPil(SuperGlueImageProcessorPil): + @requires(backends=("torch",)) def post_process_keypoint_matching( self, outputs: "LightGlueKeypointMatchingOutput", target_sizes: TensorType | list[tuple], threshold: float = 0.0, - ) -> list[dict[str, torch.Tensor]]: - requires_backends(self, "torch") + ) -> list[dict[str, "torch.Tensor"]]: return super().post_process_keypoint_matching(outputs, target_sizes, threshold) diff --git a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py index 51ab51dbb536..ec994f6db6d3 100644 --- a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py @@ -31,13 +31,10 @@ from ...image_processing_utils import BatchFeature from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available +from ...utils import TensorType, auto_docstring from .image_processing_paddleocr_vl import PaddleOCRVLImageProcessorKwargs, smart_resize -if is_torchvision_available(): - - @auto_docstring class PaddleOCRVLImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/paddleocr_vl/modular_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/modular_paddleocr_vl.py index be683fad4f3a..3ba29cbb37ea 100644 --- a/src/transformers/models/paddleocr_vl/modular_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/modular_paddleocr_vl.py @@ -50,7 +50,6 @@ TransformersKwargs, auto_docstring, can_return_tuple, - is_torchvision_available, logging, torch_compilable_check, torch_int, @@ -88,10 +87,6 @@ ) -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - logger = logging.get_logger(__name__) @@ -147,7 +142,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | int | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, @@ -258,7 +253,7 @@ def _preprocess( images: list["torch.Tensor"], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | int | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py index 5ea50c33fe22..10eff3cb4f97 100644 --- a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py @@ -37,7 +37,6 @@ if TYPE_CHECKING: - from ...modeling_outputs import DepthEstimatorOutput from .image_processing_prompt_depth_anything import PromptDepthAnythingImageProcessorKwargs diff --git a/src/transformers/models/seggpt/image_processing_pil_seggpt.py b/src/transformers/models/seggpt/image_processing_pil_seggpt.py index c32e2e469282..96542e227956 100644 --- a/src/transformers/models/seggpt/image_processing_pil_seggpt.py +++ b/src/transformers/models/seggpt/image_processing_pil_seggpt.py @@ -13,7 +13,6 @@ # limitations under the License. """Image processor class for SegGPT.""" - import numpy as np from ...image_processing_backends import PilBackend diff --git a/src/transformers/models/superglue/image_processing_pil_superglue.py b/src/transformers/models/superglue/image_processing_pil_superglue.py index e7e4761c6814..f8628c5cfc88 100644 --- a/src/transformers/models/superglue/image_processing_pil_superglue.py +++ b/src/transformers/models/superglue/image_processing_pil_superglue.py @@ -21,7 +21,9 @@ from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput, PILImageResampling, SizeDict, to_numpy_array from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_vision_available +from PIL import Image, ImageDraw + +from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires from .image_processing_superglue import validate_and_format_image_pairs @@ -31,10 +33,6 @@ from .modeling_superglue import SuperGlueKeypointMatchingOutput -if is_vision_available(): - import PIL - from PIL import Image, ImageDraw - def is_grayscale(image: np.ndarray): if image.shape[0] == 1: @@ -63,7 +61,7 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: gray_image = np.stack([gray_image] * 3, axis=0) return gray_image - if not isinstance(image, PIL.Image.Image): + if not isinstance(image, Image.Image): return image image = image.convert("L") diff --git a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py index 344e24b560e9..0e9d58e8f772 100644 --- a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py +++ b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py @@ -25,13 +25,10 @@ from ...image_processing_backends import PilBackend from ...image_utils import IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available +from ...utils import TensorType, auto_docstring from .image_processing_video_llama_3 import VideoLlama3ImageProcessorKwargs, smart_resize -if is_torchvision_available(): - - @auto_docstring class VideoLlama3ImageProcessorPil(PilBackend): do_resize = True diff --git a/src/transformers/models/video_llama_3/modular_video_llama_3.py b/src/transformers/models/video_llama_3/modular_video_llama_3.py index c03c6a6434c6..038422591fc0 100644 --- a/src/transformers/models/video_llama_3/modular_video_llama_3.py +++ b/src/transformers/models/video_llama_3/modular_video_llama_3.py @@ -43,7 +43,6 @@ TensorType, auto_docstring, can_return_tuple, - is_torchvision_available, logging, ) from ...utils.generic import is_flash_attention_requested, merge_with_config_defaults @@ -83,10 +82,6 @@ ) -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - logger = logging.get_logger(__name__) @@ -1133,7 +1128,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | int | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, @@ -1235,7 +1230,7 @@ def _preprocess( images: list["torch.Tensor"], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | int | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, @@ -1391,7 +1386,7 @@ def _preprocess( do_convert_rgb: bool, do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | int | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py index 59464395bedc..5cc09afa1d74 100644 --- a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py +++ b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py @@ -13,7 +13,6 @@ # limitations under the License. """Image processor class for ViTMatte.""" - import numpy as np from ...image_processing_backends import PilBackend diff --git a/src/transformers/models/vitpose/image_processing_pil_vitpose.py b/src/transformers/models/vitpose/image_processing_pil_vitpose.py index 010bbdf30106..357c603a4603 100644 --- a/src/transformers/models/vitpose/image_processing_pil_vitpose.py +++ b/src/transformers/models/vitpose/image_processing_pil_vitpose.py @@ -47,7 +47,6 @@ pass if TYPE_CHECKING: - from .modeling_vitpose import VitPoseEstimatorOutput logger = logging.get_logger(__name__) From 40999135027859d41dbc953f0e1c8b0d48767441 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 12:30:33 +0100 Subject: [PATCH 07/30] marvellous that's how we protect torch :) --- .../image_processing_pil_efficientloftr.py | 8 +++++--- .../models/efficientloftr/modular_efficientloftr.py | 9 +++++---- .../models/superglue/image_processing_pil_superglue.py | 3 +-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py index 084b25bed08b..1d440880d464 100644 --- a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py @@ -115,12 +115,13 @@ def _preprocess( return BatchFeature(data=data, tensor_type=return_tensors) + @requires(backends=("torch",)) def post_process_keypoint_matching( self, outputs: "EfficientLoFTRKeypointMatchingOutput", target_sizes: TensorType | list[tuple], threshold: float = 0.0, - ) -> list[dict[str, torch.Tensor]]: + ) -> list[dict[str, "torch.Tensor"]]: """ Converts the raw output of [`EfficientLoFTRKeypointMatchingOutput`] into lists of keypoints, scores and descriptors with coordinates absolute to the original image sizes. @@ -137,7 +138,8 @@ def post_process_keypoint_matching( `List[Dict]`: A list of dictionaries, each dictionary containing the keypoints in the first and second image of the pair, the matching scores and the matching indices. """ - requires_backends(self, ["torch"]) + import torch + if outputs.matches.shape[0] != len(target_sizes): raise ValueError("Make sure that you pass in as many target sizes as the batch dimension of the mask") if not all(len(target_size) == 2 for target_size in target_sizes): @@ -176,7 +178,7 @@ def post_process_keypoint_matching( return results def visualize_keypoint_matching( - self, images: ImageInput, keypoint_matching_output: list[dict[str, torch.Tensor]] + self, images: ImageInput, keypoint_matching_output: list[dict[str, "torch.Tensor"]] ) -> list["Image.Image"]: """ Plots the image pairs side by side with the detected keypoints as well as the matching between them. diff --git a/src/transformers/models/efficientloftr/modular_efficientloftr.py b/src/transformers/models/efficientloftr/modular_efficientloftr.py index f5bcdbdde644..f2d8e037e735 100644 --- a/src/transformers/models/efficientloftr/modular_efficientloftr.py +++ b/src/transformers/models/efficientloftr/modular_efficientloftr.py @@ -1,7 +1,7 @@ import torch from ...utils import TensorType -from ...utils.import_utils import requires, requires_backends +from ...utils.import_utils import requires from ..superglue.image_processing_pil_superglue import SuperGlueImageProcessorPil from ..superglue.image_processing_superglue import SuperGlueImageProcessor from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput @@ -68,14 +68,14 @@ def post_process_keypoint_matching( return results -@requires(backends=("torch",)) class EfficientLoFTRImageProcessorPil(SuperGlueImageProcessorPil): + @requires(backends=("torch",)) def post_process_keypoint_matching( self, outputs: "EfficientLoFTRKeypointMatchingOutput", target_sizes: TensorType | list[tuple], threshold: float = 0.0, - ) -> list[dict[str, torch.Tensor]]: + ) -> list[dict[str, "torch.Tensor"]]: """ Converts the raw output of [`EfficientLoFTRKeypointMatchingOutput`] into lists of keypoints, scores and descriptors with coordinates absolute to the original image sizes. @@ -92,7 +92,8 @@ def post_process_keypoint_matching( `List[Dict]`: A list of dictionaries, each dictionary containing the keypoints in the first and second image of the pair, the matching scores and the matching indices. """ - requires_backends(self, ["torch"]) + import torch + if outputs.matches.shape[0] != len(target_sizes): raise ValueError("Make sure that you pass in as many target sizes as the batch dimension of the mask") if not all(len(target_size) == 2 for target_size in target_sizes): diff --git a/src/transformers/models/superglue/image_processing_pil_superglue.py b/src/transformers/models/superglue/image_processing_pil_superglue.py index f8628c5cfc88..ea0b1fa98fde 100644 --- a/src/transformers/models/superglue/image_processing_pil_superglue.py +++ b/src/transformers/models/superglue/image_processing_pil_superglue.py @@ -16,13 +16,12 @@ from typing import TYPE_CHECKING import numpy as np +from PIL import Image, ImageDraw from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput, PILImageResampling, SizeDict, to_numpy_array from ...processing_utils import ImagesKwargs, Unpack -from PIL import Image, ImageDraw - from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires from .image_processing_superglue import validate_and_format_image_pairs From 6e011bf71a490c3972772eabda02103841b96d6b Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 12:34:31 +0100 Subject: [PATCH 08/30] beit is torchvisionbackend --- .../models/beit/image_processing_beit.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/transformers/models/beit/image_processing_beit.py b/src/transformers/models/beit/image_processing_beit.py index 0392914ba32f..2db084699fc5 100644 --- a/src/transformers/models/beit/image_processing_beit.py +++ b/src/transformers/models/beit/image_processing_beit.py @@ -27,17 +27,9 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, is_torch_available, is_torchvision_available - - -if is_torch_available(): - import torch - import torch.nn.functional as F - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - +from ...utils import TensorType, is_torch_available, auto_docstring +import torch +import torch.nn.functional as F class BeitImageProcessorKwargs(ImagesKwargs, total=False): r""" do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): @@ -49,7 +41,7 @@ class BeitImageProcessorKwargs(ImagesKwargs, total=False): do_reduce_labels: bool -# @auto_docstring +@auto_docstring class BeitImageProcessor(TorchvisionBackend): """PIL backend for BEiT with reduce_label support.""" @@ -70,7 +62,7 @@ class BeitImageProcessor(TorchvisionBackend): def __init__(self, **kwargs: Unpack[BeitImageProcessorKwargs]): super().__init__(**kwargs) - # @auto_docstring + @auto_docstring def preprocess( self, images: ImageInput, From a1bda86f66045879cd71b023c2e658b61ef5c0d9 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 12:55:56 +0100 Subject: [PATCH 09/30] more import cleanup --- .../models/aria/image_processing_aria.py | 7 ++----- src/transformers/models/aria/modular_aria.py | 9 ++------- .../models/beit/image_processing_beit.py | 10 +++++++--- .../bridgetower/image_processing_bridgetower.py | 11 +++-------- .../models/chameleon/image_processing_chameleon.py | 10 +++------- .../models/chmv2/image_processing_chmv2.py | 12 +++--------- .../image_processing_cohere2_vision.py | 7 ++----- .../image_processing_conditional_detr.py | 7 ++----- .../models/convnext/image_processing_convnext.py | 13 ++++--------- .../image_processing_deformable_detr.py | 7 ++----- .../models/depth_pro/image_processing_depth_pro.py | 14 ++++---------- .../models/detr/image_processing_detr.py | 6 +----- .../dinov3_vit/image_processing_dinov3_vit.py | 11 +++-------- .../models/dpt/image_processing_dpt.py | 10 +++------- src/transformers/models/dpt/modular_dpt.py | 5 ++--- .../image_processing_efficientloftr.py | 7 ++----- .../image_processing_pil_efficientloftr.py | 6 ++---- .../efficientnet/image_processing_efficientnet.py | 10 ++++------ .../models/eomt/image_processing_eomt.py | 6 +----- .../image_processing_ernie4_5_vl_moe.py | 7 ++----- .../image_processing_pil_ernie4_5_vl_moe.py | 4 ++-- .../ernie4_5_vl_moe/modular_ernie4_5_vl_moe.py | 6 +----- .../models/flava/image_processing_flava.py | 6 +----- .../models/fuyu/image_processing_fuyu.py | 6 +----- .../models/gemma3/image_processing_gemma3.py | 6 +----- .../models/glm46v/image_processing_glm46v.py | 7 ++----- .../models/glm4v/image_processing_glm4v.py | 7 ++----- .../models/glm_image/image_processing_glm_image.py | 13 ++++--------- .../models/glm_image/modular_glm_image.py | 6 ++---- .../models/glpn/image_processing_glpn.py | 8 ++++---- .../models/got_ocr2/image_processing_got_ocr2.py | 6 +----- .../image_processing_grounding_dino.py | 5 ++--- .../models/idefics2/image_processing_idefics2.py | 5 ++--- .../models/idefics3/image_processing_idefics3.py | 7 ++----- .../models/imagegpt/image_processing_imagegpt.py | 6 +----- .../layoutlmv2/image_processing_layoutlmv2.py | 5 ++--- .../layoutlmv3/image_processing_layoutlmv3.py | 5 ++--- .../models/levit/image_processing_levit.py | 13 ++++--------- .../models/lfm2_vl/image_processing_lfm2_vl.py | 5 +---- .../models/lightglue/image_processing_lightglue.py | 7 +++---- .../models/llama4/image_processing_llama4.py | 7 ++----- .../models/llava/image_processing_llava.py | 10 ++++------ .../llava_next/image_processing_llava_next.py | 12 ++++-------- .../mask2former/image_processing_mask2former.py | 6 ++---- .../image_processing_pil_mask2former.py | 5 +---- .../maskformer/image_processing_maskformer.py | 6 ++---- .../maskformer/image_processing_pil_maskformer.py | 2 +- .../models/mllama/image_processing_mllama.py | 5 ++--- .../mobilenet_v2/image_processing_mobilenet_v2.py | 12 ++++-------- .../models/mobilevit/image_processing_mobilevit.py | 11 +++-------- .../models/nougat/image_processing_nougat.py | 6 +----- .../models/oneformer/image_processing_oneformer.py | 6 +----- .../models/ovis2/image_processing_ovis2.py | 7 ++----- .../models/owlv2/image_processing_pil_owlv2.py | 10 ++++++---- .../paddleocr_vl/image_processing_paddleocr_vl.py | 8 ++------ .../image_processing_pil_paddleocr_vl.py | 3 ++- .../models/perceiver/image_processing_perceiver.py | 13 ++++--------- .../image_processing_perception_lm.py | 7 ++----- .../image_processing_phi4_multimodal.py | 7 ++----- .../models/pixtral/image_processing_pixtral.py | 7 ++----- .../poolformer/image_processing_poolformer.py | 13 ++++--------- .../image_processing_pp_doclayout_v2.py | 7 ++----- .../image_processing_pp_doclayout_v3.py | 6 ++---- .../pp_doclayout_v3/modular_pp_doclayout_v3.py | 5 +---- .../image_processing_prompt_depth_anything.py | 6 ++---- .../models/qwen2_vl/image_processing_qwen2_vl.py | 7 ++----- .../models/rt_detr/image_processing_pil_rt_detr.py | 4 ++-- .../models/sam/image_processing_pil_sam.py | 2 +- .../models/sam/image_processing_sam.py | 6 ++---- .../segformer/image_processing_pil_segformer.py | 14 +++++++------- .../models/segformer/image_processing_segformer.py | 5 +---- .../models/seggpt/image_processing_seggpt.py | 11 +++-------- .../models/siglip2/image_processing_siglip2.py | 6 +----- .../models/smolvlm/image_processing_smolvlm.py | 7 ++----- .../models/superglue/image_processing_superglue.py | 4 +--- .../superpoint/image_processing_superpoint.py | 5 ++--- .../models/swin2sr/image_processing_swin2sr.py | 10 ++++------ .../models/textnet/image_processing_textnet.py | 12 ++++-------- .../models/tvp/image_processing_tvp.py | 7 ++----- .../image_processing_pil_video_llama_3.py | 3 ++- .../image_processing_video_llama_3.py | 8 ++------ .../video_processing_video_llama_3.py | 8 ++------ .../models/videomae/image_processing_videomae.py | 12 ++++-------- .../models/vilt/image_processing_vilt.py | 6 +----- .../models/vitmatte/image_processing_vitmatte.py | 7 ++----- .../models/vitpose/image_processing_vitpose.py | 10 ++-------- .../models/zoedepth/image_processing_zoedepth.py | 5 ++--- 87 files changed, 205 insertions(+), 449 deletions(-) diff --git a/src/transformers/models/aria/image_processing_aria.py b/src/transformers/models/aria/image_processing_aria.py index d9362edda7cc..e0abc55236f0 100644 --- a/src/transformers/models/aria/image_processing_aria.py +++ b/src/transformers/models/aria/image_processing_aria.py @@ -18,17 +18,14 @@ # See the License for the specific language governing permissions and # limitations under the License. import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature, get_patch_output_size, select_best_resolution from ...image_transforms import divide_to_patches from ...image_utils import ChannelDimension, PILImageResampling, SizeDict, get_image_size from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring class AriaImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/aria/modular_aria.py b/src/transformers/models/aria/modular_aria.py index f86811bf8ceb..461af11e73fa 100644 --- a/src/transformers/models/aria/modular_aria.py +++ b/src/transformers/models/aria/modular_aria.py @@ -39,8 +39,6 @@ TransformersKwargs, auto_docstring, can_return_tuple, - is_torch_available, - is_torchvision_available, logging, ) from ..auto import CONFIG_MAPPING, AutoConfig, AutoTokenizer @@ -323,11 +321,8 @@ def forward(self, key_value_states: torch.Tensor, attn_mask: torch.Tensor | None return out -if is_torch_available(): - import torch - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +import torch +from torchvision.transforms.v2 import functional as tvF class AriaImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/beit/image_processing_beit.py b/src/transformers/models/beit/image_processing_beit.py index 2db084699fc5..53053f644539 100644 --- a/src/transformers/models/beit/image_processing_beit.py +++ b/src/transformers/models/beit/image_processing_beit.py @@ -15,6 +15,10 @@ from typing import Union +import torch +import torch.nn.functional as F +from torchvision.transforms.v2 import functional as tvF + from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images @@ -27,9 +31,9 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, is_torch_available, auto_docstring -import torch -import torch.nn.functional as F +from ...utils import TensorType, auto_docstring, is_torch_available + + class BeitImageProcessorKwargs(ImagesKwargs, total=False): r""" do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): diff --git a/src/transformers/models/bridgetower/image_processing_bridgetower.py b/src/transformers/models/bridgetower/image_processing_bridgetower.py index 1593d8e144e3..1a20dd831675 100644 --- a/src/transformers/models/bridgetower/image_processing_bridgetower.py +++ b/src/transformers/models/bridgetower/image_processing_bridgetower.py @@ -16,6 +16,8 @@ from typing import Union import numpy as np +import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -29,14 +31,7 @@ get_image_size, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available - - -if is_torch_available(): - import torch - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring, is_torch_available def get_resize_output_image_size( diff --git a/src/transformers/models/chameleon/image_processing_chameleon.py b/src/transformers/models/chameleon/image_processing_chameleon.py index 217f80fcc83d..d92188096c53 100644 --- a/src/transformers/models/chameleon/image_processing_chameleon.py +++ b/src/transformers/models/chameleon/image_processing_chameleon.py @@ -15,6 +15,8 @@ import numpy as np import PIL.Image +import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_utils import ( @@ -23,15 +25,9 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import auto_docstring, is_torch_available, is_torchvision_available, logging +from ...utils import auto_docstring, logging -if is_torch_available(): - import torch - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - logger = logging.get_logger(__name__) diff --git a/src/transformers/models/chmv2/image_processing_chmv2.py b/src/transformers/models/chmv2/image_processing_chmv2.py index be31f931c4f8..3bb82b2dea53 100644 --- a/src/transformers/models/chmv2/image_processing_chmv2.py +++ b/src/transformers/models/chmv2/image_processing_chmv2.py @@ -23,6 +23,8 @@ from typing import Union import torch +import torch.nn.functional as F +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_base import BatchFeature @@ -30,15 +32,7 @@ from ...image_utils import ChannelDimension, ImageInput, PILImageResampling, SizeDict from ...modeling_outputs import DepthEstimatorOutput from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available, requires_backends - - -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF - - -if is_torch_available(): - import torch.nn.functional as F +from ...utils import TensorType, auto_docstring, is_torch_available, requires_backends class CHMv2ImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/cohere2_vision/image_processing_cohere2_vision.py b/src/transformers/models/cohere2_vision/image_processing_cohere2_vision.py index abd149af00c8..e21c5d83c059 100644 --- a/src/transformers/models/cohere2_vision/image_processing_cohere2_vision.py +++ b/src/transformers/models/cohere2_vision/image_processing_cohere2_vision.py @@ -22,17 +22,14 @@ import numpy as np import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring class Cohere2VisionImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/conditional_detr/image_processing_conditional_detr.py b/src/transformers/models/conditional_detr/image_processing_conditional_detr.py index 6fc11470c067..539fe152f606 100644 --- a/src/transformers/models/conditional_detr/image_processing_conditional_detr.py +++ b/src/transformers/models/conditional_detr/image_processing_conditional_detr.py @@ -25,6 +25,7 @@ import torch from torch import nn from torchvision.io import read_image +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature, get_size_dict @@ -49,11 +50,7 @@ validate_annotations, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available, logging - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring, logging logger = logging.get_logger(__name__) diff --git a/src/transformers/models/convnext/image_processing_convnext.py b/src/transformers/models/convnext/image_processing_convnext.py index ccec10eac505..201376123080 100644 --- a/src/transformers/models/convnext/image_processing_convnext.py +++ b/src/transformers/models/convnext/image_processing_convnext.py @@ -13,6 +13,9 @@ # limitations under the License. """Image processor class for ConvNeXT.""" +import torch +from torchvision.transforms.v2 import functional as tvF + from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import get_resize_output_image_size, group_images_by_shape, reorder_images @@ -24,15 +27,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available - - -if is_torch_available(): - import torch - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring class ConvNextImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/deformable_detr/image_processing_deformable_detr.py b/src/transformers/models/deformable_detr/image_processing_deformable_detr.py index bd72cc681965..1bc4775255d6 100644 --- a/src/transformers/models/deformable_detr/image_processing_deformable_detr.py +++ b/src/transformers/models/deformable_detr/image_processing_deformable_detr.py @@ -23,6 +23,7 @@ import torch from torchvision.io import read_image +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature, get_size_dict @@ -47,11 +48,7 @@ validate_annotations, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring class DeformableDetrImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/depth_pro/image_processing_depth_pro.py b/src/transformers/models/depth_pro/image_processing_depth_pro.py index 400253b5c790..3a16e2bd383d 100644 --- a/src/transformers/models/depth_pro/image_processing_depth_pro.py +++ b/src/transformers/models/depth_pro/image_processing_depth_pro.py @@ -15,6 +15,9 @@ from typing import TYPE_CHECKING +import torch +from torchvision.transforms.v2 import functional as tvF + from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images @@ -23,12 +26,11 @@ IMAGENET_STANDARD_STD, PILImageResampling, SizeDict, + pil_torch_interpolation_mapping, ) from ...utils import ( TensorType, auto_docstring, - is_torch_available, - is_torchvision_available, logging, requires_backends, ) @@ -37,14 +39,6 @@ if TYPE_CHECKING: from .modeling_depth_pro import DepthProDepthEstimatorOutput -if is_torch_available(): - import torch - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - from ...image_utils import pil_torch_interpolation_mapping - logger = logging.get_logger(__name__) diff --git a/src/transformers/models/detr/image_processing_detr.py b/src/transformers/models/detr/image_processing_detr.py index 05db5e426615..e5cfa7ce14fb 100644 --- a/src/transformers/models/detr/image_processing_detr.py +++ b/src/transformers/models/detr/image_processing_detr.py @@ -20,6 +20,7 @@ import torch from torch import nn from torchvision.io import read_image +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature, get_size_dict @@ -47,15 +48,10 @@ from ...utils import ( TensorType, auto_docstring, - is_torchvision_available, logging, ) -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - logger = logging.get_logger(__name__) SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) diff --git a/src/transformers/models/dinov3_vit/image_processing_dinov3_vit.py b/src/transformers/models/dinov3_vit/image_processing_dinov3_vit.py index 65253c96fdb6..e1dfce9cc7de 100644 --- a/src/transformers/models/dinov3_vit/image_processing_dinov3_vit.py +++ b/src/transformers/models/dinov3_vit/image_processing_dinov3_vit.py @@ -13,6 +13,9 @@ # limitations under the License. """Image processor class for DINOv3.""" +import torch +from torchvision.transforms.v2 import functional as tvF + from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images @@ -20,18 +23,10 @@ from ...utils import ( TensorType, auto_docstring, - is_torch_available, - is_torchvision_available, logging, ) -if is_torch_available(): - import torch - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - logger = logging.get_logger(__name__) diff --git a/src/transformers/models/dpt/image_processing_dpt.py b/src/transformers/models/dpt/image_processing_dpt.py index 5ffb5d3852c5..d6503b0212a9 100644 --- a/src/transformers/models/dpt/image_processing_dpt.py +++ b/src/transformers/models/dpt/image_processing_dpt.py @@ -24,6 +24,7 @@ from typing import TYPE_CHECKING, Union import torch +import torch.nn.functional as F from ...image_processing_backends import TorchvisionBackend from ...image_processing_base import BatchFeature @@ -37,18 +38,13 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available, requires_backends +from ...utils import TensorType, auto_docstring, is_torch_available, requires_backends if TYPE_CHECKING: from ...modeling_outputs import DepthEstimatorOutput -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF - - -if is_torch_available(): - import torch.nn.functional as F +from torchvision.transforms.v2 import functional as tvF class DPTImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/dpt/modular_dpt.py b/src/transformers/models/dpt/modular_dpt.py index 3a7c17ab2b36..dc024ecf2e4c 100644 --- a/src/transformers/models/dpt/modular_dpt.py +++ b/src/transformers/models/dpt/modular_dpt.py @@ -29,15 +29,14 @@ SizeDict, ) from ...processing_utils import ImagesKwargs -from ...utils import TensorType, auto_docstring, is_torchvision_available, requires_backends +from ...utils import TensorType, auto_docstring, requires_backends from ..beit.image_processing_beit import BeitImageProcessor if TYPE_CHECKING: from ...modeling_outputs import DepthEstimatorOutput -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF +from torchvision.transforms.v2 import functional as tvF def get_resize_output_image_size( diff --git a/src/transformers/models/efficientloftr/image_processing_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_efficientloftr.py index 80897ada4243..4333fd6eed10 100644 --- a/src/transformers/models/efficientloftr/image_processing_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_efficientloftr.py @@ -6,6 +6,7 @@ # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 import torch +from PIL import Image, ImageDraw from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend @@ -22,14 +23,10 @@ to_numpy_array, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_vision_available +from ...utils import TensorType, auto_docstring from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput -if is_vision_available(): - from PIL import Image, ImageDraw - - class EfficientLoFTRImageProcessorKwargs(ImagesKwargs, total=False): r""" do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`): diff --git a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py index 1d440880d464..866fedfb3d9c 100644 --- a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py @@ -6,8 +6,6 @@ # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 -from typing import TYPE_CHECKING - import numpy as np from PIL import Image, ImageDraw @@ -15,13 +13,13 @@ from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput, PILImageResampling, SizeDict, to_numpy_array from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring +from ...utils import TensorType, auto_docstring, is_torch_available from ...utils.import_utils import requires from .image_processing_efficientloftr import EfficientLoFTRImageProcessorKwargs, validate_and_format_image_pairs from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput -if TYPE_CHECKING: +if is_torch_available(): import torch diff --git a/src/transformers/models/efficientnet/image_processing_efficientnet.py b/src/transformers/models/efficientnet/image_processing_efficientnet.py index 6e18e4d944af..830a6c1730ae 100644 --- a/src/transformers/models/efficientnet/image_processing_efficientnet.py +++ b/src/transformers/models/efficientnet/image_processing_efficientnet.py @@ -16,6 +16,9 @@ from functools import lru_cache from typing import Optional +import torch +from torchvision.transforms.v2 import functional as tvF + from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images @@ -26,12 +29,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - import torch - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring class EfficientNetImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/eomt/image_processing_eomt.py b/src/transformers/models/eomt/image_processing_eomt.py index d9f931585a33..7d8099fa2487 100644 --- a/src/transformers/models/eomt/image_processing_eomt.py +++ b/src/transformers/models/eomt/image_processing_eomt.py @@ -18,6 +18,7 @@ import numpy as np import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -35,14 +36,9 @@ TensorType, auto_docstring, filter_out_non_signature_kwargs, - is_torchvision_available, ) -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - class EomtImageProcessorKwargs(ImagesKwargs, total=False): r""" do_split_image (`bool`, *optional*, defaults to `self.do_split_image`): diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_ernie4_5_vl_moe.py index 7b9d97f632fd..7d50f63d77df 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_ernie4_5_vl_moe.py @@ -20,17 +20,14 @@ import math import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available, logging - - -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF +from ...utils import TensorType, auto_docstring, logging logger = logging.get_logger(__name__) diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py index b4421d72d7b3..1bbff5d029c9 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py @@ -31,7 +31,7 @@ if is_torch_available(): import torch if is_torchvision_available(): - pass + import torchvision.transforms.v2.functional as tvF logger = logging.get_logger(__name__) @@ -82,7 +82,7 @@ def _preprocess( images: list["torch.Tensor"], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: "PILImageResampling | tvF.InterpolationMode | int | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/ernie4_5_vl_moe/modular_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/modular_ernie4_5_vl_moe.py index a09e0df5e703..aa7a22ff3579 100644 --- a/src/transformers/models/ernie4_5_vl_moe/modular_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/modular_ernie4_5_vl_moe.py @@ -22,6 +22,7 @@ import torch.nn as nn import torch.nn.functional as F from huggingface_hub.dataclasses import strict +from torchvision.transforms.v2 import functional as tvF from ... import initialization as init from ...cache_utils import Cache, DynamicCache @@ -48,7 +49,6 @@ TransformersKwargs, auto_docstring, can_return_tuple, - is_torchvision_available, logging, ) from ...utils.generic import maybe_autocast, merge_with_config_defaults @@ -79,10 +79,6 @@ from ..qwen2_vl.modeling_qwen2_vl import Qwen2VisionTransformerPretrainedModel, Qwen2VLModel, VisionMlp -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF - - logger = logging.get_logger(__name__) diff --git a/src/transformers/models/flava/image_processing_flava.py b/src/transformers/models/flava/image_processing_flava.py index 82e18142bf8c..bab4b1113e3b 100644 --- a/src/transformers/models/flava/image_processing_flava.py +++ b/src/transformers/models/flava/image_processing_flava.py @@ -20,6 +20,7 @@ from typing import Any import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature, get_size_dict @@ -36,14 +37,9 @@ from ...utils import ( TensorType, auto_docstring, - is_torchvision_available, ) -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - # These values are taken from CLIP FLAVA_IMAGE_MEAN = OPENAI_CLIP_MEAN FLAVA_IMAGE_STD = OPENAI_CLIP_STD diff --git a/src/transformers/models/fuyu/image_processing_fuyu.py b/src/transformers/models/fuyu/image_processing_fuyu.py index ba354c730841..97d96ec923d7 100644 --- a/src/transformers/models/fuyu/image_processing_fuyu.py +++ b/src/transformers/models/fuyu/image_processing_fuyu.py @@ -16,6 +16,7 @@ import math import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature, get_size_dict @@ -31,16 +32,11 @@ from ...utils import ( TensorType, auto_docstring, - is_torchvision_available, logging, requires_backends, ) -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - logger = logging.get_logger(__name__) diff --git a/src/transformers/models/gemma3/image_processing_gemma3.py b/src/transformers/models/gemma3/image_processing_gemma3.py index 2075c2e0a988..b36ad3dd59ff 100644 --- a/src/transformers/models/gemma3/image_processing_gemma3.py +++ b/src/transformers/models/gemma3/image_processing_gemma3.py @@ -17,6 +17,7 @@ import math import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -32,14 +33,9 @@ from ...utils import ( TensorType, auto_docstring, - is_torchvision_available, ) -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - class Gemma3ImageProcessorKwargs(ImagesKwargs, total=False): """ do_pan_and_scan (`bool`, *optional*): diff --git a/src/transformers/models/glm46v/image_processing_glm46v.py b/src/transformers/models/glm46v/image_processing_glm46v.py index 759ee3415986..9ed4ceba6d6e 100644 --- a/src/transformers/models/glm46v/image_processing_glm46v.py +++ b/src/transformers/models/glm46v/image_processing_glm46v.py @@ -22,17 +22,14 @@ import math import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF +from ...utils import TensorType, auto_docstring class Glm46VImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/glm4v/image_processing_glm4v.py b/src/transformers/models/glm4v/image_processing_glm4v.py index d719a1dacd5a..10704e82c722 100644 --- a/src/transformers/models/glm4v/image_processing_glm4v.py +++ b/src/transformers/models/glm4v/image_processing_glm4v.py @@ -16,6 +16,7 @@ import math import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -28,11 +29,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available, logging - - -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF +from ...utils import TensorType, auto_docstring, logging logger = logging.get_logger(__name__) diff --git a/src/transformers/models/glm_image/image_processing_glm_image.py b/src/transformers/models/glm_image/image_processing_glm_image.py index ade3453afb03..6412ad5d3a48 100644 --- a/src/transformers/models/glm_image/image_processing_glm_image.py +++ b/src/transformers/models/glm_image/image_processing_glm_image.py @@ -21,20 +21,15 @@ import math from collections.abc import Iterable +import torch +from torchvision.transforms.v2 import functional as tvF + from ...feature_extraction_utils import BatchFeature from ...image_processing_backends import TorchvisionBackend from ...image_transforms import group_images_by_shape, reorder_images from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available - - -if is_torch_available(): - import torch - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring class GlmImageImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/glm_image/modular_glm_image.py b/src/transformers/models/glm_image/modular_glm_image.py index e6890c191f68..2da4c882521d 100644 --- a/src/transformers/models/glm_image/modular_glm_image.py +++ b/src/transformers/models/glm_image/modular_glm_image.py @@ -16,6 +16,7 @@ from collections.abc import Callable from typing import Any +import torch import torch.nn as nn import torch.nn.functional as F from huggingface_hub.dataclasses import strict @@ -29,7 +30,7 @@ from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel from ...processing_utils import ImagesKwargs, ProcessorMixin, Unpack from ...tokenization_utils_base import PreTokenizedInput, TextInput -from ...utils import TransformersKwargs, auto_docstring, can_return_tuple, is_torch_available, logging +from ...utils import TransformersKwargs, auto_docstring, can_return_tuple, logging from ...utils.generic import merge_with_config_defaults from ...utils.output_capturing import capture_outputs from ..chameleon.modeling_chameleon import ChameleonVQVAE, ChameleonVQVAEModelOutput, ChameleonVQVAEVectorQuantizer @@ -53,9 +54,6 @@ from ..siglip.modeling_siglip import SiglipMLP -if is_torch_available(): - import torch - logger = logging.get_logger(__name__) diff --git a/src/transformers/models/glpn/image_processing_glpn.py b/src/transformers/models/glpn/image_processing_glpn.py index dfd899ee8be0..3a83bdd96464 100644 --- a/src/transformers/models/glpn/image_processing_glpn.py +++ b/src/transformers/models/glpn/image_processing_glpn.py @@ -24,15 +24,15 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available, logging, requires_backends +from ...utils import TensorType, auto_docstring, logging, requires_backends if TYPE_CHECKING: from ...modeling_outputs import DepthEstimatorOutput -if is_torchvision_available(): - import torch - from torchvision.transforms.v2 import functional as tvF +import torch +from torchvision.transforms.v2 import functional as tvF + logger = logging.get_logger(__name__) diff --git a/src/transformers/models/got_ocr2/image_processing_got_ocr2.py b/src/transformers/models/got_ocr2/image_processing_got_ocr2.py index 7f8eb2b5430e..d035921873db 100644 --- a/src/transformers/models/got_ocr2/image_processing_got_ocr2.py +++ b/src/transformers/models/got_ocr2/image_processing_got_ocr2.py @@ -16,6 +16,7 @@ from functools import lru_cache import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -25,14 +26,9 @@ from ...utils import ( TensorType, auto_docstring, - is_torchvision_available, ) -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - class GotOcr2ImageProcessorKwargs(ImagesKwargs, total=False): r""" crop_to_patches (`bool`, *optional*, defaults to `self.crop_to_patches`): diff --git a/src/transformers/models/grounding_dino/image_processing_grounding_dino.py b/src/transformers/models/grounding_dino/image_processing_grounding_dino.py index 1314df102f19..34284dfdc8f5 100644 --- a/src/transformers/models/grounding_dino/image_processing_grounding_dino.py +++ b/src/transformers/models/grounding_dino/image_processing_grounding_dino.py @@ -52,15 +52,14 @@ validate_annotations, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available +from ...utils import TensorType, auto_docstring if TYPE_CHECKING: from .modeling_grounding_dino import GroundingDinoObjectDetectionOutput -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from torchvision.transforms.v2 import functional as tvF class GroundingDinoImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/idefics2/image_processing_idefics2.py b/src/transformers/models/idefics2/image_processing_idefics2.py index 29649885e032..61f2037ce276 100644 --- a/src/transformers/models/idefics2/image_processing_idefics2.py +++ b/src/transformers/models/idefics2/image_processing_idefics2.py @@ -28,14 +28,13 @@ make_nested_list_of_images, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available, is_vision_available +from ...utils import TensorType, auto_docstring, is_vision_available if is_vision_available(): from PIL import Image -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from torchvision.transforms.v2 import functional as tvF def get_resize_output_image_size(image, size: SizeDict) -> tuple[int, int]: diff --git a/src/transformers/models/idefics3/image_processing_idefics3.py b/src/transformers/models/idefics3/image_processing_idefics3.py index 96bef23e84dd..96f7699b8220 100644 --- a/src/transformers/models/idefics3/image_processing_idefics3.py +++ b/src/transformers/models/idefics3/image_processing_idefics3.py @@ -17,6 +17,7 @@ import numpy as np import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -30,11 +31,7 @@ make_nested_list_of_images, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available, logging - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring, logging logger = logging.get_logger(__name__) diff --git a/src/transformers/models/imagegpt/image_processing_imagegpt.py b/src/transformers/models/imagegpt/image_processing_imagegpt.py index ef220560cfee..924ed268aff2 100644 --- a/src/transformers/models/imagegpt/image_processing_imagegpt.py +++ b/src/transformers/models/imagegpt/image_processing_imagegpt.py @@ -17,6 +17,7 @@ import numpy as np import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -26,14 +27,9 @@ from ...utils import ( TensorType, auto_docstring, - is_torchvision_available, ) -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - class ImageGPTImageProcessorKwargs(ImagesKwargs, total=False): r""" clusters (`np.ndarray` or `list[list[int]]` or `torch.Tensor`, *optional*, defaults to `self.clusters`): diff --git a/src/transformers/models/layoutlmv2/image_processing_layoutlmv2.py b/src/transformers/models/layoutlmv2/image_processing_layoutlmv2.py index 402c234d268a..34ed26abf1b2 100644 --- a/src/transformers/models/layoutlmv2/image_processing_layoutlmv2.py +++ b/src/transformers/models/layoutlmv2/image_processing_layoutlmv2.py @@ -25,7 +25,6 @@ TensorType, auto_docstring, is_pytesseract_available, - is_torchvision_available, logging, requires_backends, ) @@ -34,8 +33,8 @@ if is_pytesseract_available(): import pytesseract -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from torchvision.transforms.v2 import functional as tvF + logger = logging.get_logger(__name__) diff --git a/src/transformers/models/layoutlmv3/image_processing_layoutlmv3.py b/src/transformers/models/layoutlmv3/image_processing_layoutlmv3.py index 9df120217ab3..8d7933b3aeed 100644 --- a/src/transformers/models/layoutlmv3/image_processing_layoutlmv3.py +++ b/src/transformers/models/layoutlmv3/image_processing_layoutlmv3.py @@ -31,7 +31,6 @@ TensorType, auto_docstring, is_pytesseract_available, - is_torchvision_available, logging, requires_backends, ) @@ -40,8 +39,8 @@ if is_pytesseract_available(): import pytesseract -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from torchvision.transforms.v2 import functional as tvF + logger = logging.get_logger(__name__) diff --git a/src/transformers/models/levit/image_processing_levit.py b/src/transformers/models/levit/image_processing_levit.py index 530a91cb680e..026c295c74e5 100644 --- a/src/transformers/models/levit/image_processing_levit.py +++ b/src/transformers/models/levit/image_processing_levit.py @@ -13,6 +13,9 @@ # limitations under the License. """Image processor class for LeViT.""" +import torch +from torchvision.transforms.v2 import functional as tvF + from ...image_processing_backends import TorchvisionBackend from ...image_transforms import get_resize_output_image_size from ...image_utils import ( @@ -23,15 +26,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import auto_docstring, is_torch_available, is_torchvision_available - - -if is_torch_available(): - import torch - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import auto_docstring @auto_docstring diff --git a/src/transformers/models/lfm2_vl/image_processing_lfm2_vl.py b/src/transformers/models/lfm2_vl/image_processing_lfm2_vl.py index 21f451873f19..70cf5c06f02c 100644 --- a/src/transformers/models/lfm2_vl/image_processing_lfm2_vl.py +++ b/src/transformers/models/lfm2_vl/image_processing_lfm2_vl.py @@ -15,6 +15,7 @@ from functools import lru_cache import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -29,14 +30,10 @@ from ...utils import ( TensorType, auto_docstring, - is_torchvision_available, logging, ) -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF - logger = logging.get_logger(__name__) diff --git a/src/transformers/models/lightglue/image_processing_lightglue.py b/src/transformers/models/lightglue/image_processing_lightglue.py index d55a0717ebdd..e7d812b7d749 100644 --- a/src/transformers/models/lightglue/image_processing_lightglue.py +++ b/src/transformers/models/lightglue/image_processing_lightglue.py @@ -20,7 +20,7 @@ from typing import TYPE_CHECKING import torch -from torchvision.transforms.v2 import functional as tvF +from PIL import Image, ImageDraw from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -36,14 +36,13 @@ to_numpy_array, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_vision_available +from ...utils import TensorType, auto_docstring if TYPE_CHECKING: from .modeling_lightglue import LightGlueKeypointMatchingOutput -if is_vision_available(): - from PIL import Image, ImageDraw +from torchvision.transforms.v2 import functional as tvF class LightGlueImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/llama4/image_processing_llama4.py b/src/transformers/models/llama4/image_processing_llama4.py index 2923f0af8ee1..1c055427251d 100644 --- a/src/transformers/models/llama4/image_processing_llama4.py +++ b/src/transformers/models/llama4/image_processing_llama4.py @@ -18,17 +18,14 @@ from functools import lru_cache import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images, split_to_tiles from ...image_utils import ImageInput, PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF +from ...utils import TensorType, auto_docstring def get_factors(dividend: int) -> set[int]: diff --git a/src/transformers/models/llava/image_processing_llava.py b/src/transformers/models/llava/image_processing_llava.py index 85d8b6b85a85..14d749e49ed3 100644 --- a/src/transformers/models/llava/image_processing_llava.py +++ b/src/transformers/models/llava/image_processing_llava.py @@ -15,6 +15,9 @@ from typing import Union +import torch +from torchvision.transforms.v2 import functional as tvF + from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import ( @@ -28,12 +31,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - import torch - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring @auto_docstring diff --git a/src/transformers/models/llava_next/image_processing_llava_next.py b/src/transformers/models/llava_next/image_processing_llava_next.py index b3b465f29d83..0b5c3b0c18af 100644 --- a/src/transformers/models/llava_next/image_processing_llava_next.py +++ b/src/transformers/models/llava_next/image_processing_llava_next.py @@ -15,6 +15,9 @@ from typing import Union +import torch +from torchvision.transforms.v2 import functional as tvF + from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import ( BatchFeature, @@ -32,14 +35,7 @@ get_image_size, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available - - -if is_torch_available(): - import torch - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring class LlavaNextImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/mask2former/image_processing_mask2former.py b/src/transformers/models/mask2former/image_processing_mask2former.py index aad154f71df3..a72540fe7a03 100644 --- a/src/transformers/models/mask2former/image_processing_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_mask2former.py @@ -24,6 +24,7 @@ import numpy as np import torch from torch import nn +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature, get_size_dict @@ -39,12 +40,9 @@ get_max_height_width, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available, logging +from ...utils import TensorType, auto_docstring, logging -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - logger = logging.get_logger(__name__) diff --git a/src/transformers/models/mask2former/image_processing_pil_mask2former.py b/src/transformers/models/mask2former/image_processing_pil_mask2former.py index 662f7b40c8c9..cfe412785caf 100644 --- a/src/transformers/models/mask2former/image_processing_pil_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_pil_mask2former.py @@ -44,7 +44,6 @@ TensorType, auto_docstring, is_torch_available, - is_torchvision_available, logging, requires_backends, ) @@ -60,8 +59,6 @@ if is_torch_available(): import torch from torch import nn -if is_torchvision_available(): - pass logger = logging.get_logger(__name__) @@ -172,7 +169,7 @@ def resize( Size of the image's `(height, width)` dimensions after resizing. size_divisor (`int`, *optional*, defaults to 0): If `size_divisor` is given, the output image size will be divisible by the number. - resample (`PILImageResampling | tvF.InterpolationMode | int | None`, *optional*): + resample (`PILImageResampling | int | None`, *optional*): Resampling filter to use if resizing the image. """ diff --git a/src/transformers/models/maskformer/image_processing_maskformer.py b/src/transformers/models/maskformer/image_processing_maskformer.py index 268a53cac4e7..9415bcf60e0b 100644 --- a/src/transformers/models/maskformer/image_processing_maskformer.py +++ b/src/transformers/models/maskformer/image_processing_maskformer.py @@ -19,6 +19,7 @@ import numpy as np import torch from torch import nn +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature, get_size_dict @@ -33,12 +34,9 @@ get_image_size_for_max_height_width, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available, logging +from ...utils import TensorType, auto_docstring, logging -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - logger = logging.get_logger(__name__) diff --git a/src/transformers/models/maskformer/image_processing_pil_maskformer.py b/src/transformers/models/maskformer/image_processing_pil_maskformer.py index cd06d34c8832..69f64ff84bb3 100644 --- a/src/transformers/models/maskformer/image_processing_pil_maskformer.py +++ b/src/transformers/models/maskformer/image_processing_pil_maskformer.py @@ -155,7 +155,7 @@ def resize( Size of the image's `(height, width)` dimensions after resizing. size_divisor (`int`, *optional*, defaults to 0): If `size_divisor` is given, the output image size will be divisible by the number. - resample (`PILImageResampling | tvF.InterpolationMode | int | None`, *optional*): + resample (`PILImageResampling | int | None`, *optional*): Resampling filter to use if resizing the image. """ diff --git a/src/transformers/models/mllama/image_processing_mllama.py b/src/transformers/models/mllama/image_processing_mllama.py index c64f283492ee..42ad79c8ad93 100644 --- a/src/transformers/models/mllama/image_processing_mllama.py +++ b/src/transformers/models/mllama/image_processing_mllama.py @@ -31,14 +31,13 @@ make_nested_list_of_images, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available, is_vision_available +from ...utils import TensorType, auto_docstring, is_vision_available if is_vision_available(): from PIL import Image -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from torchvision.transforms.v2 import functional as tvF class MllamaImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2.py b/src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2.py index 75c024050aaf..5ebe8ba72b5d 100644 --- a/src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2.py +++ b/src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2.py @@ -15,6 +15,9 @@ from typing import Union +import torch +from torchvision.transforms.v2 import functional as tvF + from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images @@ -27,14 +30,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available - - -if is_torch_available(): - import torch - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring, is_torch_available class MobileNetV2ImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/mobilevit/image_processing_mobilevit.py b/src/transformers/models/mobilevit/image_processing_mobilevit.py index d0a88d6b25da..d94c1912fbd9 100644 --- a/src/transformers/models/mobilevit/image_processing_mobilevit.py +++ b/src/transformers/models/mobilevit/image_processing_mobilevit.py @@ -15,6 +15,9 @@ from typing import Union +import torch +from torchvision.transforms.v2 import functional as tvF + from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images @@ -30,19 +33,11 @@ from ...utils import ( TensorType, auto_docstring, - is_torch_available, - is_torchvision_available, logging, requires_backends, ) -if is_torch_available(): - import torch - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - logger = logging.get_logger(__name__) diff --git a/src/transformers/models/nougat/image_processing_nougat.py b/src/transformers/models/nougat/image_processing_nougat.py index d4f28d497c9c..8e738dbf1838 100644 --- a/src/transformers/models/nougat/image_processing_nougat.py +++ b/src/transformers/models/nougat/image_processing_nougat.py @@ -14,6 +14,7 @@ """Image processor class for Nougat.""" import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -34,14 +35,9 @@ from ...utils import ( TensorType, auto_docstring, - is_torchvision_available, ) -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - class NougatImageProcessorKwargs(ImagesKwargs, total=False): r""" do_crop_margin (`bool`, *optional*, defaults to `self.do_crop_margin`): diff --git a/src/transformers/models/oneformer/image_processing_oneformer.py b/src/transformers/models/oneformer/image_processing_oneformer.py index 27d0e5e53193..ccee798bc39e 100644 --- a/src/transformers/models/oneformer/image_processing_oneformer.py +++ b/src/transformers/models/oneformer/image_processing_oneformer.py @@ -19,6 +19,7 @@ import torch from torch import nn +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -36,7 +37,6 @@ from ...utils import ( TensorType, auto_docstring, - is_torchvision_available, logging, ) @@ -52,10 +52,6 @@ logger = logging.get_logger(__name__) -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - class OneFormerImageProcessorKwargs(ImagesKwargs, total=False): r""" repo_path (`str`, *optional*, defaults to `shi-labs/oneformer_demo`): diff --git a/src/transformers/models/ovis2/image_processing_ovis2.py b/src/transformers/models/ovis2/image_processing_ovis2.py index c2f44db24b32..e15f35b2d4f1 100644 --- a/src/transformers/models/ovis2/image_processing_ovis2.py +++ b/src/transformers/models/ovis2/image_processing_ovis2.py @@ -16,6 +16,7 @@ from functools import lru_cache import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -28,11 +29,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring # Helper functions for patch/tile calculations diff --git a/src/transformers/models/owlv2/image_processing_pil_owlv2.py b/src/transformers/models/owlv2/image_processing_pil_owlv2.py index a2918840d288..d68c5da6abdb 100644 --- a/src/transformers/models/owlv2/image_processing_pil_owlv2.py +++ b/src/transformers/models/owlv2/image_processing_pil_owlv2.py @@ -49,7 +49,7 @@ if is_torch_available(): import torch if is_torchvision_available(): - pass + import torchvision.transforms.v2.functional as tvF def _preprocess_resize_output_shape(image, output_shape): @@ -206,7 +206,8 @@ def post_process_object_detection( - "labels": Indexes of the classes predicted by the model on the image. - "boxes": Image bounding boxes in (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format. """ - requires_backends(self, "torch") + import torch + batch_logits, batch_boxes = outputs.logits, outputs.pred_boxes batch_size = len(batch_logits) @@ -258,7 +259,8 @@ def post_process_image_guided_detection(self, outputs, threshold=0.0, nms_thresh in the batch as predicted by the model. All labels are set to None as `Owlv2ForObjectDetection.image_guided_detection` perform one-shot object detection. """ - requires_backends(self, "torch") + import torch + logits, target_boxes = outputs.logits, outputs.target_pred_boxes if target_sizes is not None and len(logits) != len(target_sizes): @@ -391,7 +393,7 @@ def _preprocess( images: list["torch.Tensor"], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: "PILImageResampling | tvF.InterpolationMode | int | None", do_pad: bool, do_rescale: bool, rescale_factor: float, diff --git a/src/transformers/models/paddleocr_vl/image_processing_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/image_processing_paddleocr_vl.py index 1ea14a179b23..14045b22d0f2 100644 --- a/src/transformers/models/paddleocr_vl/image_processing_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/image_processing_paddleocr_vl.py @@ -33,11 +33,7 @@ from ...image_transforms import group_images_by_shape, reorder_images from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring class PaddleOCRVLImageProcessorKwargs(ImagesKwargs, total=False): @@ -151,7 +147,7 @@ def _preprocess( images: list["torch.Tensor"], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | int | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py index ec994f6db6d3..506173f464ef 100644 --- a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py @@ -23,6 +23,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + from collections.abc import Iterable import numpy as np @@ -97,7 +98,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: "PILImageResampling | int | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/perceiver/image_processing_perceiver.py b/src/transformers/models/perceiver/image_processing_perceiver.py index 7d0db10d8f10..a6b8e5896d34 100644 --- a/src/transformers/models/perceiver/image_processing_perceiver.py +++ b/src/transformers/models/perceiver/image_processing_perceiver.py @@ -13,6 +13,9 @@ # limitations under the License. """Image processor class for Perceiver.""" +import torch +from torchvision.transforms.v2 import functional as tvF + from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images @@ -23,15 +26,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available - - -if is_torch_available(): - import torch - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring @auto_docstring diff --git a/src/transformers/models/perception_lm/image_processing_perception_lm.py b/src/transformers/models/perception_lm/image_processing_perception_lm.py index 12f4dc62b981..8de148bba50b 100644 --- a/src/transformers/models/perception_lm/image_processing_perception_lm.py +++ b/src/transformers/models/perception_lm/image_processing_perception_lm.py @@ -16,6 +16,7 @@ from functools import reduce import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -28,11 +29,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF +from ...utils import TensorType, auto_docstring class PerceptionLMImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/phi4_multimodal/image_processing_phi4_multimodal.py b/src/transformers/models/phi4_multimodal/image_processing_phi4_multimodal.py index 395a3ff23806..8cf56ad043f3 100644 --- a/src/transformers/models/phi4_multimodal/image_processing_phi4_multimodal.py +++ b/src/transformers/models/phi4_multimodal/image_processing_phi4_multimodal.py @@ -16,16 +16,13 @@ import math import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput, PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available, logging - - -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF +from ...utils import TensorType, auto_docstring, logging logger = logging.get_logger(__name__) diff --git a/src/transformers/models/pixtral/image_processing_pixtral.py b/src/transformers/models/pixtral/image_processing_pixtral.py index ecfb99ff5ba4..7988e446ec59 100644 --- a/src/transformers/models/pixtral/image_processing_pixtral.py +++ b/src/transformers/models/pixtral/image_processing_pixtral.py @@ -16,17 +16,14 @@ import math import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature, get_size_dict from ...image_transforms import group_images_by_shape, reorder_images from ...image_utils import ChannelDimension, ImageInput, PILImageResampling, SizeDict, get_image_size from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring def _num_image_tokens(image_size: tuple[int, int], patch_size: tuple[int, int]) -> int: diff --git a/src/transformers/models/poolformer/image_processing_poolformer.py b/src/transformers/models/poolformer/image_processing_poolformer.py index 47e836d701af..a316f2efb059 100644 --- a/src/transformers/models/poolformer/image_processing_poolformer.py +++ b/src/transformers/models/poolformer/image_processing_poolformer.py @@ -13,6 +13,9 @@ # limitations under the License. """Image processor class for PoolFormer.""" +import torch +from torchvision.transforms.v2 import functional as tvF + from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import get_resize_output_image_size, group_images_by_shape, reorder_images @@ -24,15 +27,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - -if is_torch_available(): - import torch +from ...utils import TensorType, auto_docstring class PoolFormerImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/pp_doclayout_v2/image_processing_pp_doclayout_v2.py b/src/transformers/models/pp_doclayout_v2/image_processing_pp_doclayout_v2.py index e0e9c769014e..ec8dce9c8b25 100644 --- a/src/transformers/models/pp_doclayout_v2/image_processing_pp_doclayout_v2.py +++ b/src/transformers/models/pp_doclayout_v2/image_processing_pp_doclayout_v2.py @@ -20,19 +20,16 @@ import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images from ...image_utils import PILImageResampling, SizeDict -from ...utils import auto_docstring, is_torchvision_available +from ...utils import auto_docstring from ...utils.generic import TensorType -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - @auto_docstring class PPDocLayoutV2ImageProcessor(TorchvisionBackend): resample = PILImageResampling.BICUBIC diff --git a/src/transformers/models/pp_doclayout_v3/image_processing_pp_doclayout_v3.py b/src/transformers/models/pp_doclayout_v3/image_processing_pp_doclayout_v3.py index ed8fea6bf9a1..be789f8bf8f4 100644 --- a/src/transformers/models/pp_doclayout_v3/image_processing_pp_doclayout_v3.py +++ b/src/transformers/models/pp_doclayout_v3/image_processing_pp_doclayout_v3.py @@ -21,18 +21,16 @@ import numpy as np import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images from ...image_utils import PILImageResampling, SizeDict -from ...utils import auto_docstring, is_cv2_available, is_torchvision_available, requires_backends +from ...utils import auto_docstring, is_cv2_available, requires_backends from ...utils.generic import TensorType -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - if is_cv2_available(): import cv2 diff --git a/src/transformers/models/pp_doclayout_v3/modular_pp_doclayout_v3.py b/src/transformers/models/pp_doclayout_v3/modular_pp_doclayout_v3.py index 3c4a68b9a9ff..0c4303598ac8 100644 --- a/src/transformers/models/pp_doclayout_v3/modular_pp_doclayout_v3.py +++ b/src/transformers/models/pp_doclayout_v3/modular_pp_doclayout_v3.py @@ -20,6 +20,7 @@ import torch.nn.functional as F from huggingface_hub.dataclasses import strict from torch import nn +from torchvision.transforms.v2 import functional as tvF from ... import initialization as init from ...backbone_utils import consolidate_backbone_kwargs_to_config @@ -38,7 +39,6 @@ TransformersKwargs, auto_docstring, is_cv2_available, - is_torchvision_available, logging, requires_backends, ) @@ -60,9 +60,6 @@ ) -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - if is_cv2_available(): import cv2 diff --git a/src/transformers/models/prompt_depth_anything/image_processing_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/image_processing_prompt_depth_anything.py index 6df03a3f2cfe..5d8bda908269 100644 --- a/src/transformers/models/prompt_depth_anything/image_processing_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/image_processing_prompt_depth_anything.py @@ -31,7 +31,6 @@ from ...utils import ( TensorType, auto_docstring, - is_torchvision_available, requires_backends, ) @@ -39,9 +38,8 @@ if TYPE_CHECKING: from ...modeling_outputs import DepthEstimatorOutput -if is_torchvision_available(): - import torch - from torchvision.transforms.v2 import functional as tvF +import torch +from torchvision.transforms.v2 import functional as tvF class PromptDepthAnythingImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/qwen2_vl/image_processing_qwen2_vl.py b/src/transformers/models/qwen2_vl/image_processing_qwen2_vl.py index 1cda5ce8d238..70a4868eeee1 100644 --- a/src/transformers/models/qwen2_vl/image_processing_qwen2_vl.py +++ b/src/transformers/models/qwen2_vl/image_processing_qwen2_vl.py @@ -22,6 +22,7 @@ from collections.abc import Iterable import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -34,11 +35,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring class Qwen2VLImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py index 8ea4102cd926..fd290d22940a 100644 --- a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py @@ -55,7 +55,7 @@ if is_torch_available(): import torch if is_torchvision_available(): - pass + import torchvision.transforms.v2.functional as tvF SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) @@ -392,7 +392,7 @@ def _preprocess( masks_path: str | pathlib.Path | None, do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: "PILImageResampling | tvF.InterpolationMode | int | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/sam/image_processing_pil_sam.py b/src/transformers/models/sam/image_processing_pil_sam.py index 69141339556f..e7f6f8b5e54c 100644 --- a/src/transformers/models/sam/image_processing_pil_sam.py +++ b/src/transformers/models/sam/image_processing_pil_sam.py @@ -143,7 +143,7 @@ def resize( Dictionary in the format `{"longest_edge": int}` specifying the size of the output image. The longest edge of the image will be resized to the specified size, while the other edge will be resized to maintain the aspect ratio. - resample (`PILImageResampling | tvF.InterpolationMode | int | None`, *optional*): + resample (`PILImageResampling | int | None`, *optional*): Resampling filter to use when resizing the image. Returns: diff --git a/src/transformers/models/sam/image_processing_sam.py b/src/transformers/models/sam/image_processing_sam.py index 34d782b6ac5c..a6b30fca76c0 100644 --- a/src/transformers/models/sam/image_processing_sam.py +++ b/src/transformers/models/sam/image_processing_sam.py @@ -23,6 +23,7 @@ import torch from torch.nn import functional as F from torchvision.ops.boxes import batched_nms +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature, get_size_dict @@ -36,12 +37,9 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available, is_vision_available +from ...utils import TensorType, auto_docstring, is_vision_available -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF - if is_vision_available(): import PIL diff --git a/src/transformers/models/segformer/image_processing_pil_segformer.py b/src/transformers/models/segformer/image_processing_pil_segformer.py index f7774fd8fb9c..9da099ec661b 100644 --- a/src/transformers/models/segformer/image_processing_pil_segformer.py +++ b/src/transformers/models/segformer/image_processing_pil_segformer.py @@ -32,14 +32,13 @@ SizeDict, ) from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available +from ...utils import TensorType, is_torch_available, is_torchvision_available from ...utils.import_utils import requires from .image_processing_segformer import SegformerImageProcessorKwargs if is_torch_available(): - import torch - import torch.nn.functional as F + pass if is_torchvision_available(): import torchvision.transforms.v2.functional as tvF @@ -65,7 +64,7 @@ class SegformerImageProcessorPil(PilBackend): def __init__(self, **kwargs: Unpack[SegformerImageProcessorKwargs]): super().__init__(**kwargs) - @auto_docstring + # @auto_docstring def preprocess( self, images: ImageInput, @@ -139,7 +138,7 @@ def _preprocess( self, images: list["np.ndarray"], do_reduce_labels: bool, - resample: PILImageResampling | int | None, + resample: "PILImageResampling | tvF.InterpolationMode | int | None", do_resize: bool, do_rescale: bool, do_normalize: bool, @@ -164,6 +163,7 @@ def _preprocess( return processed_images + @requires(backends=("torch",)) def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] | None = None): """ Converts the output of [`SegformerForSemanticSegmentation`] into semantic segmentation maps. @@ -180,8 +180,8 @@ def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. """ - if not is_torch_available(): - raise ImportError("PyTorch is required for post_process_semantic_segmentation") + import torch + import torch.nn.functional as F logits = outputs.logits diff --git a/src/transformers/models/segformer/image_processing_segformer.py b/src/transformers/models/segformer/image_processing_segformer.py index fbf85e451954..4f00674bcf82 100644 --- a/src/transformers/models/segformer/image_processing_segformer.py +++ b/src/transformers/models/segformer/image_processing_segformer.py @@ -21,6 +21,7 @@ from typing import Union import torch +import torch.nn.functional as F import torchvision.transforms.v2.functional as tvF from ...image_processing_backends import TorchvisionBackend @@ -38,10 +39,6 @@ from ...utils import TensorType, auto_docstring, is_torch_available -if is_torch_available(): - import torch.nn.functional as F - - class SegformerImageProcessorKwargs(ImagesKwargs, total=False): r""" do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): diff --git a/src/transformers/models/seggpt/image_processing_seggpt.py b/src/transformers/models/seggpt/image_processing_seggpt.py index 24c0c221a67d..f9fbb288c4a7 100644 --- a/src/transformers/models/seggpt/image_processing_seggpt.py +++ b/src/transformers/models/seggpt/image_processing_seggpt.py @@ -16,6 +16,8 @@ from typing import Union import numpy as np +import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -29,14 +31,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available, requires_backends - - -if is_torch_available(): - import torch - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring, requires_backends # See https://huggingface.co/papers/2212.02499 at 3.1 Redefining Output Spaces as "Images" - Semantic Segmentation diff --git a/src/transformers/models/siglip2/image_processing_siglip2.py b/src/transformers/models/siglip2/image_processing_siglip2.py index 5a090fc42445..5e21a6e34ad7 100644 --- a/src/transformers/models/siglip2/image_processing_siglip2.py +++ b/src/transformers/models/siglip2/image_processing_siglip2.py @@ -14,6 +14,7 @@ """Image processor class for SigLIP2.""" import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -22,14 +23,9 @@ from ...utils import ( TensorType, auto_docstring, - is_torchvision_available, ) -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - class Siglip2ImageProcessorKwargs(ImagesKwargs, total=False): r""" patch_size (`int`, *optional*, defaults to `self.patch_size`): diff --git a/src/transformers/models/smolvlm/image_processing_smolvlm.py b/src/transformers/models/smolvlm/image_processing_smolvlm.py index 3c670612577f..6c22e496c669 100644 --- a/src/transformers/models/smolvlm/image_processing_smolvlm.py +++ b/src/transformers/models/smolvlm/image_processing_smolvlm.py @@ -24,6 +24,7 @@ import numpy as np import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -37,11 +38,7 @@ make_nested_list_of_images, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring class SmolVLMImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/superglue/image_processing_superglue.py b/src/transformers/models/superglue/image_processing_superglue.py index d26eb79244fe..1e47fcb99e5b 100644 --- a/src/transformers/models/superglue/image_processing_superglue.py +++ b/src/transformers/models/superglue/image_processing_superglue.py @@ -34,7 +34,6 @@ from ...utils import ( TensorType, auto_docstring, - is_torchvision_available, is_vision_available, ) @@ -45,8 +44,7 @@ if is_vision_available(): from PIL import Image, ImageDraw -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from torchvision.transforms.v2 import functional as tvF def _is_valid_image(image): diff --git a/src/transformers/models/superpoint/image_processing_superpoint.py b/src/transformers/models/superpoint/image_processing_superpoint.py index b0424ca13762..a0e9660b5216 100644 --- a/src/transformers/models/superpoint/image_processing_superpoint.py +++ b/src/transformers/models/superpoint/image_processing_superpoint.py @@ -22,14 +22,13 @@ from ...image_transforms import group_images_by_shape, reorder_images from ...image_utils import PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available +from ...utils import TensorType, auto_docstring if TYPE_CHECKING: from .modeling_superpoint import SuperPointKeypointDescriptionOutput -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF +from torchvision.transforms.v2 import functional as tvF class SuperPointImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/swin2sr/image_processing_swin2sr.py b/src/transformers/models/swin2sr/image_processing_swin2sr.py index 9bebd5be8072..0c1a3ce7466f 100644 --- a/src/transformers/models/swin2sr/image_processing_swin2sr.py +++ b/src/transformers/models/swin2sr/image_processing_swin2sr.py @@ -13,6 +13,9 @@ # limitations under the License. """Image processor class for Swin2SR.""" +import torch +from torchvision.transforms.v2 import functional as tvF + from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images @@ -22,12 +25,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - import torch - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring class Swin2SRImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/textnet/image_processing_textnet.py b/src/transformers/models/textnet/image_processing_textnet.py index 733442fc5ff4..df82faf0a265 100644 --- a/src/transformers/models/textnet/image_processing_textnet.py +++ b/src/transformers/models/textnet/image_processing_textnet.py @@ -13,6 +13,9 @@ # limitations under the License. """Image processor class for TextNet.""" +import torch +from torchvision.transforms.v2 import functional as tvF + from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import get_resize_output_image_size, group_images_by_shape, reorder_images @@ -25,14 +28,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - -if is_torch_available(): - import torch +from ...utils import TensorType, auto_docstring class TextNetImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/tvp/image_processing_tvp.py b/src/transformers/models/tvp/image_processing_tvp.py index 75524f3b1ba0..c13feeddf947 100644 --- a/src/transformers/models/tvp/image_processing_tvp.py +++ b/src/transformers/models/tvp/image_processing_tvp.py @@ -14,6 +14,7 @@ """Image processor class for TVP.""" import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -27,11 +28,7 @@ make_nested_list_of_images, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF +from ...utils import TensorType, auto_docstring class TvpImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py index 0e9d58e8f772..eca723afaae7 100644 --- a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py +++ b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py @@ -17,6 +17,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + from collections.abc import Iterable import numpy as np @@ -95,7 +96,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: "PILImageResampling | int | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/video_llama_3/image_processing_video_llama_3.py b/src/transformers/models/video_llama_3/image_processing_video_llama_3.py index 46cfbc5b0487..ef81b31bc97a 100644 --- a/src/transformers/models/video_llama_3/image_processing_video_llama_3.py +++ b/src/transformers/models/video_llama_3/image_processing_video_llama_3.py @@ -27,11 +27,7 @@ from ...image_transforms import group_images_by_shape, reorder_images from ...image_utils import IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring class VideoLlama3ImageProcessorKwargs(ImagesKwargs, total=False): @@ -150,7 +146,7 @@ def _preprocess( images: list["torch.Tensor"], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | int | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/video_llama_3/video_processing_video_llama_3.py b/src/transformers/models/video_llama_3/video_processing_video_llama_3.py index 74aed4957621..d6a04c8f0be6 100644 --- a/src/transformers/models/video_llama_3/video_processing_video_llama_3.py +++ b/src/transformers/models/video_llama_3/video_processing_video_llama_3.py @@ -32,16 +32,12 @@ get_image_size, ) from ...processing_utils import Unpack, VideosKwargs -from ...utils import TensorType, add_start_docstrings, is_torchvision_available +from ...utils import TensorType, add_start_docstrings from ...video_processing_utils import BASE_VIDEO_PROCESSOR_DOCSTRING, BaseVideoProcessor from ...video_utils import VideoMetadata, group_videos_by_shape, reorder_videos from .image_processing_video_llama_3 import smart_resize -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - class VideoLlama3VideoProcessorInitKwargs(VideosKwargs, total=False): min_pixels: int max_pixels: int @@ -201,7 +197,7 @@ def _preprocess( do_convert_rgb: bool, do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | int | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/videomae/image_processing_videomae.py b/src/transformers/models/videomae/image_processing_videomae.py index a6fe544efebf..fa0ca726f4cd 100644 --- a/src/transformers/models/videomae/image_processing_videomae.py +++ b/src/transformers/models/videomae/image_processing_videomae.py @@ -13,6 +13,9 @@ # limitations under the License. """Image processor class for VideoMAE.""" +import torch +from torchvision.transforms.v2 import functional as tvF + from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature from ...image_transforms import group_images_by_shape, reorder_images @@ -25,14 +28,7 @@ make_nested_list_of_images, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available - - -if is_torch_available(): - import torch - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring @auto_docstring diff --git a/src/transformers/models/vilt/image_processing_vilt.py b/src/transformers/models/vilt/image_processing_vilt.py index 0a4d3807ec21..488691aef0f2 100644 --- a/src/transformers/models/vilt/image_processing_vilt.py +++ b/src/transformers/models/vilt/image_processing_vilt.py @@ -14,6 +14,7 @@ """Image processor class for Vilt.""" import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -29,14 +30,9 @@ from ...utils import ( TensorType, auto_docstring, - is_torchvision_available, ) -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - - # Set maximum size based on the typical aspect ratio of the COCO dataset MAX_LONGER_EDGE = 1333 MAX_SHORTER_EDGE = 800 diff --git a/src/transformers/models/vitmatte/image_processing_vitmatte.py b/src/transformers/models/vitmatte/image_processing_vitmatte.py index 215879c8cb24..7e6b5da92119 100644 --- a/src/transformers/models/vitmatte/image_processing_vitmatte.py +++ b/src/transformers/models/vitmatte/image_processing_vitmatte.py @@ -16,6 +16,7 @@ from typing import Union import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -28,11 +29,7 @@ get_image_size, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available - - -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF +from ...utils import TensorType, auto_docstring class VitMatteImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/vitpose/image_processing_vitpose.py b/src/transformers/models/vitpose/image_processing_vitpose.py index 8e173c42b681..6ca1e5a97f28 100644 --- a/src/transformers/models/vitpose/image_processing_vitpose.py +++ b/src/transformers/models/vitpose/image_processing_vitpose.py @@ -18,6 +18,8 @@ from typing import TYPE_CHECKING, Union import numpy as np +import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -35,19 +37,11 @@ TensorType, auto_docstring, is_scipy_available, - is_torch_available, - is_torchvision_available, logging, requires_backends, ) -if is_torch_available(): - import torch - -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF - if is_scipy_available(): from scipy.linalg import inv from scipy.ndimage import affine_transform, gaussian_filter diff --git a/src/transformers/models/zoedepth/image_processing_zoedepth.py b/src/transformers/models/zoedepth/image_processing_zoedepth.py index 6daa768e2277..fd1c0f9f79d5 100644 --- a/src/transformers/models/zoedepth/image_processing_zoedepth.py +++ b/src/transformers/models/zoedepth/image_processing_zoedepth.py @@ -34,14 +34,13 @@ pil_torch_interpolation_mapping, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available, requires_backends +from ...utils import TensorType, auto_docstring, requires_backends if TYPE_CHECKING: from .modeling_zoedepth import ZoeDepthDepthEstimatorOutput -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF +from torchvision.transforms.v2 import functional as tvF class ZoeDepthImageProcessorKwargs(ImagesKwargs, total=False): From 6e906b5491e3388f2a671eeb364b9ddc737988f6 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 12:56:27 +0100 Subject: [PATCH 10/30] fiixup --- src/transformers/models/aria/modular_aria.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/transformers/models/aria/modular_aria.py b/src/transformers/models/aria/modular_aria.py index 461af11e73fa..bfd5191e4135 100644 --- a/src/transformers/models/aria/modular_aria.py +++ b/src/transformers/models/aria/modular_aria.py @@ -14,6 +14,7 @@ import torch from huggingface_hub.dataclasses import strict from torch import nn +from torchvision.transforms.v2 import functional as tvF from ... import initialization as init from ...activations import ACT2FN @@ -321,10 +322,6 @@ def forward(self, key_value_states: torch.Tensor, attn_mask: torch.Tensor | None return out -import torch -from torchvision.transforms.v2 import functional as tvF - - class AriaImageProcessorKwargs(ImagesKwargs, total=False): r""" max_image_size (`int`, *optional*, defaults to `self.max_image_size`): From d266fedc3e9b2cc73d8b9d8ddfdca04e161c9c52 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 12:58:18 +0100 Subject: [PATCH 11/30] fix-repo --- src/transformers/models/aria/modeling_aria.py | 7 +------ .../models/cohere2_vision/modeling_cohere2_vision.py | 7 +------ .../image_processing_pil_conditional_detr.py | 9 +-------- .../models/deformable_detr/modeling_deformable_detr.py | 7 +------ src/transformers/models/dpt/image_processing_dpt.py | 3 +-- .../image_processing_pil_ernie4_5_vl_moe.py | 2 +- .../models/ernie4_5_vl_moe/modeling_ernie4_5_vl_moe.py | 8 +------- .../models/glm46v/video_processing_glm46v.py | 7 ++----- src/transformers/models/glm_image/modeling_glm_image.py | 7 ++----- .../models/glm_image/processing_glm_image.py | 7 ++----- .../grounding_dino/image_processing_grounding_dino.py | 4 +--- .../image_processing_pil_grounding_dino.py | 8 +------- .../models/lightglue/image_processing_lightglue.py | 3 +-- .../models/lightglue/image_processing_pil_lightglue.py | 8 ++++---- src/transformers/models/lightglue/modeling_lightglue.py | 7 +------ .../models/lighton_ocr/modeling_lighton_ocr.py | 7 +------ .../models/llava_onevision/modeling_llava_onevision.py | 6 +----- .../mask2former/image_processing_pil_mask2former.py | 8 +------- .../models/paddleocr_vl/modeling_paddleocr_vl.py | 9 +-------- .../models/rt_detr/image_processing_pil_rt_detr.py | 8 +++++++- src/transformers/models/sam2/image_processing_sam2.py | 6 +----- src/transformers/models/smolvlm/modeling_smolvlm.py | 8 +------- .../models/video_llama_3/modeling_video_llama_3.py | 7 +------ .../models/yolos/image_processing_pil_yolos.py | 8 +------- 24 files changed, 36 insertions(+), 125 deletions(-) diff --git a/src/transformers/models/aria/modeling_aria.py b/src/transformers/models/aria/modeling_aria.py index 85f41712db02..e66b12438940 100644 --- a/src/transformers/models/aria/modeling_aria.py +++ b/src/transformers/models/aria/modeling_aria.py @@ -41,12 +41,7 @@ from ...modeling_rope_utils import ROPE_INIT_FUNCTIONS, dynamic_rope_update from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel from ...processing_utils import Unpack -from ...utils import ( - TransformersKwargs, - auto_docstring, - can_return_tuple, - torch_compilable_check, -) +from ...utils import TransformersKwargs, auto_docstring, can_return_tuple, torch_compilable_check from ...utils.generic import maybe_autocast, merge_with_config_defaults from ...utils.output_capturing import capture_outputs from ..auto import AutoModel diff --git a/src/transformers/models/cohere2_vision/modeling_cohere2_vision.py b/src/transformers/models/cohere2_vision/modeling_cohere2_vision.py index ed861f58006f..048412b383e7 100644 --- a/src/transformers/models/cohere2_vision/modeling_cohere2_vision.py +++ b/src/transformers/models/cohere2_vision/modeling_cohere2_vision.py @@ -28,12 +28,7 @@ from ...modeling_outputs import BaseModelOutputWithPast, BaseModelOutputWithPooling, ModelOutput from ...modeling_utils import PreTrainedModel from ...processing_utils import Unpack -from ...utils import ( - TransformersKwargs, - auto_docstring, - can_return_tuple, - torch_compilable_check, -) +from ...utils import TransformersKwargs, auto_docstring, can_return_tuple, torch_compilable_check from ..auto import AutoModel from .configuration_cohere2_vision import Cohere2VisionConfig diff --git a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py index 94fc944123ba..ec34b05d72f1 100644 --- a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py +++ b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py @@ -49,14 +49,7 @@ validate_annotations, ) from ...processing_utils import Unpack -from ...utils import ( - TensorType, - auto_docstring, - is_torch_available, - is_vision_available, - logging, - requires_backends, -) +from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, logging, requires_backends from ...utils.import_utils import requires from .image_processing_conditional_detr import ( ConditionalDetrImageProcessorKwargs, diff --git a/src/transformers/models/deformable_detr/modeling_deformable_detr.py b/src/transformers/models/deformable_detr/modeling_deformable_detr.py index 21d739f57d03..3ee685a887c1 100755 --- a/src/transformers/models/deformable_detr/modeling_deformable_detr.py +++ b/src/transformers/models/deformable_detr/modeling_deformable_detr.py @@ -36,12 +36,7 @@ from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel from ...processing_utils import Unpack from ...pytorch_utils import compile_compatible_method_lru_cache -from ...utils import ( - ModelOutput, - TransformersKwargs, - auto_docstring, - torch_compilable_check, -) +from ...utils import ModelOutput, TransformersKwargs, auto_docstring, torch_compilable_check from ...utils.generic import can_return_tuple, merge_with_config_defaults from ...utils.output_capturing import OutputRecorder, capture_outputs from .configuration_deformable_detr import DeformableDetrConfig diff --git a/src/transformers/models/dpt/image_processing_dpt.py b/src/transformers/models/dpt/image_processing_dpt.py index d6503b0212a9..6d157f6385c0 100644 --- a/src/transformers/models/dpt/image_processing_dpt.py +++ b/src/transformers/models/dpt/image_processing_dpt.py @@ -25,6 +25,7 @@ import torch import torch.nn.functional as F +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_base import BatchFeature @@ -44,8 +45,6 @@ if TYPE_CHECKING: from ...modeling_outputs import DepthEstimatorOutput -from torchvision.transforms.v2 import functional as tvF - class DPTImageProcessorKwargs(ImagesKwargs, total=False): r""" diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py index 1bbff5d029c9..fcac9a91a21b 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py @@ -31,7 +31,7 @@ if is_torch_available(): import torch if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF + from torchvision.transforms.v2 import functional as tvF logger = logging.get_logger(__name__) diff --git a/src/transformers/models/ernie4_5_vl_moe/modeling_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/modeling_ernie4_5_vl_moe.py index 44dfb84e1431..d66e9fdc5dc7 100644 --- a/src/transformers/models/ernie4_5_vl_moe/modeling_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/modeling_ernie4_5_vl_moe.py @@ -38,13 +38,7 @@ from ...modeling_rope_utils import dynamic_rope_update from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel from ...processing_utils import Unpack -from ...utils import ( - TransformersKwargs, - auto_docstring, - can_return_tuple, - logging, - torch_compilable_check, -) +from ...utils import TransformersKwargs, auto_docstring, can_return_tuple, logging, torch_compilable_check from ...utils.generic import is_flash_attention_requested, maybe_autocast, merge_with_config_defaults from ...utils.output_capturing import OutputRecorder, capture_outputs from .configuration_ernie4_5_vl_moe import Ernie4_5_VLMoeConfig, Ernie4_5_VLMoeTextConfig, Ernie4_5_VLMoeVisionConfig diff --git a/src/transformers/models/glm46v/video_processing_glm46v.py b/src/transformers/models/glm46v/video_processing_glm46v.py index 7aac978113b7..5ca284d61856 100644 --- a/src/transformers/models/glm46v/video_processing_glm46v.py +++ b/src/transformers/models/glm46v/video_processing_glm46v.py @@ -21,6 +21,7 @@ import numpy as np import torch +from torchvision.transforms.v2 import functional as tvF from ...image_processing_utils import BatchFeature from ...image_utils import ( @@ -32,16 +33,12 @@ get_image_size, ) from ...processing_utils import Unpack, VideosKwargs -from ...utils import TensorType, add_start_docstrings, is_torchvision_available +from ...utils import TensorType, add_start_docstrings from ...video_processing_utils import BASE_VIDEO_PROCESSOR_DOCSTRING, BaseVideoProcessor from ...video_utils import VideoMetadata, group_videos_by_shape, reorder_videos from .image_processing_glm46v import smart_resize -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF - - class Glm46VVideoProcessorInitKwargs(VideosKwargs, total=False): max_image_size: dict[str, int] patch_size: int diff --git a/src/transformers/models/glm_image/modeling_glm_image.py b/src/transformers/models/glm_image/modeling_glm_image.py index c70bdd641181..7215076ea8cf 100644 --- a/src/transformers/models/glm_image/modeling_glm_image.py +++ b/src/transformers/models/glm_image/modeling_glm_image.py @@ -22,6 +22,7 @@ from dataclasses import dataclass from typing import Any, Optional +import torch import torch.nn as nn import torch.nn.functional as F @@ -36,16 +37,12 @@ from ...modeling_rope_utils import ROPE_INIT_FUNCTIONS, dynamic_rope_update from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel from ...processing_utils import Unpack -from ...utils import TransformersKwargs, auto_docstring, can_return_tuple, is_torch_available +from ...utils import TransformersKwargs, auto_docstring, can_return_tuple from ...utils.generic import maybe_autocast, merge_with_config_defaults from ...utils.output_capturing import capture_outputs from .configuration_glm_image import GlmImageConfig, GlmImageTextConfig, GlmImageVisionConfig, GlmImageVQVAEConfig -if is_torch_available(): - import torch - - class GlmImageVisionMLP(nn.Module): def __init__(self, config): super().__init__() diff --git a/src/transformers/models/glm_image/processing_glm_image.py b/src/transformers/models/glm_image/processing_glm_image.py index d60deb77c0e5..07f4103a10a4 100644 --- a/src/transformers/models/glm_image/processing_glm_image.py +++ b/src/transformers/models/glm_image/processing_glm_image.py @@ -20,15 +20,12 @@ import math +import torch + from ...feature_extraction_utils import BatchFeature from ...image_utils import ImageInput from ...processing_utils import ImagesKwargs, ProcessingKwargs, ProcessorMixin, Unpack from ...tokenization_utils_base import PreTokenizedInput, TextInput -from ...utils import is_torch_available - - -if is_torch_available(): - import torch class GlmImageImagesKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/grounding_dino/image_processing_grounding_dino.py b/src/transformers/models/grounding_dino/image_processing_grounding_dino.py index 34284dfdc8f5..a9af064973d3 100644 --- a/src/transformers/models/grounding_dino/image_processing_grounding_dino.py +++ b/src/transformers/models/grounding_dino/image_processing_grounding_dino.py @@ -28,6 +28,7 @@ import torch from torchvision.io import read_image +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature, get_size_dict @@ -59,9 +60,6 @@ from .modeling_grounding_dino import GroundingDinoObjectDetectionOutput -from torchvision.transforms.v2 import functional as tvF - - class GroundingDinoImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): diff --git a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py index da1c57930f59..0b1367d4507e 100644 --- a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py +++ b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py @@ -54,13 +54,7 @@ validate_annotations, ) from ...processing_utils import Unpack -from ...utils import ( - TensorType, - auto_docstring, - is_torch_available, - is_vision_available, - requires_backends, -) +from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, requires_backends from ...utils.import_utils import requires from .image_processing_grounding_dino import GroundingDinoImageProcessorKwargs diff --git a/src/transformers/models/lightglue/image_processing_lightglue.py b/src/transformers/models/lightglue/image_processing_lightglue.py index e7d812b7d749..c2c5b02922d5 100644 --- a/src/transformers/models/lightglue/image_processing_lightglue.py +++ b/src/transformers/models/lightglue/image_processing_lightglue.py @@ -21,6 +21,7 @@ import torch from PIL import Image, ImageDraw +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature @@ -42,8 +43,6 @@ if TYPE_CHECKING: from .modeling_lightglue import LightGlueKeypointMatchingOutput -from torchvision.transforms.v2 import functional as tvF - class LightGlueImageProcessorKwargs(ImagesKwargs, total=False): r""" diff --git a/src/transformers/models/lightglue/image_processing_pil_lightglue.py b/src/transformers/models/lightglue/image_processing_pil_lightglue.py index 2b6fc3dfc480..67168e868358 100644 --- a/src/transformers/models/lightglue/image_processing_pil_lightglue.py +++ b/src/transformers/models/lightglue/image_processing_pil_lightglue.py @@ -27,15 +27,15 @@ from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput, PILImageResampling, SizeDict, to_numpy_array from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring +from ...utils import TensorType, auto_docstring, is_torch_available from ...utils.import_utils import requires from .image_processing_lightglue import LightGlueImageProcessorKwargs, validate_and_format_image_pairs if TYPE_CHECKING: - import torch - from .modeling_lightglue import LightGlueKeypointMatchingOutput +if is_torch_available(): + import torch def is_grayscale(image: np.ndarray): @@ -72,7 +72,7 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: return image -@auto_docstring +@requires(backends=("torch",)) class LightGlueImageProcessorPil(PilBackend): valid_kwargs = LightGlueImageProcessorKwargs resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/lightglue/modeling_lightglue.py b/src/transformers/models/lightglue/modeling_lightglue.py index e6f07b7bbced..f3aea377356e 100644 --- a/src/transformers/models/lightglue/modeling_lightglue.py +++ b/src/transformers/models/lightglue/modeling_lightglue.py @@ -30,12 +30,7 @@ from ...modeling_flash_attention_utils import FlashAttentionKwargs from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel from ...processing_utils import Unpack -from ...utils import ( - ModelOutput, - TransformersKwargs, - auto_docstring, - can_return_tuple, -) +from ...utils import ModelOutput, TransformersKwargs, auto_docstring, can_return_tuple from ..auto.modeling_auto import AutoModelForKeypointDetection from .configuration_lightglue import LightGlueConfig diff --git a/src/transformers/models/lighton_ocr/modeling_lighton_ocr.py b/src/transformers/models/lighton_ocr/modeling_lighton_ocr.py index 998f57cf56a2..946971c02119 100644 --- a/src/transformers/models/lighton_ocr/modeling_lighton_ocr.py +++ b/src/transformers/models/lighton_ocr/modeling_lighton_ocr.py @@ -29,12 +29,7 @@ from ...modeling_outputs import BaseModelOutputWithPast, BaseModelOutputWithPooling, ModelOutput from ...modeling_utils import PreTrainedModel from ...processing_utils import Unpack -from ...utils import ( - TransformersKwargs, - auto_docstring, - can_return_tuple, - torch_compilable_check, -) +from ...utils import TransformersKwargs, auto_docstring, can_return_tuple, torch_compilable_check from ...utils.generic import merge_with_config_defaults from ..auto import AutoModel from .configuration_lighton_ocr import LightOnOcrConfig diff --git a/src/transformers/models/llava_onevision/modeling_llava_onevision.py b/src/transformers/models/llava_onevision/modeling_llava_onevision.py index 22164ea7e218..a6ef4131de48 100644 --- a/src/transformers/models/llava_onevision/modeling_llava_onevision.py +++ b/src/transformers/models/llava_onevision/modeling_llava_onevision.py @@ -34,11 +34,7 @@ from ...modeling_outputs import BaseModelOutputWithPast, BaseModelOutputWithPooling, ModelOutput from ...modeling_utils import PreTrainedModel from ...processing_utils import Unpack -from ...utils import ( - TransformersKwargs, - auto_docstring, - torch_compilable_check, -) +from ...utils import TransformersKwargs, auto_docstring, torch_compilable_check from ...utils.generic import can_return_tuple, merge_with_config_defaults from ..auto import AutoModel from .configuration_llava_onevision import LlavaOnevisionConfig diff --git a/src/transformers/models/mask2former/image_processing_pil_mask2former.py b/src/transformers/models/mask2former/image_processing_pil_mask2former.py index cfe412785caf..935d767b4956 100644 --- a/src/transformers/models/mask2former/image_processing_pil_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_pil_mask2former.py @@ -40,13 +40,7 @@ get_max_height_width, ) from ...processing_utils import Unpack -from ...utils import ( - TensorType, - auto_docstring, - is_torch_available, - logging, - requires_backends, -) +from ...utils import TensorType, auto_docstring, is_torch_available, logging, requires_backends from ...utils.import_utils import requires from .image_processing_mask2former import ( Mask2FormerImageProcessorKwargs, diff --git a/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py index 31db841cc0a0..8ed3be0ad4be 100644 --- a/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/modeling_paddleocr_vl.py @@ -43,14 +43,7 @@ from ...modeling_rope_utils import ROPE_INIT_FUNCTIONS from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel from ...processing_utils import Unpack -from ...utils import ( - TransformersKwargs, - auto_docstring, - can_return_tuple, - logging, - torch_compilable_check, - torch_int, -) +from ...utils import TransformersKwargs, auto_docstring, can_return_tuple, logging, torch_compilable_check, torch_int from ...utils.generic import is_flash_attention_requested, maybe_autocast, merge_with_config_defaults from ...utils.output_capturing import capture_outputs from .configuration_paddleocr_vl import PaddleOCRTextConfig, PaddleOCRVisionConfig, PaddleOCRVLConfig diff --git a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py index fd290d22940a..74079f0102d5 100644 --- a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py @@ -47,7 +47,13 @@ validate_annotations, ) from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available, requires_backends +from ...utils import ( + TensorType, + auto_docstring, + is_torch_available, + is_torchvision_available, + requires_backends, +) from ...utils.import_utils import requires from .image_processing_rt_detr import RTDetrImageProcessorKwargs diff --git a/src/transformers/models/sam2/image_processing_sam2.py b/src/transformers/models/sam2/image_processing_sam2.py index 55bfbb07ad98..e5eb4d2d747d 100644 --- a/src/transformers/models/sam2/image_processing_sam2.py +++ b/src/transformers/models/sam2/image_processing_sam2.py @@ -38,11 +38,7 @@ SizeDict, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import ( - TensorType, - auto_docstring, - is_vision_available, -) +from ...utils import TensorType, auto_docstring, is_vision_available if is_vision_available(): diff --git a/src/transformers/models/smolvlm/modeling_smolvlm.py b/src/transformers/models/smolvlm/modeling_smolvlm.py index 493c7630587a..ed2eec345b75 100644 --- a/src/transformers/models/smolvlm/modeling_smolvlm.py +++ b/src/transformers/models/smolvlm/modeling_smolvlm.py @@ -35,13 +35,7 @@ from ...modeling_outputs import BaseModelOutput, BaseModelOutputWithPooling, ModelOutput from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel from ...processing_utils import Unpack -from ...utils import ( - TransformersKwargs, - auto_docstring, - can_return_tuple, - logging, - torch_compilable_check, -) +from ...utils import TransformersKwargs, auto_docstring, can_return_tuple, logging, torch_compilable_check from ...utils.generic import merge_with_config_defaults from ...utils.output_capturing import capture_outputs from ..auto import AutoModel diff --git a/src/transformers/models/video_llama_3/modeling_video_llama_3.py b/src/transformers/models/video_llama_3/modeling_video_llama_3.py index d686ccce2cae..26d89b313167 100644 --- a/src/transformers/models/video_llama_3/modeling_video_llama_3.py +++ b/src/transformers/models/video_llama_3/modeling_video_llama_3.py @@ -33,12 +33,7 @@ from ...modeling_outputs import BaseModelOutput, BaseModelOutputWithPooling, ModelOutput from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel from ...processing_utils import Unpack -from ...utils import ( - TransformersKwargs, - auto_docstring, - can_return_tuple, - torch_compilable_check, -) +from ...utils import TransformersKwargs, auto_docstring, can_return_tuple, torch_compilable_check from ...utils.generic import is_flash_attention_requested, merge_with_config_defaults from ...utils.output_capturing import capture_outputs from ..auto.modeling_auto import AutoModel diff --git a/src/transformers/models/yolos/image_processing_pil_yolos.py b/src/transformers/models/yolos/image_processing_pil_yolos.py index 9616ca3a85b9..8ddf312fbd1c 100644 --- a/src/transformers/models/yolos/image_processing_pil_yolos.py +++ b/src/transformers/models/yolos/image_processing_pil_yolos.py @@ -34,13 +34,7 @@ validate_annotations, ) from ...processing_utils import Unpack -from ...utils import ( - TensorType, - auto_docstring, - is_torch_available, - is_vision_available, - requires_backends, -) +from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, requires_backends from ...utils.import_utils import requires from .image_processing_yolos import YolosImageProcessorKwargs From 756aa7ce448687b612e9cf32a2e6d3b239f89808 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 16:02:40 +0100 Subject: [PATCH 12/30] update --- .../models/aria/image_processing_pil_aria.py | 23 +- .../models/beit/image_processing_pil_beit.py | 16 +- .../image_processing_pil_bridgetower.py | 57 +++- .../image_processing_pil_conditional_detr.py | 144 ++++++++- .../convnext/image_processing_pil_convnext.py | 14 +- .../image_processing_pil_deepseek_vl.py | 16 +- ...image_processing_pil_deepseek_vl_hybrid.py | 33 +- .../image_processing_pil_deformable_detr.py | 29 +- .../models/detr/image_processing_pil_detr.py | 145 +++++++-- .../donut/image_processing_pil_donut.py | 17 +- .../models/dpt/image_processing_pil_dpt.py | 69 +++- .../image_processing_pil_efficientloftr.py | 50 ++- .../image_processing_pil_efficientnet.py | 17 +- .../models/eomt/image_processing_pil_eomt.py | 122 +++++++- .../image_processing_pil_ernie4_5_vl_moe.py | 52 ++- .../flava/image_processing_pil_flava.py | 193 +++++++++++- .../models/fuyu/image_processing_pil_fuyu.py | 155 ++++++++- .../gemma3/image_processing_pil_gemma3.py | 23 +- .../glm46v/image_processing_pil_glm46v.py | 57 +++- .../glm4v/image_processing_pil_glm4v.py | 41 ++- .../image_processing_pil_glm_image.py | 67 +++- .../models/glm_image/processing_glm_image.py | 6 +- .../models/glpn/image_processing_pil_glpn.py | 16 +- .../got_ocr2/image_processing_pil_got_ocr2.py | 63 +++- .../image_processing_pil_grounding_dino.py | 31 +- .../idefics2/image_processing_pil_idefics2.py | 74 ++++- .../idefics3/image_processing_pil_idefics3.py | 150 ++++++++- .../imagegpt/image_processing_pil_imagegpt.py | 25 +- .../janus/image_processing_pil_janus.py | 15 +- .../image_processing_pil_kosmos2_5.py | 51 ++- .../image_processing_pil_layoutlmv2.py | 71 ++++- .../image_processing_pil_layoutlmv3.py | 72 ++++- .../image_processing_pil_lightglue.py | 48 ++- .../image_processing_pil_llava_next.py | 16 +- .../image_processing_pil_llava_onevision.py | 17 +- .../image_processing_pil_mask2former.py | 154 ++++++++- .../image_processing_pil_maskformer.py | 155 ++++++++- .../mllama/image_processing_pil_mllama.py | 195 +++++++++++- .../image_processing_pil_mobilenet_v2.py | 17 +- .../image_processing_pil_mobilevit.py | 21 +- .../nougat/image_processing_pil_nougat.py | 20 +- .../image_processing_pil_oneformer.py | 189 ++++++++++- .../ovis2/image_processing_pil_ovis2.py | 88 +++++- .../image_processing_pil_paddleocr_vl.py | 55 +++- .../image_processing_pil_pix2struct.py | 125 +++++++- .../pixtral/image_processing_pil_pixtral.py | 54 +++- .../image_processing_pil_poolformer.py | 14 +- ...ge_processing_pil_prompt_depth_anything.py | 27 +- .../qwen2_vl/image_processing_pil_qwen2_vl.py | 55 +++- .../rt_detr/image_processing_pil_rt_detr.py | 28 +- .../models/sam/image_processing_pil_sam.py | 38 ++- .../image_processing_pil_segformer.py | 21 +- .../seggpt/image_processing_pil_seggpt.py | 39 ++- .../siglip2/image_processing_pil_siglip2.py | 71 ++++- .../smolvlm/image_processing_pil_smolvlm.py | 151 ++++++++- .../models/smolvlm/processing_smolvlm.py | 14 +- .../image_processing_pil_superglue.py | 35 ++- .../image_processing_pil_superpoint.py | 17 +- .../swin2sr/image_processing_pil_swin2sr.py | 14 +- .../textnet/image_processing_pil_textnet.py | 14 +- .../models/tvp/image_processing_pil_tvp.py | 22 +- .../image_processing_pil_video_llama_3.py | 55 +++- .../models/vilt/image_processing_pil_vilt.py | 18 +- .../vitmatte/image_processing_pil_vitmatte.py | 15 +- .../vitpose/image_processing_pil_vitpose.py | 296 +++++++++++++++++- .../yolos/image_processing_pil_yolos.py | 30 +- .../zoedepth/image_processing_pil_zoedepth.py | 68 +++- src/transformers/utils/auto_docstring.py | 5 +- 68 files changed, 3662 insertions(+), 453 deletions(-) diff --git a/src/transformers/models/aria/image_processing_pil_aria.py b/src/transformers/models/aria/image_processing_pil_aria.py index 7909809b8064..bdec33c04ace 100644 --- a/src/transformers/models/aria/image_processing_pil_aria.py +++ b/src/transformers/models/aria/image_processing_pil_aria.py @@ -24,9 +24,27 @@ SizeDict, get_image_size, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_aria import AriaImageProcessorKwargs + + +# Copied from transformers.models.aria.image_processing_aria.AriaImageProcessorKwargs +class AriaImageProcessorKwargs(ImagesKwargs, total=False): + r""" + max_image_size (`int`, *optional*, defaults to `self.max_image_size`): + Maximum image size. Must be either 490 or 980. + min_image_size (`int`, *optional*, defaults to `self.min_image_size`): + Minimum image size. Images smaller than this in any dimension will be scaled up. + split_resolutions (`list[list[int]]`, *optional*, defaults to `self.split_resolutions`): + A list of possible resolutions as (height, width) pairs for splitting high-resolution images into patches. + split_image (`bool`, *optional*, defaults to `self.split_image`): + Whether to split the image into patches using the best matching resolution from `split_resolutions`. + """ + + max_image_size: int + min_image_size: int + split_resolutions: list[list[int]] + split_image: bool @auto_docstring @@ -208,5 +226,4 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non num_patches = 1 if not split_image else resized_height // max_image_size * resized_width // max_image_size return num_patches - __all__ = ["AriaImageProcessorPil"] diff --git a/src/transformers/models/beit/image_processing_pil_beit.py b/src/transformers/models/beit/image_processing_pil_beit.py index 9587fb3eca82..a026b76f4162 100644 --- a/src/transformers/models/beit/image_processing_pil_beit.py +++ b/src/transformers/models/beit/image_processing_pil_beit.py @@ -25,10 +25,21 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires -from .image_processing_beit import BeitImageProcessorKwargs + + +# Copied from transformers.models.beit.image_processing_beit.BeitImageProcessorKwargs +class BeitImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): + Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 + is used for background, and background itself is not included in all classes of a dataset (e.g. + ADE20k). The background label will be replaced by 255. + """ + + do_reduce_labels: bool @auto_docstring @@ -194,5 +205,4 @@ def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] return semantic_segmentation - __all__ = ["BeitImageProcessorPil"] diff --git a/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py b/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py index 7cbc779930e1..24ba073a4421 100644 --- a/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py +++ b/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py @@ -13,6 +13,8 @@ # limitations under the License. """Image processor class for BridgeTower.""" +from typing import Union + import numpy as np from ...image_processing_backends import PilBackend @@ -20,12 +22,60 @@ from ...image_utils import ( OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, + ChannelDimension, PILImageResampling, SizeDict, + get_image_size, ) -from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring -from .image_processing_bridgetower import BridgeTowerImageProcessorKwargs, get_resize_output_image_size +from ...processing_utils import ImagesKwargs, Unpack +from ...utils import TensorType, auto_docstring, is_torch_available + + +# Copied from transformers.models.bridgetower.image_processing_bridgetower.BridgeTowerImageProcessorKwargs +class BridgeTowerImageProcessorKwargs(ImagesKwargs, total=False): + r""" + size_divisor (`int`, *optional*, defaults to `self.size_divisor`): + The size by which to make sure both the height and width can be divided. + """ + + size_divisor: int + +# Copied from transformers.models.bridgetower.image_processing_bridgetower.get_resize_output_image_size +def get_resize_output_image_size( + input_image: Union[np.ndarray, "torch.Tensor"], + shorter: int = 800, + longer: int = 1333, + size_divisor: int = 32, +) -> tuple[int, int]: + """Get output image size after resizing with size_divisor.""" + if is_torch_available(): + import torch + + if is_torch_available() and isinstance(input_image, torch.Tensor): + input_height, input_width = input_image.shape[-2:] + else: + input_height, input_width = get_image_size(input_image, channel_dim=ChannelDimension.FIRST) + + min_size, max_size = shorter, longer + scale = min_size / min(input_height, input_width) + + if input_height < input_width: + new_height = min_size + new_width = scale * input_width + else: + new_height = scale * input_height + new_width = min_size + + if max(new_height, new_width) > max_size: + scale = max_size / max(new_height, new_width) + new_height = scale * new_height + new_width = scale * new_width + + new_height, new_width = int(new_height + 0.5), int(new_width + 0.5) + new_height = new_height // size_divisor * size_divisor + new_width = new_width // size_divisor * size_divisor + + return new_height, new_width @auto_docstring @@ -121,5 +171,4 @@ def _preprocess( return BatchFeature(data=data, tensor_type=return_tensors) - __all__ = ["BridgeTowerImageProcessorPil"] diff --git a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py index ec34b05d72f1..49681ad8cbe2 100644 --- a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py +++ b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py @@ -35,11 +35,11 @@ safe_squeeze, ) from ...image_utils import ( - IMAGENET_DEFAULT_MEAN, - IMAGENET_DEFAULT_STD, AnnotationFormat, AnnotationType, ChannelDimension, + IMAGENET_DEFAULT_MEAN, + IMAGENET_DEFAULT_STD, ImageInput, PILImageResampling, SizeDict, @@ -48,16 +48,9 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, logging, requires_backends from ...utils.import_utils import requires -from .image_processing_conditional_detr import ( - ConditionalDetrImageProcessorKwargs, - compute_segments, - convert_segmentation_to_rle, - remove_low_and_no_objects, -) - if is_vision_available(): import PIL.Image @@ -65,12 +58,10 @@ import torch from torch import nn - logger = logging.get_logger(__name__) SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) - # inspired by https://github.com/facebookresearch/conditional_detr/blob/master/datasets/coco.py#L33 def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndarray: """ @@ -105,7 +96,6 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar return masks - # inspired by https://github.com/facebookresearch/conditional_detr/blob/master/datasets/coco.py#L50 def prepare_coco_detection_annotation( image, @@ -166,7 +156,6 @@ def prepare_coco_detection_annotation( return new_target - def masks_to_boxes(masks: np.ndarray) -> np.ndarray: """ Compute the bounding boxes around the provided panoptic segmentation masks. @@ -200,7 +189,6 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: return np.stack([x_min, y_min, x_max, y_max], 1) - # 2 functions below adapted from https://github.com/cocodataset/panopticapi/blob/master/panopticapi/utils.py # Copyright (c) 2018, Alexander Kirillov # All rights reserved. @@ -214,7 +202,6 @@ def rgb_to_id(color): return color[:, :, 0] + 256 * color[:, :, 1] + 256 * 256 * color[:, :, 2] return int(color[0] + 256 * color[1] + 256 * 256 * color[2]) - def prepare_coco_panoptic_annotation( image: np.ndarray, target: dict, @@ -256,6 +243,130 @@ def prepare_coco_panoptic_annotation( return new_target +# Copied from transformers.models.conditional_detr.image_processing_conditional_detr.ConditionalDetrImageProcessorKwargs +class ConditionalDetrImageProcessorKwargs(ImagesKwargs, total=False): + r""" + format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): + Data format of the annotations. One of "coco_detection" or "coco_panoptic". + do_convert_annotations (`bool`, *optional*, defaults to `True`): + Controls whether to convert the annotations to the format expected by the CONDITIONAL_DETR model. Converts the + bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. + Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. + """ + + format: str | AnnotationFormat + do_convert_annotations: bool + +# Copied from transformers.models.conditional_detr.image_processing_conditional_detr.compute_segments +def compute_segments( + mask_probs, + pred_scores, + pred_labels, + mask_threshold: float = 0.5, + overlap_mask_area_threshold: float = 0.8, + label_ids_to_fuse: set[int] | None = None, + target_size: tuple[int, int] | None = None, +): + height = mask_probs.shape[1] if target_size is None else target_size[0] + width = mask_probs.shape[2] if target_size is None else target_size[1] + + segmentation = torch.zeros((height, width), dtype=torch.int32, device=mask_probs.device) + segments: list[dict] = [] + + if target_size is not None: + mask_probs = nn.functional.interpolate( + mask_probs.unsqueeze(0), size=target_size, mode="bilinear", align_corners=False + )[0] + + current_segment_id = 0 + + # Weigh each mask by its prediction score + mask_probs *= pred_scores.view(-1, 1, 1) + mask_labels = mask_probs.argmax(0) # [height, width] + + # Keep track of instances of each class + stuff_memory_list: dict[str, int] = {} + for k in range(pred_labels.shape[0]): + pred_class = pred_labels[k].item() + should_fuse = pred_class in label_ids_to_fuse + + # Check if mask exists and large enough to be a segment + mask_exists, mask_k = check_segment_validity( + mask_labels, mask_probs, k, mask_threshold, overlap_mask_area_threshold + ) + + if mask_exists: + if pred_class in stuff_memory_list: + current_segment_id = stuff_memory_list[pred_class] + else: + current_segment_id += 1 + + # Add current object segment to final segmentation map + segmentation[mask_k] = current_segment_id + segment_score = round(pred_scores[k].item(), 6) + segments.append( + { + "id": current_segment_id, + "label_id": pred_class, + "was_fused": should_fuse, + "score": segment_score, + } + ) + if should_fuse: + stuff_memory_list[pred_class] = current_segment_id + + return segmentation, segments + +# Copied from transformers.models.conditional_detr.image_processing_conditional_detr.convert_segmentation_to_rle +def convert_segmentation_to_rle(segmentation): + """ + Converts given segmentation map of shape `(height, width)` to the run-length encoding (RLE) format. + + Args: + segmentation (`torch.Tensor` or `numpy.array`): + A segmentation map of shape `(height, width)` where each value denotes a segment or class id. + Returns: + `list[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. + """ + segment_ids = torch.unique(segmentation) + + run_length_encodings = [] + for idx in segment_ids: + mask = torch.where(segmentation == idx, 1, 0) + rle = binary_mask_to_rle(mask) + run_length_encodings.append(rle) + + return run_length_encodings + +# Copied from transformers.models.conditional_detr.image_processing_conditional_detr.remove_low_and_no_objects +def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): + """ + Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and + `labels`. + + Args: + masks (`torch.Tensor`): + A tensor of shape `(num_queries, height, width)`. + scores (`torch.Tensor`): + A tensor of shape `(num_queries)`. + labels (`torch.Tensor`): + A tensor of shape `(num_queries)`. + object_mask_threshold (`float`): + A number between 0 and 1 used to binarize the masks. + Raises: + `ValueError`: Raised when the first dimension doesn't match in all input tensors. + Returns: + `tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region + < `object_mask_threshold`. + """ + if not (masks.shape[0] == scores.shape[0] == labels.shape[0]): + raise ValueError("mask, scores and labels must have the same shape!") + + to_keep = labels.ne(num_labels) & (scores > object_mask_threshold) + + return masks[to_keep], scores[to_keep], labels[to_keep] + + @auto_docstring class ConditionalDetrImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR @@ -963,5 +1074,4 @@ def post_process_panoptic_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results - __all__ = ["ConditionalDetrImageProcessorPil"] diff --git a/src/transformers/models/convnext/image_processing_pil_convnext.py b/src/transformers/models/convnext/image_processing_pil_convnext.py index 78972e98e963..e4db381bccd0 100644 --- a/src/transformers/models/convnext/image_processing_pil_convnext.py +++ b/src/transformers/models/convnext/image_processing_pil_convnext.py @@ -25,9 +25,18 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_convnext import ConvNextImageProcessorKwargs + + +# Copied from transformers.models.convnext.image_processing_convnext.ConvNextImageProcessorKwargs +class ConvNextImageProcessorKwargs(ImagesKwargs, total=False): + r""" + crop_pct (`float`, *optional*, defaults to `self.crop_pct`): + Percentage of the image to crop. Only has an effect if size < 384. + """ + + crop_pct: float @auto_docstring @@ -123,5 +132,4 @@ def _preprocess( return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) - __all__ = ["ConvNextImageProcessorPil"] diff --git a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py index d29296535277..3f5e554fd22a 100644 --- a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py +++ b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py @@ -18,7 +18,6 @@ # See the License for the specific language governing permissions and # limitations under the License. - import numpy as np from ...image_processing_backends import PilBackend @@ -32,9 +31,19 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_deepseek_vl import DeepseekVLImageProcessorKwargs + + +# Copied from transformers.models.deepseek_vl.image_processing_deepseek_vl.DeepseekVLImageProcessorKwargs +class DeepseekVLImageProcessorKwargs(ImagesKwargs, total=False): + r""" + min_size (`int`, *optional*, defaults to 14): + The minimum allowed size for the resized image. Ensures that neither the height nor width + falls below this value after resizing. + """ + + min_size: int @auto_docstring @@ -161,5 +170,4 @@ def postprocess(self): """Applies post-processing to the decoded image tokens by reversing transformations applied during preprocessing.""" raise AttributeError("Not needed for DeepseekVL") - __all__ = ["DeepseekVLImageProcessorPil"] diff --git a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py index 3c9bc6b7436a..86b5ca962fc7 100644 --- a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py +++ b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py @@ -19,6 +19,7 @@ # limitations under the License. from collections.abc import Iterable +from typing import Union import numpy as np @@ -33,15 +34,40 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available -from .image_processing_deepseek_vl_hybrid import DeepseekVLHybridImageProcessorKwargs - if is_torch_available(): import torch +# Copied from transformers.models.deepseek_vl_hybrid.image_processing_deepseek_vl_hybrid.DeepseekVLHybridImageProcessorKwargs +class DeepseekVLHybridImageProcessorKwargs(ImagesKwargs, total=False): + r""" + min_size (`int`, *optional*, defaults to 14): + The minimum allowed size for the resized image. Ensures that neither the height nor width + falls below this value after resizing. + high_res_size (`dict`, *optional*, defaults to `{"height": 1024, "width": 1024}`): + Size of the high resolution output image after resizing. Can be overridden by the `high_res_size` parameter in the `preprocess` + method. + high_res_resample (`PILImageResampling`, *optional*, defaults to `Resampling.BICUBIC`): + Resampling filter to use if resizing the image. Only has an effect if `do_resize` is set to `True`. Can be + overridden by the `high_res_resample` parameter in the `preprocess` method. + high_res_image_mean (`float` or `list[float]`, *optional*, defaults to `OPENAI_CLIP_MEAN`): + Mean to use if normalizing the high resolution image. This is a float or list of floats the length of the number of + channels in the image. Can be overridden by the `high_res_image_mean` parameter in the `preprocess` method. + high_res_image_std (`float` or `list[float]`, *optional*, defaults to `OPENAI_CLIP_STD`): + Standard deviation to use if normalizing the high resolution image. This is a float or list of floats the length of the + number of channels in the image. Can be overridden by the `high_res_image_std` parameter in the `preprocess` method. + """ + + min_size: int + high_res_size: dict + high_res_resample: Union["PILImageResampling", int] + high_res_image_mean: float | list[float] | tuple[float, ...] + high_res_image_std: float | list[float] | tuple[float, ...] + + @auto_docstring class DeepseekVLHybridImageProcessorPil(PilBackend): resample = PILImageResampling.BICUBIC @@ -235,5 +261,4 @@ def _standardize_kwargs( return kwargs - __all__ = ["DeepseekVLHybridImageProcessorPil"] diff --git a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py index dc38be54c3df..8403ab0629ec 100644 --- a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py +++ b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py @@ -34,11 +34,11 @@ safe_squeeze, ) from ...image_utils import ( - IMAGENET_DEFAULT_MEAN, - IMAGENET_DEFAULT_STD, AnnotationFormat, AnnotationType, ChannelDimension, + IMAGENET_DEFAULT_MEAN, + IMAGENET_DEFAULT_STD, ImageInput, PILImageResampling, SizeDict, @@ -47,11 +47,9 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available from ...utils.import_utils import requires, requires_backends -from .image_processing_deformable_detr import DeformableDetrImageProcessorKwargs - if is_vision_available(): import PIL.Image @@ -60,7 +58,6 @@ SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) - # inspired by https://github.com/facebookresearch/deformable_detr/blob/master/datasets/coco.py#L33 def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndarray: """ @@ -95,7 +92,6 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar return masks - # inspired by https://github.com/facebookresearch/deformable_detr/blob/master/datasets/coco.py#L50 def prepare_coco_detection_annotation( image, @@ -156,7 +152,6 @@ def prepare_coco_detection_annotation( return new_target - def masks_to_boxes(masks: np.ndarray) -> np.ndarray: """ Compute the bounding boxes around the provided panoptic segmentation masks. @@ -190,7 +185,6 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: return np.stack([x_min, y_min, x_max, y_max], 1) - # 2 functions below adapted from https://github.com/cocodataset/panopticapi/blob/master/panopticapi/utils.py # Copyright (c) 2018, Alexander Kirillov # All rights reserved. @@ -204,7 +198,6 @@ def rgb_to_id(color): return color[:, :, 0] + 256 * color[:, :, 1] + 256 * 256 * color[:, :, 2] return int(color[0] + 256 * color[1] + 256 * 256 * color[2]) - def prepare_coco_panoptic_annotation( image: np.ndarray, target: dict, @@ -246,6 +239,21 @@ def prepare_coco_panoptic_annotation( return new_target +# Copied from transformers.models.deformable_detr.image_processing_deformable_detr.DeformableDetrImageProcessorKwargs +class DeformableDetrImageProcessorKwargs(ImagesKwargs, total=False): + r""" + format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): + Data format of the annotations. One of "coco_detection" or "coco_panoptic". + do_convert_annotations (`bool`, *optional*, defaults to `True`): + Controls whether to convert the annotations to the format expected by the DEFORMABLE_DETR model. Converts the + bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. + Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. + """ + + format: str | AnnotationFormat + do_convert_annotations: bool + + @auto_docstring class DeformableDetrImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR @@ -721,5 +729,4 @@ def post_process_object_detection( return results - __all__ = ["DeformableDetrImageProcessorPil"] diff --git a/src/transformers/models/detr/image_processing_pil_detr.py b/src/transformers/models/detr/image_processing_pil_detr.py index cbf5a157526e..735254b72be8 100644 --- a/src/transformers/models/detr/image_processing_pil_detr.py +++ b/src/transformers/models/detr/image_processing_pil_detr.py @@ -29,11 +29,11 @@ safe_squeeze, ) from ...image_utils import ( - IMAGENET_DEFAULT_MEAN, - IMAGENET_DEFAULT_STD, AnnotationFormat, AnnotationType, ChannelDimension, + IMAGENET_DEFAULT_MEAN, + IMAGENET_DEFAULT_STD, ImageInput, PILImageResampling, SizeDict, @@ -42,7 +42,7 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import ( TensorType, auto_docstring, @@ -51,24 +51,14 @@ logging, ) from ...utils.import_utils import requires -from .image_processing_detr import ( - DetrImageProcessorKwargs, - compute_segments, - convert_segmentation_to_rle, - get_size_with_aspect_ratio, - remove_low_and_no_objects, -) - if is_vision_available(): import PIL.Image - logger = logging.get_logger(__name__) SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) - # inspired by https://github.com/facebookresearch/detr/blob/master/datasets/coco.py#L33 def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndarray: """ @@ -103,7 +93,6 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar return masks - # inspired by https://github.com/facebookresearch/detr/blob/master/datasets/coco.py#L50 def prepare_coco_detection_annotation( image, @@ -164,7 +153,6 @@ def prepare_coco_detection_annotation( return new_target - def masks_to_boxes(masks: np.ndarray) -> np.ndarray: """ Compute the bounding boxes around the provided panoptic segmentation masks. @@ -198,7 +186,6 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: return np.stack([x_min, y_min, x_max, y_max], 1) - # 2 functions below adapted from https://github.com/cocodataset/panopticapi/blob/master/panopticapi/utils.py # Copyright (c) 2018, Alexander Kirillov # All rights reserved. @@ -212,7 +199,6 @@ def rgb_to_id(color): return color[:, :, 0] + 256 * color[:, :, 1] + 256 * 256 * color[:, :, 2] return int(color[0] + 256 * color[1] + 256 * 256 * color[2]) - def prepare_coco_panoptic_annotation( image: np.ndarray, target: dict, @@ -254,6 +240,130 @@ def prepare_coco_panoptic_annotation( return new_target +# Copied from transformers.models.detr.image_processing_detr.DetrImageProcessorKwargs +class DetrImageProcessorKwargs(ImagesKwargs, total=False): + r""" + format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): + Data format of the annotations. One of "coco_detection" or "coco_panoptic". + do_convert_annotations (`bool`, *optional*, defaults to `True`): + Controls whether to convert the annotations to the format expected by the DETR model. Converts the + bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. + Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. + """ + + format: str | AnnotationFormat + do_convert_annotations: bool + +# Copied from transformers.models.detr.image_processing_detr.compute_segments +def compute_segments( + mask_probs, + pred_scores, + pred_labels, + mask_threshold: float = 0.5, + overlap_mask_area_threshold: float = 0.8, + label_ids_to_fuse: set[int] | None = None, + target_size: tuple[int, int] | None = None, +): + height = mask_probs.shape[1] if target_size is None else target_size[0] + width = mask_probs.shape[2] if target_size is None else target_size[1] + + segmentation = torch.zeros((height, width), dtype=torch.int32, device=mask_probs.device) + segments: list[dict] = [] + + if target_size is not None: + mask_probs = nn.functional.interpolate( + mask_probs.unsqueeze(0), size=target_size, mode="bilinear", align_corners=False + )[0] + + current_segment_id = 0 + + # Weigh each mask by its prediction score + mask_probs *= pred_scores.view(-1, 1, 1) + mask_labels = mask_probs.argmax(0) # [height, width] + + # Keep track of instances of each class + stuff_memory_list: dict[str, int] = {} + for k in range(pred_labels.shape[0]): + pred_class = pred_labels[k].item() + should_fuse = pred_class in label_ids_to_fuse + + # Check if mask exists and large enough to be a segment + mask_exists, mask_k = check_segment_validity( + mask_labels, mask_probs, k, mask_threshold, overlap_mask_area_threshold + ) + + if mask_exists: + if pred_class in stuff_memory_list: + current_segment_id = stuff_memory_list[pred_class] + else: + current_segment_id += 1 + + # Add current object segment to final segmentation map + segmentation[mask_k] = current_segment_id + segment_score = round(pred_scores[k].item(), 6) + segments.append( + { + "id": current_segment_id, + "label_id": pred_class, + "was_fused": should_fuse, + "score": segment_score, + } + ) + if should_fuse: + stuff_memory_list[pred_class] = current_segment_id + + return segmentation, segments + +# Copied from transformers.models.detr.image_processing_detr.convert_segmentation_to_rle +def convert_segmentation_to_rle(segmentation): + """ + Converts given segmentation map of shape `(height, width)` to the run-length encoding (RLE) format. + + Args: + segmentation (`torch.Tensor` or `numpy.array`): + A segmentation map of shape `(height, width)` where each value denotes a segment or class id. + Returns: + `list[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. + """ + segment_ids = torch.unique(segmentation) + + run_length_encodings = [] + for idx in segment_ids: + mask = torch.where(segmentation == idx, 1, 0) + rle = binary_mask_to_rle(mask) + run_length_encodings.append(rle) + + return run_length_encodings + +# Copied from transformers.models.detr.image_processing_detr.remove_low_and_no_objects +def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): + """ + Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and + `labels`. + + Args: + masks (`torch.Tensor`): + A tensor of shape `(num_queries, height, width)`. + scores (`torch.Tensor`): + A tensor of shape `(num_queries)`. + labels (`torch.Tensor`): + A tensor of shape `(num_queries)`. + object_mask_threshold (`float`): + A number between 0 and 1 used to binarize the masks. + Raises: + `ValueError`: Raised when the first dimension doesn't match in all input tensors. + Returns: + `tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region + < `object_mask_threshold`. + """ + if not (masks.shape[0] == scores.shape[0] == labels.shape[0]): + raise ValueError("mask, scores and labels must have the same shape!") + + to_keep = labels.ne(num_labels) & (scores > object_mask_threshold) + + return masks[to_keep], scores[to_keep], labels[to_keep] + + @auto_docstring class DetrImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR @@ -963,5 +1073,4 @@ def post_process_panoptic_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results - __all__ = ["DetrImageProcessorPil"] diff --git a/src/transformers/models/donut/image_processing_pil_donut.py b/src/transformers/models/donut/image_processing_pil_donut.py index ba73d9f24b23..cfdcc870d875 100644 --- a/src/transformers/models/donut/image_processing_pil_donut.py +++ b/src/transformers/models/donut/image_processing_pil_donut.py @@ -25,9 +25,21 @@ SizeDict, get_image_size, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_donut import DonutImageProcessorKwargs + + +# Copied from transformers.models.donut.image_processing_donut.DonutImageProcessorKwargs +class DonutImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_thumbnail (`bool`, *optional*, defaults to `self.do_thumbnail`): + Whether to resize the image using thumbnail method. + do_align_long_axis (`bool`, *optional*, defaults to `self.do_align_long_axis`): + Whether to align the long axis of the image with the long axis of `size` by rotating by 90 degrees. + """ + + do_thumbnail: bool + do_align_long_axis: bool @auto_docstring @@ -196,5 +208,4 @@ def _preprocess( return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) - __all__ = ["DonutImageProcessorPil"] diff --git a/src/transformers/models/dpt/image_processing_pil_dpt.py b/src/transformers/models/dpt/image_processing_pil_dpt.py index 4c59c1557c5c..828e0fb08b31 100644 --- a/src/transformers/models/dpt/image_processing_pil_dpt.py +++ b/src/transformers/models/dpt/image_processing_pil_dpt.py @@ -22,26 +22,84 @@ from ...image_processing_utils import BatchFeature from ...image_transforms import pad as np_pad from ...image_utils import ( + ChannelDimension, IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, - ChannelDimension, ImageInput, PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, logging from ...utils.import_utils import requires -from .image_processing_dpt import DPTImageProcessorKwargs, get_resize_output_image_size - +from collections.abc import Iterable if TYPE_CHECKING: from ...modeling_outputs import DepthEstimatorOutput - logger = logging.get_logger(__name__) +# Copied from transformers.models.dpt.image_processing_dpt.DPTImageProcessorKwargs +class DPTImageProcessorKwargs(ImagesKwargs, total=False): + r""" + ensure_multiple_of (`int`, *optional*, defaults to 1): + If `do_resize` is `True`, the image is resized to a size that is a multiple of this value. Can be overridden + by `ensure_multiple_of` in `preprocess`. + keep_aspect_ratio (`bool`, *optional*, defaults to `False`): + If `True`, the image is resized to the largest possible size such that the aspect ratio is preserved. Can + be overridden by `keep_aspect_ratio` in `preprocess`. + do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): + Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 + is used for background, and background itself is not included in all classes of a dataset (e.g. + ADE20k). The background label will be replaced by 255. + """ + + ensure_multiple_of: int + size_divisor: int + keep_aspect_ratio: bool + do_reduce_labels: bool + +# Copied from transformers.models.dpt.image_processing_dpt.get_resize_output_image_size +def get_resize_output_image_size( + input_image: "torch.Tensor", + output_size: int | Iterable[int], + keep_aspect_ratio: bool, + multiple: int, +) -> SizeDict: + def constrain_to_multiple_of(val, multiple, min_val=0, max_val=None): + x = round(val / multiple) * multiple + + if max_val is not None and x > max_val: + x = math.floor(val / multiple) * multiple + + if x < min_val: + x = math.ceil(val / multiple) * multiple + + return x + + input_height, input_width = input_image.shape[-2:] + output_height, output_width = output_size + + # determine new height and width + scale_height = output_height / input_height + scale_width = output_width / input_width + + if keep_aspect_ratio: + # scale as little as possible + if abs(1 - scale_width) < abs(1 - scale_height): + # fit width + scale_height = scale_width + else: + # fit height + scale_width = scale_height + + new_height = constrain_to_multiple_of(scale_height * input_height, multiple=multiple) + new_width = constrain_to_multiple_of(scale_width * input_width, multiple=multiple) + + return SizeDict(height=new_height, width=new_width) + + @auto_docstring class DPTImageProcessorPil(PilBackend): """PIL backend for DPT with custom resize and pad.""" @@ -248,5 +306,4 @@ def post_process_depth_estimation( results.append({"predicted_depth": depth}) return results - __all__ = ["DPTImageProcessorPil"] diff --git a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py index 866fedfb3d9c..a1914cac9747 100644 --- a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py @@ -5,30 +5,29 @@ # modular_efficientloftr.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 - import numpy as np from PIL import Image, ImageDraw from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput, PILImageResampling, SizeDict, to_numpy_array -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack +from typing import TYPE_CHECKING + from ...utils import TensorType, auto_docstring, is_torch_available from ...utils.import_utils import requires -from .image_processing_efficientloftr import EfficientLoFTRImageProcessorKwargs, validate_and_format_image_pairs -from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput - if is_torch_available(): import torch +if TYPE_CHECKING: + from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput def is_grayscale(image: np.ndarray): if image.shape[0] == 1: return True return np.all(image[0, ...] == image[1, ...]) and np.all(image[1, ...] == image[2, ...]) - def convert_to_grayscale(image: ImageInput) -> ImageInput: """ Converts an image to grayscale format using the NTSC formula. Only support numpy and PIL Image. @@ -57,6 +56,44 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: return image +# Copied from transformers.models.efficientloftr.image_processing_efficientloftr.EfficientLoFTRImageProcessorKwargs +class EfficientLoFTRImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`): + Whether to convert the image to grayscale. Can be overridden by `do_grayscale` in the `preprocess` method. + """ + + do_grayscale: bool + +# Copied from transformers.models.efficientloftr.image_processing_efficientloftr.validate_and_format_image_pairs +def validate_and_format_image_pairs(images: ImageInput): + error_message = ( + "Input images must be a one of the following :", + " - A pair of PIL images.", + " - A pair of 3D arrays.", + " - A list of pairs of PIL images.", + " - A list of pairs of 3D arrays.", + ) + + def _is_valid_image(image): + """images is a PIL Image or a 3D array.""" + return is_pil_image(image) or ( + is_valid_image(image) and get_image_type(image) != ImageType.PIL and len(image.shape) == 3 + ) + + if isinstance(images, list): + if len(images) == 2 and all((_is_valid_image(image)) for image in images): + return images + if all( + isinstance(image_pair, list) + and len(image_pair) == 2 + and all(_is_valid_image(image) for image in image_pair) + for image_pair in images + ): + return [image for image_pair in images for image in image_pair] + raise ValueError(error_message) + + @auto_docstring class EfficientLoFTRImageProcessorPil(PilBackend): valid_kwargs = EfficientLoFTRImageProcessorKwargs @@ -230,5 +267,4 @@ def _get_color(self, score): b = 0 return (r, g, b) - __all__ = ["EfficientLoFTRImageProcessorPil"] diff --git a/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py b/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py index a9469f357fd0..fb86c4662055 100644 --- a/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py +++ b/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py @@ -23,9 +23,21 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_efficientnet import EfficientNetImageProcessorKwargs + + +# Copied from transformers.models.efficientnet.image_processing_efficientnet.EfficientNetImageProcessorKwargs +class EfficientNetImageProcessorKwargs(ImagesKwargs, total=False): + """ + rescale_offset (`bool`, *optional*, defaults to `self.rescale_offset`): + Whether to rescale the image between [-max_range/2, scale_range/2] instead of [0, scale_range]. + include_top (`bool`, *optional*, defaults to `self.include_top`): + Normalize the image again with the standard deviation only for image classification if set to True. + """ + + rescale_offset: bool + include_top: bool @auto_docstring @@ -98,5 +110,4 @@ def _preprocess( processed_images.append(image) return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) - __all__ = ["EfficientNetImageProcessorPil"] diff --git a/src/transformers/models/eomt/image_processing_pil_eomt.py b/src/transformers/models/eomt/image_processing_pil_eomt.py index 0bc561ce3327..e53dc1322932 100644 --- a/src/transformers/models/eomt/image_processing_pil_eomt.py +++ b/src/transformers/models/eomt/image_processing_pil_eomt.py @@ -16,8 +16,6 @@ import math import numpy as np -import torch - from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_transforms import PaddingMode, get_size_with_aspect_ratio @@ -31,16 +29,13 @@ SizeDict, get_image_size, ) -from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring +from ...processing_utils import ImagesKwargs, Unpack +from ...utils import TensorType, auto_docstring, is_torch_available from ...utils.import_utils import requires -from .image_processing_eomt import ( - EomtImageProcessorKwargs, - compute_segments, - get_target_size, - remove_low_and_no_objects, -) +if is_torch_available(): + import torch + from torch import nn # Adapted from transformers.models.maskformer.image_processing_maskformer.convert_segmentation_map_to_binary_masks def convert_segmentation_map_to_binary_masks( @@ -79,6 +74,110 @@ def convert_segmentation_map_to_binary_masks( return binary_masks.astype(np.float32), labels.astype(np.int64) +# Copied from transformers.models.eomt.image_processing_eomt.EomtImageProcessorKwargs +class EomtImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_split_image (`bool`, *optional*, defaults to `self.do_split_image`): + Whether to split the input images into overlapping patches for semantic segmentation. If set to `True`, the + input images will be split into patches of size `size["shortest_edge"]` with an overlap between patches. + Otherwise, the input images will be padded to the target size. + ignore_index (`int`, *optional*, defaults to `self.ignore_index`): + Label to be assigned to background pixels in segmentation maps. If provided, segmentation map pixels + denoted with 0 (background) will be replaced with `ignore_index`. + """ + + do_split_image: bool + ignore_index: int | None + +# Copied from transformers.models.eomt.image_processing_eomt.compute_segments +def compute_segments( + mask_probs, + pred_scores, + pred_labels, + stuff_classes, + mask_threshold: float = 0.5, + overlap_mask_area_threshold: float = 0.8, + target_size: tuple[int, int] | None = None, +): + height = mask_probs.shape[1] if target_size is None else target_size[0] + width = mask_probs.shape[2] if target_size is None else target_size[1] + + segmentation = torch.zeros((height, width), dtype=torch.long, device=mask_probs.device) - 1 + segments: list[dict] = [] + + # Compute per-pixel assignment based on weighted mask scores + mask_probs = mask_probs.sigmoid() + mask_labels = (pred_scores[:, None, None] * mask_probs).argmax(0) + + # Keep track of instances of each class + current_segment_id = 0 + stuff_memory_list: dict[str, int] = {} + + for k in range(pred_labels.shape[0]): + pred_class = pred_labels[k].item() + + # Check if mask exists and large enough to be a segment + mask_exists, final_mask = check_segment_validity( + mask_labels, mask_probs, k, mask_threshold, overlap_mask_area_threshold + ) + + if not mask_exists: + continue + + if stuff_classes and pred_class in stuff_classes: + if pred_class in stuff_memory_list: + segmentation[final_mask] = stuff_memory_list[pred_class] + continue + else: + stuff_memory_list[pred_class] = current_segment_id + + segmentation[final_mask] = current_segment_id + segment_score = round(pred_scores[k].item(), 6) + segments.append( + { + "id": current_segment_id, + "label_id": pred_class, + "score": segment_score, + } + ) + current_segment_id += 1 + return segmentation, segments + +# Copied from transformers.models.eomt.image_processing_eomt.get_target_size +def get_target_size(size_dict: dict[str, int]) -> tuple[int, int]: + """Returns the height and width from a size dict.""" + target_height = size_dict["shortest_edge"] + target_width = size_dict["longest_edge"] or target_height + + return target_height, target_width + +# Copied from transformers.models.eomt.image_processing_eomt.remove_low_and_no_objects +def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): + """ + Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and + `labels`. + + Args: + masks (`torch.Tensor`): + A tensor of shape `(num_queries, height, width)`. + scores (`torch.Tensor`): + A tensor of shape `(num_queries)`. + labels (`torch.Tensor`): + A tensor of shape `(num_queries)`. + object_mask_threshold (`float`): + A number between 0 and 1 used to binarize the masks. + Raises: + `ValueError`: Raised when the first dimension doesn't match in all input tensors. + Returns: + `tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region + < `object_mask_threshold`. + """ + if not (masks.shape[0] == scores.shape[0] == labels.shape[0]): + raise ValueError("mask, scores and labels must have the same shape!") + + to_keep = labels.ne(num_labels) & (scores > object_mask_threshold) + + return masks[to_keep], scores[to_keep], labels[to_keep] @auto_docstring @requires(backends=("torch",)) @@ -509,5 +608,4 @@ def post_process_instance_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results - -__all__ = ["EomtImageProcessorPil"] +__all__ = ["EomtImageProcessorPil"] \ No newline at end of file diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py index fcac9a91a21b..d4da29660818 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py @@ -23,20 +23,62 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available, logging -from .image_processing_ernie4_5_vl_moe import Ernie4_5_VLMoeImageProcessorKwargs, smart_resize - if is_torch_available(): import torch if is_torchvision_available(): from torchvision.transforms.v2 import functional as tvF - logger = logging.get_logger(__name__) +# Copied from transformers.models.ernie4_5_vl_moe.image_processing_ernie4_5_vl_moe.Ernie4_5_VLMoeImageProcessorKwargs +class Ernie4_5_VLMoeImageProcessorKwargs(ImagesKwargs, total=False): + r""" + patch_size (`int`, *optional*, defaults to 14): + The spatial patch size of the vision encoder. + temporal_patch_size (`int`, *optional*): + The temporal patch size of the vision encoder. Unused in the image processor, only used for videos. + merge_size (`int`, *optional*, defaults to 2): + The merge size of the vision encoder to llm encoder. + """ + + patch_size: int + temporal_patch_size: int + merge_size: int + +# Copied from transformers.models.ernie4_5_vl_moe.image_processing_ernie4_5_vl_moe.smart_resize +def smart_resize( + height: int, width: int, factor: int = 28, min_pixels: int = 56 * 56, max_pixels: int = 14 * 14 * 4 * 1280 +): + """Rescales the image so that the following conditions are met: + + 1. Both dimensions (height and width) are divisible by 'factor'. + + 2. The total number of pixels is within the range ['min_pixels', 'max_pixels']. + + 3. The aspect ratio of the image is maintained as closely as possible. + + """ + if max(height, width) / min(height, width) > 200: + raise ValueError( + f"absolute aspect ratio must be smaller than 200, got {max(height, width) / min(height, width)}" + ) + h_bar = round(height / factor) * factor + w_bar = round(width / factor) * factor + if h_bar * w_bar > max_pixels: + beta = math.sqrt((height * width) / max_pixels) + h_bar = max(factor, math.floor(height / beta / factor) * factor) + w_bar = max(factor, math.floor(width / beta / factor) * factor) + elif h_bar * w_bar < min_pixels: + beta = math.sqrt(min_pixels / (height * width)) + h_bar = math.ceil(height * beta / factor) * factor + w_bar = math.ceil(width * beta / factor) * factor + return h_bar, w_bar + + @auto_docstring class Ernie4_5_VLMoeImageProcessorPil(PilBackend): do_resize = True @@ -194,7 +236,6 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non grid_h, grid_w = resized_height // patch_size, resized_width // patch_size return grid_h * grid_w - class Ernie4_5_VL_MoeImageProcessorPil(Ernie4_5_VLMoeImageProcessorPil): def __init__(self, *args, **kwargs): logger.warning_once( @@ -202,5 +243,4 @@ def __init__(self, *args, **kwargs): ) super().__init__(*args, **kwargs) - __all__ = ["Ernie4_5_VL_MoeImageProcessorPil", "Ernie4_5_VLMoeImageProcessorPil"] diff --git a/src/transformers/models/flava/image_processing_pil_flava.py b/src/transformers/models/flava/image_processing_pil_flava.py index 424f97928723..0fbdacc70bf3 100644 --- a/src/transformers/models/flava/image_processing_pil_flava.py +++ b/src/transformers/models/flava/image_processing_pil_flava.py @@ -24,23 +24,195 @@ from ...image_utils import ( ChannelDimension, ImageInput, + OPENAI_CLIP_MEAN, + OPENAI_CLIP_STD, PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import ( TensorType, auto_docstring, ) -from .image_processing_flava import ( - FLAVA_CODEBOOK_MEAN, - FLAVA_CODEBOOK_STD, - FLAVA_IMAGE_MEAN, - FLAVA_IMAGE_STD, - LOGIT_LAPLACE_EPS, - FlavaImageProcessorKwargs, - FlavaMaskingGenerator, -) + + +# Copied from transformers.models.flava.image_processing_flava.FLAVA_CODEBOOK_MEAN +FLAVA_CODEBOOK_MEAN = [0.0, 0.0, 0.0] + +# Copied from transformers.models.flava.image_processing_flava.FLAVA_CODEBOOK_STD +FLAVA_CODEBOOK_STD = [1.0, 1.0, 1.0] + +# Copied from transformers.models.flava.image_processing_flava.FLAVA_IMAGE_MEAN +# These values are taken from CLIP +FLAVA_IMAGE_MEAN = OPENAI_CLIP_MEAN + +# Copied from transformers.models.flava.image_processing_flava.FLAVA_IMAGE_STD +FLAVA_IMAGE_STD = OPENAI_CLIP_STD + +# Copied from transformers.models.flava.image_processing_flava.LOGIT_LAPLACE_EPS +LOGIT_LAPLACE_EPS: float = 0.1 + +# Copied from transformers.models.flava.image_processing_flava.FlavaImageProcessorKwargs +class FlavaImageProcessorKwargs(ImagesKwargs, total=False): + """ + return_image_mask (`bool`, *optional*, defaults to `False`): + Whether to return the image mask. Can be overridden by the `return_image_mask` parameter in `preprocess`. + input_size_patches (`int`, *optional*, defaults to `14`): + Number of patches in the image in height and width direction. 14x14 = 196 total patches. Can be overridden + by the `input_size_patches` parameter in `preprocess`. + total_mask_patches (`int`, *optional*, defaults to `75`): + Total number of patches that should be masked. Can be overridden by the `total_mask_patches` parameter in + `preprocess`. + mask_group_min_patches (`int`, *optional*, defaults to `16`): + Minimum number of patches that should be masked. Can be overridden by the `mask_group_min_patches` + parameter in `preprocess`. + mask_group_max_patches (`int`, *optional*): + Maximum number of patches that should be masked. Can be overridden by the `mask_group_max_patches` + parameter in `preprocess`. + mask_group_min_aspect_ratio (`float`, *optional*, defaults to `0.3`): + Minimum aspect ratio of the mask window. Can be overridden by the `mask_group_min_aspect_ratio` parameter + in `preprocess`. + mask_group_max_aspect_ratio (`float`, *optional*): + Maximum aspect ratio of the mask window. Can be overridden by the `mask_group_max_aspect_ratio` parameter + in `preprocess`. + return_codebook_pixels (`bool`, *optional*, defaults to `False`): + Whether to return the codebook pixel values. + codebook_do_resize (`bool`, *optional*, defaults to `True`): + Whether to resize the input for codebook to a certain. Can be overridden by the `codebook_do_resize` + parameter in `preprocess`. `codebook_size`. + codebook_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): + Resize the input for codebook to the given size. Can be overridden by the `codebook_size` parameter in + `preprocess`. + codebook_resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.LANCZOS` for PIL backend, + `PILImageResampling.BICUBIC` for torchvision backend): + Resampling filter to use if resizing the codebook image. LANCZOS is not supported for torch Tensors; + BICUBIC is used as the closest alternative for the torchvision backend. Can be overridden by the + `codebook_resample` parameter in `preprocess`. + codebook_do_center_crop (`bool`, *optional*, defaults to `True`): + Whether to crop the input for codebook at the center. If the input size is smaller than + `codebook_crop_size` along any edge, the image is padded with 0's and then center cropped. Can be + overridden by the `codebook_do_center_crop` parameter in `preprocess`. + codebook_crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): + Desired output size for codebook input when applying center-cropping. Can be overridden by the + `codebook_crop_size` parameter in `preprocess`. + codebook_do_rescale (`bool`, *optional*, defaults to `True`): + Whether to rescale the input for codebook by the specified scale `codebook_rescale_factor`. Can be + overridden by the `codebook_do_rescale` parameter in `preprocess`. + codebook_rescale_factor (`int` or `float`, *optional*, defaults to `1/255`): + Defines the scale factor to use if rescaling the codebook image. Can be overridden by the + `codebook_rescale_factor` parameter in `preprocess`. + codebook_do_map_pixels (`bool`, *optional*, defaults to `True`): + Whether to map the pixel values of the codebook input to (1 - 2e)x + e. Can be overridden by the + `codebook_do_map_pixels` parameter in `preprocess`. + codebook_do_normalize (`bool`, *optional*, defaults to `True`): + Whether or not to normalize the input for codebook with `codebook_image_mean` and `codebook_image_std`. Can + be overridden by the `codebook_do_normalize` parameter in `preprocess`. + codebook_image_mean (`Optional[Union[float, Iterable[float]]]`, *optional*, defaults to `[0, 0, 0]`): + The sequence of means for each channel, to be used when normalizing images for codebook. Can be overridden + by the `codebook_image_mean` parameter in `preprocess`. + codebook_image_std (`Optional[Union[float, Iterable[float]]]`, *optional*, defaults to `[0.5, 0.5, 0.5]`): + The sequence of standard deviations for each channel, to be used when normalizing images for codebook. Can + be overridden by the `codebook_image_std` parameter in `preprocess`. + """ + + # Mask related params + return_image_mask: bool + input_size_patches: int + total_mask_patches: int + mask_group_min_patches: int + mask_group_max_patches: int + mask_group_min_aspect_ratio: float + mask_group_max_aspect_ratio: float + # Codebook related params + return_codebook_pixels: bool + codebook_do_resize: bool + codebook_size: dict[str, int] + codebook_resample: int + codebook_do_center_crop: bool + codebook_crop_size: dict[str, int] + codebook_do_rescale: bool + codebook_rescale_factor: int | float + codebook_do_map_pixels: bool + codebook_do_normalize: bool + codebook_image_mean: float | Iterable[float] + codebook_image_std: float | Iterable[float] + +# Copied from transformers.models.flava.image_processing_flava.FlavaMaskingGenerator +# Inspired from https://github.com/microsoft/unilm/blob/master/beit/masking_generator.py +class FlavaMaskingGenerator: + def __init__( + self, + input_size: int | tuple[int, int] = 14, + total_mask_patches: int = 75, + mask_group_max_patches: int | None = None, + mask_group_min_patches: int = 16, + mask_group_min_aspect_ratio: float | None = 0.3, + mask_group_max_aspect_ratio: float | None = None, + ): + if not isinstance(input_size, tuple): + input_size = (input_size,) * 2 + self.height, self.width = input_size + + self.num_patches = self.height * self.width + self.total_mask_patches = total_mask_patches + + self.mask_group_min_patches = mask_group_min_patches + self.mask_group_max_patches = total_mask_patches if mask_group_max_patches is None else mask_group_max_patches + + mask_group_max_aspect_ratio = mask_group_max_aspect_ratio or 1 / mask_group_min_aspect_ratio + self.log_aspect_ratio = (math.log(mask_group_min_aspect_ratio), math.log(mask_group_max_aspect_ratio)) + + def __repr__(self): + repr_str = "MaskingGenerator(%d, %d -> [%d ~ %d], max = %d, %.3f ~ %.3f)" % ( + self.height, + self.width, + self.mask_group_min_patches, + self.mask_group_max_patches, + self.total_mask_patches, + self.log_aspect_ratio[0], + self.log_aspect_ratio[1], + ) + return repr_str + + def get_shape(self): + return self.height, self.width + + def _mask(self, mask, max_mask_patches): + delta = 0 + for _attempt in range(10): + target_area = random.uniform(self.mask_group_min_patches, max_mask_patches) + aspect_ratio = math.exp(random.uniform(*self.log_aspect_ratio)) + height = int(round(math.sqrt(target_area * aspect_ratio))) + width = int(round(math.sqrt(target_area / aspect_ratio))) + if width < self.width and height < self.height: + top = random.randint(0, self.height - height) + left = random.randint(0, self.width - width) + + num_masked = mask[top : top + height, left : left + width].sum() + # Overlap + if 0 < height * width - num_masked <= max_mask_patches: + zeros_pos = mask[top : top + height, left : left + width] == 0 + mask[top : top + height, left : left + width][zeros_pos] = 1 + delta += zeros_pos.sum() + + if delta > 0: + break + return delta + + def __call__(self): + mask = torch.zeros(self.get_shape(), dtype=torch.int) + mask_count = 0 + while mask_count < self.total_mask_patches: + max_mask_patches = self.total_mask_patches - mask_count + max_mask_patches = min(max_mask_patches, self.mask_group_max_patches) + + delta = self._mask(mask, max_mask_patches) + if delta == 0: + break + else: + mask_count += delta + + return mask @auto_docstring @@ -288,5 +460,4 @@ def _preprocess( return BatchFeature(data=data, tensor_type=return_tensors) - __all__ = ["FlavaImageProcessorPil"] diff --git a/src/transformers/models/fuyu/image_processing_pil_fuyu.py b/src/transformers/models/fuyu/image_processing_pil_fuyu.py index e76c40ed518f..a36903ba8f91 100644 --- a/src/transformers/models/fuyu/image_processing_pil_fuyu.py +++ b/src/transformers/models/fuyu/image_processing_pil_fuyu.py @@ -16,16 +16,153 @@ import math import numpy as np -import torch - from ...image_processing_backends import PilBackend -from ...image_processing_utils import get_size_dict +from ...image_processing_utils import BatchFeature, get_size_dict from ...image_utils import ImageInput, PILImageResampling, SizeDict, get_image_size -from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, requires_backends +from ...processing_utils import ImagesKwargs, Unpack +from ...utils import TensorType, auto_docstring, requires_backends, is_torch_available from ...utils.import_utils import requires -from .image_processing_fuyu import FuyuBatchFeature, FuyuImagesKwargs, make_list_of_list_of_images +# Copied from transformers.models.fuyu.image_processing_fuyu.FuyuBatchFeature +class FuyuBatchFeature(BatchFeature): + """ + BatchFeature class for Fuyu image processor and processor. + + The outputs dictionary from the processors contains a mix of tensors and lists of tensors. + """ + + def convert_to_tensors(self, tensor_type: str | TensorType | None = None, **kwargs): + """ + Convert the inner content to tensors. + + Args: + tensor_type (`str` or [`~utils.TensorType`], *optional*): + The type of tensors to use. If `str`, should be one of the values of the enum [`~utils.TensorType`]. If + `None`, no modification is done. + """ + if tensor_type is None: + return self + + is_tensor, as_tensor = self._get_is_as_tensor_fns(tensor_type=tensor_type) + + def _convert_tensor(elem): + if is_tensor(elem): + return elem + return as_tensor(elem) + + def _safe_convert_tensor(elem): + try: + return _convert_tensor(elem) + except: # noqa E722 + if key == "overflowing_values": + raise ValueError("Unable to create tensor returning overflowing values of different lengths. ") + raise ValueError( + "Unable to create tensor, you should probably activate padding " + "with 'padding=True' to have batched tensors with the same length." + ) + + # Do the tensor conversion in batch + for key, value in self.items(): + if isinstance(value, list) and isinstance(value[0], list): + # list[list[Any]] -> list[list[Tensor]] + self[key] = [[_safe_convert_tensor(elem) for elem in elems] for elems in value] + elif isinstance(value, list): + # list[Any] -> list[Tensor] + self[key] = [_safe_convert_tensor(elem) for elem in value] + else: + # Any -> Tensor + self[key] = _safe_convert_tensor(value) + return self + + def to(self, *args, **kwargs) -> "BatchFeature": + """ + Send all values to device by calling `v.to(*args, **kwargs)` (PyTorch only). This should support casting in + different `dtypes` and sending the `BatchFeature` to a different `device`. + + Args: + args (`Tuple`): + Will be passed to the `to(...)` function of the tensors. + kwargs (`Dict`, *optional*): + Will be passed to the `to(...)` function of the tensors. + + Returns: + [`BatchFeature`]: The same instance after modification. + """ + requires_backends(self, ["torch"]) + import torch + + from ...utils import is_torch_device, is_torch_dtype + + new_data = {} + device = kwargs.get("device") + # Check if the args are a device or a dtype + if device is None and len(args) > 0: + # device should be always the first argument + arg = args[0] + if is_torch_dtype(arg): + # The first argument is a dtype + pass + elif isinstance(arg, str) or is_torch_device(arg) or isinstance(arg, int): + device = arg + else: + # it's something else + raise ValueError(f"Attempting to cast a BatchFeature to type {str(arg)}. This is not supported.") + + def _to(elem): + # check if v is a floating point + if torch.is_floating_point(elem): + # cast and send to device + return elem.to(*args, **kwargs) + if device is not None: + return elem.to(device=device) + + return elem + + # We cast only floating point tensors to avoid issues with tokenizers casting `LongTensor` to `FloatTensor` + for k, v in self.items(): + if isinstance(v, list) and isinstance(v[0], list): + # Data structure is a list of lists + new_v = [] + for elems in v: + new_v.append([_to(elem) for elem in elems]) + new_data[k] = new_v + elif isinstance(v, list): + # Data structure is a list + new_data[k] = [_to(elem) for elem in v] + else: + new_data[k] = _to(v) + self.data = new_data + return self + +# Copied from transformers.models.fuyu.image_processing_fuyu.FuyuImagesKwargs +class FuyuImagesKwargs(ImagesKwargs, total=False): + r""" + patch_size (`dict[str, int]`, *optional*, defaults to `{"height": 30, "width": 30}`): + Dictionary in the format `{"height": int, "width": int}` specifying the size of the patches. + padding_value (`float`, *optional*, defaults to 1.0): + The value to pad the image with. + padding_mode (`str`, *optional*, defaults to "constant"): + The padding mode to use when padding the image. + """ + + patch_size: SizeDict | None + padding_value: float + padding_mode: str + +# Copied from transformers.models.fuyu.image_processing_fuyu.make_list_of_list_of_images +def make_list_of_list_of_images( + images: list[list[ImageInput]] | list[ImageInput] | ImageInput, +) -> list[list[ImageInput]]: + if is_valid_image(images): + return [[images]] + + if isinstance(images, list) and all(isinstance(image, list) for image in images): + return images + + if isinstance(images, list): + return [make_list_of_images(image) for image in images] + + raise ValueError("images must be a list of list of images or a list of images or an image.") @auto_docstring @requires(backends=("torch",)) @@ -307,6 +444,9 @@ def preprocess_with_tokenizer_info( requires_backends(self, ["torch"]) import torch +if is_torch_available(): + import torch + if patch_size is None: if isinstance(self.patch_size, SizeDict): patch_size = self.patch_size @@ -419,5 +559,4 @@ def _standardize_kwargs(self, patch_size: dict[str, int] | SizeDict | None = Non kwargs["patch_size"] = patch_size return kwargs - -__all__ = ["FuyuImageProcessorPil"] +__all__ = ["FuyuImageProcessorPil"] \ No newline at end of file diff --git a/src/transformers/models/gemma3/image_processing_pil_gemma3.py b/src/transformers/models/gemma3/image_processing_pil_gemma3.py index a849e05aead0..06896a4fffa0 100644 --- a/src/transformers/models/gemma3/image_processing_pil_gemma3.py +++ b/src/transformers/models/gemma3/image_processing_pil_gemma3.py @@ -28,12 +28,30 @@ SizeDict, get_image_size, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import ( TensorType, auto_docstring, ) -from .image_processing_gemma3 import Gemma3ImageProcessorKwargs + + +# Copied from transformers.models.gemma3.image_processing_gemma3.Gemma3ImageProcessorKwargs +class Gemma3ImageProcessorKwargs(ImagesKwargs, total=False): + """ + do_pan_and_scan (`bool`, *optional*): + Whether to apply `pan_and_scan` to images. + pan_and_scan_min_crop_size (`int`, *optional*): + Minimum size of each crop in pan and scan. + pan_and_scan_max_num_crops (`int`, *optional*): + Maximum number of crops per image in pan and scan. + pan_and_scan_min_ratio_to_activate (`float`, *optional*): + Minimum aspect ratio to activate pan and scan. + """ + + do_pan_and_scan: bool + pan_and_scan_min_crop_size: int + pan_and_scan_max_num_crops: int + pan_and_scan_min_ratio_to_activate: float @auto_docstring @@ -203,5 +221,4 @@ def _preprocess( data={"pixel_values": processed_images, "num_crops": num_crops}, tensor_type=return_tensors ) - __all__ = ["Gemma3ImageProcessorPil"] diff --git a/src/transformers/models/glm46v/image_processing_pil_glm46v.py b/src/transformers/models/glm46v/image_processing_pil_glm46v.py index 58071c11fa3b..13934edd4bb2 100644 --- a/src/transformers/models/glm46v/image_processing_pil_glm46v.py +++ b/src/transformers/models/glm46v/image_processing_pil_glm46v.py @@ -18,15 +18,65 @@ # See the License for the specific language governing permissions and # limitations under the License. - import numpy as np from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_glm46v import Glm46VImageProcessorKwargs, smart_resize + + +# Copied from transformers.models.glm46v.image_processing_glm46v.Glm46VImageProcessorKwargs +class Glm46VImageProcessorKwargs(ImagesKwargs, total=False): + """ + patch_size (`int`, *optional*, defaults to 14): + The spatial patch size of the vision encoder. + temporal_patch_size (`int`, *optional*, defaults to 2): + The temporal patch size of the vision encoder. + merge_size (`int`, *optional*, defaults to 2): + The merge size of the vision encoder to llm encoder. + """ + + patch_size: int + temporal_patch_size: int + merge_size: int + +# Copied from transformers.models.glm46v.image_processing_glm46v.smart_resize +def smart_resize( + num_frames: int, + height: int, + width: int, + temporal_factor: int = 2, + factor: int = 28, + min_pixels: int = 112 * 112, + max_pixels: int = 14 * 14 * 2 * 2 * 2 * 6144, +): + if num_frames < temporal_factor: + raise ValueError(f"t:{num_frames} must be larger than temporal_factor:{temporal_factor}") + if height < factor or width < factor: + scale = max(factor / height, factor / width) + height = int(height * scale) + width = int(width * scale) + + if max(height, width) / min(height, width) > 200: + raise ValueError( + f"absolute aspect ratio must be smaller than 200, got {max(height, width) / min(height, width)}" + ) + h_bar = round(height / factor) * factor + w_bar = round(width / factor) * factor + t_bar = round(num_frames / temporal_factor) * temporal_factor + + if t_bar * h_bar * w_bar > max_pixels: + beta = math.sqrt((num_frames * height * width) / max_pixels) + h_bar = max(factor, math.floor(height / beta / factor) * factor) + w_bar = max(factor, math.floor(width / beta / factor) * factor) + elif t_bar * h_bar * w_bar < min_pixels: + beta = math.sqrt(min_pixels / (num_frames * height * width)) + h_bar = math.ceil(height * beta / factor) * factor + w_bar = math.ceil(width * beta / factor) * factor + + return h_bar, w_bar @auto_docstring @@ -206,5 +256,4 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non grid_h, grid_w = resized_height // patch_size, resized_width // patch_size return grid_h * grid_w - __all__ = ["Glm46VImageProcessorPil"] diff --git a/src/transformers/models/glm4v/image_processing_pil_glm4v.py b/src/transformers/models/glm4v/image_processing_pil_glm4v.py index dcd5a127b934..8e3279b70fae 100644 --- a/src/transformers/models/glm4v/image_processing_pil_glm4v.py +++ b/src/transformers/models/glm4v/image_processing_pil_glm4v.py @@ -26,12 +26,47 @@ ) from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, logging -from .image_processing_glm4v import smart_resize - logger = logging.get_logger(__name__) +# Copied from transformers.models.glm4v.image_processing_glm4v.smart_resize +def smart_resize( + num_frames: int, + height: int, + width: int, + temporal_factor: int = 2, + factor: int = 28, + min_pixels: int = 112 * 112, + max_pixels: int = 14 * 14 * 2 * 2 * 2 * 6144, +): + if num_frames < temporal_factor: + raise ValueError(f"t:{num_frames} must be larger than temporal_factor:{temporal_factor}") + if height < factor or width < factor: + scale = max(factor / height, factor / width) + height = int(height * scale) + width = int(width * scale) + + if max(height, width) / min(height, width) > 200: + raise ValueError( + f"absolute aspect ratio must be smaller than 200, got {max(height, width) / min(height, width)}" + ) + h_bar = round(height / factor) * factor + w_bar = round(width / factor) * factor + t_bar = round(num_frames / temporal_factor) * temporal_factor + + if t_bar * h_bar * w_bar > max_pixels: + beta = math.sqrt((num_frames * height * width) / max_pixels) + h_bar = max(factor, math.floor(height / beta / factor) * factor) + w_bar = max(factor, math.floor(width / beta / factor) * factor) + elif t_bar * h_bar * w_bar < min_pixels: + beta = math.sqrt(min_pixels / (num_frames * height * width)) + h_bar = math.ceil(height * beta / factor) * factor + w_bar = math.ceil(width * beta / factor) * factor + + return h_bar, w_bar + + class Glm4vImageProcessorKwargs(ImagesKwargs, total=False): """ patch_size (`int`, *optional*, defaults to 14): @@ -46,7 +81,6 @@ class Glm4vImageProcessorKwargs(ImagesKwargs, total=False): temporal_patch_size: int merge_size: int - @auto_docstring class Glm4vImageProcessorPil(PilBackend): do_resize = True @@ -224,5 +258,4 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non grid_h, grid_w = resized_height // patch_size, resized_width // patch_size return grid_h * grid_w - __all__ = ["Glm4vImageProcessorPil"] diff --git a/src/transformers/models/glm_image/image_processing_pil_glm_image.py b/src/transformers/models/glm_image/image_processing_pil_glm_image.py index 1d36a1b095de..ea990d04b69b 100644 --- a/src/transformers/models/glm_image/image_processing_pil_glm_image.py +++ b/src/transformers/models/glm_image/image_processing_pil_glm_image.py @@ -25,9 +25,71 @@ from ...feature_extraction_utils import BatchFeature from ...image_processing_backends import PilBackend from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_glm_image import GlmImageImageProcessorKwargs, smart_resize + + +# Copied from transformers.models.glm_image.image_processing_glm_image.GlmImageImageProcessorKwargs +class GlmImageImageProcessorKwargs(ImagesKwargs, total=False): + r""" + min_pixels (`int`, *optional*, defaults to `56 * 56`): + The min pixels of the image to resize the image. + max_pixels (`int`, *optional*, defaults to `28 * 28 * 1280`): + The max pixels of the image to resize the image. + patch_size (`int`, *optional*, defaults to 14): + The spatial patch size of the vision encoder. + temporal_patch_size (`int`, *optional*, defaults to 2): + The temporal patch size of the vision encoder. + merge_size (`int`, *optional*, defaults to 2): + The merge size of the vision encoder to llm encoder. + """ + + min_pixels: int + max_pixels: int + patch_size: int + temporal_patch_size: int + merge_size: int + +# Copied from transformers.models.glm_image.image_processing_glm_image.smart_resize +def smart_resize( + height: int, + width: int, + factor: int = 16, + min_pixels: int = 512 * 512, + max_pixels: int = 2048 * 2048, +) -> tuple[int, int]: + if height < factor or width < factor: + raise ValueError(f"height:{height} or width:{width} must be larger than factor:{factor}") + elif max(height, width) / min(height, width) > 4: + raise ValueError( + f"absolute aspect ratio must be smaller than 4, got {max(height, width) / min(height, width)}" + ) + + shortest_edge = int(round(math.sqrt(min_pixels))) + longest_edge = int(round(math.sqrt(max_pixels))) + min_side = min(height, width) + max_side = max(height, width) + + scale = 1.0 + + if min_side < shortest_edge: + scale = shortest_edge / min_side + + if max_side * scale > longest_edge: + scale = longest_edge / max_side + + height = height // 2 + width = width // 2 + + h_bar = max(factor, int(round(height * scale / factor)) * factor) + w_bar = max(factor, int(round(width * scale / factor)) * factor) + + if max(h_bar, w_bar) > longest_edge: + beta = max(h_bar, w_bar) / longest_edge + h_bar = max(factor, int(math.floor((h_bar / beta) / factor)) * factor) + w_bar = max(factor, int(math.floor((w_bar / beta) / factor)) * factor) + + return h_bar, w_bar @auto_docstring @@ -199,5 +261,4 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non grid_h, grid_w = resized_height // patch_size, resized_width // patch_size return grid_h * grid_w - __all__ = ["GlmImageImageProcessorPil"] diff --git a/src/transformers/models/glm_image/processing_glm_image.py b/src/transformers/models/glm_image/processing_glm_image.py index 07f4103a10a4..3c89d1e908c5 100644 --- a/src/transformers/models/glm_image/processing_glm_image.py +++ b/src/transformers/models/glm_image/processing_glm_image.py @@ -20,8 +20,6 @@ import math -import torch - from ...feature_extraction_utils import BatchFeature from ...image_utils import ImageInput from ...processing_utils import ImagesKwargs, ProcessingKwargs, ProcessorMixin, Unpack @@ -187,6 +185,8 @@ def __call__( is_text_to_image=is_text_to_image, ) ) + import torch + image_inputs["image_grid_thw"] = torch.cat(all_grids, dim=0) # Store images_per_sample for later use (add target images count) @@ -237,6 +237,8 @@ def _build_target_image_grid_thw( prev_token_w: int, is_text_to_image: bool = True, ): + import torch + if is_text_to_image: # Text-to-image: 2 target grids (large + small preview) return torch.tensor( diff --git a/src/transformers/models/glpn/image_processing_pil_glpn.py b/src/transformers/models/glpn/image_processing_pil_glpn.py index 6a70a1cd926e..929839f056b7 100644 --- a/src/transformers/models/glpn/image_processing_pil_glpn.py +++ b/src/transformers/models/glpn/image_processing_pil_glpn.py @@ -20,11 +20,9 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput, PILImageResampling, SizeDict -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, logging from ...utils.import_utils import requires -from .image_processing_glpn import GLPNImageProcessorKwargs - if TYPE_CHECKING: from ...modeling_outputs import DepthEstimatorOutput @@ -32,6 +30,17 @@ logger = logging.get_logger(__name__) +# Copied from transformers.models.glpn.image_processing_glpn.GLPNImageProcessorKwargs +class GLPNImageProcessorKwargs(ImagesKwargs, total=False): + """ + size_divisor (`int`, *optional*, defaults to 32): + When `do_resize` is `True`, images are resized so their height and width are rounded down to the closest + multiple of `size_divisor`. + """ + + size_divisor: int + + @auto_docstring class GLPNImageProcessorPil(PilBackend): """PIL backend for GLPN with size_divisor resize.""" @@ -126,5 +135,4 @@ def post_process_depth_estimation( results.append({"predicted_depth": depth}) return results - __all__ = ["GLPNImageProcessorPil"] diff --git a/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py b/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py index c238d2625b2e..5a35521677fc 100644 --- a/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py +++ b/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py @@ -27,12 +27,70 @@ get_image_size, infer_channel_dimension_format, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import ( TensorType, auto_docstring, ) -from .image_processing_got_ocr2 import GotOcr2ImageProcessorKwargs, get_optimal_tiled_canvas +from functools import lru_cache + + +# Copied from transformers.models.got_ocr2.image_processing_got_ocr2.GotOcr2ImageProcessorKwargs +class GotOcr2ImageProcessorKwargs(ImagesKwargs, total=False): + r""" + crop_to_patches (`bool`, *optional*, defaults to `self.crop_to_patches`): + Whether to crop the image to patches. Can be overridden by the `crop_to_patches` parameter in the + `preprocess` method. + min_patches (`int`, *optional*, defaults to `self.min_patches`): + The minimum number of patches to be extracted from the image. Only has an effect if `crop_to_patches` is + set to `True`. Can be overridden by the `min_patches` parameter in the `preprocess` method. + max_patches (`int`, *optional*, defaults to `self.max_patches`): + The maximum number of patches to be extracted from the image. Only has an effect if `crop_to_patches` is + set to `True`. Can be overridden by the `max_patches` parameter in the `preprocess` method. + """ + + crop_to_patches: bool + min_patches: int + max_patches: int + +# Copied from transformers.models.got_ocr2.image_processing_got_ocr2.get_optimal_tiled_canvas +@lru_cache(maxsize=100) +def get_optimal_tiled_canvas( + original_image_size: tuple[int, int], + target_tile_size: tuple[int, int], + min_image_tiles: int, + max_image_tiles: int, +) -> tuple[int, int]: + """ + Given a minimum and maximum number of tiles, find the canvas with the closest aspect ratio to the + original image aspect ratio. + In case of tie-breaking condition when two canvases have the same aspect ratio difference, we favor the canvas with + more tiles, until the area covered by the tiles is more than twice the target area, in order to avoid unnecessarily + excessive tiling. + """ + possible_tile_arrangements = get_all_supported_aspect_ratios(min_image_tiles, max_image_tiles) + + original_height, original_width = original_image_size + target_tile_height, target_tile_width = target_tile_size + aspect_ratio = original_width / original_height + area = original_width * original_height + + # find the grid with the best aspect ratio + best_ratio_diff = float("inf") + best_grid = (1, 1) + for grid in possible_tile_arrangements: + grid_aspect_ratio = grid[0] / grid[1] + ratio_diff = abs(aspect_ratio - grid_aspect_ratio) + if ratio_diff < best_ratio_diff: + best_ratio_diff = ratio_diff + best_grid = grid + elif ratio_diff == best_ratio_diff: + # if the aspect ratio difference is the same, we favor the grid with more patches + # until the area covered by the patches is more than twice the original image area + if area > 0.5 * target_tile_height * target_tile_width * grid[0] * grid[1]: + best_grid = grid + + return best_grid @auto_docstring @@ -210,5 +268,4 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non return num_patches - __all__ = ["GotOcr2ImageProcessorPil"] diff --git a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py index 0b1367d4507e..e3f15205f71a 100644 --- a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py +++ b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py @@ -40,11 +40,11 @@ safe_squeeze, ) from ...image_utils import ( - IMAGENET_DEFAULT_MEAN, - IMAGENET_DEFAULT_STD, AnnotationFormat, AnnotationType, ChannelDimension, + IMAGENET_DEFAULT_MEAN, + IMAGENET_DEFAULT_STD, ImageInput, PILImageResampling, SizeDict, @@ -53,16 +53,13 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, requires_backends from ...utils.import_utils import requires -from .image_processing_grounding_dino import GroundingDinoImageProcessorKwargs - if TYPE_CHECKING: from .modeling_grounding_dino import GroundingDinoObjectDetectionOutput - if is_vision_available(): import PIL.Image if is_torch_available(): @@ -70,7 +67,6 @@ SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) - # inspired by https://github.com/facebookresearch/grounding_dino/blob/master/datasets/coco.py#L33 def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndarray: """ @@ -105,7 +101,6 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar return masks - # inspired by https://github.com/facebookresearch/grounding_dino/blob/master/datasets/coco.py#L50 def prepare_coco_detection_annotation( image, @@ -166,7 +161,6 @@ def prepare_coco_detection_annotation( return new_target - def masks_to_boxes(masks: np.ndarray) -> np.ndarray: """ Compute the bounding boxes around the provided panoptic segmentation masks. @@ -200,7 +194,6 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: return np.stack([x_min, y_min, x_max, y_max], 1) - # 2 functions below adapted from https://github.com/cocodataset/panopticapi/blob/master/panopticapi/utils.py # Copyright (c) 2018, Alexander Kirillov # All rights reserved. @@ -214,7 +207,6 @@ def rgb_to_id(color): return color[:, :, 0] + 256 * color[:, :, 1] + 256 * 256 * color[:, :, 2] return int(color[0] + 256 * color[1] + 256 * 256 * color[2]) - def prepare_coco_panoptic_annotation( image: np.ndarray, target: dict, @@ -255,7 +247,6 @@ def prepare_coco_panoptic_annotation( return new_target - def _scale_boxes(boxes, target_sizes): """ Scale batch of bounding boxes to the target sizes. @@ -284,6 +275,21 @@ def _scale_boxes(boxes, target_sizes): return boxes +# Copied from transformers.models.grounding_dino.image_processing_grounding_dino.GroundingDinoImageProcessorKwargs +class GroundingDinoImageProcessorKwargs(ImagesKwargs, total=False): + r""" + format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): + Data format of the annotations. One of "coco_detection" or "coco_panoptic". + do_convert_annotations (`bool`, *optional*, defaults to `True`): + Controls whether to convert the annotations to the format expected by the GROUNDING_DINO model. Converts the + bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. + Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. + """ + + format: str | AnnotationFormat + do_convert_annotations: bool + + @auto_docstring class GroundingDinoImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR @@ -753,5 +759,4 @@ def post_process_object_detection( return results - __all__ = ["GroundingDinoImageProcessorPil"] diff --git a/src/transformers/models/idefics2/image_processing_pil_idefics2.py b/src/transformers/models/idefics2/image_processing_pil_idefics2.py index 8b6373564aca..4cb2c18e4495 100644 --- a/src/transformers/models/idefics2/image_processing_pil_idefics2.py +++ b/src/transformers/models/idefics2/image_processing_pil_idefics2.py @@ -26,15 +26,8 @@ SizeDict, make_nested_list_of_images, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_idefics2 import ( - Idefics2ImageProcessorKwargs, - convert_to_rgb, - get_max_height_width, - get_resize_output_image_size, -) - def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: """Make pixel mask: 1=valid, 0=padding. Images are CHW.""" @@ -44,6 +37,70 @@ def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndar return mask +# Copied from transformers.models.idefics2.image_processing_idefics2.Idefics2ImageProcessorKwargs +class Idefics2ImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_image_splitting (`bool`, *optional*, defaults to `self.do_image_splitting`): + Whether to split the image into a sequence 4 equal sub-images concatenated with the original image. + """ + + do_image_splitting: bool + +# Copied from transformers.models.idefics2.image_processing_idefics2.convert_to_rgb +def convert_to_rgb(image: ImageInput) -> ImageInput: + """ + Converts an image to RGB format. Only converts if the image is of type PIL.Image.Image, otherwise returns the image + as is. + """ + if not is_vision_available() or not isinstance(image, Image.Image): + return image + + if image.mode == "RGB": + return image + + image_rgba = image.convert("RGBA") + background = Image.new("RGBA", image_rgba.size, (255, 255, 255)) + alpha_composite = Image.alpha_composite(background, image_rgba) + alpha_composite = alpha_composite.convert("RGB") + return alpha_composite + +# Copied from transformers.models.idefics2.image_processing_idefics2.get_max_height_width +def get_max_height_width(images_list: list[list["torch.Tensor|np.ndarray"]]) -> tuple[int, int]: + """ + Get the maximum height and width across all images in a batch. + """ + image_sizes = [] + for images in images_list: + for image in images: + image_sizes.append(image.shape[-2:]) + + max_height = max(size[0] for size in image_sizes) + max_width = max(size[1] for size in image_sizes) + return (max_height, max_width) + +# Copied from transformers.models.idefics2.image_processing_idefics2.get_resize_output_image_size +def get_resize_output_image_size(image, size: SizeDict) -> tuple[int, int]: + """ + Get the output size of the image after resizing given a dictionary specifying the max and min sizes. + Images are always channels-first (CHW). + """ + height, width = image.shape[-2:] + + min_len = size.shortest_edge + max_len = size.longest_edge + aspect_ratio = width / height + + if width >= height and width > max_len: + width = max_len + height = int(width / aspect_ratio) + elif height > width and height > max_len: + height = max_len + width = int(height * aspect_ratio) + height = max(height, min_len) + width = max(width, min_len) + return height, width + + @auto_docstring class Idefics2ImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR @@ -206,5 +263,4 @@ def _preprocess( return BatchFeature(data=data, tensor_type=return_tensors) - __all__ = ["Idefics2ImageProcessorPil"] diff --git a/src/transformers/models/idefics3/image_processing_pil_idefics3.py b/src/transformers/models/idefics3/image_processing_pil_idefics3.py index 2430590b8e07..16b0b86f220e 100644 --- a/src/transformers/models/idefics3/image_processing_pil_idefics3.py +++ b/src/transformers/models/idefics3/image_processing_pil_idefics3.py @@ -28,18 +28,8 @@ SizeDict, make_nested_list_of_images, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_idefics3 import ( - MAX_IMAGE_SIZE, - Idefics3ImageProcessorKwargs, - _resize_output_size_rescale_to_max_len, - _resize_output_size_scale_below_upper_bound, - get_max_height_width, - get_num_channels, - get_resize_output_image_size, -) - def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: """Make pixel mask: 1=valid, 0=padding. Images are CHW.""" @@ -49,6 +39,143 @@ def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndar return mask +# Copied from transformers.models.idefics3.image_processing_idefics3.MAX_IMAGE_SIZE +MAX_IMAGE_SIZE = 4096 # 4k resolution as absolute maximum + +# Copied from transformers.models.idefics3.image_processing_idefics3.Idefics3ImageProcessorKwargs +class Idefics3ImageProcessorKwargs(ImagesKwargs, total=False): + """ + do_image_splitting (`bool`, *optional*, defaults to `True`): + Whether to split the image into sub-images concatenated with the original image. They are split into patches + such that each patch has a size of `max_image_size["height"]` x `max_image_size["width"]`. + max_image_size (`Dict`, *optional*, defaults to `{"longest_edge": 364}`): + Maximum resolution of the patches of images accepted by the model. This is a dictionary containing the key "longest_edge". + return_row_col_info (`bool`, *optional*, defaults to `False`): + Whether to return the row and column information of the images. + """ + + do_image_splitting: bool + max_image_size: dict[str, int] + return_row_col_info: bool + +# Copied from transformers.models.idefics3.image_processing_idefics3._resize_output_size_rescale_to_max_len +def _resize_output_size_rescale_to_max_len( + height: int, width: int, min_len: int | None = 1, max_len: int | None = None +) -> tuple[int, int]: + """ + Get the output size of the image after resizing given a dictionary specifying the max and min sizes. + Args: + height (`int`): + Height of the input image. + width (`int`): + Width of the input image. + min_len (`int`, *optional*, defaults to 1): + Minimum size of the output image. + max_len (`int`, *optional*, defaults to the maximum size of the image): + Maximum size of the output image. + Returns: + The output size of the image after resizing. + """ + max_len = max(height, width) if max_len is None else max_len + aspect_ratio = width / height + + if width >= height: + width = max_len + height = int(width / aspect_ratio) + if height % 2 != 0: + height += 1 + elif height > width: + height = max_len + width = int(height * aspect_ratio) + if width % 2 != 0: + width += 1 + + # Avoid resizing to a size smaller than min_len + height = max(height, min_len) + width = max(width, min_len) + return height, width + +# Copied from transformers.models.idefics3.image_processing_idefics3._resize_output_size_scale_below_upper_bound +def _resize_output_size_scale_below_upper_bound( + height: int, width: int, max_len: dict[str, int] | None = None +) -> tuple[int, int]: + """ + Get the output size of the image after resizing given a dictionary specifying the max and min sizes. + Args: + height (`int`): + Height of the input image. + width (`int`): + Width of the input image. + max_len (`Dict[str, int]`, *optional*, defaults to the maximum size of the image): + Defines the maximum dimensions of the image. + Returns: + The output size of the image after resizing. + """ + max_len = max(height, width) if max_len is None else max_len + + aspect_ratio = width / height + if width >= height and width > max_len: + width = max_len + height = int(width / aspect_ratio) + elif height > width and height > max_len: + height = max_len + width = int(height * aspect_ratio) + + # Avoid resizing to a size smaller than 1 + height = max(height, 1) + width = max(width, 1) + return height, width + +# Copied from transformers.models.idefics3.image_processing_idefics3.get_max_height_width +def get_max_height_width(images_list: list[list["torch.Tensor|np.ndarray"]]) -> tuple[int, int]: + """ + Get the maximum height and width across all images in a batch. + """ + image_sizes = [] + for images in images_list: + for image in images: + image_sizes.append(image.shape[-2:]) + + max_height = max(size[0] for size in image_sizes) + max_width = max(size[1] for size in image_sizes) + return (max_height, max_width) + +# Copied from transformers.models.idefics3.image_processing_idefics3.get_num_channels +def get_num_channels(images_list: list[list["torch.Tensor|np.ndarray"]]) -> int: + """ + Get the number of channels across all images in a batch. Handle empty sublists like in [[], [image]]. + """ + for images in images_list: + if images: + return images[0].shape[0] + + raise ValueError("No images found in the batch.") + +# Copied from transformers.models.idefics3.image_processing_idefics3.get_resize_output_image_size +def get_resize_output_image_size( + image: "torch.Tensor", + resolution_max_side: int, +) -> tuple[int, int]: + """ + Get the output size of the image after resizing given a dictionary specifying the max and min sizes. + Args: + image (`torch.Tensor`): + Image to resize. + resolution_max_side (`int`): + The longest edge of the image will be resized to this value. The shortest edge will be resized to keep the + input aspect ratio. + Returns: + The output size of the image after resizing. + """ + height, width = image.shape[-2:] + + # Find the output size, when rescaling the longest edge to max_len and preserving the aspect ratio + height, width = _resize_output_size_rescale_to_max_len(height, width, max_len=resolution_max_side) + # Find the output size when scaling the image to be below the MAX_IMAGE_SIZE + height, width = _resize_output_size_scale_below_upper_bound(height, width, max_len=MAX_IMAGE_SIZE) + return height, width + + @auto_docstring class Idefics3ImageProcessorPil(PilBackend): resample = PILImageResampling.LANCZOS @@ -340,5 +467,4 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs: di return num_patches, num_rows, num_cols - __all__ = ["Idefics3ImageProcessorPil"] diff --git a/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py b/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py index fc9c9e6a7337..2584b17b2eaf 100644 --- a/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py +++ b/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py @@ -13,6 +13,8 @@ # limitations under the License. """Image processor class for ImageGPT.""" +from typing import Union + import numpy as np from ...image_processing_backends import PilBackend @@ -21,20 +23,16 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import ( TensorType, auto_docstring, is_torch_available, ) - if is_torch_available(): import torch -from .image_processing_imagegpt import ImageGPTImageProcessorKwargs - - def squared_euclidean_distance(a, b): b = b.T a2 = np.sum(np.square(a), axis=1) @@ -43,13 +41,27 @@ def squared_euclidean_distance(a, b): d = a2[:, None] - 2 * ab + b2[None, :] return d - def color_quantize(x, clusters): x = x.reshape(-1, 3) d = squared_euclidean_distance(x, clusters) return np.argmin(d, axis=1) +# Copied from transformers.models.imagegpt.image_processing_imagegpt.ImageGPTImageProcessorKwargs +class ImageGPTImageProcessorKwargs(ImagesKwargs, total=False): + r""" + clusters (`np.ndarray` or `list[list[int]]` or `torch.Tensor`, *optional*, defaults to `self.clusters`): + The color clusters to use, of shape `(n_clusters, 3)` when color quantizing. Can be overridden by `clusters` + in `preprocess`. + do_color_quantize (`bool`, *optional*, defaults to `self.do_color_quantize`): + Controls whether to apply color quantization to convert continuous pixel values to discrete cluster indices. + When True, each pixel is assigned to its nearest color cluster, enabling ImageGPT's discrete token modeling. + """ + + clusters: Union[np.ndarray, list[list[int]], "torch.Tensor"] | None + do_color_quantize: bool + + @auto_docstring class ImageGPTImageProcessorPil(PilBackend): model_input_names = ["input_ids"] @@ -136,5 +148,4 @@ def to_dict(self): return output - __all__ = ["ImageGPTImageProcessorPil"] diff --git a/src/transformers/models/janus/image_processing_pil_janus.py b/src/transformers/models/janus/image_processing_pil_janus.py index 7cc38a538e70..60a6a3f57bbc 100644 --- a/src/transformers/models/janus/image_processing_pil_janus.py +++ b/src/transformers/models/janus/image_processing_pil_janus.py @@ -33,12 +33,22 @@ make_flat_list_of_images, to_numpy_array, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import ( TensorType, auto_docstring, ) -from .image_processing_janus import JanusImageProcessorKwargs + + +# Copied from transformers.models.janus.image_processing_janus.JanusImageProcessorKwargs +class JanusImageProcessorKwargs(ImagesKwargs, total=False): + r""" + min_size (`int`, *optional*, defaults to 14): + The minimum allowed size for the resized image. Ensures that neither the height nor width + falls below this value after resizing. + """ + + min_size: int @auto_docstring @@ -257,5 +267,4 @@ def unnormalize( ) return image - __all__ = ["JanusImageProcessorPil"] diff --git a/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py b/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py index 4a8d7ad9396c..47d0478762b1 100644 --- a/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py +++ b/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py @@ -16,16 +16,54 @@ import math import numpy as np -import torch - from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature, get_size_dict from ...image_utils import ChannelDimension, ImageInput, SizeDict, get_image_size -from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, requires_backends +from ...processing_utils import ImagesKwargs, Unpack +from ...utils import TensorType, auto_docstring, requires_backends, is_torch_available from ...utils.import_utils import requires -from .image_processing_kosmos2_5 import Kosmos2_5ImageProcessorKwargs, torch_extract_patches +if is_torch_available(): + import torch + from torch import nn + +# Copied from transformers.models.kosmos2_5.image_processing_kosmos2_5.Kosmos2_5ImageProcessorKwargs +class Kosmos2_5ImageProcessorKwargs(ImagesKwargs, total=False): + r""" + patch_size (`Dict[str, int]`, *optional*, defaults to `{"height": 16, "width": 16}`): + The patch size to use for the image. According to Kosmos2_5 paper and code, the patch size is 16x16. + max_patches (`int`, *optional*, defaults to 4096): + The maximum number of patches to extract from the image as per the + [KOSMOS 2.5 paper](https://huggingface.co/papers/2309.11419). + """ + + patch_size: SizeDict | None + max_patches: int + +# Copied from transformers.models.kosmos2_5.image_processing_kosmos2_5.torch_extract_patches +# Similar to transformers.models.pix2struct.image_processing_pix2struct.torch_extract_patches but dealing with a batch of images directly. +def torch_extract_patches(image_tensor, patch_height, patch_width): + """ + Utility function to extract patches from a given tensor representing a batch of images. Returns a tensor of shape + (batch_size, `rows`, `columns`, `num_channels` x `patch_height` x `patch_width`). + + Args: + image_tensor (torch.Tensor): + The image tensor to extract patches from. + patch_height (int): + The height of the patches to extract. + patch_width (int): + The width of the patches to extract. + """ + patches = torch.nn.functional.unfold(image_tensor, (patch_height, patch_width), stride=(patch_height, patch_width)) + patches = patches.reshape(image_tensor.size(0), image_tensor.size(1), patch_height, patch_width, -1) + patches = patches.permute(0, 4, 2, 3, 1).reshape( + image_tensor.size(0), + image_tensor.size(2) // patch_height, + image_tensor.size(3) // patch_width, + image_tensor.size(1) * patch_height * patch_width, + ) + return patches @auto_docstring @requires(backends=("torch",)) @@ -210,5 +248,4 @@ def _standardize_kwargs(self, patch_size: dict[str, int] | SizeDict | None = Non kwargs["patch_size"] = patch_size return kwargs - -__all__ = ["Kosmos2_5ImageProcessorPil"] +__all__ = ["Kosmos2_5ImageProcessorPil"] \ No newline at end of file diff --git a/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py b/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py index 6a2884f7d899..a2b460e4d5a3 100644 --- a/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py +++ b/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py @@ -24,9 +24,75 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, requires_backends -from .image_processing_layoutlmv2 import LayoutLMv2ImageProcessorKwargs, apply_tesseract + + +# Copied from transformers.models.layoutlmv2.image_processing_layoutlmv2.LayoutLMv2ImageProcessorKwargs +class LayoutLMv2ImageProcessorKwargs(ImagesKwargs, total=False): + r""" + apply_ocr (`bool`, *optional*, defaults to `self.apply_ocr`): + Whether to apply the Tesseract OCR engine to get words + normalized bounding boxes. Can be overridden by + the `apply_ocr` parameter in the `preprocess` method. + ocr_lang (`str`, *optional*): + The language, specified by its ISO code, to be used by the Tesseract OCR engine. By default, English is + used. Can be overridden by the `ocr_lang` parameter in the `preprocess` method. + tesseract_config (`str`, *optional*): + Any additional custom configuration flags that are forwarded to the `config` parameter when calling + Tesseract. For example: '--psm 6'. Can be overridden by the `tesseract_config` parameter in the + `preprocess` method. + """ + + apply_ocr: bool + ocr_lang: str | None + tesseract_config: str | None + +# Copied from transformers.models.layoutlmv2.image_processing_layoutlmv2.apply_tesseract +def apply_tesseract( + image: "np.ndarray | torch.Tensor", + lang: str | None, + tesseract_config: str | None = None, + input_data_format: str | ChannelDimension | None = None, +): + """Applies Tesseract OCR on a document image, and returns recognized words + normalized bounding boxes.""" + requires_backends(apply_tesseract, ["pytesseract"]) + + # Convert torch tensor to numpy if needed + if hasattr(image, "cpu"): + image = image.cpu().numpy() + elif not isinstance(image, np.ndarray): + image = np.array(image) + + tesseract_config = tesseract_config if tesseract_config is not None else "" + + # apply OCR + pil_image = to_pil_image(image, input_data_format=input_data_format) + image_width, image_height = pil_image.size + data = pytesseract.image_to_data(pil_image, lang=lang, output_type="dict", config=tesseract_config) + words, left, top, width, height = data["text"], data["left"], data["top"], data["width"], data["height"] + + # filter empty words and corresponding coordinates + irrelevant_indices = [idx for idx, word in enumerate(words) if not word.strip()] + words = [word for idx, word in enumerate(words) if idx not in irrelevant_indices] + left = [coord for idx, coord in enumerate(left) if idx not in irrelevant_indices] + top = [coord for idx, coord in enumerate(top) if idx not in irrelevant_indices] + width = [coord for idx, coord in enumerate(width) if idx not in irrelevant_indices] + height = [coord for idx, coord in enumerate(height) if idx not in irrelevant_indices] + + # turn coordinates into (left, top, left+width, top+height) format + actual_boxes = [] + for x, y, w, h in zip(left, top, width, height): + actual_box = [x, y, x + w, y + h] + actual_boxes.append(actual_box) + + # finally, normalize the bounding boxes + normalized_boxes = [] + for box in actual_boxes: + normalized_boxes.append(normalize_box(box, image_width, image_height)) + + assert len(words) == len(normalized_boxes), "Not as many words as there are bounding boxes" + + return words, normalized_boxes @auto_docstring @@ -89,5 +155,4 @@ def _preprocess( return data - __all__ = ["LayoutLMv2ImageProcessorPil"] diff --git a/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py b/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py index 031c6332049b..0e7338816496 100644 --- a/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py +++ b/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py @@ -25,9 +25,76 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, requires_backends -from .image_processing_layoutlmv3 import LayoutLMv3ImageProcessorKwargs, apply_tesseract +from ...image_transforms import ChannelDimension + + +# Copied from transformers.models.layoutlmv3.image_processing_layoutlmv3.LayoutLMv3ImageProcessorKwargs +class LayoutLMv3ImageProcessorKwargs(ImagesKwargs, total=False): + r""" + apply_ocr (`bool`, *optional*, defaults to `True`): + Whether to apply the Tesseract OCR engine to get words + normalized bounding boxes. Can be overridden by + the `apply_ocr` parameter in the `preprocess` method. + ocr_lang (`str`, *optional*): + The language, specified by its ISO code, to be used by the Tesseract OCR engine. By default, English is + used. Can be overridden by the `ocr_lang` parameter in the `preprocess` method. + tesseract_config (`str`, *optional*): + Any additional custom configuration flags that are forwarded to the `config` parameter when calling + Tesseract. For example: '--psm 6'. Can be overridden by the `tesseract_config` parameter in the + `preprocess` method. + """ + + apply_ocr: bool + ocr_lang: str | None + tesseract_config: str | None + +# Copied from transformers.models.layoutlmv3.image_processing_layoutlmv3.apply_tesseract +def apply_tesseract( + image: "np.ndarray | torch.Tensor", + lang: str | None, + tesseract_config: str | None = None, + input_data_format: str | ChannelDimension | None = None, +): + """Applies Tesseract OCR on a document image, and returns recognized words + normalized bounding boxes.""" + requires_backends(apply_tesseract, ["pytesseract"]) + + # Convert torch tensor to numpy if needed + if hasattr(image, "cpu"): + image = image.cpu().numpy() + elif not isinstance(image, np.ndarray): + image = np.array(image) + + tesseract_config = tesseract_config if tesseract_config is not None else "" + + # apply OCR + pil_image = to_pil_image(image, input_data_format=input_data_format) + image_width, image_height = pil_image.size + data = pytesseract.image_to_data(pil_image, lang=lang, output_type="dict", config=tesseract_config) + words, left, top, width, height = data["text"], data["left"], data["top"], data["width"], data["height"] + + # filter empty words and corresponding coordinates + irrelevant_indices = [idx for idx, word in enumerate(words) if not word.strip()] + words = [word for idx, word in enumerate(words) if idx not in irrelevant_indices] + left = [coord for idx, coord in enumerate(left) if idx not in irrelevant_indices] + top = [coord for idx, coord in enumerate(top) if idx not in irrelevant_indices] + width = [coord for idx, coord in enumerate(width) if idx not in irrelevant_indices] + height = [coord for idx, coord in enumerate(height) if idx not in irrelevant_indices] + + # turn coordinates into (left, top, left+width, top+height) format + actual_boxes = [] + for x, y, w, h in zip(left, top, width, height): + actual_box = [x, y, x + w, y + h] + actual_boxes.append(actual_box) + + # finally, normalize the bounding boxes + normalized_boxes = [] + for box in actual_boxes: + normalized_boxes.append(normalize_box(box, image_width, image_height)) + + assert len(words) == len(normalized_boxes), "Not as many words as there are bounding boxes" + + return words, normalized_boxes @auto_docstring @@ -102,5 +169,4 @@ def _preprocess( return data - __all__ = ["LayoutLMv3ImageProcessorPil"] diff --git a/src/transformers/models/lightglue/image_processing_pil_lightglue.py b/src/transformers/models/lightglue/image_processing_pil_lightglue.py index 67168e868358..58fcad9db5a3 100644 --- a/src/transformers/models/lightglue/image_processing_pil_lightglue.py +++ b/src/transformers/models/lightglue/image_processing_pil_lightglue.py @@ -26,24 +26,20 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput, PILImageResampling, SizeDict, to_numpy_array -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available from ...utils.import_utils import requires -from .image_processing_lightglue import LightGlueImageProcessorKwargs, validate_and_format_image_pairs - if TYPE_CHECKING: from .modeling_lightglue import LightGlueKeypointMatchingOutput if is_torch_available(): import torch - def is_grayscale(image: np.ndarray): if image.shape[0] == 1: return True return np.all(image[0, ...] == image[1, ...]) and np.all(image[1, ...] == image[2, ...]) - def convert_to_grayscale(image: ImageInput) -> ImageInput: """ Converts an image to grayscale format using the NTSC formula. Only support numpy and PIL Image. @@ -71,8 +67,47 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: image = image.convert("L") return image - @requires(backends=("torch",)) + + +# Copied from transformers.models.lightglue.image_processing_lightglue.LightGlueImageProcessorKwargs +class LightGlueImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`): + Whether to convert the image to grayscale. Can be overridden by `do_grayscale` in the `preprocess` method. + """ + + do_grayscale: bool + +# Copied from transformers.models.lightglue.image_processing_lightglue.validate_and_format_image_pairs +def validate_and_format_image_pairs(images: ImageInput): + error_message = ( + "Input images must be a one of the following :", + " - A pair of PIL images.", + " - A pair of 3D arrays.", + " - A list of pairs of PIL images.", + " - A list of pairs of 3D arrays.", + ) + + def _is_valid_image(image): + """images is a PIL Image or a 3D array.""" + return is_pil_image(image) or ( + is_valid_image(image) and get_image_type(image) != ImageType.PIL and len(image.shape) == 3 + ) + + if isinstance(images, list): + if len(images) == 2 and all((_is_valid_image(image)) for image in images): + return images + if all( + isinstance(image_pair, list) + and len(image_pair) == 2 + and all(_is_valid_image(image) for image in image_pair) + for image_pair in images + ): + return [image for image_pair in images for image in image_pair] + raise ValueError(error_message) + + class LightGlueImageProcessorPil(PilBackend): valid_kwargs = LightGlueImageProcessorKwargs resample = PILImageResampling.BILINEAR @@ -254,5 +289,4 @@ def _get_color(self, score): b = 0 return (r, g, b) - __all__ = ["LightGlueImageProcessorPil"] diff --git a/src/transformers/models/llava_next/image_processing_pil_llava_next.py b/src/transformers/models/llava_next/image_processing_pil_llava_next.py index 1dfda3142a44..b46c7fb5c8b2 100644 --- a/src/transformers/models/llava_next/image_processing_pil_llava_next.py +++ b/src/transformers/models/llava_next/image_processing_pil_llava_next.py @@ -30,9 +30,20 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_llava_next import LlavaNextImageProcessorKwargs + + +# Copied from transformers.models.llava_next.image_processing_llava_next.LlavaNextImageProcessorKwargs +class LlavaNextImageProcessorKwargs(ImagesKwargs, total=False): + r""" + image_grid_pinpoints (`list[list[int]]`, *optional*): + A list of possible resolutions to use for processing high resolution images. The best resolution is selected + based on the original size of the image. Can be overridden by `image_grid_pinpoints` in the `preprocess` + method. + """ + + image_grid_pinpoints: list[list[int]] @auto_docstring @@ -224,5 +235,4 @@ def _preprocess( data={"pixel_values": processed_images, "image_sizes": image_sizes}, tensor_type=return_tensors ) - __all__ = ["LlavaNextImageProcessorPil"] diff --git a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py index 1a1025a923c8..3904e800ca39 100644 --- a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py +++ b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py @@ -18,7 +18,6 @@ # See the License for the specific language governing permissions and # limitations under the License. - import numpy as np from ...image_processing_backends import PilBackend @@ -32,9 +31,20 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_llava_onevision import LlavaOnevisionImageProcessorKwargs + + +# Copied from transformers.models.llava_onevision.image_processing_llava_onevision.LlavaOnevisionImageProcessorKwargs +class LlavaOnevisionImageProcessorKwargs(ImagesKwargs, total=False): + r""" + image_grid_pinpoints (`list[list[int]]`, *optional*): + A list of possible resolutions to use for processing high resolution images. The best resolution is selected + based on the original size of the image. Can be overridden by `image_grid_pinpoints` in the `preprocess` + method. + """ + + image_grid_pinpoints: list[list[int]] @auto_docstring @@ -293,5 +303,4 @@ def pad_to_square( return result - __all__ = ["LlavaOnevisionImageProcessorPil"] diff --git a/src/transformers/models/mask2former/image_processing_pil_mask2former.py b/src/transformers/models/mask2former/image_processing_pil_mask2former.py index 935d767b4956..6c50a6390d5a 100644 --- a/src/transformers/models/mask2former/image_processing_pil_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_pil_mask2former.py @@ -18,7 +18,6 @@ # See the License for the specific language governing permissions and # limitations under the License. - import math from typing import Any @@ -29,9 +28,9 @@ from ...image_transforms import PaddingMode, get_size_with_aspect_ratio from ...image_transforms import pad as np_pad from ...image_utils import ( + ChannelDimension, IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, - ChannelDimension, ImageInput, PILImageResampling, SizeDict, @@ -39,25 +38,16 @@ get_image_size_for_max_height_width, get_max_height_width, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, logging, requires_backends from ...utils.import_utils import requires -from .image_processing_mask2former import ( - Mask2FormerImageProcessorKwargs, - compute_segments, - convert_segmentation_to_rle, - remove_low_and_no_objects, -) - if is_torch_available(): import torch from torch import nn - logger = logging.get_logger(__name__) - def convert_segmentation_map_to_binary_masks( segmentation_map: np.ndarray, instance_id_to_semantic_id: dict[int, int] | None = None, @@ -93,8 +83,145 @@ def convert_segmentation_map_to_binary_masks( labels = all_labels.astype(np.int64) return binary_masks.astype(np.float32), labels - @requires(backends=("torch",)) + + +# Copied from transformers.models.mask2former.image_processing_mask2former.Mask2FormerImageProcessorKwargs +class Mask2FormerImageProcessorKwargs(ImagesKwargs, total=False): + r""" + ignore_index (`int`, *optional*): + Label to be assigned to background pixels in segmentation maps. If provided, segmentation map pixels + denoted with 0 (background) will be replaced with `ignore_index`. + do_reduce_labels (`bool`, *optional*, defaults to `False`): + Whether or not to decrement all label values of segmentation maps by 1. Usually used for datasets where 0 + is used for background, and background itself is not included in all classes of a dataset (e.g. ADE20k). + The background label will be replaced by `ignore_index`. + num_labels (`int`, *optional*): + The number of labels in the segmentation map. + size_divisor (`int`, *optional*, defaults to `32`): + Some backbones need images divisible by a certain number. If not passed, it defaults to the value used in + Swin Transformer. + pad_size (`SizeDict`, *optional*): + The size to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` + is not provided, images will be padded to the largest height and width in the batch. + """ + + ignore_index: int | None + do_reduce_labels: bool + num_labels: int | None + size_divisor: int + pad_size: SizeDict | None + +# Copied from transformers.models.mask2former.image_processing_mask2former.compute_segments +def compute_segments( + mask_probs, + pred_scores, + pred_labels, + mask_threshold: float = 0.5, + overlap_mask_area_threshold: float = 0.8, + label_ids_to_fuse: set[int] | None = None, + target_size: tuple[int, int] | None = None, +): + height = mask_probs.shape[1] if target_size is None else target_size[0] + width = mask_probs.shape[2] if target_size is None else target_size[1] + + segmentation = torch.zeros((height, width), dtype=torch.int32, device=mask_probs.device) + segments: list[dict] = [] + + if target_size is not None: + mask_probs = nn.functional.interpolate( + mask_probs.unsqueeze(0), size=target_size, mode="bilinear", align_corners=False + )[0] + + current_segment_id = 0 + + # Weigh each mask by its prediction score + mask_probs *= pred_scores.view(-1, 1, 1) + mask_labels = mask_probs.argmax(0) # [height, width] + + # Keep track of instances of each class + stuff_memory_list: dict[str, int] = {} + for k in range(pred_labels.shape[0]): + pred_class = pred_labels[k].item() + should_fuse = pred_class in label_ids_to_fuse + + # Check if mask exists and large enough to be a segment + mask_exists, mask_k = check_segment_validity( + mask_labels, mask_probs, k, mask_threshold, overlap_mask_area_threshold + ) + + if mask_exists: + if pred_class in stuff_memory_list: + current_segment_id = stuff_memory_list[pred_class] + else: + current_segment_id += 1 + + # Add current object segment to final segmentation map + segmentation[mask_k] = current_segment_id + segment_score = round(pred_scores[k].item(), 6) + segments.append( + { + "id": current_segment_id, + "label_id": pred_class, + "was_fused": should_fuse, + "score": segment_score, + } + ) + if should_fuse: + stuff_memory_list[pred_class] = current_segment_id + + return segmentation, segments + +# Copied from transformers.models.mask2former.image_processing_mask2former.convert_segmentation_to_rle +def convert_segmentation_to_rle(segmentation): + """ + Converts given segmentation map of shape `(height, width)` to the run-length encoding (RLE) format. + + Args: + segmentation (`torch.Tensor`): + A segmentation map of shape `(height, width)` where each value denotes a segment or class id. + Returns: + `list[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. + """ + segment_ids = torch.unique(segmentation) + + run_length_encodings = [] + for idx in segment_ids: + mask = torch.where(segmentation == idx, 1, 0) + rle = binary_mask_to_rle(mask) + run_length_encodings.append(rle) + + return run_length_encodings + +# Copied from transformers.models.mask2former.image_processing_mask2former.remove_low_and_no_objects +def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): + """ + Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and + `labels`. + + Args: + masks (`torch.Tensor`): + A tensor of shape `(num_queries, height, width)`. + scores (`torch.Tensor`): + A tensor of shape `(num_queries)`. + labels (`torch.Tensor`): + A tensor of shape `(num_queries)`. + object_mask_threshold (`float`): + A number between 0 and 1 used to binarize the masks. + Raises: + `ValueError`: Raised when the first dimension doesn't match in all input tensors. + Returns: + `tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region + < `object_mask_threshold`. + """ + if not (masks.shape[0] == scores.shape[0] == labels.shape[0]): + raise ValueError("mask, scores and labels must have the same shape!") + + to_keep = labels.ne(num_labels) & (scores > object_mask_threshold) + + return masks[to_keep], scores[to_keep], labels[to_keep] + + class Mask2FormerImageProcessorPil(PilBackend): valid_kwargs = Mask2FormerImageProcessorKwargs resample = PILImageResampling.BILINEAR @@ -682,5 +809,4 @@ def post_process_panoptic_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results - __all__ = ["Mask2FormerImageProcessorPil"] diff --git a/src/transformers/models/maskformer/image_processing_pil_maskformer.py b/src/transformers/models/maskformer/image_processing_pil_maskformer.py index 69f64ff84bb3..0bdc5bdd2037 100644 --- a/src/transformers/models/maskformer/image_processing_pil_maskformer.py +++ b/src/transformers/models/maskformer/image_processing_pil_maskformer.py @@ -17,17 +17,15 @@ from typing import Any import numpy as np -import torch -from torch import nn from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature, get_size_dict from ...image_transforms import PaddingMode, get_size_with_aspect_ratio from ...image_transforms import pad as np_pad from ...image_utils import ( + ChannelDimension, IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, - ChannelDimension, ImageInput, PILImageResampling, SizeDict, @@ -35,20 +33,16 @@ get_image_size_for_max_height_width, get_max_height_width, ) -from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, logging, requires_backends +from ...processing_utils import ImagesKwargs, Unpack +from ...utils import TensorType, auto_docstring, logging, requires_backends, is_torch_available from ...utils.import_utils import requires -from .image_processing_maskformer import ( - MaskFormerImageProcessorKwargs, - compute_segments, - convert_segmentation_to_rle, - remove_low_and_no_objects, -) +if is_torch_available(): + import torch + from torch import nn logger = logging.get_logger(__name__) - def convert_segmentation_map_to_binary_masks( segmentation_map: np.ndarray, instance_id_to_semantic_id: dict[int, int] | None = None, @@ -84,6 +78,140 @@ def convert_segmentation_map_to_binary_masks( labels = all_labels.astype(np.int64) return binary_masks.astype(np.float32), labels +# Copied from transformers.models.maskformer.image_processing_maskformer.MaskFormerImageProcessorKwargs +class MaskFormerImageProcessorKwargs(ImagesKwargs, total=False): + r""" + ignore_index (`int`, *optional*): + Label to be assigned to background pixels in segmentation maps. If provided, segmentation map pixels + denoted with 0 (background) will be replaced with `ignore_index`. + do_reduce_labels (`bool`, *optional*, defaults to `False`): + Whether or not to decrement all label values of segmentation maps by 1. Usually used for datasets where 0 + is used for background, and background itself is not included in all classes of a dataset (e.g. ADE20k). + The background label will be replaced by `ignore_index`. + num_labels (`int`, *optional*): + The number of labels in the segmentation map. + size_divisor (`int`, *optional*, defaults to `32`): + Some backbones need images divisible by a certain number. If not passed, it defaults to the value used in + Swin Transformer. + pad_size (`SizeDict`, *optional*): + The size to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` + is not provided, images will be padded to the largest height and width in the batch. + """ + + ignore_index: int | None + do_reduce_labels: bool + num_labels: int | None + size_divisor: int + pad_size: SizeDict | None + +# Copied from transformers.models.maskformer.image_processing_maskformer.compute_segments +def compute_segments( + mask_probs, + pred_scores, + pred_labels, + mask_threshold: float = 0.5, + overlap_mask_area_threshold: float = 0.8, + label_ids_to_fuse: set[int] | None = None, + target_size: tuple[int, int] | None = None, +): + height = mask_probs.shape[1] if target_size is None else target_size[0] + width = mask_probs.shape[2] if target_size is None else target_size[1] + + segmentation = torch.zeros((height, width), dtype=torch.int32, device=mask_probs.device) + segments: list[dict] = [] + + if target_size is not None: + mask_probs = nn.functional.interpolate( + mask_probs.unsqueeze(0), size=target_size, mode="bilinear", align_corners=False + )[0] + + current_segment_id = 0 + + # Weigh each mask by its prediction score + mask_probs *= pred_scores.view(-1, 1, 1) + mask_labels = mask_probs.argmax(0) # [height, width] + + # Keep track of instances of each class + stuff_memory_list: dict[str, int] = {} + for k in range(pred_labels.shape[0]): + pred_class = pred_labels[k].item() + should_fuse = pred_class in label_ids_to_fuse + + # Check if mask exists and large enough to be a segment + mask_exists, mask_k = check_segment_validity( + mask_labels, mask_probs, k, mask_threshold, overlap_mask_area_threshold + ) + + if mask_exists: + if pred_class in stuff_memory_list: + current_segment_id = stuff_memory_list[pred_class] + else: + current_segment_id += 1 + + # Add current object segment to final segmentation map + segmentation[mask_k] = current_segment_id + segment_score = round(pred_scores[k].item(), 6) + segments.append( + { + "id": current_segment_id, + "label_id": pred_class, + "was_fused": should_fuse, + "score": segment_score, + } + ) + if should_fuse: + stuff_memory_list[pred_class] = current_segment_id + + return segmentation, segments + +# Copied from transformers.models.maskformer.image_processing_maskformer.convert_segmentation_to_rle +def convert_segmentation_to_rle(segmentation): + """ + Converts given segmentation map of shape `(height, width)` to the run-length encoding (RLE) format. + + Args: + segmentation (`torch.Tensor`): + A segmentation map of shape `(height, width)` where each value denotes a segment or class id. + Returns: + `list[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. + """ + segment_ids = torch.unique(segmentation) + + run_length_encodings = [] + for idx in segment_ids: + mask = torch.where(segmentation == idx, 1, 0) + rle = binary_mask_to_rle(mask) + run_length_encodings.append(rle) + + return run_length_encodings + +# Copied from transformers.models.maskformer.image_processing_maskformer.remove_low_and_no_objects +def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): + """ + Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and + `labels`. + + Args: + masks (`torch.Tensor`): + A tensor of shape `(num_queries, height, width)`. + scores (`torch.Tensor`): + A tensor of shape `(num_queries)`. + labels (`torch.Tensor`): + A tensor of shape `(num_queries)`. + object_mask_threshold (`float`): + A number between 0 and 1 used to binarize the masks. + Raises: + `ValueError`: Raised when the first dimension doesn't match in all input tensors. + Returns: + `tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region + < `object_mask_threshold`. + """ + if not (masks.shape[0] == scores.shape[0] == labels.shape[0]): + raise ValueError("mask, scores and labels must have the same shape!") + + to_keep = labels.ne(num_labels) & (scores > object_mask_threshold) + + return masks[to_keep], scores[to_keep], labels[to_keep] @auto_docstring @requires(backends=("torch",)) @@ -662,5 +790,4 @@ def post_process_panoptic_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results - -__all__ = ["MaskFormerImageProcessorPil"] +__all__ = ["MaskFormerImageProcessorPil"] \ No newline at end of file diff --git a/src/transformers/models/mllama/image_processing_pil_mllama.py b/src/transformers/models/mllama/image_processing_pil_mllama.py index 3697a2632415..0224d196acb5 100644 --- a/src/transformers/models/mllama/image_processing_pil_mllama.py +++ b/src/transformers/models/mllama/image_processing_pil_mllama.py @@ -20,25 +20,17 @@ from ...image_transforms import PaddingMode, get_image_size from ...image_transforms import pad as np_pad from ...image_utils import ( + ChannelDimension, IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, - ChannelDimension, ImageInput, PILImageResampling, SizeDict, make_nested_list_of_images, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_mllama import ( - MllamaImageProcessorKwargs, - _validate_mllama_preprocess_arguments, - convert_to_rgb, - get_all_supported_aspect_ratios, - get_image_size_fit_to_canvas, - get_optimal_tiled_canvas, -) - +from functools import lru_cache def split_to_tiles_np(image: np.ndarray, num_tiles_height: int, num_tiles_width: int) -> np.ndarray: """Split an image into tiles (numpy version).""" @@ -52,7 +44,6 @@ def split_to_tiles_np(image: np.ndarray, num_tiles_height: int, num_tiles_width: return np.ascontiguousarray(image) - def build_aspect_ratio_mask_np(aspect_ratios: list[list[tuple[int, int]]], max_image_tiles: int) -> np.ndarray: """ Build aspect ratio mask (numpy version). @@ -84,7 +75,6 @@ def build_aspect_ratio_mask_np(aspect_ratios: list[list[tuple[int, int]]], max_i return aspect_ratio_mask - def pack_images( batch_images: list[list[np.ndarray]], max_image_tiles: int, @@ -136,7 +126,6 @@ def pack_images( return stacked_images, all_num_tiles - def convert_aspect_ratios_to_ids_np(aspect_ratios: list[list[tuple[int, int]]], max_image_tiles: int) -> np.ndarray: """ Convert aspect ratio tuples to ids (numpy version). @@ -167,6 +156,183 @@ def convert_aspect_ratios_to_ids_np(aspect_ratios: list[list[tuple[int, int]]], return aspect_ratios_ids +# Copied from transformers.models.mllama.image_processing_mllama.MllamaImageProcessorKwargs +class MllamaImageProcessorKwargs(ImagesKwargs, total=False): + """ + max_image_tiles (`int`, *optional*): + The maximum number of tiles allowed. + """ + + max_image_tiles: int + +# Copied from transformers.models.mllama.image_processing_mllama._validate_mllama_preprocess_arguments +def _validate_mllama_preprocess_arguments(do_resize, size, do_pad, max_image_tiles): + if not do_pad: + raise ValueError("MllamaImageProcessor doesn't support `do_pad=False` mode.") + if not do_resize: + raise ValueError("MllamaImageProcessor doesn't support `do_resize=False` mode.") + if max_image_tiles is None or max_image_tiles <= 0: + raise ValueError(f"MllamaImageProcessor `max_image_tiles` must be a positive integer, got {max_image_tiles}.") + _validate_size(size) + +# Copied from transformers.models.idefics2.image_processing_idefics2.convert_to_rgb +def convert_to_rgb(image: ImageInput) -> ImageInput: + """ + Converts an image to RGB format. Only converts if the image is of type PIL.Image.Image, otherwise returns the image + as is. + """ + if not is_vision_available() or not isinstance(image, Image.Image): + return image + + if image.mode == "RGB": + return image + + image_rgba = image.convert("RGBA") + background = Image.new("RGBA", image_rgba.size, (255, 255, 255)) + alpha_composite = Image.alpha_composite(background, image_rgba) + alpha_composite = alpha_composite.convert("RGB") + return alpha_composite + +# Copied from transformers.models.mllama.image_processing_mllama.get_all_supported_aspect_ratios +@lru_cache(maxsize=10) +def get_all_supported_aspect_ratios(max_image_tiles: int) -> list[tuple[int, int]]: + """ + Computes all allowed aspect ratios for a given maximum number of input tiles. + + This function calculates all possible arrangements of tiles that can be formed + within the constraint of the maximum number of tiles. Each arrangement is + represented by its aspect ratio (width/height) and the corresponding tile configuration. + + Args: + max_image_tiles (`int`): + The maximum number of tiles allowed. + + Returns: + `list[tuple[int, int]]`: A list of tuples, each tuple representing a valid (width, height) + configuration in terms of number of tiles. + + Example: + >>> get_all_supported_aspect_ratios(4) + [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (3, 1), (4, 1)] + + """ + aspect_ratios = [] + for width in range(1, max_image_tiles + 1): + for height in range(1, max_image_tiles + 1): + if width * height <= max_image_tiles: + aspect_ratios.append((width, height)) + return aspect_ratios + +# Copied from transformers.models.mllama.image_processing_mllama.get_image_size_fit_to_canvas +def get_image_size_fit_to_canvas( + image_height: int, + image_width: int, + canvas_height: int, + canvas_width: int, + tile_size: int, +) -> tuple[int, int]: + """ + Calculates the new size of an image to fit within a canvas while maintaining aspect ratio. + + This function calculates the optimal size for an image to fit within a canvas defined by + canvas_height and canvas_width, while ensuring that the image dimensions are not smaller than + tile_size. If the image is larger than the canvas, the returned size will fit within the canvas. + If the image already fits within the canvas, the size remains unchanged. + The aspect ratio of the original image is preserved as much as possible. + + Args: + image_height (`int`): + The height of the original image. + image_width (`int`): + The width of the original image. + canvas_height (`int`): + The height of the canvas. + canvas_width (`int`): + The width of the canvas. + tile_size (`int`): + The tile size. + + Returns: + `tuple[int, int]`: A tuple containing the new height and width of the image. + + """ + # Set target image size in between `tile_size` and canvas_size + target_width = np.clip(image_width, tile_size, canvas_width) + target_height = np.clip(image_height, tile_size, canvas_height) + + scale_h = target_height / image_height + scale_w = target_width / image_width + + if scale_w < scale_h: + new_width = target_width + # minimum height is 1 to avoid invalid height of 0 + new_height = min(math.floor(image_height * scale_w) or 1, target_height) + else: + new_height = target_height + # minimum width is 1 to avoid invalid width of 0 + new_width = min(math.floor(image_width * scale_h) or 1, target_width) + + return new_height, new_width + +# Copied from transformers.models.mllama.image_processing_mllama.get_optimal_tiled_canvas +@lru_cache(maxsize=100) +def get_optimal_tiled_canvas( + image_height: int, + image_width: int, + max_image_tiles: int, + tile_size: int, +) -> tuple[int, int]: + r""" + Determines the best canvas based on image and tile size and maximum number of tiles. + + First, calculates possible resolutions based on the maximum number of tiles and tile size. + For each possible resolution, calculates the scaling factors for width and height, and selects + the smallest one. If upscaling is possible, picks the smallest upscaling factor > 1. + If upscaling is not possible, picks the largest scaling factor <= 1. + If there are multiple resolutions with the same max scale, picks the one with the lowest area. + + Args: + image_height (`int`): + The height of the image. + image_width (`int`): + The width of the image. + max_image_tiles (`int`): + The maximum number of tiles any image can be split into. + tile_size (`int`): + The tile size. + + Returns: + `tuple[int, int]`: The best canvas resolution [height, width] for the given image. + """ + possible_tile_arrangements = get_all_supported_aspect_ratios(max_image_tiles) + possible_canvas_sizes = np.array(possible_tile_arrangements) * tile_size + + target_heights, target_widths = np.array(possible_canvas_sizes).T + + scale_h = target_heights / image_height + scale_w = target_widths / image_width + + scales = np.where(scale_w > scale_h, scale_h, scale_w) + + upscaling_options = scales[scales >= 1] + if len(upscaling_options) > 0: + selected_scale = np.min(upscaling_options) + else: + downscaling_options = scales[scales < 1] + selected_scale = np.max(downscaling_options) + + chosen_canvas = possible_canvas_sizes[scales == selected_scale] + + if len(chosen_canvas) > 1: + areas = chosen_canvas[:, 0] * chosen_canvas[:, 1] + optimal_idx = np.argmin(areas) + optimal_canvas = chosen_canvas[optimal_idx] + else: + optimal_canvas = chosen_canvas[0] + + return tuple(optimal_canvas) + + @auto_docstring class MllamaImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR @@ -351,5 +517,4 @@ def _preprocess( return encoded_inputs - __all__ = ["MllamaImageProcessorPil"] diff --git a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py index fc28202598ff..fd073d3b8f32 100644 --- a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py +++ b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py @@ -17,7 +17,6 @@ import numpy as np - if TYPE_CHECKING: pass @@ -31,10 +30,21 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires -from .image_processing_mobilenet_v2 import MobileNetV2ImageProcessorKwargs + + +# Copied from transformers.models.mobilenet_v2.image_processing_mobilenet_v2.MobileNetV2ImageProcessorKwargs +class MobileNetV2ImageProcessorKwargs(ImagesKwargs, total=False): + """ + do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): + Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 + is used for background, and background itself is not included in all classes of a dataset (e.g. + ADE20k). The background label will be replaced by 255. + """ + + do_reduce_labels: bool @auto_docstring @@ -188,5 +198,4 @@ def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] semantic_segmentation = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])] return semantic_segmentation - __all__ = ["MobileNetV2ImageProcessorPil"] diff --git a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py index fc5ee2839508..a624abedaabc 100644 --- a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py +++ b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py @@ -17,7 +17,6 @@ import numpy as np - if TYPE_CHECKING: pass @@ -32,15 +31,28 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, logging from ...utils.import_utils import requires -from .image_processing_mobilevit import MobileVitImageProcessorKwargs - logger = logging.get_logger(__name__) +# Copied from transformers.models.mobilevit.image_processing_mobilevit.MobileVitImageProcessorKwargs +class MobileVitImageProcessorKwargs(ImagesKwargs, total=False): + """ + do_flip_channel_order (`bool`, *optional*, defaults to `self.do_flip_channel_order`): + Whether to flip the color channels from RGB to BGR or vice versa. + do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): + Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 + is used for background, and background itself is not included in all classes of a dataset (e.g. + ADE20k). The background label will be replaced by 255. + """ + + do_flip_channel_order: bool + do_reduce_labels: bool + + @auto_docstring class MobileViTImageProcessorPil(PilBackend): """PIL backend for MobileViT with flip_channel_order and reduce_label support.""" @@ -194,5 +206,4 @@ def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] semantic_segmentation = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])] return semantic_segmentation - __all__ = ["MobileViTImageProcessorPil"] diff --git a/src/transformers/models/nougat/image_processing_pil_nougat.py b/src/transformers/models/nougat/image_processing_pil_nougat.py index 10a673f50c51..8b467cc3a1dd 100644 --- a/src/transformers/models/nougat/image_processing_pil_nougat.py +++ b/src/transformers/models/nougat/image_processing_pil_nougat.py @@ -32,12 +32,27 @@ SizeDict, get_image_size, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import ( TensorType, auto_docstring, ) -from .image_processing_nougat import NougatImageProcessorKwargs + + +# Copied from transformers.models.nougat.image_processing_nougat.NougatImageProcessorKwargs +class NougatImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_crop_margin (`bool`, *optional*, defaults to `self.do_crop_margin`): + Whether to crop the image margins. + do_thumbnail (`bool`, *optional*, defaults to `self.do_thumbnail`): + Whether to resize the image using thumbnail method. + do_align_long_axis (`bool`, *optional*, defaults to `self.do_align_long_axis`): + Whether to align the long axis of the image with the long axis of `size` by rotating by 90 degrees. + """ + + do_crop_margin: bool + do_thumbnail: bool + do_align_long_axis: bool @auto_docstring @@ -274,5 +289,4 @@ def _preprocess( return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) - __all__ = ["NougatImageProcessorPil"] diff --git a/src/transformers/models/oneformer/image_processing_pil_oneformer.py b/src/transformers/models/oneformer/image_processing_pil_oneformer.py index 63ce0571a6c0..26179a11276a 100644 --- a/src/transformers/models/oneformer/image_processing_pil_oneformer.py +++ b/src/transformers/models/oneformer/image_processing_pil_oneformer.py @@ -14,8 +14,6 @@ """Image processor class for OneFormer.""" import numpy as np -import torch -from torch import nn from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature @@ -31,22 +29,12 @@ get_image_size, get_max_height_width, ) -from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, logging +from ...processing_utils import ImagesKwargs, Unpack +from ...utils import TensorType, auto_docstring, logging, is_torch_available from ...utils.import_utils import requires -from .image_processing_oneformer import ( - OneFormerImageProcessorKwargs, - compute_segments, - convert_segmentation_to_rle, - load_metadata, - prepare_metadata, - remove_low_and_no_objects, -) - logger = logging.get_logger(__name__) - def make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -59,11 +47,181 @@ def make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarr """ from ...image_utils import ChannelDimension +if is_torch_available(): + import torch + from torch import nn + input_height, input_width = get_image_size(image, channel_dim=ChannelDimension.FIRST) mask = np.zeros(output_size, dtype=np.int64) mask[:input_height, :input_width] = 1 return mask +# Copied from transformers.models.oneformer.image_processing_oneformer.OneFormerImageProcessorKwargs +class OneFormerImageProcessorKwargs(ImagesKwargs, total=False): + r""" + repo_path (`str`, *optional*, defaults to `shi-labs/oneformer_demo`): + Path to a local directory or HuggingFace Hub repository containing model metadata. + class_info_file (`str`, *optional*): + Path to the JSON file within the repository that contains class metadata. + num_text (`int`, *optional*): + Number of text queries for the text encoder, used as task-guiding prompts. + num_labels (`int`, *optional*): + Number of semantic classes for segmentation, determining the output layer's size. + ignore_index (`int`, *optional*): + Label to ignore in segmentation maps, often used for padding. + do_reduce_labels (`bool`, *optional*, defaults to `False`): + Whether to decrement all label values by 1, mapping the background class to `ignore_index`. + """ + + repo_path: str | None + class_info_file: str | None + num_text: int | None + num_labels: int | None + ignore_index: int | None + do_reduce_labels: bool + +# Copied from transformers.models.oneformer.image_processing_oneformer.compute_segments +def compute_segments( + mask_probs, + pred_scores, + pred_labels, + mask_threshold: float = 0.5, + overlap_mask_area_threshold: float = 0.8, + label_ids_to_fuse: set[int] | None = None, + target_size: tuple[int, int] | None = None, +): + height = mask_probs.shape[1] if target_size is None else target_size[0] + width = mask_probs.shape[2] if target_size is None else target_size[1] + + segmentation = torch.zeros((height, width), dtype=torch.int32, device=mask_probs.device) + segments: list[dict] = [] + + if target_size is not None: + mask_probs = tvF.resize( + mask_probs.unsqueeze(0), + size=target_size, + interpolation=tvF.InterpolationMode.BILINEAR, + )[0] + + current_segment_id = 0 + + mask_probs *= pred_scores.view(-1, 1, 1) + mask_labels = mask_probs.argmax(0) # [height, width] + + stuff_memory_list: dict[str, int] = {} + for k in range(pred_labels.shape[0]): + pred_class = pred_labels[k].item() + should_fuse = pred_class in label_ids_to_fuse + + mask_exists, mask_k = check_segment_validity( + mask_labels, mask_probs, k, mask_threshold, overlap_mask_area_threshold + ) + + if mask_exists: + if pred_class in stuff_memory_list: + current_segment_id = stuff_memory_list[pred_class] + else: + current_segment_id += 1 + + segmentation[mask_k] = current_segment_id + segment_score = round(pred_scores[k].item(), 6) + segments.append( + { + "id": current_segment_id, + "label_id": pred_class, + "was_fused": should_fuse, + "score": segment_score, + } + ) + if should_fuse: + stuff_memory_list[pred_class] = current_segment_id + + return segmentation, segments + +# Copied from transformers.models.oneformer.image_processing_oneformer.convert_segmentation_to_rle +def convert_segmentation_to_rle(segmentation): + """ + Converts given segmentation map of shape `(height, width)` to the run-length encoding (RLE) format. + + Args: + segmentation (`torch.Tensor` or `numpy.array`): + A segmentation map of shape `(height, width)` where each value denotes a segment or class id. + Returns: + `List[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. + """ + segment_ids = torch.unique(segmentation) + + run_length_encodings = [] + for idx in segment_ids: + mask = torch.where(segmentation == idx, 1, 0) + rle = binary_mask_to_rle(mask) + run_length_encodings.append(rle) + + return run_length_encodings + +# Copied from transformers.models.oneformer.image_processing_oneformer.load_metadata +def load_metadata(repo_id, class_info_file): + fname = os.path.join("" if repo_id is None else repo_id, class_info_file) + + if not os.path.exists(fname) or not os.path.isfile(fname): + if repo_id is None: + raise ValueError(f"Could not file {fname} locally. repo_id must be defined if loading from the hub") + if hf_hub_download is None: + raise ImportError( + "huggingface_hub is required to download metadata files. Install it with `pip install huggingface_hub`" + ) + # We try downloading from a dataset by default for backward compatibility + try: + fname = hf_hub_download(repo_id, class_info_file, repo_type="dataset") + except RepositoryNotFoundError: + fname = hf_hub_download(repo_id, class_info_file) + + with open(fname, "r") as f: + class_info = json.load(f) + + return class_info + +# Copied from transformers.models.oneformer.image_processing_oneformer.prepare_metadata +def prepare_metadata(class_info): + metadata = {} + class_names = [] + thing_ids = [] + for key, info in class_info.items(): + metadata[key] = info["name"] + class_names.append(info["name"]) + if info["isthing"]: + thing_ids.append(int(key)) + metadata["thing_ids"] = thing_ids + metadata["class_names"] = class_names + return metadata + +# Copied from transformers.models.oneformer.image_processing_oneformer.remove_low_and_no_objects +def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): + """ + Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and + `labels`. + + Args: + masks (`torch.Tensor`): + A tensor of shape `(num_queries, height, width)`. + scores (`torch.Tensor`): + A tensor of shape `(num_queries)`. + labels (`torch.Tensor`): + A tensor of shape `(num_queries)`. + object_mask_threshold (`float`): + A number between 0 and 1 used to binarize the masks. + Raises: + `ValueError`: Raised when the first dimension doesn't match in all input tensors. + Returns: + `Tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region + < `object_mask_threshold`. + """ + if not (masks.shape[0] == scores.shape[0] == labels.shape[0]): + raise ValueError("mask, scores and labels must have the same shape!") + + to_keep = labels.ne(num_labels) & (scores > object_mask_threshold) + + return masks[to_keep], scores[to_keep], labels[to_keep] @auto_docstring @requires(backends=("torch",)) @@ -782,5 +940,4 @@ def post_process_panoptic_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results - -__all__ = ["OneFormerImageProcessorPil"] +__all__ = ["OneFormerImageProcessorPil"] \ No newline at end of file diff --git a/src/transformers/models/ovis2/image_processing_pil_ovis2.py b/src/transformers/models/ovis2/image_processing_pil_ovis2.py index ce047b7b372d..f918324b4208 100644 --- a/src/transformers/models/ovis2/image_processing_pil_ovis2.py +++ b/src/transformers/models/ovis2/image_processing_pil_ovis2.py @@ -27,13 +27,88 @@ SizeDict, infer_channel_dimension_format, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_ovis2 import ( - Ovis2ImageProcessorKwargs, - get_min_tile_covering_grid, - get_optimal_tiled_canvas, -) +from functools import lru_cache + + +# Copied from transformers.models.ovis2.image_processing_ovis2.Ovis2ImageProcessorKwargs +class Ovis2ImageProcessorKwargs(ImagesKwargs, total=False): + """ + crop_to_patches (`bool`, *optional*, defaults to `False`): + Whether to crop the image to patches. Can be overridden by the `crop_to_patches` parameter in the + `preprocess` method. + min_patches (`int`, *optional*, defaults to 1): + The minimum number of patches to be extracted from the image. Only has an effect if `crop_to_patches` is + set to `True`. Can be overridden by the `min_patches` parameter in the `preprocess` method. + max_patches (`int`, *optional*, defaults to 12): + The maximum number of patches to be extracted from the image. Only has an effect if `crop_to_patches` is + set to `True`. Can be overridden by the `max_patches` parameter in the `preprocess` method. + use_covering_area_grid (`bool`, *optional*, defaults to `True`): + Whether to use the covering area grid to determine the number of patches. Only has an effect if + `crop_to_patches` is set to `True`. Can be overridden by the `use_covering_area_grid` parameter in the + `preprocess` method. + """ + + crop_to_patches: bool + min_patches: int + max_patches: int + use_covering_area_grid: bool + +# Copied from transformers.models.ovis2.image_processing_ovis2.get_min_tile_covering_grid +@lru_cache(maxsize=100) +def get_min_tile_covering_grid( + image_size: tuple[int, int], + target_patch_size: int, + max_image_tiles: int, + covering_threshold: float = 0.9, +) -> tuple[int, int]: + image_height, image_width = image_size + image_area = image_width * image_height + candidate_tile_grids = get_all_supported_aspect_ratios(1, max_image_tiles) + evaluated_grids = [] + sufficient_covering_grids = [] + + for tile_grid in candidate_tile_grids: + tile_regions = split_image_into_grid(image_height, image_width, tile_grid) + tile_covering_ratio = ( + sum(compute_patch_covering_area(*region, target_patch_size) for region in tile_regions) / image_area + ) + evaluated_grids.append((tile_grid, tile_covering_ratio)) + if tile_covering_ratio > covering_threshold: + sufficient_covering_grids.append((tile_grid, tile_covering_ratio)) + + if sufficient_covering_grids: + return min(sufficient_covering_grids, key=lambda x: (x[0][0] * x[0][1], -x[1]))[0] + return min(evaluated_grids, key=lambda x: (-x[1], x[0][0] * x[0][1]))[0] + +# Copied from transformers.models.ovis2.image_processing_ovis2.get_optimal_tiled_canvas +@lru_cache(maxsize=100) +def get_optimal_tiled_canvas( + original_image_size: tuple[int, int], + target_tile_size: tuple[int, int], + min_image_tiles: int, + max_image_tiles: int, +) -> tuple[int, int]: + """Find the canvas with the closest aspect ratio to the original image aspect ratio.""" + possible_tile_arrangements = get_all_supported_aspect_ratios(min_image_tiles, max_image_tiles) + original_height, original_width = original_image_size + target_tile_height, target_tile_width = target_tile_size + aspect_ratio = original_width / original_height + area = original_width * original_height + + best_ratio_diff = float("inf") + best_grid = (1, 1) + for grid in possible_tile_arrangements: + grid_aspect_ratio = grid[0] / grid[1] + ratio_diff = abs(aspect_ratio - grid_aspect_ratio) + if ratio_diff < best_ratio_diff: + best_ratio_diff = ratio_diff + best_grid = grid + elif ratio_diff == best_ratio_diff: + if area > 0.5 * target_tile_height * target_tile_width * grid[0] * grid[1]: + best_grid = grid + return best_grid @auto_docstring @@ -176,5 +251,4 @@ def _preprocess( return BatchFeature(data={"pixel_values": processed_images, "grids": grids}, tensor_type=return_tensors) - __all__ = ["Ovis2ImageProcessorPil"] diff --git a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py index 506173f464ef..8757d51075af 100644 --- a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py @@ -23,7 +23,6 @@ # See the License for the specific language governing permissions and # limitations under the License. - from collections.abc import Iterable import numpy as np @@ -31,9 +30,58 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_paddleocr_vl import PaddleOCRVLImageProcessorKwargs, smart_resize + + +# Copied from transformers.models.paddleocr_vl.image_processing_paddleocr_vl.PaddleOCRVLImageProcessorKwargs +class PaddleOCRVLImageProcessorKwargs(ImagesKwargs, total=False): + r""" + patch_size (`int`, *optional*, defaults to 14): + The spatial patch size of the vision encoder. + temporal_patch_size (`int`, *optional*, defaults to 1): + The temporal patch size of the vision encoder. + merge_size (`int`, *optional*, defaults to 2): + The merge size of the vision encoder to llm encoder. + """ + + min_pixels: int + max_pixels: int + patch_size: int + temporal_patch_size: int + merge_size: int + +# Copied from transformers.models.paddleocr_vl.image_processing_paddleocr_vl.smart_resize +def smart_resize( + height: int, + width: int, + factor: int = 28, + min_pixels: int = 384 * 384, + max_pixels: int = 1536 * 1536, +): + if height < factor: + width = round((width * factor) / height) + height = factor + + if width < factor: + height = round((height * factor) / width) + width = factor + + if max(height, width) / min(height, width) > 200: + raise ValueError( + f"absolute aspect ratio must be smaller than 200, got {max(height, width) / min(height, width)}" + ) + h_bar = round(height / factor) * factor + w_bar = round(width / factor) * factor + if h_bar * w_bar > max_pixels: + beta = math.sqrt((height * width) / max_pixels) + h_bar = max(factor, math.floor(height / beta / factor) * factor) + w_bar = max(factor, math.floor(width / beta / factor) * factor) + elif h_bar * w_bar < min_pixels: + beta = math.sqrt(min_pixels / (height * width)) + h_bar = math.ceil(height * beta / factor) * factor + w_bar = math.ceil(width * beta / factor) * factor + return h_bar, w_bar @auto_docstring @@ -199,5 +247,4 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non grid_h, grid_w = resized_height // patch_size, resized_width // patch_size return grid_h * grid_w - __all__ = ["PaddleOCRVLImageProcessorPil"] diff --git a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py index 54b21d47c3f4..c275fedee715 100644 --- a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py +++ b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py @@ -16,18 +16,132 @@ import math import numpy as np -import torch from PIL import Image from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature, get_size_dict from ...image_transforms import to_channel_dimension_format, to_pil_image from ...image_utils import ChannelDimension, ImageInput, SizeDict -from ...processing_utils import Unpack -from ...utils import TensorType, auto_docstring, requires_backends +from ...processing_utils import ImagesKwargs, Unpack +from ...utils import TensorType, auto_docstring, requires_backends, is_torch_available from ...utils.import_utils import requires -from .image_processing_pix2struct import Pix2StructImageProcessorKwargs, render_text, torch_extract_patches +if is_torch_available(): + import torch + from torch import nn + +# Copied from transformers.models.pix2struct.image_processing_pix2struct.Pix2StructImageProcessorKwargs +class Pix2StructImageProcessorKwargs(ImagesKwargs, total=False): + """ + max_patches (`int`, *optional*): + Maximum number of patches to extract. + patch_size (`dict[str, int]`, *optional*, defaults to `{"height": 16, "width": 16}`): + The patch size to use for the image. According to Pix2Struct paper and code, the patch size is 16x16. + is_vqa (`bool`, *optional*, defaults to `False`): + Whether or not the image processor is for the VQA task. If `True` and `header_text` is passed in, text is + rendered onto the input images. + header_text (`Union[list[str], str]`, *optional*): + Text to render as a header. Only has an effect if `image_processor.is_vqa` is `True`. + """ + + max_patches: int + patch_size: dict[str, int] + is_vqa: bool + header_text: list[str] | str | None + +# Copied from transformers.models.pix2struct.image_processing_pix2struct.render_text +# Adapted from https://github.com/google-research/pix2struct/blob/0e1779af0f4db4b652c1d92b3bbd2550a7399123/pix2struct/preprocessing/preprocessing_utils.py#L106 +def render_text( + text: str, + text_size: int = 36, + text_color: str = "black", + background_color: str = "white", + left_padding: int = 5, + right_padding: int = 5, + top_padding: int = 5, + bottom_padding: int = 5, + font_bytes: bytes | None = None, + font_path: str | None = None, +) -> Image.Image: + """ + Render text. This script is entirely adapted from the original script that can be found here: + https://github.com/google-research/pix2struct/blob/main/pix2struct/preprocessing/preprocessing_utils.py + + Args: + text (`str`, *optional*, defaults to ): + Text to render. + text_size (`int`, *optional*, defaults to 36): + Size of the text. + text_color (`str`, *optional*, defaults to `"black"`): + Color of the text. + background_color (`str`, *optional*, defaults to `"white"`): + Color of the background. + left_padding (`int`, *optional*, defaults to 5): + Padding on the left. + right_padding (`int`, *optional*, defaults to 5): + Padding on the right. + top_padding (`int`, *optional*, defaults to 5): + Padding on the top. + bottom_padding (`int`, *optional*, defaults to 5): + Padding on the bottom. + font_bytes (`bytes`, *optional*): + Bytes of the font to use. If `None`, the default font will be used. + font_path (`str`, *optional*): + Path to the font to use. If `None`, the default font will be used. + """ + requires_backends(render_text, "vision") + # Add new lines so that each line is no more than 80 characters. + + wrapper = textwrap.TextWrapper(width=80) + lines = wrapper.wrap(text=text) + wrapped_text = "\n".join(lines) + + if font_bytes is not None and font_path is None: + font = io.BytesIO(font_bytes) + elif font_path is not None: + font = font_path + else: + font = hf_hub_download(DEFAULT_FONT_PATH, "Arial.TTF") + font = ImageFont.truetype(font, encoding="UTF-8", size=text_size) + + # Use a temporary canvas to determine the width and height in pixels when + # rendering the text. + temp_img = Image.new("RGB", (1, 1)) + temp_draw = ImageDraw.Draw(temp_img) + _, _, w, h = temp_draw.textbbox((0, 0), wrapped_text, font=font) + + text_width = w + left_padding + right_padding + text_height = h + top_padding + bottom_padding + + # Create the actual image with the text. + img = Image.new("RGB", (text_width, text_height), background_color) + draw = ImageDraw.Draw(img) + draw.text((left_padding, top_padding), wrapped_text, fill=text_color, font=font) + + return img + +# Copied from transformers.models.pix2struct.image_processing_pix2struct.torch_extract_patches +# Disable as it causes issues with torch.compile +@torch.compiler.disable +def torch_extract_patches(image_tensor, patch_height, patch_width): + """ + Extract patches from image tensor. Returns tensor of shape (batch, rows, columns, patch_height*patch_width*channels). + + Args: + image_tensor (`torch.Tensor`): + Image tensor of shape (batch, channels, height, width). + patch_height (`int`): + Height of patches to extract. + patch_width (`int`): + Width of patches to extract. + """ + batch_size, channels, height, width = image_tensor.shape + patches = torch.nn.functional.unfold(image_tensor, (patch_height, patch_width), stride=(patch_height, patch_width)) + patches = patches.reshape(batch_size, channels, patch_height, patch_width, -1) + patches = patches.permute(0, 4, 2, 3, 1).reshape( + batch_size, height // patch_height, width // patch_width, channels * patch_height * patch_width + ) + return patches @auto_docstring @requires(backends=("torch",)) @@ -275,5 +389,4 @@ def _preprocess( tensor_type=return_tensors, ) - -__all__ = ["Pix2StructImageProcessorPil"] +__all__ = ["Pix2StructImageProcessorPil"] \ No newline at end of file diff --git a/src/transformers/models/pixtral/image_processing_pil_pixtral.py b/src/transformers/models/pixtral/image_processing_pil_pixtral.py index cd1759efacbf..0bb8f96605e0 100644 --- a/src/transformers/models/pixtral/image_processing_pil_pixtral.py +++ b/src/transformers/models/pixtral/image_processing_pil_pixtral.py @@ -25,9 +25,58 @@ SizeDict, get_image_size, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_pixtral import PixtralImageProcessorKwargs, get_resize_output_image_size + + +# Copied from transformers.models.pixtral.image_processing_pixtral.PixtralImageProcessorKwargs +class PixtralImageProcessorKwargs(ImagesKwargs, total=False): + """ + patch_size (`Union[dict[str, int], int]` *optional*, defaults to `{"height": 16, "width": 16}`): + Size of the patches in the model, used to calculate the output image size. + """ + + patch_size: dict[str, int] | int + +# Copied from transformers.models.pixtral.image_processing_pixtral.get_resize_output_image_size +def get_resize_output_image_size( + input_image: ImageInput, + size: int | tuple[int, int] | list[int] | tuple[int], + patch_size: int | tuple[int, int] | list[int] | tuple[int], + input_data_format: str | ChannelDimension | None = None, +) -> tuple: + """ + Find the target (height, width) dimension of the output image after resizing given the input image and the desired + size. + + Args: + input_image (`ImageInput`): + The image to resize. + size (`int` or `tuple[int, int]`): + Max image size an input image can be. Must be a dictionary with the key "longest_edge". + patch_size (`int` or `tuple[int, int]`): + The patch_size as `(height, width)` to use for resizing the image. If patch_size is an integer, `(patch_size, patch_size)` + will be used + input_data_format (`ChannelDimension`, *optional*): + The channel dimension format of the input image. If unset, will use the inferred format from the input. + + Returns: + `tuple`: The target (height, width) dimension of the output image after resizing. + """ + max_height, max_width = size if isinstance(size, (tuple, list)) else (size, size) + patch_height, patch_width = patch_size if isinstance(patch_size, (tuple, list)) else (patch_size, patch_size) + height, width = get_image_size(input_image, input_data_format) + + ratio = max(height / max_height, width / max_width) + + if ratio > 1: + # Original implementation uses `round` which utilises bankers rounding, which can lead to surprising results + # Here we use floor to ensure the image is always smaller than the given "longest_edge" + height = int(math.floor(height / ratio)) + width = int(math.floor(width / ratio)) + + num_height_tokens, num_width_tokens = _num_image_tokens((height, width), (patch_height, patch_width)) + return num_height_tokens * patch_height, num_width_tokens * patch_width @auto_docstring @@ -150,5 +199,4 @@ def _preprocess( data={"pixel_values": padded_images, "image_sizes": batch_image_sizes}, tensor_type=return_tensors ) - __all__ = ["PixtralImageProcessorPil"] diff --git a/src/transformers/models/poolformer/image_processing_pil_poolformer.py b/src/transformers/models/poolformer/image_processing_pil_poolformer.py index 8208a9c79b5c..ad1426cefb59 100644 --- a/src/transformers/models/poolformer/image_processing_pil_poolformer.py +++ b/src/transformers/models/poolformer/image_processing_pil_poolformer.py @@ -25,9 +25,18 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_poolformer import PoolFormerImageProcessorKwargs + + +# Copied from transformers.models.poolformer.image_processing_poolformer.PoolFormerImageProcessorKwargs +class PoolFormerImageProcessorKwargs(ImagesKwargs, total=False): + r""" + crop_pct (`float`, *optional*, defaults to `self.crop_pct`): + Percentage of the image to crop. Only has an effect if `do_resize` is set to `True`. + """ + + crop_pct: float @auto_docstring @@ -120,5 +129,4 @@ def _preprocess( return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) - __all__ = ["PoolFormerImageProcessorPil"] diff --git a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py index 10eff3cb4f97..5cc4a335d360 100644 --- a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py @@ -31,17 +31,13 @@ SizeDict, get_image_size, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires - if TYPE_CHECKING: from ...modeling_outputs import DepthEstimatorOutput -from .image_processing_prompt_depth_anything import PromptDepthAnythingImageProcessorKwargs - - def _constrain_to_multiple_of(val, multiple, min_val=0, max_val=None): """Constrain a value to be a multiple of another value.""" x = round(val / multiple) * multiple @@ -54,7 +50,6 @@ def _constrain_to_multiple_of(val, multiple, min_val=0, max_val=None): return x - def _get_resize_output_image_size( input_image: np.ndarray, output_size: tuple[int, int], keep_aspect_ratio: bool, multiple: int ) -> tuple[int, int]: @@ -81,6 +76,25 @@ def _get_resize_output_image_size( return (new_height, new_width) +# Copied from transformers.models.prompt_depth_anything.image_processing_prompt_depth_anything.PromptDepthAnythingImageProcessorKwargs +class PromptDepthAnythingImageProcessorKwargs(ImagesKwargs, total=False): + r""" + keep_aspect_ratio (`bool`, *optional*): + If `True`, the image is resized to the largest possible size such that the aspect ratio is preserved. + ensure_multiple_of (`int`, *optional*): + If `do_resize` is `True`, the image is resized to a size that is a multiple of this value. + prompt_scale_to_meter (`float`, *optional*): + Scale factor to convert the prompt depth to meters. + size_divisor (`int`, *optional*): + If `do_pad` is `True`, pads the image dimensions to be divisible by this value. + """ + + keep_aspect_ratio: bool + ensure_multiple_of: int + size_divisor: int + prompt_scale_to_meter: float + + @auto_docstring class PromptDepthAnythingImageProcessorPil(PilBackend): model_input_names = ["pixel_values", "prompt_depth"] @@ -312,5 +326,4 @@ def post_process_depth_estimation( return results - __all__ = ["PromptDepthAnythingImageProcessorPil"] diff --git a/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py b/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py index 92cec311be12..50a83d1957a1 100644 --- a/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py +++ b/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py @@ -26,9 +26,59 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_qwen2_vl import Qwen2VLImageProcessorKwargs, smart_resize + + +# Copied from transformers.models.qwen2_vl.image_processing_qwen2_vl.Qwen2VLImageProcessorKwargs +class Qwen2VLImageProcessorKwargs(ImagesKwargs, total=False): + r""" + min_pixels (`int`, *optional*, defaults to `56 * 56`): + The min pixels of the image to resize the image. + max_pixels (`int`, *optional*, defaults to `28 * 28 * 1280`): + The max pixels of the image to resize the image. + patch_size (`int`, *optional*, defaults to 14): + The spatial patch size of the vision encoder. + temporal_patch_size (`int`, *optional*, defaults to 2): + The temporal patch size of the vision encoder. + merge_size (`int`, *optional*, defaults to 2): + The merge size of the vision encoder to llm encoder. + """ + + min_pixels: int + max_pixels: int + patch_size: int + temporal_patch_size: int + merge_size: int + +# Copied from transformers.models.qwen2_vl.image_processing_qwen2_vl.smart_resize +def smart_resize( + height: int, width: int, factor: int = 28, min_pixels: int = 56 * 56, max_pixels: int = 14 * 14 * 4 * 1280 +): + """Rescales the image so that the following conditions are met: + + 1. Both dimensions (height and width) are divisible by 'factor'. + + 2. The total number of pixels is within the range ['min_pixels', 'max_pixels']. + + 3. The aspect ratio of the image is maintained as closely as possible. + + """ + if max(height, width) / min(height, width) > 200: + raise ValueError( + f"absolute aspect ratio must be smaller than 200, got {max(height, width) / min(height, width)}" + ) + h_bar = round(height / factor) * factor + w_bar = round(width / factor) * factor + if h_bar * w_bar > max_pixels: + beta = math.sqrt((height * width) / max_pixels) + h_bar = max(factor, math.floor(height / beta / factor) * factor) + w_bar = max(factor, math.floor(width / beta / factor) * factor) + elif h_bar * w_bar < min_pixels: + beta = math.sqrt(min_pixels / (height * width)) + h_bar = math.ceil(height * beta / factor) * factor + w_bar = math.ceil(width * beta / factor) * factor + return h_bar, w_bar @auto_docstring @@ -200,5 +250,4 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non grid_h, grid_w = resized_height // patch_size, resized_width // patch_size return grid_h * grid_w - __all__ = ["Qwen2VLImageProcessorPil"] diff --git a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py index 74079f0102d5..c07d9e3d64f8 100644 --- a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py @@ -34,11 +34,11 @@ safe_squeeze, ) from ...image_utils import ( - IMAGENET_DEFAULT_MEAN, - IMAGENET_DEFAULT_STD, AnnotationFormat, AnnotationType, ChannelDimension, + IMAGENET_DEFAULT_MEAN, + IMAGENET_DEFAULT_STD, ImageInput, PILImageResampling, get_image_size, @@ -46,7 +46,7 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import ( TensorType, auto_docstring, @@ -55,8 +55,6 @@ requires_backends, ) from ...utils.import_utils import requires -from .image_processing_rt_detr import RTDetrImageProcessorKwargs - if is_torch_available(): import torch @@ -65,7 +63,6 @@ SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) - def prepare_coco_detection_annotation_pil( image, target, @@ -120,8 +117,24 @@ def prepare_coco_detection_annotation_pil( return new_target - @requires(backends=("torch",)) + + +# Copied from transformers.models.rt_detr.image_processing_rt_detr.RTDetrImageProcessorKwargs +class RTDetrImageProcessorKwargs(ImagesKwargs, total=False): + r""" + format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): + Data format of the annotations. One of "coco_detection" or "coco_panoptic". + do_convert_annotations (`bool`, *optional*, defaults to `True`): + Controls whether to convert the annotations to the format expected by the RT_DETR model. Converts the + bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. + Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. + """ + + format: str | AnnotationFormat + do_convert_annotations: bool + + class RTDetrImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR image_mean = IMAGENET_DEFAULT_MEAN @@ -572,5 +585,4 @@ def post_process_object_detection( return results - __all__ = ["RTDetrImageProcessorPil"] diff --git a/src/transformers/models/sam/image_processing_pil_sam.py b/src/transformers/models/sam/image_processing_pil_sam.py index e7f6f8b5e54c..7fc6cc51b11f 100644 --- a/src/transformers/models/sam/image_processing_pil_sam.py +++ b/src/transformers/models/sam/image_processing_pil_sam.py @@ -20,8 +20,6 @@ from typing import Any, Optional import numpy as np -import torch -import torch.nn.functional as F from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature, get_size_dict @@ -34,16 +32,13 @@ SizeDict, get_image_size, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available from ...utils.import_utils import requires -from .image_processing_sam import SamImageProcessorKwargs - if is_vision_available(): import PIL - def get_resize_output_image_size( input_image: np.ndarray, longest_edge: int, @@ -60,6 +55,18 @@ def get_resize_output_image_size( new_height = int(new_width * ratio) return (new_height, new_width) +# Copied from transformers.models.sam.image_processing_sam.SamImageProcessorKwargs +class SamImageProcessorKwargs(ImagesKwargs, total=False): + r""" + mask_size (`dict[str, int]`, *optional*): + The size `{"longest_edge": int}` to resize the segmentation maps to. + mask_pad_size (`dict[str, int]`, *optional*): + The size `{"height": int, "width": int}` to pad the segmentation maps to. Must be larger than any segmentation + map size provided for preprocessing. + """ + + mask_size: dict[str, int] + mask_pad_size: dict[str, int] @auto_docstring @requires(backends=("torch",)) @@ -447,7 +454,6 @@ def post_process_for_mask_generation(self, all_masks, all_scores, all_boxes, cro """ return _post_process_for_mask_generation(all_masks, all_scores, all_boxes, crops_nms_thresh) - def _compute_stability_score(masks: "torch.Tensor", mask_threshold: float, stability_score_offset: int): # One mask is always contained inside the other. # Save memory by preventing unnecessary cast to torch.int64 @@ -458,7 +464,6 @@ def _compute_stability_score(masks: "torch.Tensor", mask_threshold: float, stabi stability_scores = intersections / unions return stability_scores - def _batched_mask_to_box(masks: "torch.Tensor"): """ Computes the bounding boxes around the given input masks. The bounding boxes are in the XYXY format which @@ -507,7 +512,6 @@ def _batched_mask_to_box(masks: "torch.Tensor"): out = out.reshape(*shape[:-2], 4) return out - def _is_box_near_crop_edge(boxes, crop_box, orig_box, atol=20.0): """Filter masks at the edge of a crop, but not at the edge of the original image.""" crop_box_torch = torch.as_tensor(crop_box, dtype=torch.float, device=boxes.device) @@ -525,7 +529,6 @@ def _is_box_near_crop_edge(boxes, crop_box, orig_box, atol=20.0): near_crop_edge = torch.logical_and(near_crop_edge, ~near_image_edge) return torch.any(near_crop_edge, dim=1) - def _pad_masks(masks, crop_box: list[int], orig_height: int, orig_width: int): left, top, right, bottom = crop_box if left == 0 and top == 0 and right == orig_width and bottom == orig_height: @@ -535,7 +538,6 @@ def _pad_masks(masks, crop_box: list[int], orig_height: int, orig_width: int): pad = (left, pad_x - left, top, pad_y - top) return torch.nn.functional.pad(masks, pad, value=0) - def _generate_crop_boxes( image, target_size: int, @@ -587,7 +589,6 @@ def _generate_crop_boxes( return crop_boxes, points_per_crop, cropped_images, input_labels - def _generate_per_layer_crops(crop_n_layers, overlap_ratio, original_size): """ Generates 2 ** (layers idx + 1) crops for each crop_n_layers. Crops are in the XYWH format : The XYWH format @@ -621,7 +622,6 @@ def _generate_per_layer_crops(crop_n_layers, overlap_ratio, original_size): return crop_boxes, layer_idxs - def _build_point_grid(n_per_side: int) -> np.ndarray: """Generates a 2D grid of points evenly spaced in [0,1]x[0,1].""" offset = 1 / (2 * n_per_side) @@ -631,7 +631,6 @@ def _build_point_grid(n_per_side: int) -> np.ndarray: points = np.stack([points_x, points_y], axis=-1).reshape(-1, 2) return points - def _generate_crop_images( crop_boxes, image, points_grid, layer_idxs, target_size, original_size, input_data_format=None ): @@ -656,7 +655,6 @@ def _generate_crop_images( return cropped_images, total_points_per_crop - def _normalize_coordinates( target_size: int, coords: np.ndarray, original_size: tuple[int, int], is_bounding_box=False ) -> np.ndarray: @@ -684,7 +682,6 @@ def _normalize_coordinates( return coords - def _rle_to_mask(rle: dict[str, Any]) -> np.ndarray: """Compute a binary mask from an uncompressed RLE.""" height, width = rle["size"] @@ -698,7 +695,6 @@ def _rle_to_mask(rle: dict[str, Any]) -> np.ndarray: mask = mask.reshape(width, height) return mask.transpose() # Reshape to original shape - def _post_process_for_mask_generation(rle_masks, iou_scores, mask_boxes, amg_crops_nms_thresh=0.7): """ Perform NMS (Non Maximum Suppression) on the outputs. @@ -717,6 +713,10 @@ def _post_process_for_mask_generation(rle_masks, iou_scores, mask_boxes, amg_cro raise ImportError("PyTorch is required for post_process_for_mask_generation") from torchvision.ops.boxes import batched_nms +if is_torch_available(): + import torch + from torch import nn + keep_by_nms = batched_nms( boxes=mask_boxes.float(), scores=iou_scores, @@ -731,7 +731,6 @@ def _post_process_for_mask_generation(rle_masks, iou_scores, mask_boxes, amg_cro return masks, iou_scores, rle_masks, mask_boxes - def _mask_to_rle(input_mask: "torch.Tensor"): """ Encodes masks the run-length encoding (RLE), in the format expected by pycoco tools. @@ -762,5 +761,4 @@ def _mask_to_rle(input_mask: "torch.Tensor"): out.append({"size": [height, width], "counts": counts}) return out - -__all__ = ["SamImageProcessorPil"] +__all__ = ["SamImageProcessorPil"] \ No newline at end of file diff --git a/src/transformers/models/segformer/image_processing_pil_segformer.py b/src/transformers/models/segformer/image_processing_pil_segformer.py index 9da099ec661b..e2961f249933 100644 --- a/src/transformers/models/segformer/image_processing_pil_segformer.py +++ b/src/transformers/models/segformer/image_processing_pil_segformer.py @@ -18,7 +18,6 @@ # See the License for the specific language governing permissions and # limitations under the License. - import numpy as np from ...image_processing_backends import PilBackend @@ -31,19 +30,30 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, is_torch_available, is_torchvision_available from ...utils.import_utils import requires -from .image_processing_segformer import SegformerImageProcessorKwargs - if is_torch_available(): pass if is_torchvision_available(): import torchvision.transforms.v2.functional as tvF - @requires(backends=("torch", "torchvision")) + + +# Copied from transformers.models.segformer.image_processing_segformer.SegformerImageProcessorKwargs +class SegformerImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): + Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 + is used for background, and background itself is not included in all classes of a dataset (e.g. + ADE20k). The background label will be replaced by 255. + """ + + do_reduce_labels: bool + + class SegformerImageProcessorPil(PilBackend): """PIL backend for Segformer with reduce_label support.""" @@ -209,5 +219,4 @@ def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] return semantic_segmentation - __all__ = ["SegformerImageProcessorPil"] diff --git a/src/transformers/models/seggpt/image_processing_pil_seggpt.py b/src/transformers/models/seggpt/image_processing_pil_seggpt.py index 96542e227956..06b93236d933 100644 --- a/src/transformers/models/seggpt/image_processing_pil_seggpt.py +++ b/src/transformers/models/seggpt/image_processing_pil_seggpt.py @@ -25,10 +25,44 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires -from .image_processing_seggpt import SegGptImageProcessorKwargs, build_palette + + +# Copied from transformers.models.seggpt.image_processing_seggpt.SegGptImageProcessorKwargs +class SegGptImageProcessorKwargs(ImagesKwargs, total=False): + r""" + num_labels (`int`, *optional*): + Number of classes in the segmentation task (excluding the background). If specified, a palette will be + built, assuming that class_idx 0 is the background, to map the prompt mask from a plain segmentation map + to a 3-channel RGB image. Not specifying this will result in the prompt mask being duplicated across the + channel dimension when `do_convert_rgb` is `True`. + """ + + num_labels: int + +# Copied from transformers.models.seggpt.image_processing_seggpt.build_palette +# See https://huggingface.co/papers/2212.02499 at 3.1 Redefining Output Spaces as "Images" - Semantic Segmentation +# Taken from https://github.com/Abdullah-Meda/Painter/blob/main/Painter/data/coco_semseg/gen_color_coco_panoptic_segm.py#L31 +def build_palette(num_labels: int) -> list[tuple[int, int, int]]: + base = int(num_labels ** (1 / 3)) + 1 + margin = 256 // base + + # class_idx 0 is the background which is mapped to black + color_list = [(0, 0, 0)] + for location in range(num_labels): + num_seq_r = location // base**2 + num_seq_g = (location % base**2) // base + num_seq_b = location % base + + R = 255 - num_seq_r * margin + G = 255 - num_seq_g * margin + B = 255 - num_seq_b * margin + + color_list.append((R, G, B)) + + return color_list @auto_docstring @@ -256,5 +290,4 @@ def post_process_semantic_segmentation( return semantic_segmentation - __all__ = ["SegGptImageProcessorPil"] diff --git a/src/transformers/models/siglip2/image_processing_pil_siglip2.py b/src/transformers/models/siglip2/image_processing_pil_siglip2.py index c90a3a2f9e90..ac99a2689a3f 100644 --- a/src/transformers/models/siglip2/image_processing_pil_siglip2.py +++ b/src/transformers/models/siglip2/image_processing_pil_siglip2.py @@ -18,16 +18,11 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput, PILImageResampling, SizeDict -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import ( TensorType, auto_docstring, ) -from .image_processing_siglip2 import ( - Siglip2ImageProcessorKwargs, - get_image_size_for_max_num_patches, -) - def convert_image_to_patches(image: np.ndarray, patch_size: int) -> np.ndarray: """ @@ -42,7 +37,6 @@ def convert_image_to_patches(image: np.ndarray, patch_size: int) -> np.ndarray: patched_image = patched_image.reshape(num_patches_height * num_patches_width, -1) return patched_image - def pad_along_first_dim(array: np.ndarray, target_length: int, pad_value: int = 0) -> tuple[np.ndarray, np.ndarray]: """ Pad the array along the first dimension. @@ -57,6 +51,68 @@ def pad_along_first_dim(array: np.ndarray, target_length: int, pad_value: int = return array, mask +# Copied from transformers.models.siglip2.image_processing_siglip2.Siglip2ImageProcessorKwargs +class Siglip2ImageProcessorKwargs(ImagesKwargs, total=False): + r""" + patch_size (`int`, *optional*, defaults to `self.patch_size`): + The size (resolution) of each patch the image will be split to. + max_num_patches (`int`, *optional*, defaults to `self.max_num_patches`): + The image will be resized to have at most this number of patches, + and then padded in "patch" dimension to match this number exactly. + """ + + patch_size: int + max_num_patches: int + +# Copied from transformers.models.siglip2.image_processing_siglip2.get_image_size_for_max_num_patches +def get_image_size_for_max_num_patches( + image_height: int, image_width: int, patch_size: int, max_num_patches: int, eps: float = 1e-5 +) -> tuple[int, int]: + """ + Determine image size based on max number of patches, ensure dimensions are divisible by patch size and image is at least 1 patch. + + Args: + image_height (`int`): + Original image height. + image_width (`int`): + Original image width. + patch_size (`int`): + Patch size for processing. + max_num_patches (`int`): + Maximum number of patches. + eps (`float`): + Small threshold for binary search. + + Returns: + Tuple: (target_height, target_width) + """ + import math + + def get_scaled_image_size(scale: float, size: int, patch_size: int) -> int: + scaled_size = size * scale + scaled_size = math.ceil(scaled_size / patch_size) * patch_size # make divisible by patch_size + scaled_size = max(patch_size, scaled_size) # ensure at least 1 patch + return int(scaled_size) + + # Binary search for optimal scale + scale_min, scale_max = eps / 10, 100.0 + while (scale_max - scale_min) >= eps: + scale = (scale_min + scale_max) / 2 + target_height = get_scaled_image_size(scale, image_height, patch_size) + target_width = get_scaled_image_size(scale, image_width, patch_size) + num_patches = (target_height / patch_size) * (target_width / patch_size) + + if num_patches <= max_num_patches: + scale_min = scale + else: + scale_max = scale + + scale = scale_min + target_height = get_scaled_image_size(scale, image_height, patch_size) + target_width = get_scaled_image_size(scale, image_width, patch_size) + return target_height, target_width + + @auto_docstring class Siglip2ImageProcessorPil(PilBackend): valid_kwargs = Siglip2ImageProcessorKwargs @@ -138,5 +194,4 @@ def _preprocess( ) return batch_feature - __all__ = ["Siglip2ImageProcessorPil"] diff --git a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py index 35edc052a533..fd1f19292c70 100644 --- a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py +++ b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py @@ -19,7 +19,6 @@ # See the License for the specific language governing permissions and # limitations under the License. - import math import numpy as np @@ -35,18 +34,8 @@ SizeDict, make_nested_list_of_images, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_smolvlm import ( - MAX_IMAGE_SIZE, - SmolVLMImageProcessorKwargs, - _resize_output_size_rescale_to_max_len, - _resize_output_size_scale_below_upper_bound, - get_max_height_width, - get_num_channels, - get_resize_output_image_size, -) - def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: """Make pixel mask: 1=valid, 0=padding. Images are CHW.""" @@ -56,6 +45,143 @@ def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndar return mask +# Copied from transformers.models.smolvlm.image_processing_smolvlm.MAX_IMAGE_SIZE +MAX_IMAGE_SIZE = 4096 # 4k resolution as absolute maximum + +# Copied from transformers.models.smolvlm.image_processing_smolvlm.SmolVLMImageProcessorKwargs +class SmolVLMImageProcessorKwargs(ImagesKwargs, total=False): + """ + do_image_splitting (`bool`, *optional*, defaults to `True`): + Whether to split the image into sub-images concatenated with the original image. They are split into patches + such that each patch has a size of `max_image_size["height"]` x `max_image_size["width"]`. + max_image_size (`Dict`, *optional*, defaults to `{"longest_edge": 364}`): + Maximum resolution of the patches of images accepted by the model. This is a dictionary containing the key "longest_edge". + return_row_col_info (`bool`, *optional*, defaults to `False`): + Whether to return the row and column information of the images. + """ + + do_image_splitting: bool + max_image_size: dict[str, int] + return_row_col_info: bool + +# Copied from transformers.models.smolvlm.image_processing_smolvlm._resize_output_size_rescale_to_max_len +def _resize_output_size_rescale_to_max_len( + height: int, width: int, min_len: int | None = 1, max_len: int | None = None +) -> tuple[int, int]: + """ + Get the output size of the image after resizing given a dictionary specifying the max and min sizes. + Args: + height (`int`): + Height of the input image. + width (`int`): + Width of the input image. + min_len (`int`, *optional*, defaults to 1): + Minimum size of the output image. + max_len (`int`, *optional*, defaults to the maximum size of the image): + Maximum size of the output image. + Returns: + The output size of the image after resizing. + """ + max_len = max(height, width) if max_len is None else max_len + aspect_ratio = width / height + + if width >= height: + width = max_len + height = int(width / aspect_ratio) + if height % 2 != 0: + height += 1 + elif height > width: + height = max_len + width = int(height * aspect_ratio) + if width % 2 != 0: + width += 1 + + # Avoid resizing to a size smaller than min_len + height = max(height, min_len) + width = max(width, min_len) + return height, width + +# Copied from transformers.models.smolvlm.image_processing_smolvlm._resize_output_size_scale_below_upper_bound +def _resize_output_size_scale_below_upper_bound( + height: int, width: int, max_len: dict[str, int] | None = None +) -> tuple[int, int]: + """ + Get the output size of the image after resizing given a dictionary specifying the max and min sizes. + Args: + height (`int`): + Height of the input image. + width (`int`): + Width of the input image. + max_len (`Dict[str, int]`, *optional*, defaults to the maximum size of the image): + Defines the maximum dimensions of the image. + Returns: + The output size of the image after resizing. + """ + max_len = max(height, width) if max_len is None else max_len + + aspect_ratio = width / height + if width >= height and width > max_len: + width = max_len + height = int(width / aspect_ratio) + elif height > width and height > max_len: + height = max_len + width = int(height * aspect_ratio) + + # Avoid resizing to a size smaller than 1 + height = max(height, 1) + width = max(width, 1) + return height, width + +# Copied from transformers.models.smolvlm.image_processing_smolvlm.get_max_height_width +def get_max_height_width(images_list: list[list["torch.Tensor|np.ndarray"]]) -> tuple[int, int]: + """ + Get the maximum height and width across all images in a batch. + """ + image_sizes = [] + for images in images_list: + for image in images: + image_sizes.append(image.shape[-2:]) + + max_height = max(size[0] for size in image_sizes) + max_width = max(size[1] for size in image_sizes) + return (max_height, max_width) + +# Copied from transformers.models.smolvlm.image_processing_smolvlm.get_num_channels +def get_num_channels(images_list: list[list["torch.Tensor|np.ndarray"]]) -> int: + """ + Get the number of channels across all images in a batch. Handle empty sublists like in [[], [image]]. + """ + for images in images_list: + if images: + return images[0].shape[0] + + raise ValueError("No images found in the batch.") + +# Copied from transformers.models.smolvlm.image_processing_smolvlm.get_resize_output_image_size +def get_resize_output_image_size( + image: "torch.Tensor", + resolution_max_side: int, +) -> tuple[int, int]: + """ + Get the output size of the image after resizing given a dictionary specifying the max and min sizes. + Args: + image (`torch.Tensor`): + Image to resize. + resolution_max_side (`int`): + The longest edge of the image will be resized to this value. The shortest edge will be resized to keep the + input aspect ratio. + Returns: + The output size of the image after resizing. + """ + height, width = image.shape[-2:] + + # Find the output size, when rescaling the longest edge to max_len and preserving the aspect ratio + height, width = _resize_output_size_rescale_to_max_len(height, width, max_len=resolution_max_side) + # Find the output size when scaling the image to be below the MAX_IMAGE_SIZE + height, width = _resize_output_size_scale_below_upper_bound(height, width, max_len=MAX_IMAGE_SIZE) + return height, width + + @auto_docstring class SmolVLMImageProcessorPil(PilBackend): resample = PILImageResampling.LANCZOS @@ -347,5 +473,4 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs: di return num_patches, num_rows, num_cols - __all__ = ["SmolVLMImageProcessorPil"] diff --git a/src/transformers/models/smolvlm/processing_smolvlm.py b/src/transformers/models/smolvlm/processing_smolvlm.py index 351eb265926d..82c31e2f38ae 100644 --- a/src/transformers/models/smolvlm/processing_smolvlm.py +++ b/src/transformers/models/smolvlm/processing_smolvlm.py @@ -26,12 +26,14 @@ from ...video_utils import VideoInput -if is_vision_available(): - from .video_processing_smolvlm import ( - DEFAULT_MEDIA_OUTTRO, - DEFAULT_VIDEO_INTRO, - FRAME_TIMESTAMP_MESSAGE, - ) +# Copied from transformers.models.smolvlm.video_processing_smolvlm.DEFAULT_VIDEO_INTRO +DEFAULT_VIDEO_INTRO = ( + "You are provided the following series of {frame_count} frames from a {video_duration} [H:MM:SS] video.\n" +) +# Copied from transformers.models.smolvlm.video_processing_smolvlm.DEFAULT_MEDIA_OUTTRO +DEFAULT_MEDIA_OUTTRO = "\n\n" +# Copied from transformers.models.smolvlm.video_processing_smolvlm.FRAME_TIMESTAMP_MESSAGE +FRAME_TIMESTAMP_MESSAGE = "\nFrame from {timestamp}:" if TYPE_CHECKING: from ...tokenization_utils_base import PreTokenizedInput diff --git a/src/transformers/models/superglue/image_processing_pil_superglue.py b/src/transformers/models/superglue/image_processing_pil_superglue.py index ea0b1fa98fde..94218450aa6b 100644 --- a/src/transformers/models/superglue/image_processing_pil_superglue.py +++ b/src/transformers/models/superglue/image_processing_pil_superglue.py @@ -24,21 +24,17 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires -from .image_processing_superglue import validate_and_format_image_pairs - if TYPE_CHECKING: import torch from .modeling_superglue import SuperGlueKeypointMatchingOutput - def is_grayscale(image: np.ndarray): if image.shape[0] == 1: return True return np.all(image[0, ...] == image[1, ...]) and np.all(image[1, ...] == image[2, ...]) - def convert_to_grayscale(image: ImageInput) -> ImageInput: """ Converts an image to grayscale format using the NTSC formula. Only support numpy and PIL Image. @@ -67,6 +63,35 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: return image +# Copied from transformers.models.superglue.image_processing_superglue.validate_and_format_image_pairs +def validate_and_format_image_pairs(images: ImageInput): + error_message = ( + "Input images must be a one of the following :", + " - A pair of PIL images.", + " - A pair of 3D arrays.", + " - A list of pairs of PIL images.", + " - A list of pairs of 3D arrays.", + ) + + def _is_valid_image(image): + """images is a PIL Image or a 3D array.""" + return is_pil_image(image) or ( + is_valid_image(image) and get_image_type(image) != ImageType.PIL and len(image.shape) == 3 + ) + + if isinstance(images, list): + if len(images) == 2 and all((_is_valid_image(image)) for image in images): + return images + if all( + isinstance(image_pair, list) + and len(image_pair) == 2 + and all(_is_valid_image(image) for image in image_pair) + for image_pair in images + ): + return [image for image_pair in images for image in image_pair] + raise ValueError(error_message) + + class SuperGlueImageProcessorKwargs(ImagesKwargs, total=False): r""" do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`): @@ -75,7 +100,6 @@ class SuperGlueImageProcessorKwargs(ImagesKwargs, total=False): do_grayscale: bool - @auto_docstring class SuperGlueImageProcessorPil(PilBackend): valid_kwargs = SuperGlueImageProcessorKwargs @@ -258,5 +282,4 @@ def _get_color(self, score): b = 0 return (r, g, b) - __all__ = ["SuperGlueImageProcessorPil"] diff --git a/src/transformers/models/superpoint/image_processing_pil_superpoint.py b/src/transformers/models/superpoint/image_processing_pil_superpoint.py index 6dc427aa31e6..f6d4887fb35a 100644 --- a/src/transformers/models/superpoint/image_processing_pil_superpoint.py +++ b/src/transformers/models/superpoint/image_processing_pil_superpoint.py @@ -20,25 +20,21 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import PILImageResampling, SizeDict -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires -from .image_processing_superpoint import SuperPointImageProcessorKwargs - if TYPE_CHECKING: import torch from .modeling_superpoint import SuperPointKeypointDescriptionOutput - def is_grayscale(image: np.ndarray) -> bool: """Checks if an image is grayscale (all RGB channels are identical).""" if image.shape[0] == 1: return True return np.all(image[0, ...] == image[1, ...]) and np.all(image[1, ...] == image[2, ...]) - def convert_to_grayscale(image: np.ndarray) -> np.ndarray: """ Converts an image to grayscale format using the NTSC formula. Only support numpy arrays. @@ -59,6 +55,16 @@ def convert_to_grayscale(image: np.ndarray) -> np.ndarray: return gray_image +# Copied from transformers.models.superpoint.image_processing_superpoint.SuperPointImageProcessorKwargs +class SuperPointImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`): + Whether to convert the image to grayscale. Can be overridden by `do_grayscale` in the `preprocess` method. + """ + + do_grayscale: bool + + @auto_docstring class SuperPointImageProcessorPil(PilBackend): valid_kwargs = SuperPointImageProcessorKwargs @@ -153,5 +159,4 @@ def post_process_keypoint_detection( return results - __all__ = ["SuperPointImageProcessorPil"] diff --git a/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py b/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py index d81311024510..2f95b12dac2f 100644 --- a/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py +++ b/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py @@ -24,9 +24,18 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_swin2sr import Swin2SRImageProcessorKwargs + + +# Copied from transformers.models.swin2sr.image_processing_swin2sr.Swin2SRImageProcessorKwargs +class Swin2SRImageProcessorKwargs(ImagesKwargs, total=False): + """ + size_divisor (`int`, *optional*, defaults to `self.size_divisor`): + The size to make the height and width divisible by when padding. + """ + + size_divisor: int @auto_docstring @@ -103,5 +112,4 @@ def _preprocess( processed_images.append(image) return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) - __all__ = ["Swin2SRImageProcessorPil"] diff --git a/src/transformers/models/textnet/image_processing_pil_textnet.py b/src/transformers/models/textnet/image_processing_pil_textnet.py index 29b4059aaa7b..15ad7a025210 100644 --- a/src/transformers/models/textnet/image_processing_pil_textnet.py +++ b/src/transformers/models/textnet/image_processing_pil_textnet.py @@ -27,9 +27,18 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_textnet import TextNetImageProcessorKwargs + + +# Copied from transformers.models.textnet.image_processing_textnet.TextNetImageProcessorKwargs +class TextNetImageProcessorKwargs(ImagesKwargs, total=False): + """ + size_divisor (`int`, *optional*, defaults to `self.size_divisor`): + Ensures height and width are rounded to a multiple of this value after resizing. + """ + + size_divisor: int @auto_docstring @@ -121,5 +130,4 @@ def _preprocess( processed_images.append(image) return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) - __all__ = ["TextNetImageProcessorPil"] diff --git a/src/transformers/models/tvp/image_processing_pil_tvp.py b/src/transformers/models/tvp/image_processing_pil_tvp.py index b85b85db4fdd..175edcdfde12 100644 --- a/src/transformers/models/tvp/image_processing_pil_tvp.py +++ b/src/transformers/models/tvp/image_processing_pil_tvp.py @@ -29,15 +29,12 @@ get_image_size, make_nested_list_of_images, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torchvision_available -from .image_processing_tvp import TvpImageProcessorKwargs - if is_torchvision_available(): pass - def get_resize_output_image_size( input_image: np.ndarray, max_size: int = 448, @@ -57,6 +54,22 @@ def get_resize_output_image_size( return size +# Copied from transformers.models.tvp.image_processing_tvp.TvpImageProcessorKwargs +class TvpImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_flip_channel_order (`bool`, *optional*, defaults to `self.do_flip_channel_order`): + Whether to flip the channel order of the image from RGB to BGR. + constant_values (`float` or `List[float]`, *optional*, defaults to `self.constant_values`): + Value used to fill the padding area when `pad_mode` is `'constant'`. + pad_mode (`str`, *optional*, defaults to `self.pad_mode`): + Padding mode to use — `'constant'`, `'edge'`, `'reflect'`, or `'symmetric'`. + """ + + do_flip_channel_order: bool + constant_values: float | list[float] | None + pad_mode: str | None + + @auto_docstring class TvpImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR @@ -245,5 +258,4 @@ def _preprocess( return BatchFeature(data={"pixel_values": processed_videos}, tensor_type=return_tensors) - __all__ = ["TvpImageProcessorPil"] diff --git a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py index eca723afaae7..f42787e0aa9c 100644 --- a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py +++ b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py @@ -25,9 +25,59 @@ from ...feature_extraction_utils import BatchFeature from ...image_processing_backends import PilBackend from ...image_utils import IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ImageInput, PILImageResampling, SizeDict -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_video_llama_3 import VideoLlama3ImageProcessorKwargs, smart_resize + + +# Copied from transformers.models.video_llama_3.image_processing_video_llama_3.VideoLlama3ImageProcessorKwargs +class VideoLlama3ImageProcessorKwargs(ImagesKwargs, total=False): + r""" + min_pixels (`int`, *optional*, defaults to `56 * 56`): + The min pixels of the image to resize the image. + max_pixels (`int`, *optional*, defaults to `28 * 28 * 1280`): + The max pixels of the image to resize the image. + patch_size (`int`, *optional*, defaults to 14): + The spatial patch size of the vision encoder. + temporal_patch_size (`int`, *optional*, defaults to 2): + The temporal patch size of the vision encoder. + merge_size (`int`, *optional*, defaults to 2): + The merge size of the vision encoder to llm encoder. + """ + + min_pixels: int + max_pixels: int + patch_size: int + temporal_patch_size: int + merge_size: int + +# Copied from transformers.models.video_llama_3.image_processing_video_llama_3.smart_resize +def smart_resize( + height: int, width: int, factor: int = 28, min_pixels: int = 56 * 56, max_pixels: int = 14 * 14 * 4 * 1280 +): + """Rescales the image so that the following conditions are met: + + 1. Both dimensions (height and width) are divisible by 'factor'. + + 2. The total number of pixels is within the range ['min_pixels', 'max_pixels']. + + 3. The aspect ratio of the image is maintained as closely as possible. + + """ + if max(height, width) / min(height, width) > 200: + raise ValueError( + f"absolute aspect ratio must be smaller than 200, got {max(height, width) / min(height, width)}" + ) + h_bar = round(height / factor) * factor + w_bar = round(width / factor) * factor + if h_bar * w_bar > max_pixels: + beta = math.sqrt((height * width) / max_pixels) + h_bar = max(factor, math.floor(height / beta / factor) * factor) + w_bar = max(factor, math.floor(width / beta / factor) * factor) + elif h_bar * w_bar < min_pixels: + beta = math.sqrt(min_pixels / (height * width)) + h_bar = math.ceil(height * beta / factor) * factor + w_bar = math.ceil(width * beta / factor) * factor + return h_bar, w_bar @auto_docstring @@ -209,5 +259,4 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non grid_h, grid_w = resized_height // patch_size, resized_width // patch_size return grid_h * grid_w - __all__ = ["VideoLlama3ImageProcessorPil"] diff --git a/src/transformers/models/vilt/image_processing_pil_vilt.py b/src/transformers/models/vilt/image_processing_pil_vilt.py index 529d3ec0fc6f..2e131e70ab90 100644 --- a/src/transformers/models/vilt/image_processing_pil_vilt.py +++ b/src/transformers/models/vilt/image_processing_pil_vilt.py @@ -34,21 +34,18 @@ TensorType, auto_docstring, ) -from .image_processing_vilt import ViltImageProcessorKwargs - +from ...processing_utils import ImagesKwargs # Set maximum size based on the typical aspect ratio of the COCO dataset MAX_LONGER_EDGE = 1333 MAX_SHORTER_EDGE = 800 - def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ return [max(values_i) for values_i in zip(*values)] - def make_pixel_mask( image: np.ndarray, output_size: tuple[int, int], input_data_format: str | ChannelDimension | None = None ) -> np.ndarray: @@ -66,7 +63,6 @@ def make_pixel_mask( mask[:input_height, :input_width] = 1 return mask - def get_resize_output_image_size( input_image: np.ndarray, shorter: int = 800, @@ -98,6 +94,17 @@ def get_resize_output_image_size( return new_height, new_width +# Copied from transformers.models.vilt.image_processing_vilt.ViltImageProcessorKwargs +class ViltImageProcessorKwargs(ImagesKwargs, total=False): + r""" + size_divisor (`int`, *optional*, defaults to `self.size_divisor`): + The size by which to make sure both the height and width can be divided. Only has an effect if `do_resize` + is set to `True`. + """ + + size_divisor: int + + @auto_docstring class ViltImageProcessorPil(PilBackend): valid_kwargs = ViltImageProcessorKwargs @@ -230,5 +237,4 @@ def _preprocess( return BatchFeature(data=data, tensor_type=return_tensors) - __all__ = ["ViltImageProcessorPil"] diff --git a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py index 5cc09afa1d74..d82670825d71 100644 --- a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py +++ b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py @@ -26,15 +26,23 @@ ImageInput, get_image_size, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available -from .image_processing_vitmatte import VitMatteImageProcessorKwargs - if is_torch_available(): pass +# Copied from transformers.models.vitmatte.image_processing_vitmatte.VitMatteImageProcessorKwargs +class VitMatteImageProcessorKwargs(ImagesKwargs, total=False): + r""" + size_divisor (`int`, *optional*, defaults to `self.size_divisor`): + The width and height of the image will be padded to be divisible by this number. + """ + + size_divisor: int + + @auto_docstring class VitMatteImageProcessorPil(PilBackend): do_rescale = True @@ -146,5 +154,4 @@ def _preprocess( return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) - __all__ = ["VitMatteImageProcessorPil"] diff --git a/src/transformers/models/vitpose/image_processing_pil_vitpose.py b/src/transformers/models/vitpose/image_processing_pil_vitpose.py index 357c603a4603..c875842f8b45 100644 --- a/src/transformers/models/vitpose/image_processing_pil_vitpose.py +++ b/src/transformers/models/vitpose/image_processing_pil_vitpose.py @@ -28,20 +28,9 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_scipy_available, logging from ...utils.import_utils import requires -from .image_processing_vitpose import ( - VitPoseImageProcessorKwargs, - box_to_center_and_scale, - coco_to_pascal_voc, - get_keypoint_predictions, - get_warp_matrix, - post_dark_unbiased_data_processing, - scipy_warp_affine, - transform_preds, -) - if is_scipy_available(): pass @@ -52,6 +41,288 @@ logger = logging.get_logger(__name__) +# Copied from transformers.models.vitpose.image_processing_vitpose.VitPoseImageProcessorKwargs +class VitPoseImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_affine_transform (`bool`, *optional*): + Whether to apply an affine transformation to the input images based on the bounding boxes. + normalize_factor (`float`, *optional*, defaults to `200.0`): + Width and height scale factor used for normalization when computing center and scale from bounding boxes. + """ + + do_affine_transform: bool | None + normalize_factor: float | None + +# Copied from transformers.models.vitpose.image_processing_vitpose.box_to_center_and_scale +# inspired by https://github.com/ViTAE-Transformer/ViTPose/blob/d5216452796c90c6bc29f5c5ec0bdba94366768a/mmpose/datasets/datasets/base/kpt_2d_sview_rgb_img_top_down_dataset.py#L132 +def box_to_center_and_scale( + box: tuple | list | np.ndarray, + image_width: int, + image_height: int, + normalize_factor: float = 200.0, + padding_factor: float = 1.25, +): + """ + Encodes a bounding box in COCO format into (center, scale). + + Args: + box (`Tuple`, `List`, or `np.ndarray`): + Bounding box in COCO format (top_left_x, top_left_y, width, height). + image_width (`int`): + Image width. + image_height (`int`): + Image height. + normalize_factor (`float`): + Width and height scale factor. + padding_factor (`float`): + Bounding box padding factor. + + Returns: + tuple: A tuple containing center and scale. + + - `np.ndarray` [float32](2,): Center of the bbox (x, y). + - `np.ndarray` [float32](2,): Scale of the bbox width & height. + """ + + top_left_x, top_left_y, width, height = box[:4] + aspect_ratio = image_width / image_height + center = np.array([top_left_x + width * 0.5, top_left_y + height * 0.5], dtype=np.float32) + + if width > aspect_ratio * height: + height = width * 1.0 / aspect_ratio + elif width < aspect_ratio * height: + width = height * aspect_ratio + + scale = np.array([width / normalize_factor, height / normalize_factor], dtype=np.float32) + scale = scale * padding_factor + + return center, scale + +# Copied from transformers.models.vitpose.image_processing_vitpose.coco_to_pascal_voc +def coco_to_pascal_voc(bboxes: np.ndarray) -> np.ndarray: + """ + Converts bounding boxes from the COCO format to the Pascal VOC format. + + In other words, converts from (top_left_x, top_left_y, width, height) format + to (top_left_x, top_left_y, bottom_right_x, bottom_right_y). + + Args: + bboxes (`np.ndarray` of shape `(batch_size, 4)): + Bounding boxes in COCO format. + + Returns: + `np.ndarray` of shape `(batch_size, 4) in Pascal VOC format. + """ + bboxes[:, 2] = bboxes[:, 2] + bboxes[:, 0] - 1 + bboxes[:, 3] = bboxes[:, 3] + bboxes[:, 1] - 1 + + return bboxes + +# Copied from transformers.models.vitpose.image_processing_vitpose.get_keypoint_predictions +def get_keypoint_predictions(heatmaps: np.ndarray) -> tuple[np.ndarray, np.ndarray]: + """Get keypoint predictions from score maps. + + Args: + heatmaps (`np.ndarray` of shape `(batch_size, num_keypoints, height, width)`): + Model predicted heatmaps. + + Returns: + tuple: A tuple containing aggregated results. + + - coords (`np.ndarray` of shape `(batch_size, num_keypoints, 2)`): + Predicted keypoint location. + - scores (`np.ndarray` of shape `(batch_size, num_keypoints, 1)`): + Scores (confidence) of the keypoints. + """ + if not isinstance(heatmaps, np.ndarray): + raise TypeError("Heatmaps should be np.ndarray") + if heatmaps.ndim != 4: + raise ValueError("Heatmaps should be 4-dimensional") + + batch_size, num_keypoints, _, width = heatmaps.shape + heatmaps_reshaped = heatmaps.reshape((batch_size, num_keypoints, -1)) + idx = np.argmax(heatmaps_reshaped, 2).reshape((batch_size, num_keypoints, 1)) + scores = np.amax(heatmaps_reshaped, 2).reshape((batch_size, num_keypoints, 1)) + + preds = np.tile(idx, (1, 1, 2)).astype(np.float32) + preds[:, :, 0] = preds[:, :, 0] % width + preds[:, :, 1] = preds[:, :, 1] // width + + preds = np.where(np.tile(scores, (1, 1, 2)) > 0.0, preds, -1) + return preds, scores + +# Copied from transformers.models.vitpose.image_processing_vitpose.get_warp_matrix +def get_warp_matrix(theta: float, size_input: np.ndarray, size_dst: np.ndarray, size_target: np.ndarray): + """ + Calculate the transformation matrix under the constraint of unbiased. Paper ref: Huang et al. The Devil is in the + Details: Delving into Unbiased Data Processing for Human Pose Estimation (CVPR 2020). + + Source: https://github.com/open-mmlab/mmpose/blob/master/mmpose/core/post_processing/post_transforms.py + + Args: + theta (`float`): + Rotation angle in degrees. + size_input (`np.ndarray`): + Size of input image [width, height]. + size_dst (`np.ndarray`): + Size of output image [width, height]. + size_target (`np.ndarray`): + Size of ROI in input plane [w, h]. + + Returns: + `np.ndarray`: A matrix for transformation. + """ + theta = np.deg2rad(theta) + matrix = np.zeros((2, 3), dtype=np.float32) + scale_x = size_dst[0] / size_target[0] + scale_y = size_dst[1] / size_target[1] + matrix[0, 0] = math.cos(theta) * scale_x + matrix[0, 1] = -math.sin(theta) * scale_x + matrix[0, 2] = scale_x * ( + -0.5 * size_input[0] * math.cos(theta) + 0.5 * size_input[1] * math.sin(theta) + 0.5 * size_target[0] + ) + matrix[1, 0] = math.sin(theta) * scale_y + matrix[1, 1] = math.cos(theta) * scale_y + matrix[1, 2] = scale_y * ( + -0.5 * size_input[0] * math.sin(theta) - 0.5 * size_input[1] * math.cos(theta) + 0.5 * size_target[1] + ) + return matrix + +# Copied from transformers.models.vitpose.image_processing_vitpose.post_dark_unbiased_data_processing +def post_dark_unbiased_data_processing(coords: np.ndarray, batch_heatmaps: np.ndarray, kernel: int = 3) -> np.ndarray: + """DARK post-pocessing. Implemented by unbiased_data_processing. + + Paper references: + - Huang et al. The Devil is in the Details: Delving into Unbiased Data Processing for Human Pose Estimation (CVPR 2020). + - Zhang et al. Distribution-Aware Coordinate Representation for Human Pose Estimation (CVPR 2020). + + Args: + coords (`np.ndarray` of shape `(num_persons, num_keypoints, 2)`): + Initial coordinates of human pose. + batch_heatmaps (`np.ndarray` of shape `(batch_size, num_keypoints, height, width)`): + Batched heatmaps as predicted by the model. + A batch_size of 1 is used for the bottom up paradigm where all persons share the same heatmap. + A batch_size of `num_persons` is used for the top down paradigm where each person has its own heatmaps. + kernel (`int`, *optional*, defaults to 3): + Gaussian kernel size (K) for modulation. + + Returns: + `np.ndarray` of shape `(num_persons, num_keypoints, 2)` ): + Refined coordinates. + """ + batch_size, num_keypoints, height, width = batch_heatmaps.shape + num_coords = coords.shape[0] + if not (batch_size == 1 or batch_size == num_coords): + raise ValueError("The batch size of heatmaps should be 1 or equal to the batch size of coordinates.") + radius = int((kernel - 1) // 2) + batch_heatmaps = np.array( + [ + [gaussian_filter(heatmap, sigma=0.8, radius=(radius, radius), axes=(0, 1)) for heatmap in heatmaps] + for heatmaps in batch_heatmaps + ] + ) + batch_heatmaps = np.clip(batch_heatmaps, 0.001, 50) + batch_heatmaps = np.log(batch_heatmaps) + + batch_heatmaps_pad = np.pad(batch_heatmaps, ((0, 0), (0, 0), (1, 1), (1, 1)), mode="edge").flatten() + index = coords[..., 0] + 1 + (coords[..., 1] + 1) * (width + 2) + index += (width + 2) * (height + 2) * np.arange(0, batch_size * num_keypoints).reshape(-1, num_keypoints) + index = index.astype(int).reshape(-1, 1) + i_ = batch_heatmaps_pad[index] + ix1 = batch_heatmaps_pad[index + 1] + iy1 = batch_heatmaps_pad[index + width + 2] + ix1y1 = batch_heatmaps_pad[index + width + 3] + ix1_y1_ = batch_heatmaps_pad[index - width - 3] + ix1_ = batch_heatmaps_pad[index - 1] + iy1_ = batch_heatmaps_pad[index - 2 - width] + dx = 0.5 * (ix1 - ix1_) + dy = 0.5 * (iy1 - iy1_) + derivative = np.concatenate([dx, dy], axis=1) + derivative = derivative.reshape(num_coords, num_keypoints, 2, 1) + dxx = ix1 - 2 * i_ + ix1_ + dyy = iy1 - 2 * i_ + iy1_ + dxy = 0.5 * (ix1y1 - ix1 - iy1 + i_ + i_ - ix1_ - iy1_ + ix1_y1_) + hessian = np.concatenate([dxx, dxy, dxy, dyy], axis=1) + hessian = hessian.reshape(num_coords, num_keypoints, 2, 2) + hessian = np.linalg.inv(hessian + np.finfo(np.float32).eps * np.eye(2)) + coords -= np.einsum("ijmn,ijnk->ijmk", hessian, derivative).squeeze() + return coords + +# Copied from transformers.models.vitpose.image_processing_vitpose.scipy_warp_affine +def scipy_warp_affine(src, M, size): + """ + This function implements cv2.warpAffine function using affine_transform in scipy. See https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.affine_transform.html and https://docs.opencv.org/4.x/d4/d61/tutorial_warp_affine.html for more details. + + Note: the original implementation of cv2.warpAffine uses cv2.INTER_LINEAR. + """ + channels = [src[..., i] for i in range(src.shape[-1])] + + # Convert to a 3x3 matrix used by SciPy + M_scipy = np.vstack([M, [0, 0, 1]]) + # If you have a matrix for the 'push' transformation, use its inverse (numpy.linalg.inv) in this function. + M_inv = inv(M_scipy) + M_inv[0, 0], M_inv[0, 1], M_inv[1, 0], M_inv[1, 1], M_inv[0, 2], M_inv[1, 2] = ( + M_inv[1, 1], + M_inv[1, 0], + M_inv[0, 1], + M_inv[0, 0], + M_inv[1, 2], + M_inv[0, 2], + ) + + new_src = [affine_transform(channel, M_inv, output_shape=size, order=1) for channel in channels] + new_src = np.stack(new_src, axis=-1) + return new_src + +# Copied from transformers.models.vitpose.image_processing_vitpose.transform_preds +def transform_preds(coords: np.ndarray, center: np.ndarray, scale: np.ndarray, output_size: np.ndarray) -> np.ndarray: + """Get final keypoint predictions from heatmaps and apply scaling and + translation to map them back to the image. + + Note: + num_keypoints: K + + Args: + coords (`np.ndarray` of shape `(num_keypoints, ndims)`): + + * If ndims=2, corrds are predicted keypoint location. + * If ndims=4, corrds are composed of (x, y, scores, tags) + * If ndims=5, corrds are composed of (x, y, scores, tags, + flipped_tags) + + center (`np.ndarray` of shape `(2,)`): + Center of the bounding box (x, y). + scale (`np.ndarray` of shape `(2,)`): + Scale of the bounding box wrt original image of width and height. + output_size (`np.ndarray` of shape `(2,)`): + Size of the destination heatmaps in (height, width) format. + + Returns: + np.ndarray: Predicted coordinates in the images. + """ + if coords.shape[1] not in (2, 4, 5): + raise ValueError("Coordinates need to have either 2, 4 or 5 dimensions.") + if len(center) != 2: + raise ValueError("Center needs to have 2 elements, one for x and one for y.") + if len(scale) != 2: + raise ValueError("Scale needs to consist of a width and height") + if len(output_size) != 2: + raise ValueError("Output size needs to consist of a height and width") + + # Recover the scale which is normalized by a factor of 200. + scale = scale * 200.0 + + # We use unbiased data processing + scale_y = scale[1] / (output_size[0] - 1.0) + scale_x = scale[0] / (output_size[1] - 1.0) + + target_coords = np.ones_like(coords) + target_coords[:, 0] = coords[:, 0] * scale_x + center[0] - scale[0] * 0.5 + target_coords[:, 1] = coords[:, 1] * scale_y + center[1] - scale[1] * 0.5 + + return target_coords + + @auto_docstring class VitPoseImageProcessorPil(PilBackend): """PIL backend for VitPose with affine transform.""" @@ -241,5 +512,4 @@ def post_process_pose_estimation( results.append(image_results) return results - __all__ = ["VitPoseImageProcessorPil"] diff --git a/src/transformers/models/yolos/image_processing_pil_yolos.py b/src/transformers/models/yolos/image_processing_pil_yolos.py index 8ddf312fbd1c..6b3156037ddd 100644 --- a/src/transformers/models/yolos/image_processing_pil_yolos.py +++ b/src/transformers/models/yolos/image_processing_pil_yolos.py @@ -20,11 +20,11 @@ safe_squeeze, ) from ...image_utils import ( - IMAGENET_DEFAULT_MEAN, - IMAGENET_DEFAULT_STD, AnnotationFormat, AnnotationType, ChannelDimension, + IMAGENET_DEFAULT_MEAN, + IMAGENET_DEFAULT_STD, ImageInput, PILImageResampling, SizeDict, @@ -33,11 +33,9 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, requires_backends from ...utils.import_utils import requires -from .image_processing_yolos import YolosImageProcessorKwargs - if is_vision_available(): import PIL.Image @@ -47,7 +45,6 @@ SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) - # inspired by https://github.com/facebookresearch/yolos/blob/master/datasets/coco.py#L33 def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndarray: """ @@ -82,7 +79,6 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar return masks - # inspired by https://github.com/facebookresearch/yolos/blob/master/datasets/coco.py#L50 def prepare_coco_detection_annotation( image, @@ -143,7 +139,6 @@ def prepare_coco_detection_annotation( return new_target - def masks_to_boxes(masks: np.ndarray) -> np.ndarray: """ Compute the bounding boxes around the provided panoptic segmentation masks. @@ -177,7 +172,6 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: return np.stack([x_min, y_min, x_max, y_max], 1) - # 2 functions below adapted from https://github.com/cocodataset/panopticapi/blob/master/panopticapi/utils.py # Copyright (c) 2018, Alexander Kirillov # All rights reserved. @@ -191,7 +185,6 @@ def rgb_to_id(color): return color[:, :, 0] + 256 * color[:, :, 1] + 256 * 256 * color[:, :, 2] return int(color[0] + 256 * color[1] + 256 * 256 * color[2]) - def prepare_coco_panoptic_annotation( image: np.ndarray, target: dict, @@ -232,7 +225,6 @@ def prepare_coco_panoptic_annotation( return new_target - def get_size_with_aspect_ratio_yolos( image_size: tuple[int, int], size: int, max_size: int | None = None, mod_size: int = 16 ) -> tuple[int, int]: @@ -274,6 +266,21 @@ def get_size_with_aspect_ratio_yolos( return (oh, ow) +# Copied from transformers.models.yolos.image_processing_yolos.YolosImageProcessorKwargs +class YolosImageProcessorKwargs(ImagesKwargs, total=False): + r""" + format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): + Data format of the annotations. One of "coco_detection" or "coco_panoptic". + do_convert_annotations (`bool`, *optional*, defaults to `True`): + Controls whether to convert the annotations to the format expected by the YOLOS model. Converts the + bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. + Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. + """ + + format: str | AnnotationFormat + do_convert_annotations: bool + + @auto_docstring class YolosImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR @@ -743,5 +750,4 @@ def post_process_object_detection( return results - __all__ = ["YolosImageProcessorPil"] diff --git a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py index 305b39a9f08e..06aff518cad5 100644 --- a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py +++ b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py @@ -22,31 +22,88 @@ from ...image_transforms import PaddingMode from ...image_transforms import pad as np_pad from ...image_utils import ( + ChannelDimension, IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, - ChannelDimension, ImageInput, PILImageResampling, SizeDict, get_image_size, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torchvision_available, requires_backends from ...utils.import_utils import requires -from .image_processing_zoedepth import ZoeDepthImageProcessorKwargs, get_resize_output_image_size - if TYPE_CHECKING: from .modeling_zoedepth import ZoeDepthDepthEstimatorOutput import torch from torch import nn - +from collections.abc import Iterable if is_torchvision_available(): import torchvision.transforms.v2.functional as tvF +# Copied from transformers.models.zoedepth.image_processing_zoedepth.ZoeDepthImageProcessorKwargs +class ZoeDepthImageProcessorKwargs(ImagesKwargs, total=False): + r""" + keep_aspect_ratio (`bool`, *optional*, defaults to `self.keep_aspect_ratio`): + If `True`, the image is resized by choosing the smaller of the height and width scaling factors and using it + for both dimensions. This ensures that the image is scaled down as little as possible while still fitting + within the desired output size. In case `ensure_multiple_of` is also set, the image is further resized to a + size that is a multiple of this value by flooring the height and width to the nearest multiple of this value. + Can be overridden by `keep_aspect_ratio` in `preprocess`. + ensure_multiple_of (`int`, *optional*, defaults to `self.ensure_multiple_of`): + If `do_resize` is `True`, the image is resized to a size that is a multiple of this value. Works by flooring + the height and width to the nearest multiple of this value. + Works both with and without `keep_aspect_ratio` being set to `True`. + Can be overridden by `ensure_multiple_of` in `preprocess`. + """ + + keep_aspect_ratio: bool + ensure_multiple_of: int + +# Copied from transformers.models.zoedepth.image_processing_zoedepth.get_resize_output_image_size +def get_resize_output_image_size( + input_image: "torch.Tensor | np.ndarray", + output_size: int | Iterable[int], + keep_aspect_ratio: bool, + multiple: int, + input_data_format: str | ChannelDimension | None = None, +) -> tuple[int, int]: + def constrain_to_multiple_of(val, multiple, min_val=0): + x = (np.round(val / multiple) * multiple).astype(int) + + if x < min_val: + x = math.ceil(val / multiple) * multiple + + return x + + output_size = (output_size, output_size) if isinstance(output_size, int) else output_size + + input_height, input_width = get_image_size(input_image, input_data_format) + output_height, output_width = output_size + + # determine new height and width + scale_height = output_height / input_height + scale_width = output_width / input_width + + if keep_aspect_ratio: + # scale as little as possible + if abs(1 - scale_width) < abs(1 - scale_height): + # fit width + scale_height = scale_width + else: + # fit height + scale_width = scale_height + + new_height = constrain_to_multiple_of(scale_height * input_height, multiple=multiple) + new_width = constrain_to_multiple_of(scale_width * input_width, multiple=multiple) + + return (new_height, new_width) + + @auto_docstring @requires(backends=("torch",)) class ZoeDepthImageProcessorPil(PilBackend): @@ -279,5 +336,4 @@ def post_process_depth_estimation( return results - __all__ = ["ZoeDepthImageProcessorPil"] diff --git a/src/transformers/utils/auto_docstring.py b/src/transformers/utils/auto_docstring.py index 6a9370b4fcf3..6375851dc770 100644 --- a/src/transformers/utils/auto_docstring.py +++ b/src/transformers/utils/auto_docstring.py @@ -4071,7 +4071,10 @@ def _process_example_section( else: # Check if the model is in a pipeline to get an example for name_model_list_for_task in MODELS_TO_PIPELINE: - model_list_for_task = getattr(auto_module.modeling_auto, name_model_list_for_task) + try: + model_list_for_task = getattr(auto_module.modeling_auto, name_model_list_for_task) + except (ImportError, AttributeError): + continue if class_name in model_list_for_task.values(): pipeline_name = MODELS_TO_PIPELINE[name_model_list_for_task] example_annotation = PIPELINE_TASKS_TO_SAMPLE_DOCSTRINGS[pipeline_name].format( From 2f0267a351122ecc6d9394802f627685c7023eb7 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 16:05:49 +0100 Subject: [PATCH 13/30] style --- .../image_processing_pil_conditional_detr.py | 5 +++-- .../image_processing_pil_deepseek_vl_hybrid.py | 1 + .../image_processing_pil_deformable_detr.py | 5 +++-- src/transformers/models/detr/image_processing_pil_detr.py | 5 +++-- src/transformers/models/dpt/image_processing_pil_dpt.py | 5 +++-- .../efficientloftr/image_processing_pil_efficientloftr.py | 5 +++-- src/transformers/models/eomt/image_processing_pil_eomt.py | 5 +++-- .../image_processing_pil_ernie4_5_vl_moe.py | 1 + .../models/flava/image_processing_pil_flava.py | 4 ++-- .../models/glm4v/image_processing_pil_glm4v.py | 1 + src/transformers/models/glpn/image_processing_pil_glpn.py | 1 + .../models/got_ocr2/image_processing_pil_got_ocr2.py | 3 ++- .../grounding_dino/image_processing_pil_grounding_dino.py | 5 +++-- .../models/idefics2/image_processing_pil_idefics2.py | 1 + .../models/idefics3/image_processing_pil_idefics3.py | 1 + .../models/imagegpt/image_processing_pil_imagegpt.py | 1 + .../models/kosmos2_5/image_processing_pil_kosmos2_5.py | 7 ++++--- .../models/layoutlmv3/image_processing_pil_layoutlmv3.py | 2 +- .../models/lightglue/image_processing_pil_lightglue.py | 1 + .../models/mask2former/image_processing_pil_mask2former.py | 3 ++- .../models/maskformer/image_processing_pil_maskformer.py | 7 ++++--- .../models/mllama/image_processing_pil_mllama.py | 6 ++++-- .../mobilenet_v2/image_processing_pil_mobilenet_v2.py | 1 + .../models/mobilevit/image_processing_pil_mobilevit.py | 2 ++ .../models/oneformer/image_processing_pil_oneformer.py | 6 +++--- .../models/ovis2/image_processing_pil_ovis2.py | 3 ++- .../models/pix2struct/image_processing_pil_pix2struct.py | 6 +++--- .../image_processing_pil_prompt_depth_anything.py | 1 + .../models/rt_detr/image_processing_pil_rt_detr.py | 5 +++-- src/transformers/models/sam/image_processing_pil_sam.py | 5 ++--- .../models/segformer/image_processing_pil_segformer.py | 1 + .../models/siglip2/image_processing_pil_siglip2.py | 1 + .../models/smolvlm/image_processing_pil_smolvlm.py | 1 + src/transformers/models/smolvlm/processing_smolvlm.py | 2 +- .../models/superglue/image_processing_pil_superglue.py | 1 + .../models/superpoint/image_processing_pil_superpoint.py | 1 + src/transformers/models/tvp/image_processing_pil_tvp.py | 1 + src/transformers/models/vilt/image_processing_pil_vilt.py | 3 ++- .../models/vitmatte/image_processing_pil_vitmatte.py | 1 + .../models/vitpose/image_processing_pil_vitpose.py | 1 + .../models/yolos/image_processing_pil_yolos.py | 5 +++-- .../models/zoedepth/image_processing_pil_zoedepth.py | 7 +++++-- 42 files changed, 84 insertions(+), 45 deletions(-) diff --git a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py index 49681ad8cbe2..711003ba889f 100644 --- a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py +++ b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py @@ -35,11 +35,11 @@ safe_squeeze, ) from ...image_utils import ( + IMAGENET_DEFAULT_MEAN, + IMAGENET_DEFAULT_STD, AnnotationFormat, AnnotationType, ChannelDimension, - IMAGENET_DEFAULT_MEAN, - IMAGENET_DEFAULT_STD, ImageInput, PILImageResampling, SizeDict, @@ -52,6 +52,7 @@ from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, logging, requires_backends from ...utils.import_utils import requires + if is_vision_available(): import PIL.Image if is_torch_available(): diff --git a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py index 86b5ca962fc7..48579911e545 100644 --- a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py +++ b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py @@ -37,6 +37,7 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available + if is_torch_available(): import torch diff --git a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py index 8403ab0629ec..0dc5570cffb0 100644 --- a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py +++ b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py @@ -34,11 +34,11 @@ safe_squeeze, ) from ...image_utils import ( + IMAGENET_DEFAULT_MEAN, + IMAGENET_DEFAULT_STD, AnnotationFormat, AnnotationType, ChannelDimension, - IMAGENET_DEFAULT_MEAN, - IMAGENET_DEFAULT_STD, ImageInput, PILImageResampling, SizeDict, @@ -51,6 +51,7 @@ from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available from ...utils.import_utils import requires, requires_backends + if is_vision_available(): import PIL.Image if is_torch_available(): diff --git a/src/transformers/models/detr/image_processing_pil_detr.py b/src/transformers/models/detr/image_processing_pil_detr.py index 735254b72be8..ddc5a0c0cdba 100644 --- a/src/transformers/models/detr/image_processing_pil_detr.py +++ b/src/transformers/models/detr/image_processing_pil_detr.py @@ -29,11 +29,11 @@ safe_squeeze, ) from ...image_utils import ( + IMAGENET_DEFAULT_MEAN, + IMAGENET_DEFAULT_STD, AnnotationFormat, AnnotationType, ChannelDimension, - IMAGENET_DEFAULT_MEAN, - IMAGENET_DEFAULT_STD, ImageInput, PILImageResampling, SizeDict, @@ -52,6 +52,7 @@ ) from ...utils.import_utils import requires + if is_vision_available(): import PIL.Image diff --git a/src/transformers/models/dpt/image_processing_pil_dpt.py b/src/transformers/models/dpt/image_processing_pil_dpt.py index 828e0fb08b31..d868e4547564 100644 --- a/src/transformers/models/dpt/image_processing_pil_dpt.py +++ b/src/transformers/models/dpt/image_processing_pil_dpt.py @@ -14,6 +14,7 @@ """Image processor class for DPT.""" import math +from collections.abc import Iterable from typing import TYPE_CHECKING import numpy as np @@ -22,9 +23,9 @@ from ...image_processing_utils import BatchFeature from ...image_transforms import pad as np_pad from ...image_utils import ( - ChannelDimension, IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, + ChannelDimension, ImageInput, PILImageResampling, SizeDict, @@ -32,7 +33,7 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, logging from ...utils.import_utils import requires -from collections.abc import Iterable + if TYPE_CHECKING: from ...modeling_outputs import DepthEstimatorOutput diff --git a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py index a1914cac9747..f8c25293caf0 100644 --- a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py @@ -5,6 +5,8 @@ # modular_efficientloftr.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 +from typing import TYPE_CHECKING + import numpy as np from PIL import Image, ImageDraw @@ -12,11 +14,10 @@ from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput, PILImageResampling, SizeDict, to_numpy_array from ...processing_utils import ImagesKwargs, Unpack -from typing import TYPE_CHECKING - from ...utils import TensorType, auto_docstring, is_torch_available from ...utils.import_utils import requires + if is_torch_available(): import torch diff --git a/src/transformers/models/eomt/image_processing_pil_eomt.py b/src/transformers/models/eomt/image_processing_pil_eomt.py index e53dc1322932..20029fa2b51e 100644 --- a/src/transformers/models/eomt/image_processing_pil_eomt.py +++ b/src/transformers/models/eomt/image_processing_pil_eomt.py @@ -16,6 +16,7 @@ import math import numpy as np + from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_transforms import PaddingMode, get_size_with_aspect_ratio @@ -33,9 +34,9 @@ from ...utils import TensorType, auto_docstring, is_torch_available from ...utils.import_utils import requires + if is_torch_available(): import torch - from torch import nn # Adapted from transformers.models.maskformer.image_processing_maskformer.convert_segmentation_map_to_binary_masks def convert_segmentation_map_to_binary_masks( @@ -608,4 +609,4 @@ def post_process_instance_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results -__all__ = ["EomtImageProcessorPil"] \ No newline at end of file +__all__ = ["EomtImageProcessorPil"] diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py index d4da29660818..7d652771060a 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py @@ -26,6 +26,7 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available, logging + if is_torch_available(): import torch if is_torchvision_available(): diff --git a/src/transformers/models/flava/image_processing_pil_flava.py b/src/transformers/models/flava/image_processing_pil_flava.py index 0fbdacc70bf3..815c3ec63c43 100644 --- a/src/transformers/models/flava/image_processing_pil_flava.py +++ b/src/transformers/models/flava/image_processing_pil_flava.py @@ -22,10 +22,10 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature, get_size_dict from ...image_utils import ( - ChannelDimension, - ImageInput, OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, + ChannelDimension, + ImageInput, PILImageResampling, SizeDict, ) diff --git a/src/transformers/models/glm4v/image_processing_pil_glm4v.py b/src/transformers/models/glm4v/image_processing_pil_glm4v.py index 8e3279b70fae..44f4506c6fc5 100644 --- a/src/transformers/models/glm4v/image_processing_pil_glm4v.py +++ b/src/transformers/models/glm4v/image_processing_pil_glm4v.py @@ -27,6 +27,7 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, logging + logger = logging.get_logger(__name__) diff --git a/src/transformers/models/glpn/image_processing_pil_glpn.py b/src/transformers/models/glpn/image_processing_pil_glpn.py index 929839f056b7..4dd193d84589 100644 --- a/src/transformers/models/glpn/image_processing_pil_glpn.py +++ b/src/transformers/models/glpn/image_processing_pil_glpn.py @@ -24,6 +24,7 @@ from ...utils import TensorType, auto_docstring, logging from ...utils.import_utils import requires + if TYPE_CHECKING: from ...modeling_outputs import DepthEstimatorOutput diff --git a/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py b/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py index 5a35521677fc..77401575be77 100644 --- a/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py +++ b/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py @@ -13,6 +13,8 @@ # limitations under the License. """Image processor class for Got-OCR-2.""" +from functools import lru_cache + import numpy as np from ...image_processing_backends import PilBackend @@ -32,7 +34,6 @@ TensorType, auto_docstring, ) -from functools import lru_cache # Copied from transformers.models.got_ocr2.image_processing_got_ocr2.GotOcr2ImageProcessorKwargs diff --git a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py index e3f15205f71a..34cde0a420c3 100644 --- a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py +++ b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py @@ -40,11 +40,11 @@ safe_squeeze, ) from ...image_utils import ( + IMAGENET_DEFAULT_MEAN, + IMAGENET_DEFAULT_STD, AnnotationFormat, AnnotationType, ChannelDimension, - IMAGENET_DEFAULT_MEAN, - IMAGENET_DEFAULT_STD, ImageInput, PILImageResampling, SizeDict, @@ -57,6 +57,7 @@ from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, requires_backends from ...utils.import_utils import requires + if TYPE_CHECKING: from .modeling_grounding_dino import GroundingDinoObjectDetectionOutput diff --git a/src/transformers/models/idefics2/image_processing_pil_idefics2.py b/src/transformers/models/idefics2/image_processing_pil_idefics2.py index 4cb2c18e4495..40dac81f46ae 100644 --- a/src/transformers/models/idefics2/image_processing_pil_idefics2.py +++ b/src/transformers/models/idefics2/image_processing_pil_idefics2.py @@ -29,6 +29,7 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring + def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: """Make pixel mask: 1=valid, 0=padding. Images are CHW.""" h, w = image.shape[-2:] diff --git a/src/transformers/models/idefics3/image_processing_pil_idefics3.py b/src/transformers/models/idefics3/image_processing_pil_idefics3.py index 16b0b86f220e..daf41c4b5f8d 100644 --- a/src/transformers/models/idefics3/image_processing_pil_idefics3.py +++ b/src/transformers/models/idefics3/image_processing_pil_idefics3.py @@ -31,6 +31,7 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring + def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: """Make pixel mask: 1=valid, 0=padding. Images are CHW.""" h, w = image.shape[-2:] diff --git a/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py b/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py index 2584b17b2eaf..3e091bc0c87d 100644 --- a/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py +++ b/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py @@ -30,6 +30,7 @@ is_torch_available, ) + if is_torch_available(): import torch diff --git a/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py b/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py index 47d0478762b1..77e2d94d5521 100644 --- a/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py +++ b/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py @@ -16,16 +16,17 @@ import math import numpy as np + from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature, get_size_dict from ...image_utils import ChannelDimension, ImageInput, SizeDict, get_image_size from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, requires_backends, is_torch_available +from ...utils import TensorType, auto_docstring, is_torch_available, requires_backends from ...utils.import_utils import requires + if is_torch_available(): import torch - from torch import nn # Copied from transformers.models.kosmos2_5.image_processing_kosmos2_5.Kosmos2_5ImageProcessorKwargs class Kosmos2_5ImageProcessorKwargs(ImagesKwargs, total=False): @@ -248,4 +249,4 @@ def _standardize_kwargs(self, patch_size: dict[str, int] | SizeDict | None = Non kwargs["patch_size"] = patch_size return kwargs -__all__ = ["Kosmos2_5ImageProcessorPil"] \ No newline at end of file +__all__ = ["Kosmos2_5ImageProcessorPil"] diff --git a/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py b/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py index 0e7338816496..e1b796710d63 100644 --- a/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py +++ b/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py @@ -17,6 +17,7 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature +from ...image_transforms import ChannelDimension from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, @@ -27,7 +28,6 @@ ) from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, requires_backends -from ...image_transforms import ChannelDimension # Copied from transformers.models.layoutlmv3.image_processing_layoutlmv3.LayoutLMv3ImageProcessorKwargs diff --git a/src/transformers/models/lightglue/image_processing_pil_lightglue.py b/src/transformers/models/lightglue/image_processing_pil_lightglue.py index 58fcad9db5a3..9c22e3f83ac3 100644 --- a/src/transformers/models/lightglue/image_processing_pil_lightglue.py +++ b/src/transformers/models/lightglue/image_processing_pil_lightglue.py @@ -30,6 +30,7 @@ from ...utils import TensorType, auto_docstring, is_torch_available from ...utils.import_utils import requires + if TYPE_CHECKING: from .modeling_lightglue import LightGlueKeypointMatchingOutput if is_torch_available(): diff --git a/src/transformers/models/mask2former/image_processing_pil_mask2former.py b/src/transformers/models/mask2former/image_processing_pil_mask2former.py index 6c50a6390d5a..57280f896bd3 100644 --- a/src/transformers/models/mask2former/image_processing_pil_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_pil_mask2former.py @@ -28,9 +28,9 @@ from ...image_transforms import PaddingMode, get_size_with_aspect_ratio from ...image_transforms import pad as np_pad from ...image_utils import ( - ChannelDimension, IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, + ChannelDimension, ImageInput, PILImageResampling, SizeDict, @@ -42,6 +42,7 @@ from ...utils import TensorType, auto_docstring, is_torch_available, logging, requires_backends from ...utils.import_utils import requires + if is_torch_available(): import torch from torch import nn diff --git a/src/transformers/models/maskformer/image_processing_pil_maskformer.py b/src/transformers/models/maskformer/image_processing_pil_maskformer.py index 0bdc5bdd2037..80ea3f0a4b33 100644 --- a/src/transformers/models/maskformer/image_processing_pil_maskformer.py +++ b/src/transformers/models/maskformer/image_processing_pil_maskformer.py @@ -23,9 +23,9 @@ from ...image_transforms import PaddingMode, get_size_with_aspect_ratio from ...image_transforms import pad as np_pad from ...image_utils import ( - ChannelDimension, IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, + ChannelDimension, ImageInput, PILImageResampling, SizeDict, @@ -34,9 +34,10 @@ get_max_height_width, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, logging, requires_backends, is_torch_available +from ...utils import TensorType, auto_docstring, is_torch_available, logging, requires_backends from ...utils.import_utils import requires + if is_torch_available(): import torch from torch import nn @@ -790,4 +791,4 @@ def post_process_panoptic_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results -__all__ = ["MaskFormerImageProcessorPil"] \ No newline at end of file +__all__ = ["MaskFormerImageProcessorPil"] diff --git a/src/transformers/models/mllama/image_processing_pil_mllama.py b/src/transformers/models/mllama/image_processing_pil_mllama.py index 0224d196acb5..baffecb496c1 100644 --- a/src/transformers/models/mllama/image_processing_pil_mllama.py +++ b/src/transformers/models/mllama/image_processing_pil_mllama.py @@ -13,6 +13,8 @@ # limitations under the License. """PIL Image processor class for Mllama.""" +from functools import lru_cache + import numpy as np from ...image_processing_backends import PilBackend @@ -20,9 +22,9 @@ from ...image_transforms import PaddingMode, get_image_size from ...image_transforms import pad as np_pad from ...image_utils import ( - ChannelDimension, IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, + ChannelDimension, ImageInput, PILImageResampling, SizeDict, @@ -30,7 +32,7 @@ ) from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from functools import lru_cache + def split_to_tiles_np(image: np.ndarray, num_tiles_height: int, num_tiles_width: int) -> np.ndarray: """Split an image into tiles (numpy version).""" diff --git a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py index fd073d3b8f32..d78fcfde70df 100644 --- a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py +++ b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py @@ -17,6 +17,7 @@ import numpy as np + if TYPE_CHECKING: pass diff --git a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py index a624abedaabc..0871204d761b 100644 --- a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py +++ b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py @@ -17,6 +17,7 @@ import numpy as np + if TYPE_CHECKING: pass @@ -35,6 +36,7 @@ from ...utils import TensorType, auto_docstring, logging from ...utils.import_utils import requires + logger = logging.get_logger(__name__) diff --git a/src/transformers/models/oneformer/image_processing_pil_oneformer.py b/src/transformers/models/oneformer/image_processing_pil_oneformer.py index 26179a11276a..3d8d30e19a94 100644 --- a/src/transformers/models/oneformer/image_processing_pil_oneformer.py +++ b/src/transformers/models/oneformer/image_processing_pil_oneformer.py @@ -30,9 +30,10 @@ get_max_height_width, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, logging, is_torch_available +from ...utils import TensorType, auto_docstring, is_torch_available, logging from ...utils.import_utils import requires + logger = logging.get_logger(__name__) def make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: @@ -45,7 +46,6 @@ def make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarr output_size (`Tuple[int, int]`): Output size of the mask. """ - from ...image_utils import ChannelDimension if is_torch_available(): import torch @@ -940,4 +940,4 @@ def post_process_panoptic_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results -__all__ = ["OneFormerImageProcessorPil"] \ No newline at end of file +__all__ = ["OneFormerImageProcessorPil"] diff --git a/src/transformers/models/ovis2/image_processing_pil_ovis2.py b/src/transformers/models/ovis2/image_processing_pil_ovis2.py index f918324b4208..12717a1ea3ba 100644 --- a/src/transformers/models/ovis2/image_processing_pil_ovis2.py +++ b/src/transformers/models/ovis2/image_processing_pil_ovis2.py @@ -13,6 +13,8 @@ # limitations under the License. """PIL Image processor class for OVIS2.""" +from functools import lru_cache + import numpy as np from ...image_processing_backends import PilBackend @@ -29,7 +31,6 @@ ) from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from functools import lru_cache # Copied from transformers.models.ovis2.image_processing_ovis2.Ovis2ImageProcessorKwargs diff --git a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py index c275fedee715..3f9862325060 100644 --- a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py +++ b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py @@ -23,12 +23,12 @@ from ...image_transforms import to_channel_dimension_format, to_pil_image from ...image_utils import ChannelDimension, ImageInput, SizeDict from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, requires_backends, is_torch_available +from ...utils import TensorType, auto_docstring, is_torch_available, requires_backends from ...utils.import_utils import requires + if is_torch_available(): import torch - from torch import nn # Copied from transformers.models.pix2struct.image_processing_pix2struct.Pix2StructImageProcessorKwargs class Pix2StructImageProcessorKwargs(ImagesKwargs, total=False): @@ -389,4 +389,4 @@ def _preprocess( tensor_type=return_tensors, ) -__all__ = ["Pix2StructImageProcessorPil"] \ No newline at end of file +__all__ = ["Pix2StructImageProcessorPil"] diff --git a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py index 5cc4a335d360..0badb5a2a9f3 100644 --- a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py @@ -35,6 +35,7 @@ from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires + if TYPE_CHECKING: from ...modeling_outputs import DepthEstimatorOutput diff --git a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py index c07d9e3d64f8..adb783d4c203 100644 --- a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py @@ -34,11 +34,11 @@ safe_squeeze, ) from ...image_utils import ( + IMAGENET_DEFAULT_MEAN, + IMAGENET_DEFAULT_STD, AnnotationFormat, AnnotationType, ChannelDimension, - IMAGENET_DEFAULT_MEAN, - IMAGENET_DEFAULT_STD, ImageInput, PILImageResampling, get_image_size, @@ -56,6 +56,7 @@ ) from ...utils.import_utils import requires + if is_torch_available(): import torch if is_torchvision_available(): diff --git a/src/transformers/models/sam/image_processing_pil_sam.py b/src/transformers/models/sam/image_processing_pil_sam.py index 7fc6cc51b11f..3d914d4f7cc0 100644 --- a/src/transformers/models/sam/image_processing_pil_sam.py +++ b/src/transformers/models/sam/image_processing_pil_sam.py @@ -36,6 +36,7 @@ from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available from ...utils.import_utils import requires + if is_vision_available(): import PIL @@ -711,11 +712,9 @@ def _post_process_for_mask_generation(rle_masks, iou_scores, mask_boxes, amg_cro """ if not is_torch_available(): raise ImportError("PyTorch is required for post_process_for_mask_generation") - from torchvision.ops.boxes import batched_nms if is_torch_available(): import torch - from torch import nn keep_by_nms = batched_nms( boxes=mask_boxes.float(), @@ -761,4 +760,4 @@ def _mask_to_rle(input_mask: "torch.Tensor"): out.append({"size": [height, width], "counts": counts}) return out -__all__ = ["SamImageProcessorPil"] \ No newline at end of file +__all__ = ["SamImageProcessorPil"] diff --git a/src/transformers/models/segformer/image_processing_pil_segformer.py b/src/transformers/models/segformer/image_processing_pil_segformer.py index e2961f249933..3ffb6b49f3c4 100644 --- a/src/transformers/models/segformer/image_processing_pil_segformer.py +++ b/src/transformers/models/segformer/image_processing_pil_segformer.py @@ -34,6 +34,7 @@ from ...utils import TensorType, is_torch_available, is_torchvision_available from ...utils.import_utils import requires + if is_torch_available(): pass if is_torchvision_available(): diff --git a/src/transformers/models/siglip2/image_processing_pil_siglip2.py b/src/transformers/models/siglip2/image_processing_pil_siglip2.py index ac99a2689a3f..ff3567c74637 100644 --- a/src/transformers/models/siglip2/image_processing_pil_siglip2.py +++ b/src/transformers/models/siglip2/image_processing_pil_siglip2.py @@ -24,6 +24,7 @@ auto_docstring, ) + def convert_image_to_patches(image: np.ndarray, patch_size: int) -> np.ndarray: """ Convert 3D array image of shape (num_channels, image_height, image_width) into 2D array of patches of shape diff --git a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py index fd1f19292c70..e2025eb466e5 100644 --- a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py +++ b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py @@ -37,6 +37,7 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring + def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: """Make pixel mask: 1=valid, 0=padding. Images are CHW.""" h, w = image.shape[-2:] diff --git a/src/transformers/models/smolvlm/processing_smolvlm.py b/src/transformers/models/smolvlm/processing_smolvlm.py index 82c31e2f38ae..e3d5360d6aa8 100644 --- a/src/transformers/models/smolvlm/processing_smolvlm.py +++ b/src/transformers/models/smolvlm/processing_smolvlm.py @@ -22,7 +22,7 @@ from ...image_utils import ImageInput, make_nested_list_of_images from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack from ...tokenization_utils_base import BatchEncoding, TextInput -from ...utils import auto_docstring, is_num2words_available, is_vision_available, logging +from ...utils import auto_docstring, is_num2words_available, logging from ...video_utils import VideoInput diff --git a/src/transformers/models/superglue/image_processing_pil_superglue.py b/src/transformers/models/superglue/image_processing_pil_superglue.py index 94218450aa6b..efa531754dcb 100644 --- a/src/transformers/models/superglue/image_processing_pil_superglue.py +++ b/src/transformers/models/superglue/image_processing_pil_superglue.py @@ -25,6 +25,7 @@ from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires + if TYPE_CHECKING: import torch diff --git a/src/transformers/models/superpoint/image_processing_pil_superpoint.py b/src/transformers/models/superpoint/image_processing_pil_superpoint.py index f6d4887fb35a..0a6bb7e421f7 100644 --- a/src/transformers/models/superpoint/image_processing_pil_superpoint.py +++ b/src/transformers/models/superpoint/image_processing_pil_superpoint.py @@ -24,6 +24,7 @@ from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires + if TYPE_CHECKING: import torch diff --git a/src/transformers/models/tvp/image_processing_pil_tvp.py b/src/transformers/models/tvp/image_processing_pil_tvp.py index 175edcdfde12..01ab76f31ab0 100644 --- a/src/transformers/models/tvp/image_processing_pil_tvp.py +++ b/src/transformers/models/tvp/image_processing_pil_tvp.py @@ -32,6 +32,7 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torchvision_available + if is_torchvision_available(): pass diff --git a/src/transformers/models/vilt/image_processing_pil_vilt.py b/src/transformers/models/vilt/image_processing_pil_vilt.py index 2e131e70ab90..0502e419bac4 100644 --- a/src/transformers/models/vilt/image_processing_pil_vilt.py +++ b/src/transformers/models/vilt/image_processing_pil_vilt.py @@ -30,11 +30,12 @@ get_image_size, get_max_height_width, ) +from ...processing_utils import ImagesKwargs from ...utils import ( TensorType, auto_docstring, ) -from ...processing_utils import ImagesKwargs + # Set maximum size based on the typical aspect ratio of the COCO dataset MAX_LONGER_EDGE = 1333 diff --git a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py index d82670825d71..4837e0ddbd77 100644 --- a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py +++ b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py @@ -29,6 +29,7 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available + if is_torch_available(): pass diff --git a/src/transformers/models/vitpose/image_processing_pil_vitpose.py b/src/transformers/models/vitpose/image_processing_pil_vitpose.py index c875842f8b45..680c09db0a8f 100644 --- a/src/transformers/models/vitpose/image_processing_pil_vitpose.py +++ b/src/transformers/models/vitpose/image_processing_pil_vitpose.py @@ -32,6 +32,7 @@ from ...utils import TensorType, auto_docstring, is_scipy_available, logging from ...utils.import_utils import requires + if is_scipy_available(): pass diff --git a/src/transformers/models/yolos/image_processing_pil_yolos.py b/src/transformers/models/yolos/image_processing_pil_yolos.py index 6b3156037ddd..d1493773d0a0 100644 --- a/src/transformers/models/yolos/image_processing_pil_yolos.py +++ b/src/transformers/models/yolos/image_processing_pil_yolos.py @@ -20,11 +20,11 @@ safe_squeeze, ) from ...image_utils import ( + IMAGENET_DEFAULT_MEAN, + IMAGENET_DEFAULT_STD, AnnotationFormat, AnnotationType, ChannelDimension, - IMAGENET_DEFAULT_MEAN, - IMAGENET_DEFAULT_STD, ImageInput, PILImageResampling, SizeDict, @@ -37,6 +37,7 @@ from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, requires_backends from ...utils.import_utils import requires + if is_vision_available(): import PIL.Image if is_torch_available(): diff --git a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py index 06aff518cad5..f4bf9aa8a46b 100644 --- a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py +++ b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py @@ -22,9 +22,9 @@ from ...image_transforms import PaddingMode from ...image_transforms import pad as np_pad from ...image_utils import ( - ChannelDimension, IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, + ChannelDimension, ImageInput, PILImageResampling, SizeDict, @@ -34,12 +34,15 @@ from ...utils import TensorType, auto_docstring, is_torchvision_available, requires_backends from ...utils.import_utils import requires + if TYPE_CHECKING: from .modeling_zoedepth import ZoeDepthDepthEstimatorOutput +from collections.abc import Iterable + import torch from torch import nn -from collections.abc import Iterable + if is_torchvision_available(): import torchvision.transforms.v2.functional as tvF From f9d73c9740640c5a5f79437432af82bdfbcf2434 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 16:23:58 +0100 Subject: [PATCH 14/30] fixes --- .../models/eomt/image_processing_pil_eomt.py | 33 ++++++-- .../idefics/image_processing_pil_idefics.py | 31 +++++++- .../owlv2/image_processing_pil_owlv2.py | 45 ++++++++++- .../owlvit/image_processing_pil_owlvit.py | 75 ++++++++++++++++++- .../image_processing_pil_pix2struct.py | 51 +++++++------ .../zoedepth/image_processing_pil_zoedepth.py | 12 +-- 6 files changed, 211 insertions(+), 36 deletions(-) diff --git a/src/transformers/models/eomt/image_processing_pil_eomt.py b/src/transformers/models/eomt/image_processing_pil_eomt.py index 20029fa2b51e..a2d38818446b 100644 --- a/src/transformers/models/eomt/image_processing_pil_eomt.py +++ b/src/transformers/models/eomt/image_processing_pil_eomt.py @@ -75,6 +75,29 @@ def convert_segmentation_map_to_binary_masks( return binary_masks.astype(np.float32), labels.astype(np.int64) +# Copied from transformers.models.eomt.image_processing_eomt.check_segment_validity +def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overlap_mask_area_threshold=0.8): + # Get the mask associated with the k class + mask_k = mask_labels == k + mask_k_area = mask_k.sum() + + # Compute the area of all the stuff in query k + original_mask = mask_probs[k] >= mask_threshold + original_area = original_mask.sum() + + final_mask = mask_k & original_mask + final_mask_area = final_mask.sum() + + mask_exists = mask_k_area > 0 and original_area > 0 and final_mask_area > 0 + + if mask_exists: + area_ratio = mask_k_area / original_area + if not area_ratio.item() > overlap_mask_area_threshold: + mask_exists = False + + return mask_exists, final_mask + + # Copied from transformers.models.eomt.image_processing_eomt.EomtImageProcessorKwargs class EomtImageProcessorKwargs(ImagesKwargs, total=False): r""" @@ -252,7 +275,7 @@ def _pad(self, image: np.ndarray, size: SizeDict) -> np.ndarray: def preprocess( self, images: ImageInput, - segmentation_maps: list[torch.Tensor] | None = None, + segmentation_maps: "list[torch.Tensor] | None" = None, instance_id_to_semantic_id: dict[int, int] | None = None, **kwargs: Unpack[EomtImageProcessorKwargs], ) -> BatchFeature: @@ -392,11 +415,11 @@ def _preprocess( def merge_image_patches( self, - segmentation_logits: torch.Tensor, + segmentation_logits: "torch.Tensor", patch_offsets: list[tuple[int, int, int]], target_sizes: list[tuple[int, int]], size: dict[str, int], - ) -> list[torch.Tensor]: + ) -> "list[torch.Tensor]": """ Reconstructs full-size semantic segmentation logits from patch predictions. @@ -445,8 +468,8 @@ def merge_image_patches( return reconstructed_logits def unpad_image( - self, segmentation_logits: torch.Tensor, target_sizes: list[tuple[int, int]], size: dict[str, int] - ) -> list[torch.Tensor]: + self, segmentation_logits: "torch.Tensor", target_sizes: list[tuple[int, int]], size: dict[str, int] + ) -> "list[torch.Tensor]": """Restores panoptic segmentation logits to their original image resolutions.""" resized_logits = [] diff --git a/src/transformers/models/idefics/image_processing_pil_idefics.py b/src/transformers/models/idefics/image_processing_pil_idefics.py index 57905fd6d7d4..37eba9839eb2 100644 --- a/src/transformers/models/idefics/image_processing_pil_idefics.py +++ b/src/transformers/models/idefics/image_processing_pil_idefics.py @@ -13,11 +13,38 @@ # limitations under the License. """Image processor class for Idefics.""" +from collections.abc import Callable + from ...image_processing_backends import PilBackend from ...image_utils import ImageInput, PILImageResampling, make_flat_list_of_images -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import auto_docstring, is_torch_available -from .image_processing_idefics import IDEFICS_STANDARD_MEAN, IDEFICS_STANDARD_STD, IdeficsImageProcessorKwargs + + +# Copied from transformers.models.idefics.image_processing_idefics.IDEFICS_STANDARD_MEAN +IDEFICS_STANDARD_MEAN = [0.48145466, 0.4578275, 0.40821073] + +# Copied from transformers.models.idefics.image_processing_idefics.IDEFICS_STANDARD_STD +IDEFICS_STANDARD_STD = [0.26862954, 0.26130258, 0.27577711] + + +# Copied from transformers.models.idefics.image_processing_idefics.IdeficsImageProcessorKwargs +class IdeficsImageProcessorKwargs(ImagesKwargs, total=False): + r""" + transform (`Callable`, *optional*, defaults to `None`): + A custom transform function that accepts a single image can be passed for training. For example, + `torchvision.Compose` can be used to compose multiple transforms. If `None` - an inference mode is + assumed - and then a preset of inference-specific transforms will be applied to the images. + image_size (`int`, *optional*, defaults to `self.image_size`): + Resize to image size. This is a backward-compatible alias for `size`. When provided, it overrides + `size` and sets it to `{"height": image_size, "width": image_size}`. + image_num_channels (`int`, *optional*, defaults to `3`): + The number of channels of the image. + """ + + transform: Callable | None + image_size: int + image_num_channels: int @auto_docstring diff --git a/src/transformers/models/owlv2/image_processing_pil_owlv2.py b/src/transformers/models/owlv2/image_processing_pil_owlv2.py index d68c5da6abdb..15226a9257ff 100644 --- a/src/transformers/models/owlv2/image_processing_pil_owlv2.py +++ b/src/transformers/models/owlv2/image_processing_pil_owlv2.py @@ -37,7 +37,50 @@ requires_backends, ) from ...utils.import_utils import requires -from ..owlv2.image_processing_owlv2 import box_iou + + +def _upcast(t): + # Protects from numerical overflows in multiplications by upcasting to the equivalent higher type + import torch + + if t.is_floating_point(): + return t if t.dtype in (torch.float32, torch.float64) else t.float() + else: + return t if t.dtype in (torch.int32, torch.int64) else t.int() + + +def box_area(boxes): + """ + Computes the area of a set of bounding boxes, which are specified by its (x1, y1, x2, y2) coordinates. + + Args: + boxes (`torch.FloatTensor` of shape `(number_of_boxes, 4)`): + Boxes for which the area will be computed. They are expected to be in (x1, y1, x2, y2) format with `0 <= x1 + < x2` and `0 <= y1 < y2`. + Returns: + `torch.FloatTensor`: a tensor containing the area for each box. + """ + boxes = _upcast(boxes) + return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) + + +# Copied from transformers.models.owlv2.image_processing_owlv2.box_iou +def box_iou(boxes1, boxes2): + import torch + + area1 = box_area(boxes1) + area2 = box_area(boxes2) + + left_top = torch.max(boxes1[:, None, :2], boxes2[:, :2]) # [N,M,2] + right_bottom = torch.min(boxes1[:, None, 2:], boxes2[:, 2:]) # [N,M,2] + + width_height = (right_bottom - left_top).clamp(min=0) # [N,M,2] + inter = width_height[:, :, 0] * width_height[:, :, 1] # [N,M] + + union = area1[:, None] + area2 - inter + + iou = inter / union + return iou, union if is_scipy_available(): diff --git a/src/transformers/models/owlvit/image_processing_pil_owlvit.py b/src/transformers/models/owlvit/image_processing_pil_owlvit.py index a130516ab5a7..b34af3051dfa 100644 --- a/src/transformers/models/owlvit/image_processing_pil_owlvit.py +++ b/src/transformers/models/owlvit/image_processing_pil_owlvit.py @@ -21,7 +21,80 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, logging from ...utils.import_utils import requires -from ..owlvit.image_processing_owlvit import _scale_boxes, box_iou + + +def _upcast(t): + # Protects from numerical overflows in multiplications by upcasting to the equivalent higher type + import torch + + if t.is_floating_point(): + return t if t.dtype in (torch.float32, torch.float64) else t.float() + else: + return t if t.dtype in (torch.int32, torch.int64) else t.int() + + +# Copied from transformers.models.owlvit.image_processing_owlvit._scale_boxes +def _scale_boxes(boxes, target_sizes): + """ + Scale batch of bounding boxes to the target sizes. + + Args: + boxes (`torch.Tensor` of shape `(batch_size, num_boxes, 4)`): + Bounding boxes to scale. Each box is expected to be in (x1, y1, x2, y2) format. + target_sizes (`list[tuple[int, int]]` or `torch.Tensor` of shape `(batch_size, 2)`): + Target sizes to scale the boxes to. Each target size is expected to be in (height, width) format. + + Returns: + `torch.Tensor` of shape `(batch_size, num_boxes, 4)`: Scaled bounding boxes. + """ + import torch + + if isinstance(target_sizes, (list, tuple)): + image_height = torch.tensor([i[0] for i in target_sizes]) + image_width = torch.tensor([i[1] for i in target_sizes]) + elif isinstance(target_sizes, torch.Tensor): + image_height, image_width = target_sizes.unbind(1) + else: + raise TypeError("`target_sizes` must be a list, tuple or torch.Tensor") + + scale_factor = torch.stack([image_width, image_height, image_width, image_height], dim=1) + scale_factor = scale_factor.unsqueeze(1).to(boxes.device) + boxes = boxes * scale_factor + return boxes + + +def box_area(boxes): + """ + Computes the area of a set of bounding boxes, which are specified by its (x1, y1, x2, y2) coordinates. + + Args: + boxes (`torch.FloatTensor` of shape `(number_of_boxes, 4)`): + Boxes for which the area will be computed. They are expected to be in (x1, y1, x2, y2) format with `0 <= x1 + < x2` and `0 <= y1 < y2`. + Returns: + `torch.FloatTensor`: a tensor containing the area for each box. + """ + boxes = _upcast(boxes) + return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) + + +# Copied from transformers.models.owlvit.image_processing_owlvit.box_iou +def box_iou(boxes1, boxes2): + import torch + + area1 = box_area(boxes1) + area2 = box_area(boxes2) + + left_top = torch.max(boxes1[:, None, :2], boxes2[:, :2]) # [N,M,2] + right_bottom = torch.min(boxes1[:, None, 2:], boxes2[:, 2:]) # [N,M,2] + + width_height = (right_bottom - left_top).clamp(min=0) # [N,M,2] + inter = width_height[:, :, 0] * width_height[:, :, 1] # [N,M] + + union = area1[:, None] + area2 - inter + + iou = inter / union + return iou, union if TYPE_CHECKING: diff --git a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py index 3f9862325060..7f2e19a64492 100644 --- a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py +++ b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py @@ -13,10 +13,16 @@ # limitations under the License. """Image processor class for Pix2Struct.""" +import io import math +import textwrap import numpy as np -from PIL import Image +from huggingface_hub import hf_hub_download +from PIL import Image, ImageDraw, ImageFont + + +DEFAULT_FONT_PATH = "ybelkada/fonts" from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature, get_size_dict @@ -121,27 +127,30 @@ def render_text( return img # Copied from transformers.models.pix2struct.image_processing_pix2struct.torch_extract_patches -# Disable as it causes issues with torch.compile -@torch.compiler.disable -def torch_extract_patches(image_tensor, patch_height, patch_width): - """ - Extract patches from image tensor. Returns tensor of shape (batch, rows, columns, patch_height*patch_width*channels). +if is_torch_available(): + import torch - Args: - image_tensor (`torch.Tensor`): - Image tensor of shape (batch, channels, height, width). - patch_height (`int`): - Height of patches to extract. - patch_width (`int`): - Width of patches to extract. - """ - batch_size, channels, height, width = image_tensor.shape - patches = torch.nn.functional.unfold(image_tensor, (patch_height, patch_width), stride=(patch_height, patch_width)) - patches = patches.reshape(batch_size, channels, patch_height, patch_width, -1) - patches = patches.permute(0, 4, 2, 3, 1).reshape( - batch_size, height // patch_height, width // patch_width, channels * patch_height * patch_width - ) - return patches + # Disable as it causes issues with torch.compile + @torch.compiler.disable + def torch_extract_patches(image_tensor, patch_height, patch_width): + """ + Extract patches from image tensor. Returns tensor of shape (batch, rows, columns, patch_height*patch_width*channels). + + Args: + image_tensor (`torch.Tensor`): + Image tensor of shape (batch, channels, height, width). + patch_height (`int`): + Height of patches to extract. + patch_width (`int`): + Width of patches to extract. + """ + batch_size, channels, height, width = image_tensor.shape + patches = torch.nn.functional.unfold(image_tensor, (patch_height, patch_width), stride=(patch_height, patch_width)) + patches = patches.reshape(batch_size, channels, patch_height, patch_width, -1) + patches = patches.permute(0, 4, 2, 3, 1).reshape( + batch_size, height // patch_height, width // patch_width, channels * patch_height * patch_width + ) + return patches @auto_docstring @requires(backends=("torch",)) diff --git a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py index f4bf9aa8a46b..cfda324199c3 100644 --- a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py +++ b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py @@ -13,6 +13,8 @@ # limitations under the License. """Image processor class for ZoeDepth.""" +import math +from collections.abc import Iterable from typing import TYPE_CHECKING, Union import numpy as np @@ -31,18 +33,16 @@ get_image_size, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torchvision_available, requires_backends +from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available, requires_backends from ...utils.import_utils import requires if TYPE_CHECKING: from .modeling_zoedepth import ZoeDepthDepthEstimatorOutput -from collections.abc import Iterable - -import torch -from torch import nn - +if is_torch_available(): + import torch + from torch import nn if is_torchvision_available(): import torchvision.transforms.v2.functional as tvF From 678dbc56e8c480ac73d8a361fa3cc9a5a0d62f34 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 16:53:54 +0100 Subject: [PATCH 15/30] up --- src/transformers/image_processing_backends.py | 4 +- .../models/aria/image_processing_pil_aria.py | 6 +- .../models/beit/image_processing_pil_beit.py | 2 +- .../image_processing_pil_bridgetower.py | 18 ++--- .../image_processing_pil_conditional_detr.py | 51 ++++++++++++- .../convnext/image_processing_pil_convnext.py | 4 +- ...image_processing_pil_deepseek_vl_hybrid.py | 6 +- .../modular_deepseek_vl_hybrid.py | 8 +-- .../image_processing_pil_deformable_detr.py | 2 +- .../models/detr/image_processing_pil_detr.py | 52 +++++++++++++- .../donut/image_processing_pil_donut.py | 4 +- .../models/dpt/image_processing_pil_dpt.py | 6 +- .../image_processing_pil_efficientloftr.py | 13 +++- .../image_processing_pil_efficientnet.py | 2 +- .../models/eomt/image_processing_pil_eomt.py | 2 +- .../image_processing_pil_ernie4_5_vl_moe.py | 14 ++-- .../modular_ernie4_5_vl_moe.py | 4 +- .../flava/image_processing_pil_flava.py | 15 ++-- .../models/fuyu/image_processing_pil_fuyu.py | 30 +++++--- .../gemma3/image_processing_pil_gemma3.py | 2 +- .../glm46v/image_processing_pil_glm46v.py | 4 +- .../glm4v/image_processing_pil_glm4v.py | 4 +- .../image_processing_pil_glm_image.py | 3 +- .../models/glpn/image_processing_pil_glpn.py | 4 +- .../got_ocr2/image_processing_pil_got_ocr2.py | 40 ++++++++++- .../image_processing_pil_grounding_dino.py | 2 +- .../idefics2/image_processing_pil_idefics2.py | 11 ++- .../idefics3/image_processing_pil_idefics3.py | 22 +++--- .../imagegpt/image_processing_pil_imagegpt.py | 2 +- .../image_processing_pil_layoutlmv2.py | 27 ++++++- .../image_processing_pil_layoutlmv3.py | 25 ++++++- .../levit/image_processing_pil_levit.py | 2 +- .../image_processing_pil_lightglue.py | 13 +++- .../llava/image_processing_pil_llava.py | 2 +- .../image_processing_pil_llava_next.py | 2 +- .../image_processing_pil_llava_onevision.py | 2 +- .../modular_llava_onevision.py | 4 +- .../image_processing_pil_mask2former.py | 48 ++++++++++++- .../image_processing_pil_maskformer.py | 48 ++++++++++++- .../mllama/image_processing_pil_mllama.py | 12 +++- .../image_processing_pil_mobilenet_v2.py | 2 +- .../image_processing_pil_mobilevit.py | 2 +- .../nougat/image_processing_pil_nougat.py | 4 +- .../image_processing_pil_oneformer.py | 71 +++++++++++++++++-- .../ovis2/image_processing_pil_ovis2.py | 43 ++++++++++- .../owlv2/image_processing_pil_owlv2.py | 4 +- .../models/owlv2/modular_owlv2.py | 8 +-- .../image_processing_pil_paddleocr_vl.py | 3 +- .../paddleocr_vl/modular_paddleocr_vl.py | 2 +- .../image_processing_pil_perceiver.py | 2 +- .../image_processing_pil_pix2struct.py | 6 +- .../pixtral/image_processing_pil_pixtral.py | 27 ++++++- .../image_processing_pil_poolformer.py | 4 +- ...ge_processing_pil_prompt_depth_anything.py | 4 +- .../qwen2_vl/image_processing_pil_qwen2_vl.py | 3 +- .../rt_detr/image_processing_pil_rt_detr.py | 2 +- .../models/rt_detr/modular_rt_detr.py | 4 +- .../models/sam/image_processing_pil_sam.py | 18 +++-- .../image_processing_pil_segformer.py | 2 +- .../models/segformer/modular_segformer.py | 4 +- .../seggpt/image_processing_pil_seggpt.py | 2 +- .../siglip2/image_processing_pil_siglip2.py | 2 +- .../smolvlm/image_processing_pil_smolvlm.py | 11 ++- .../image_processing_pil_superglue.py | 13 +++- .../image_processing_pil_superpoint.py | 2 +- .../swin2sr/image_processing_pil_swin2sr.py | 2 +- .../textnet/image_processing_pil_textnet.py | 4 +- .../models/tvp/image_processing_pil_tvp.py | 2 +- .../image_processing_pil_video_llama_3.py | 3 +- .../video_llama_3/modular_video_llama_3.py | 2 +- .../videomae/image_processing_pil_videomae.py | 2 +- .../models/vilt/image_processing_pil_vilt.py | 4 +- .../vitpose/image_processing_pil_vitpose.py | 7 +- .../yolos/image_processing_pil_yolos.py | 2 +- .../zoedepth/image_processing_pil_zoedepth.py | 2 +- 75 files changed, 636 insertions(+), 161 deletions(-) diff --git a/src/transformers/image_processing_backends.py b/src/transformers/image_processing_backends.py index da29f74c6384..3c291687e1b1 100644 --- a/src/transformers/image_processing_backends.py +++ b/src/transformers/image_processing_backends.py @@ -529,7 +529,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: Union["PILImageResampling", "tvF.InterpolationMode", int] | None = None, + resample: "PILImageResampling | None" = None, reducing_gap: int | None = None, **kwargs, ) -> np.ndarray: @@ -628,7 +628,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: Union["PILImageResampling", "tvF.InterpolationMode", int] | None, + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/aria/image_processing_pil_aria.py b/src/transformers/models/aria/image_processing_pil_aria.py index bdec33c04ace..778c6f0b8af2 100644 --- a/src/transformers/models/aria/image_processing_pil_aria.py +++ b/src/transformers/models/aria/image_processing_pil_aria.py @@ -81,7 +81,7 @@ def _resize_for_patching( self, image: np.ndarray, target_resolution: tuple, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", ) -> np.ndarray: """Resize an image to a target resolution while maintaining aspect ratio.""" new_height, new_width = get_patch_output_size( @@ -106,7 +106,7 @@ def get_image_patches( image: np.ndarray, grid_pinpoints: list[list[int]], patch_size: int, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", ) -> list[np.ndarray]: """ Process an image with variable resolutions by dividing it into patches. @@ -147,7 +147,7 @@ def _preprocess( min_image_size: int = 336, split_resolutions: list[list[int]] | None = None, split_image: bool = False, - resample: "PILImageResampling | int | None" = None, + resample: "PILImageResampling | None" = None, **kwargs, ) -> BatchFeature: if max_image_size not in [490, 980]: diff --git a/src/transformers/models/beit/image_processing_pil_beit.py b/src/transformers/models/beit/image_processing_pil_beit.py index a026b76f4162..77868a08809d 100644 --- a/src/transformers/models/beit/image_processing_pil_beit.py +++ b/src/transformers/models/beit/image_processing_pil_beit.py @@ -131,7 +131,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py b/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py index 24ba073a4421..97f5853a3e7c 100644 --- a/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py +++ b/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py @@ -13,7 +13,6 @@ # limitations under the License. """Image processor class for BridgeTower.""" -from typing import Union import numpy as np @@ -29,6 +28,11 @@ ) from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available +from ...utils.import_utils import requires + + +if is_torch_available(): + import torch # Copied from transformers.models.bridgetower.image_processing_bridgetower.BridgeTowerImageProcessorKwargs @@ -42,16 +46,13 @@ class BridgeTowerImageProcessorKwargs(ImagesKwargs, total=False): # Copied from transformers.models.bridgetower.image_processing_bridgetower.get_resize_output_image_size def get_resize_output_image_size( - input_image: Union[np.ndarray, "torch.Tensor"], + input_image: np.ndarray | torch.Tensor, shorter: int = 800, longer: int = 1333, size_divisor: int = 32, ) -> tuple[int, int]: """Get output image size after resizing with size_divisor.""" - if is_torch_available(): - import torch - - if is_torch_available() and isinstance(input_image, torch.Tensor): + if isinstance(input_image, torch.Tensor): input_height, input_width = input_image.shape[-2:] else: input_height, input_width = get_image_size(input_image, channel_dim=ChannelDimension.FIRST) @@ -79,6 +80,7 @@ def get_resize_output_image_size( @auto_docstring +@requires(backends=("vision", "torch")) class BridgeTowerImageProcessorPil(PilBackend): """PIL backend for BridgeTower with custom resize and center_crop.""" @@ -105,7 +107,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", size_divisor: int = 32, **kwargs, ) -> np.ndarray: @@ -130,7 +132,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py index 711003ba889f..2f9ae8858d16 100644 --- a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py +++ b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py @@ -258,6 +258,50 @@ class ConditionalDetrImageProcessorKwargs(ImagesKwargs, total=False): format: str | AnnotationFormat do_convert_annotations: bool +# Copied from transformers.models.conditional_detr.image_processing_conditional_detr.binary_mask_to_rle +def binary_mask_to_rle(mask): + """ + Converts given binary mask of shape `(height, width)` to the run-length encoding (RLE) format. + + Args: + mask (`torch.Tensor` or `numpy.array`): + A binary mask tensor of shape `(height, width)` where 0 denotes background and 1 denotes the target + segment_id or class_id. + Returns: + `List`: Run-length encoded list of the binary mask. Refer to COCO API for more information about the RLE + format. + """ + from ...utils import is_torch_tensor + + if is_torch_tensor(mask): + mask = mask.numpy() + + pixels = mask.flatten() + pixels = np.concatenate([[0], pixels, [0]]) + runs = np.where(pixels[1:] != pixels[:-1])[0] + 1 + runs[1::2] -= runs[::2] + return list(runs) + + +# Copied from transformers.models.conditional_detr.image_processing_conditional_detr.check_segment_validity +def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overlap_mask_area_threshold=0.8): + # Get the mask associated with the k class + mask_k = mask_labels == k + mask_k_area = mask_k.sum() + + # Compute the area of all the stuff in query k + original_area = (mask_probs[k] >= mask_threshold).sum() + mask_exists = mask_k_area > 0 and original_area > 0 + + # Eliminate disconnected tiny segments + if mask_exists: + area_ratio = mask_k_area / original_area + if not area_ratio.item() > overlap_mask_area_threshold: + mask_exists = False + + return mask_exists, mask_k + + # Copied from transformers.models.conditional_detr.image_processing_conditional_detr.compute_segments def compute_segments( mask_probs, @@ -268,6 +312,9 @@ def compute_segments( label_ids_to_fuse: set[int] | None = None, target_size: tuple[int, int] | None = None, ): + import torch + from torch import nn + height = mask_probs.shape[1] if target_size is None else target_size[0] width = mask_probs.shape[2] if target_size is None else target_size[1] @@ -329,6 +376,8 @@ def convert_segmentation_to_rle(segmentation): Returns: `list[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. """ + import torch + segment_ids = torch.unique(segmentation) run_length_encodings = [] @@ -664,7 +713,7 @@ def _preprocess( masks_path: str | pathlib.Path | None, do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/convnext/image_processing_pil_convnext.py b/src/transformers/models/convnext/image_processing_pil_convnext.py index e4db381bccd0..0293cb4daa1e 100644 --- a/src/transformers/models/convnext/image_processing_pil_convnext.py +++ b/src/transformers/models/convnext/image_processing_pil_convnext.py @@ -62,7 +62,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", crop_pct: float = 224 / 256, **kwargs, ) -> np.ndarray: @@ -103,7 +103,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py index 48579911e545..2523b255f388 100644 --- a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py +++ b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py @@ -170,13 +170,13 @@ def pad_to_square( def _preprocess( self, - images: list["torch.Tensor"], + images: list[np.ndarray], do_resize: bool, size: SizeDict, high_res_size: SizeDict, min_size: int, - resample: "PILImageResampling | int | None", - high_res_resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", + high_res_resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/deepseek_vl_hybrid/modular_deepseek_vl_hybrid.py b/src/transformers/models/deepseek_vl_hybrid/modular_deepseek_vl_hybrid.py index e76a9a04f1de..082524aa550d 100644 --- a/src/transformers/models/deepseek_vl_hybrid/modular_deepseek_vl_hybrid.py +++ b/src/transformers/models/deepseek_vl_hybrid/modular_deepseek_vl_hybrid.py @@ -535,8 +535,8 @@ def _preprocess( size: SizeDict, high_res_size: SizeDict, min_size: int, - resample: "PILImageResampling | int | None", - high_res_resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", + high_res_resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, @@ -646,8 +646,8 @@ def _preprocess( size: SizeDict, high_res_size: SizeDict, min_size: int, - resample: "PILImageResampling | int | None", - high_res_resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", + high_res_resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py index 0dc5570cffb0..f4dceff5966a 100644 --- a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py +++ b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py @@ -551,7 +551,7 @@ def _preprocess( masks_path: str | pathlib.Path | None, do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/detr/image_processing_pil_detr.py b/src/transformers/models/detr/image_processing_pil_detr.py index ddc5a0c0cdba..2a1dc588801c 100644 --- a/src/transformers/models/detr/image_processing_pil_detr.py +++ b/src/transformers/models/detr/image_processing_pil_detr.py @@ -24,6 +24,7 @@ PaddingMode, center_to_corners_format, corners_to_center_format, + get_size_with_aspect_ratio, pad, resize, safe_squeeze, @@ -255,6 +256,50 @@ class DetrImageProcessorKwargs(ImagesKwargs, total=False): format: str | AnnotationFormat do_convert_annotations: bool +# Copied from transformers.models.detr.image_processing_detr.binary_mask_to_rle +def binary_mask_to_rle(mask): + """ + Converts given binary mask of shape `(height, width)` to the run-length encoding (RLE) format. + + Args: + mask (`torch.Tensor` or `numpy.array`): + A binary mask tensor of shape `(height, width)` where 0 denotes background and 1 denotes the target + segment_id or class_id. + Returns: + `List`: Run-length encoded list of the binary mask. Refer to COCO API for more information about the RLE + format. + """ + from ...utils import is_torch_tensor + + if is_torch_tensor(mask): + mask = mask.numpy() + + pixels = mask.flatten() + pixels = np.concatenate([[0], pixels, [0]]) + runs = np.where(pixels[1:] != pixels[:-1])[0] + 1 + runs[1::2] -= runs[::2] + return list(runs) + + +# Copied from transformers.models.detr.image_processing_detr.check_segment_validity +def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overlap_mask_area_threshold=0.8): + # Get the mask associated with the k class + mask_k = mask_labels == k + mask_k_area = mask_k.sum() + + # Compute the area of all the stuff in query k + original_area = (mask_probs[k] >= mask_threshold).sum() + mask_exists = mask_k_area > 0 and original_area > 0 + + # Eliminate disconnected tiny segments + if mask_exists: + area_ratio = mask_k_area / original_area + if not area_ratio.item() > overlap_mask_area_threshold: + mask_exists = False + + return mask_exists, mask_k + + # Copied from transformers.models.detr.image_processing_detr.compute_segments def compute_segments( mask_probs, @@ -265,6 +310,9 @@ def compute_segments( label_ids_to_fuse: set[int] | None = None, target_size: tuple[int, int] | None = None, ): + import torch + from torch import nn + height = mask_probs.shape[1] if target_size is None else target_size[0] width = mask_probs.shape[2] if target_size is None else target_size[1] @@ -326,6 +374,8 @@ def convert_segmentation_to_rle(segmentation): Returns: `list[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. """ + import torch + segment_ids = torch.unique(segmentation) run_length_encodings = [] @@ -661,7 +711,7 @@ def _preprocess( masks_path: str | pathlib.Path | None, do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/donut/image_processing_pil_donut.py b/src/transformers/models/donut/image_processing_pil_donut.py index cfdcc870d875..d74f43be5734 100644 --- a/src/transformers/models/donut/image_processing_pil_donut.py +++ b/src/transformers/models/donut/image_processing_pil_donut.py @@ -140,7 +140,7 @@ def thumbnail( self, image: np.ndarray, size: SizeDict, - resample: "PILImageResampling | int | None" = None, + resample: "PILImageResampling | None" = None, **kwargs, ) -> np.ndarray: """Resize the image to make a thumbnail.""" @@ -171,7 +171,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/dpt/image_processing_pil_dpt.py b/src/transformers/models/dpt/image_processing_pil_dpt.py index d868e4547564..e6ac3ccbde32 100644 --- a/src/transformers/models/dpt/image_processing_pil_dpt.py +++ b/src/transformers/models/dpt/image_processing_pil_dpt.py @@ -63,7 +63,7 @@ class DPTImageProcessorKwargs(ImagesKwargs, total=False): # Copied from transformers.models.dpt.image_processing_dpt.get_resize_output_image_size def get_resize_output_image_size( - input_image: "torch.Tensor", + input_image: np.ndarray, output_size: int | Iterable[int], keep_aspect_ratio: bool, multiple: int, @@ -188,7 +188,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, ensure_multiple_of: int = 1, keep_aspect_ratio: bool = False, **kwargs, @@ -229,7 +229,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py index f8c25293caf0..1479ae3795dc 100644 --- a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py @@ -12,7 +12,16 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature -from ...image_utils import ImageInput, PILImageResampling, SizeDict, to_numpy_array +from ...image_utils import ( + ImageInput, + ImageType, + PILImageResampling, + SizeDict, + get_image_type, + is_pil_image, + is_valid_image, + to_numpy_array, +) from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available from ...utils.import_utils import requires @@ -124,7 +133,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_rescale: bool, rescale_factor: float, return_tensors: str | TensorType | None, diff --git a/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py b/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py index fb86c4662055..552648fd16ba 100644 --- a/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py +++ b/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py @@ -79,7 +79,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/eomt/image_processing_pil_eomt.py b/src/transformers/models/eomt/image_processing_pil_eomt.py index a2d38818446b..b5bccc825b73 100644 --- a/src/transformers/models/eomt/image_processing_pil_eomt.py +++ b/src/transformers/models/eomt/image_processing_pil_eomt.py @@ -368,7 +368,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py index 7d652771060a..0d8c9ba745e2 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py @@ -18,19 +18,15 @@ # See the License for the specific language governing permissions and # limitations under the License. +import math + import numpy as np from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available, logging - - -if is_torch_available(): - import torch -if is_torchvision_available(): - from torchvision.transforms.v2 import functional as tvF +from ...utils import TensorType, auto_docstring, logging logger = logging.get_logger(__name__) @@ -122,10 +118,10 @@ def _standardize_kwargs(self, **kwargs) -> dict: def _preprocess( self, - images: list["torch.Tensor"], + images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/ernie4_5_vl_moe/modular_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/modular_ernie4_5_vl_moe.py index aa7a22ff3579..ad47bc0508a3 100644 --- a/src/transformers/models/ernie4_5_vl_moe/modular_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/modular_ernie4_5_vl_moe.py @@ -1237,10 +1237,10 @@ class Ernie4_5_VLMoeImageProcessorPil(Glm4vImageProcessorPil): def _preprocess( self, - images: list["torch.Tensor"], + images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/flava/image_processing_pil_flava.py b/src/transformers/models/flava/image_processing_pil_flava.py index 815c3ec63c43..ba03da8d1c1c 100644 --- a/src/transformers/models/flava/image_processing_pil_flava.py +++ b/src/transformers/models/flava/image_processing_pil_flava.py @@ -13,6 +13,8 @@ # limitations under the License. """Image processor class for Flava.""" +import math +import random from collections.abc import Iterable from functools import lru_cache from typing import Any @@ -33,9 +35,14 @@ from ...utils import ( TensorType, auto_docstring, + is_torch_available, ) +if is_torch_available(): + import torch + + # Copied from transformers.models.flava.image_processing_flava.FLAVA_CODEBOOK_MEAN FLAVA_CODEBOOK_MEAN = [0.0, 0.0, 0.0] @@ -304,7 +311,7 @@ def _standardize_kwargs( codebook_crop_size: int | Iterable[int] | dict[str, int] | SizeDict | None = None, codebook_image_mean: float | list[float] | None = None, codebook_image_std: float | list[float] | None = None, - codebook_resample: "PILImageResampling | int | None" = None, + codebook_resample: "PILImageResampling | None" = None, data_format: ChannelDimension | None = None, **kwargs, ) -> dict: @@ -344,7 +351,7 @@ def _preprocess_image( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, @@ -377,7 +384,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, @@ -397,7 +404,7 @@ def _preprocess( return_codebook_pixels: bool | None, codebook_do_resize: bool | None, codebook_size: SizeDict | None, - codebook_resample: "PILImageResampling | int | None", + codebook_resample: "PILImageResampling | None", codebook_do_center_crop: bool | None, codebook_crop_size: SizeDict | None, codebook_do_rescale: bool | None, diff --git a/src/transformers/models/fuyu/image_processing_pil_fuyu.py b/src/transformers/models/fuyu/image_processing_pil_fuyu.py index a36903ba8f91..f4bc7bd671f5 100644 --- a/src/transformers/models/fuyu/image_processing_pil_fuyu.py +++ b/src/transformers/models/fuyu/image_processing_pil_fuyu.py @@ -14,15 +14,32 @@ """Image processor class for Fuyu.""" import math +from typing import TYPE_CHECKING import numpy as np + from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature, get_size_dict -from ...image_utils import ImageInput, PILImageResampling, SizeDict, get_image_size +from ...image_utils import ( + ImageInput, + PILImageResampling, + SizeDict, + get_image_size, + is_valid_image, + make_list_of_images, +) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, requires_backends, is_torch_available +from ...utils import TensorType, auto_docstring, is_torch_available, requires_backends from ...utils.import_utils import requires + +if TYPE_CHECKING: + import torch + +if is_torch_available(): + import torch + + # Copied from transformers.models.fuyu.image_processing_fuyu.FuyuBatchFeature class FuyuBatchFeature(BatchFeature): """ @@ -199,7 +216,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: PILImageResampling | int | None = None, + resample: PILImageResampling | None = None, **kwargs, ) -> np.ndarray: """ @@ -234,7 +251,7 @@ def _preprocess( images: list[list[np.ndarray]], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, @@ -444,9 +461,6 @@ def preprocess_with_tokenizer_info( requires_backends(self, ["torch"]) import torch -if is_torch_available(): - import torch - if patch_size is None: if isinstance(self.patch_size, SizeDict): patch_size = self.patch_size @@ -559,4 +573,4 @@ def _standardize_kwargs(self, patch_size: dict[str, int] | SizeDict | None = Non kwargs["patch_size"] = patch_size return kwargs -__all__ = ["FuyuImageProcessorPil"] \ No newline at end of file +__all__ = ["FuyuImageProcessorPil"] diff --git a/src/transformers/models/gemma3/image_processing_pil_gemma3.py b/src/transformers/models/gemma3/image_processing_pil_gemma3.py index 06896a4fffa0..2225e65325cb 100644 --- a/src/transformers/models/gemma3/image_processing_pil_gemma3.py +++ b/src/transformers/models/gemma3/image_processing_pil_gemma3.py @@ -174,7 +174,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/glm46v/image_processing_pil_glm46v.py b/src/transformers/models/glm46v/image_processing_pil_glm46v.py index 13934edd4bb2..e9bd5a239e80 100644 --- a/src/transformers/models/glm46v/image_processing_pil_glm46v.py +++ b/src/transformers/models/glm46v/image_processing_pil_glm46v.py @@ -18,6 +18,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import math + import numpy as np from ...image_processing_backends import PilBackend @@ -124,7 +126,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/glm4v/image_processing_pil_glm4v.py b/src/transformers/models/glm4v/image_processing_pil_glm4v.py index 44f4506c6fc5..f296b23e0f18 100644 --- a/src/transformers/models/glm4v/image_processing_pil_glm4v.py +++ b/src/transformers/models/glm4v/image_processing_pil_glm4v.py @@ -13,6 +13,8 @@ # limitations under the License. """PIL Image processor class for GLM-4.1V.""" +import math + import numpy as np from ...image_processing_backends import PilBackend @@ -127,7 +129,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/glm_image/image_processing_pil_glm_image.py b/src/transformers/models/glm_image/image_processing_pil_glm_image.py index ea990d04b69b..eb3b701e3322 100644 --- a/src/transformers/models/glm_image/image_processing_pil_glm_image.py +++ b/src/transformers/models/glm_image/image_processing_pil_glm_image.py @@ -18,6 +18,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import math from collections.abc import Iterable import numpy as np @@ -154,7 +155,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/glpn/image_processing_pil_glpn.py b/src/transformers/models/glpn/image_processing_pil_glpn.py index 4dd193d84589..d66cb3818573 100644 --- a/src/transformers/models/glpn/image_processing_pil_glpn.py +++ b/src/transformers/models/glpn/image_processing_pil_glpn.py @@ -70,7 +70,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, size_divisor: int = 32, **kwargs, ) -> np.ndarray: @@ -85,7 +85,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py b/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py index 77401575be77..cf311198297a 100644 --- a/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py +++ b/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py @@ -54,6 +54,42 @@ class GotOcr2ImageProcessorKwargs(ImagesKwargs, total=False): min_patches: int max_patches: int +# Copied from transformers.models.got_ocr2.image_processing_got_ocr2.get_all_supported_aspect_ratios +@lru_cache(maxsize=10) +def get_all_supported_aspect_ratios(min_image_tiles: int, max_image_tiles: int) -> list[tuple[int, int]]: + """ + Computes all allowed aspect ratios for a given minimum and maximum number of input tiles. + + This function calculates all possible arrangements of tiles that can be formed + within the constraint of the minimum and maximum number of tiles. Each arrangement is + represented by its aspect ratio (width/height) and the corresponding tile configuration. + + Args: + min_image_tiles (`int`): + The minimum number of tiles allowed. + max_image_tiles (`int`): + The maximum number of tiles allowed. + + Returns: + `list[tuple[int, int]]`: A list of tuples, each tuple representing a valid (width, height) + configuration in terms of number of tiles. + + Example: + >>> get_all_supported_aspect_ratios(1, 4) + [(1, 1), (1, 2), (2, 1), (1, 3), (3, 1), (1, 4), (2, 2), (4, 1)] + + """ + aspect_ratios = [] + for width in range(1, max_image_tiles + 1): + for height in range(1, max_image_tiles + 1): + if width * height <= max_image_tiles and width * height >= min_image_tiles: + aspect_ratios.append((width, height)) + + aspect_ratios = sorted(aspect_ratios, key=lambda x: x[0] * x[1]) + + return aspect_ratios + + # Copied from transformers.models.got_ocr2.image_processing_got_ocr2.get_optimal_tiled_canvas @lru_cache(maxsize=100) def get_optimal_tiled_canvas( @@ -119,7 +155,7 @@ def crop_image_to_patches( max_patches: int, use_thumbnail: bool = True, patch_size: SizeDict | None = None, - resample: "PILImageResampling | int | None" = None, + resample: "PILImageResampling | None" = None, ): """ Crop the image to patches and return a list of cropped images. @@ -188,7 +224,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py index 34cde0a420c3..bf7f34087d60 100644 --- a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py +++ b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py @@ -587,7 +587,7 @@ def _preprocess( masks_path: str | pathlib.Path | None, do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/idefics2/image_processing_pil_idefics2.py b/src/transformers/models/idefics2/image_processing_pil_idefics2.py index 40dac81f46ae..ec50dc02d5bd 100644 --- a/src/transformers/models/idefics2/image_processing_pil_idefics2.py +++ b/src/transformers/models/idefics2/image_processing_pil_idefics2.py @@ -13,7 +13,10 @@ # limitations under the License. """PIL Image processor class for Idefics2.""" +from typing import TYPE_CHECKING + import numpy as np +from PIL import Image from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature @@ -27,7 +30,11 @@ make_nested_list_of_images, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring +from ...utils import TensorType, auto_docstring, is_vision_available + + +if TYPE_CHECKING: + import torch def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: @@ -201,7 +208,7 @@ def _preprocess( images: list[list[np.ndarray]], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/idefics3/image_processing_pil_idefics3.py b/src/transformers/models/idefics3/image_processing_pil_idefics3.py index daf41c4b5f8d..4e98b5fe4d1d 100644 --- a/src/transformers/models/idefics3/image_processing_pil_idefics3.py +++ b/src/transformers/models/idefics3/image_processing_pil_idefics3.py @@ -14,6 +14,7 @@ """PIL Image processor class for Idefics3.""" import math +from typing import TYPE_CHECKING import numpy as np @@ -32,6 +33,10 @@ from ...utils import TensorType, auto_docstring +if TYPE_CHECKING: + import torch + + def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: """Make pixel mask: 1=valid, 0=padding. Images are CHW.""" h, w = image.shape[-2:] @@ -127,8 +132,7 @@ def _resize_output_size_scale_below_upper_bound( width = max(width, 1) return height, width -# Copied from transformers.models.idefics3.image_processing_idefics3.get_max_height_width -def get_max_height_width(images_list: list[list["torch.Tensor|np.ndarray"]]) -> tuple[int, int]: +def get_max_height_width(images_list: list[list[np.ndarray]]) -> tuple[int, int]: """ Get the maximum height and width across all images in a batch. """ @@ -141,8 +145,7 @@ def get_max_height_width(images_list: list[list["torch.Tensor|np.ndarray"]]) -> max_width = max(size[1] for size in image_sizes) return (max_height, max_width) -# Copied from transformers.models.idefics3.image_processing_idefics3.get_num_channels -def get_num_channels(images_list: list[list["torch.Tensor|np.ndarray"]]) -> int: +def get_num_channels(images_list: list[list[np.ndarray]]) -> int: """ Get the number of channels across all images in a batch. Handle empty sublists like in [[], [image]]. """ @@ -152,15 +155,14 @@ def get_num_channels(images_list: list[list["torch.Tensor|np.ndarray"]]) -> int: raise ValueError("No images found in the batch.") -# Copied from transformers.models.idefics3.image_processing_idefics3.get_resize_output_image_size def get_resize_output_image_size( - image: "torch.Tensor", + image: np.ndarray, resolution_max_side: int, ) -> tuple[int, int]: """ Get the output size of the image after resizing given a dictionary specifying the max and min sizes. Args: - image (`torch.Tensor`): + image (`np.ndarray`): Image to resize. resolution_max_side (`int`): The longest edge of the image will be resized to this value. The shortest edge will be resized to keep the @@ -224,7 +226,7 @@ def split_images( self, image: np.ndarray, max_image_size: dict[str, int], - resample: "PILImageResampling | int | None" = None, + resample: "PILImageResampling | None" = None, ): """Split an image into patches (mirrors TorchvisionBackend.split_images). Images are always CHW.""" num_channels, height, width = image.shape @@ -259,7 +261,7 @@ def resize_for_vision_encoder( self, image: np.ndarray, vision_encoder_max_size: int, - resample: "PILImageResampling | int | None" = None, + resample: "PILImageResampling | None" = None, ): """Resize images to be multiples of vision_encoder_max_size. Images are always CHW.""" height, width = image.shape[-2:] @@ -313,7 +315,7 @@ def _preprocess( images: list[list[np.ndarray]], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py b/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py index 3e091bc0c87d..3cd904da53b4 100644 --- a/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py +++ b/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py @@ -96,7 +96,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py b/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py index a2b460e4d5a3..34aee542a0d2 100644 --- a/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py +++ b/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py @@ -13,11 +13,13 @@ # limitations under the License. """Image processor class for LayoutLMv2.""" +from typing import TYPE_CHECKING + import numpy as np from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature -from ...image_transforms import flip_channel_order +from ...image_transforms import flip_channel_order, to_pil_image from ...image_utils import ( ChannelDimension, ImageInput, @@ -28,6 +30,15 @@ from ...utils import TensorType, auto_docstring, requires_backends +if TYPE_CHECKING: + import torch + +try: + import pytesseract +except ImportError: + pytesseract = None + + # Copied from transformers.models.layoutlmv2.image_processing_layoutlmv2.LayoutLMv2ImageProcessorKwargs class LayoutLMv2ImageProcessorKwargs(ImagesKwargs, total=False): r""" @@ -47,9 +58,19 @@ class LayoutLMv2ImageProcessorKwargs(ImagesKwargs, total=False): ocr_lang: str | None tesseract_config: str | None +# Copied from transformers.models.layoutlmv2.image_processing_layoutlmv2.normalize_box +def normalize_box(box, width, height): + return [ + int(1000 * (box[0] / width)), + int(1000 * (box[1] / height)), + int(1000 * (box[2] / width)), + int(1000 * (box[3] / height)), + ] + + # Copied from transformers.models.layoutlmv2.image_processing_layoutlmv2.apply_tesseract def apply_tesseract( - image: "np.ndarray | torch.Tensor", + image: np.ndarray, lang: str | None, tesseract_config: str | None = None, input_data_format: str | ChannelDimension | None = None, @@ -118,7 +139,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", return_tensors: str | TensorType | None, apply_ocr: bool = True, ocr_lang: str | None = None, diff --git a/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py b/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py index e1b796710d63..421be1056b55 100644 --- a/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py +++ b/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py @@ -13,11 +13,13 @@ # limitations under the License. """Image processor class for LayoutLMv3.""" +from typing import TYPE_CHECKING + import numpy as np from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature -from ...image_transforms import ChannelDimension +from ...image_transforms import to_pil_image from ...image_utils import ( IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, @@ -30,6 +32,15 @@ from ...utils import TensorType, auto_docstring, requires_backends +if TYPE_CHECKING: + import torch + +try: + import pytesseract +except ImportError: + pytesseract = None + + # Copied from transformers.models.layoutlmv3.image_processing_layoutlmv3.LayoutLMv3ImageProcessorKwargs class LayoutLMv3ImageProcessorKwargs(ImagesKwargs, total=False): r""" @@ -49,6 +60,16 @@ class LayoutLMv3ImageProcessorKwargs(ImagesKwargs, total=False): ocr_lang: str | None tesseract_config: str | None +# Copied from transformers.models.layoutlmv3.image_processing_layoutlmv3.normalize_box +def normalize_box(box, width, height): + return [ + int(1000 * (box[0] / width)), + int(1000 * (box[1] / height)), + int(1000 * (box[2] / width)), + int(1000 * (box[3] / height)), + ] + + # Copied from transformers.models.layoutlmv3.image_processing_layoutlmv3.apply_tesseract def apply_tesseract( image: "np.ndarray | torch.Tensor", @@ -123,7 +144,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/levit/image_processing_pil_levit.py b/src/transformers/models/levit/image_processing_pil_levit.py index d793d805ed47..4ddd8ce44830 100644 --- a/src/transformers/models/levit/image_processing_pil_levit.py +++ b/src/transformers/models/levit/image_processing_pil_levit.py @@ -51,7 +51,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: "PILImageResampling | int | None" = None, + resample: "PILImageResampling | None" = None, **kwargs, ) -> np.ndarray: """Resize: shortest_edge is rescaled to int((256/224) * shortest_edge).""" diff --git a/src/transformers/models/lightglue/image_processing_pil_lightglue.py b/src/transformers/models/lightglue/image_processing_pil_lightglue.py index 9c22e3f83ac3..ad1c9c8eec2c 100644 --- a/src/transformers/models/lightglue/image_processing_pil_lightglue.py +++ b/src/transformers/models/lightglue/image_processing_pil_lightglue.py @@ -25,7 +25,16 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature -from ...image_utils import ImageInput, PILImageResampling, SizeDict, to_numpy_array +from ...image_utils import ( + ImageInput, + ImageType, + PILImageResampling, + SizeDict, + get_image_type, + is_pil_image, + is_valid_image, + to_numpy_array, +) from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available from ...utils.import_utils import requires @@ -137,7 +146,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_rescale: bool, rescale_factor: float, return_tensors: str | TensorType | None, diff --git a/src/transformers/models/llava/image_processing_pil_llava.py b/src/transformers/models/llava/image_processing_pil_llava.py index 3dccb4f3ac03..d953f45b8c97 100644 --- a/src/transformers/models/llava/image_processing_pil_llava.py +++ b/src/transformers/models/llava/image_processing_pil_llava.py @@ -95,7 +95,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/llava_next/image_processing_pil_llava_next.py b/src/transformers/models/llava_next/image_processing_pil_llava_next.py index b46c7fb5c8b2..f11e034080f1 100644 --- a/src/transformers/models/llava_next/image_processing_pil_llava_next.py +++ b/src/transformers/models/llava_next/image_processing_pil_llava_next.py @@ -165,7 +165,7 @@ def _preprocess( do_resize: bool, size: SizeDict, image_grid_pinpoints: list[list[int]], - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py index 3904e800ca39..18a381dbf221 100644 --- a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py +++ b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py @@ -173,7 +173,7 @@ def _preprocess( do_resize: bool, size: SizeDict, image_grid_pinpoints: list[list[int]], - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/llava_onevision/modular_llava_onevision.py b/src/transformers/models/llava_onevision/modular_llava_onevision.py index 602de8a4aaf4..f44a4612cdc2 100644 --- a/src/transformers/models/llava_onevision/modular_llava_onevision.py +++ b/src/transformers/models/llava_onevision/modular_llava_onevision.py @@ -137,7 +137,7 @@ def _preprocess( do_resize: bool, size: SizeDict, image_grid_pinpoints: list[list[int]], - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, @@ -298,7 +298,7 @@ def _preprocess( do_resize: bool, size: SizeDict, image_grid_pinpoints: list[list[int]], - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/mask2former/image_processing_pil_mask2former.py b/src/transformers/models/mask2former/image_processing_pil_mask2former.py index 57280f896bd3..7f352bab42f7 100644 --- a/src/transformers/models/mask2former/image_processing_pil_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_pil_mask2former.py @@ -113,6 +113,50 @@ class Mask2FormerImageProcessorKwargs(ImagesKwargs, total=False): size_divisor: int pad_size: SizeDict | None +# Copied from transformers.models.mask2former.image_processing_mask2former.binary_mask_to_rle +def binary_mask_to_rle(mask): + """ + Converts given binary mask of shape `(height, width)` to the run-length encoding (RLE) format. + + Args: + mask (`torch.Tensor` or `numpy.array`): + A binary mask tensor of shape `(height, width)` where 0 denotes background and 1 denotes the target + segment_id or class_id. + Returns: + `List`: Run-length encoded list of the binary mask. Refer to COCO API for more information about the RLE + format. + """ + from ...utils import is_torch_tensor + + if is_torch_tensor(mask): + mask = mask.numpy() + + pixels = mask.flatten() + pixels = np.concatenate([[0], pixels, [0]]) + runs = np.where(pixels[1:] != pixels[:-1])[0] + 1 + runs[1::2] -= runs[::2] + return list(runs) + + +# Copied from transformers.models.mask2former.image_processing_mask2former.check_segment_validity +def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overlap_mask_area_threshold=0.8): + # Get the mask associated with the k class + mask_k = mask_labels == k + mask_k_area = mask_k.sum() + + # Compute the area of all the stuff in query k + original_area = (mask_probs[k] >= mask_threshold).sum() + mask_exists = mask_k_area > 0 and original_area > 0 + + # Eliminate disconnected tiny segments + if mask_exists: + area_ratio = mask_k_area / original_area + if not area_ratio.item() > overlap_mask_area_threshold: + mask_exists = False + + return mask_exists, mask_k + + # Copied from transformers.models.mask2former.image_processing_mask2former.compute_segments def compute_segments( mask_probs, @@ -278,7 +322,7 @@ def resize( image: np.ndarray, size: SizeDict, size_divisor: int = 0, - resample: PILImageResampling | int | None = None, + resample: PILImageResampling | None = None, **kwargs, ) -> np.ndarray: """ @@ -444,7 +488,7 @@ def _preprocess( size: SizeDict | None, pad_size: SizeDict | None, size_divisor: int | None, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_rescale: bool | None, rescale_factor: float | None, do_normalize: bool | None, diff --git a/src/transformers/models/maskformer/image_processing_pil_maskformer.py b/src/transformers/models/maskformer/image_processing_pil_maskformer.py index 80ea3f0a4b33..68c50471be55 100644 --- a/src/transformers/models/maskformer/image_processing_pil_maskformer.py +++ b/src/transformers/models/maskformer/image_processing_pil_maskformer.py @@ -105,6 +105,50 @@ class MaskFormerImageProcessorKwargs(ImagesKwargs, total=False): size_divisor: int pad_size: SizeDict | None +# Copied from transformers.models.maskformer.image_processing_maskformer.binary_mask_to_rle +def binary_mask_to_rle(mask): + """ + Converts given binary mask of shape `(height, width)` to the run-length encoding (RLE) format. + + Args: + mask (`torch.Tensor` or `numpy.array`): + A binary mask tensor of shape `(height, width)` where 0 denotes background and 1 denotes the target + segment_id or class_id. + Returns: + `List`: Run-length encoded list of the binary mask. Refer to COCO API for more information about the RLE + format. + """ + from ...utils import is_torch_tensor + + if is_torch_tensor(mask): + mask = mask.numpy() + + pixels = mask.flatten() + pixels = np.concatenate([[0], pixels, [0]]) + runs = np.where(pixels[1:] != pixels[:-1])[0] + 1 + runs[1::2] -= runs[::2] + return list(runs) + + +# Copied from transformers.models.maskformer.image_processing_maskformer.check_segment_validity +def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overlap_mask_area_threshold=0.8): + # Get the mask associated with the k class + mask_k = mask_labels == k + mask_k_area = mask_k.sum() + + # Compute the area of all the stuff in query k + original_area = (mask_probs[k] >= mask_threshold).sum() + mask_exists = mask_k_area > 0 and original_area > 0 + + # Eliminate disconnected tiny segments + if mask_exists: + area_ratio = mask_k_area / original_area + if not area_ratio.item() > overlap_mask_area_threshold: + mask_exists = False + + return mask_exists, mask_k + + # Copied from transformers.models.maskformer.image_processing_maskformer.compute_segments def compute_segments( mask_probs, @@ -271,7 +315,7 @@ def resize( image: np.ndarray, size: SizeDict, size_divisor: int = 0, - resample: PILImageResampling | int | None = None, + resample: PILImageResampling | None = None, **kwargs, ) -> np.ndarray: """ @@ -437,7 +481,7 @@ def _preprocess( size: SizeDict | None, pad_size: SizeDict | None, size_divisor: int | None, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_rescale: bool | None, rescale_factor: float | None, do_normalize: bool | None, diff --git a/src/transformers/models/mllama/image_processing_pil_mllama.py b/src/transformers/models/mllama/image_processing_pil_mllama.py index baffecb496c1..fc02dbdb381b 100644 --- a/src/transformers/models/mllama/image_processing_pil_mllama.py +++ b/src/transformers/models/mllama/image_processing_pil_mllama.py @@ -13,9 +13,11 @@ # limitations under the License. """PIL Image processor class for Mllama.""" +import math from functools import lru_cache import numpy as np +from PIL import Image from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature @@ -31,7 +33,7 @@ make_nested_list_of_images, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring +from ...utils import TensorType, auto_docstring, is_vision_available def split_to_tiles_np(image: np.ndarray, num_tiles_height: int, num_tiles_width: int) -> np.ndarray: @@ -167,6 +169,14 @@ class MllamaImageProcessorKwargs(ImagesKwargs, total=False): max_image_tiles: int +# Copied from transformers.models.mllama.image_processing_mllama._validate_size +def _validate_size(size: SizeDict) -> None: + if not (size.height and size.width): + raise ValueError(f"Argument `size` must be a dictionary with keys 'height' and 'width'. Got: {size}") + if size.height != size.width: + raise ValueError(f"Argument `size` must have the same height and width, got {size}") + + # Copied from transformers.models.mllama.image_processing_mllama._validate_mllama_preprocess_arguments def _validate_mllama_preprocess_arguments(do_resize, size, do_pad, max_image_tiles): if not do_pad: diff --git a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py index d78fcfde70df..ae37ab2ae067 100644 --- a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py +++ b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py @@ -145,7 +145,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py index 0871204d761b..0d3ecfddf185 100644 --- a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py +++ b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py @@ -158,7 +158,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/nougat/image_processing_pil_nougat.py b/src/transformers/models/nougat/image_processing_pil_nougat.py index 8b467cc3a1dd..c30d399fc40e 100644 --- a/src/transformers/models/nougat/image_processing_pil_nougat.py +++ b/src/transformers/models/nougat/image_processing_pil_nougat.py @@ -220,7 +220,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: "PILImageResampling | int | None" = None, + resample: "PILImageResampling | None" = None, reducing_gap: int | None = None, **kwargs, ) -> np.ndarray: @@ -256,7 +256,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/oneformer/image_processing_pil_oneformer.py b/src/transformers/models/oneformer/image_processing_pil_oneformer.py index 3d8d30e19a94..dd3a7bb5f469 100644 --- a/src/transformers/models/oneformer/image_processing_pil_oneformer.py +++ b/src/transformers/models/oneformer/image_processing_pil_oneformer.py @@ -13,6 +13,9 @@ # limitations under the License. """Image processor class for OneFormer.""" +import json +import os + import numpy as np from ...image_processing_backends import PilBackend @@ -30,12 +33,27 @@ get_max_height_width, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available, logging +from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available, logging from ...utils.import_utils import requires logger = logging.get_logger(__name__) +if is_torch_available(): + import torch + from torch import nn + +if is_torchvision_available(): + import torchvision.transforms.v2.functional as tvF + +try: + from huggingface_hub import hf_hub_download + from huggingface_hub.utils import RepositoryNotFoundError +except ImportError: + hf_hub_download = None + RepositoryNotFoundError = None + + def make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -46,11 +64,6 @@ def make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarr output_size (`Tuple[int, int]`): Output size of the mask. """ - -if is_torch_available(): - import torch - from torch import nn - input_height, input_width = get_image_size(image, channel_dim=ChannelDimension.FIRST) mask = np.zeros(output_size, dtype=np.int64) mask[:input_height, :input_width] = 1 @@ -80,6 +93,50 @@ class OneFormerImageProcessorKwargs(ImagesKwargs, total=False): ignore_index: int | None do_reduce_labels: bool +# Copied from transformers.models.oneformer.image_processing_oneformer.binary_mask_to_rle +def binary_mask_to_rle(mask): + """ + Converts given binary mask of shape `(height, width)` to the run-length encoding (RLE) format. + + Args: + mask (`torch.Tensor` or `numpy.array`): + A binary mask tensor of shape `(height, width)` where 0 denotes background and 1 denotes the target + segment_id or class_id. + Returns: + `List`: Run-length encoded list of the binary mask. Refer to COCO API for more information about the RLE + format. + """ + from ...utils import is_torch_tensor + + if is_torch_tensor(mask): + mask = mask.numpy() + + pixels = mask.flatten() + pixels = np.concatenate([[0], pixels, [0]]) + runs = np.where(pixels[1:] != pixels[:-1])[0] + 1 + runs[1::2] -= runs[::2] + return list(runs) + + +# Copied from transformers.models.oneformer.image_processing_oneformer.check_segment_validity +def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overlap_mask_area_threshold=0.8): + # Get the mask associated with the k class + mask_k = mask_labels == k + mask_k_area = mask_k.sum() + + # Compute the area of all the stuff in query k + original_area = (mask_probs[k] >= mask_threshold).sum() + mask_exists = mask_k_area > 0 and original_area > 0 + + # Eliminate disconnected tiny segments + if mask_exists: + area_ratio = mask_k_area / original_area + if not area_ratio.item() > overlap_mask_area_threshold: + mask_exists = False + + return mask_exists, mask_k + + # Copied from transformers.models.oneformer.image_processing_oneformer.compute_segments def compute_segments( mask_probs, @@ -307,7 +364,7 @@ def _preprocess( instance_id_to_semantic_id: list[dict[int, int]] | dict[int, int] | None, do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/ovis2/image_processing_pil_ovis2.py b/src/transformers/models/ovis2/image_processing_pil_ovis2.py index 12717a1ea3ba..db21675807b9 100644 --- a/src/transformers/models/ovis2/image_processing_pil_ovis2.py +++ b/src/transformers/models/ovis2/image_processing_pil_ovis2.py @@ -56,6 +56,45 @@ class Ovis2ImageProcessorKwargs(ImagesKwargs, total=False): max_patches: int use_covering_area_grid: bool +# Copied from transformers.models.ovis2.image_processing_ovis2.get_all_supported_aspect_ratios +@lru_cache(maxsize=10) +def get_all_supported_aspect_ratios(min_image_tiles: int, max_image_tiles: int) -> list[tuple[int, int]]: + """Computes all allowed aspect ratios for a given minimum and maximum number of input tiles.""" + aspect_ratios = [] + for width in range(1, max_image_tiles + 1): + for height in range(1, max_image_tiles + 1): + if width * height <= max_image_tiles and width * height >= min_image_tiles: + aspect_ratios.append((width, height)) + return sorted(aspect_ratios, key=lambda x: x[0] * x[1]) + + +# Copied from transformers.models.ovis2.image_processing_ovis2.compute_patch_covering_area +def compute_patch_covering_area(left: int, upper: int, right: int, lower: int, side: int) -> float: + w = right - left + h = lower - upper + w, h = max(w, h), min(w, h) + if w > side: + h = h / w * side + w = side + return w * h + + +# Copied from transformers.models.ovis2.image_processing_ovis2.split_image_into_grid +def split_image_into_grid(h: int, w: int, grid: tuple[int, int]) -> list[tuple[int, int, int, int]]: + row_height = h // grid[0] + col_width = w // grid[1] + return [ + ( + col * col_width, + row * row_height, + w if col == grid[1] - 1 else (col + 1) * col_width, + h if row == grid[0] - 1 else (row + 1) * row_height, + ) + for row in range(grid[0]) + for col in range(grid[1]) + ] + + # Copied from transformers.models.ovis2.image_processing_ovis2.get_min_tile_covering_grid @lru_cache(maxsize=100) def get_min_tile_covering_grid( @@ -144,7 +183,7 @@ def crop_image_to_patches( use_covering_area_grid: bool = True, covering_threshold: float = 0.9, patch_size: SizeDict | None = None, - resample: "PILImageResampling | int | None" = None, + resample: "PILImageResampling | None" = None, ): """ Crop the image to patches and return a list of cropped images. @@ -203,7 +242,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/owlv2/image_processing_pil_owlv2.py b/src/transformers/models/owlv2/image_processing_pil_owlv2.py index 15226a9257ff..a892217df468 100644 --- a/src/transformers/models/owlv2/image_processing_pil_owlv2.py +++ b/src/transformers/models/owlv2/image_processing_pil_owlv2.py @@ -433,10 +433,10 @@ def resize( def _preprocess( self, - images: list["torch.Tensor"], + images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | None", do_pad: bool, do_rescale: bool, rescale_factor: float, diff --git a/src/transformers/models/owlv2/modular_owlv2.py b/src/transformers/models/owlv2/modular_owlv2.py index b79b14d6c843..6a7729afd7c3 100644 --- a/src/transformers/models/owlv2/modular_owlv2.py +++ b/src/transformers/models/owlv2/modular_owlv2.py @@ -258,7 +258,7 @@ def _preprocess( images: list["torch.Tensor"], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | None", do_pad: bool, do_rescale: bool, rescale_factor: float, @@ -326,7 +326,7 @@ class Owlv2ImageProcessorPil(OwlViTImageProcessorPil): crop_size = None do_center_crop = None - def pad(self, image: "np.ndarray", constant_value: float = 0.0) -> "np.ndarray": + def pad(self, image: np.ndarray, constant_value: float = 0.0) -> np.ndarray: """ Pad an image with zeros to the given size. """ @@ -400,10 +400,10 @@ def resize( def _preprocess( self, - images: list["torch.Tensor"], + images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | None", do_pad: bool, do_rescale: bool, rescale_factor: float, diff --git a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py index 8757d51075af..d8281998449a 100644 --- a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py @@ -23,6 +23,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import math from collections.abc import Iterable import numpy as np @@ -146,7 +147,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/paddleocr_vl/modular_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/modular_paddleocr_vl.py index 3ba29cbb37ea..85fa2e822c75 100644 --- a/src/transformers/models/paddleocr_vl/modular_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/modular_paddleocr_vl.py @@ -142,7 +142,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/perceiver/image_processing_pil_perceiver.py b/src/transformers/models/perceiver/image_processing_pil_perceiver.py index 0be7552b241f..d1ab681929dd 100644 --- a/src/transformers/models/perceiver/image_processing_pil_perceiver.py +++ b/src/transformers/models/perceiver/image_processing_pil_perceiver.py @@ -76,7 +76,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py index 7f2e19a64492..12631c890927 100644 --- a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py +++ b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py @@ -21,9 +21,6 @@ from huggingface_hub import hf_hub_download from PIL import Image, ImageDraw, ImageFont - -DEFAULT_FONT_PATH = "ybelkada/fonts" - from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature, get_size_dict from ...image_transforms import to_channel_dimension_format, to_pil_image @@ -36,6 +33,9 @@ if is_torch_available(): import torch + +DEFAULT_FONT_PATH = "ybelkada/fonts" + # Copied from transformers.models.pix2struct.image_processing_pix2struct.Pix2StructImageProcessorKwargs class Pix2StructImageProcessorKwargs(ImagesKwargs, total=False): """ diff --git a/src/transformers/models/pixtral/image_processing_pil_pixtral.py b/src/transformers/models/pixtral/image_processing_pil_pixtral.py index 0bb8f96605e0..7d4ba7469bec 100644 --- a/src/transformers/models/pixtral/image_processing_pil_pixtral.py +++ b/src/transformers/models/pixtral/image_processing_pil_pixtral.py @@ -13,6 +13,8 @@ # limitations under the License. """Image processor class for Pixtral.""" +import math + import numpy as np from ...image_processing_backends import PilBackend @@ -38,6 +40,27 @@ class PixtralImageProcessorKwargs(ImagesKwargs, total=False): patch_size: dict[str, int] | int +# Copied from transformers.models.pixtral.image_processing_pixtral._num_image_tokens +def _num_image_tokens(image_size: tuple[int, int], patch_size: tuple[int, int]) -> int: + """ + Calculate the number of image tokens given the image size and patch size. + + Args: + image_size (`tuple[int, int]`): + The size of the image as `(height, width)`. + patch_size (`tuple[int, int]`): + The patch size as `(height, width)`. + + Returns: + `int`: The number of image tokens. + """ + height, width = image_size + patch_height, patch_width = patch_size if isinstance(patch_size, (tuple, list)) else (patch_size, patch_size) + num_width_tokens = (width - 1) // patch_width + 1 + num_height_tokens = (height - 1) // patch_height + 1 + return num_height_tokens, num_width_tokens + + # Copied from transformers.models.pixtral.image_processing_pixtral.get_resize_output_image_size def get_resize_output_image_size( input_image: ImageInput, @@ -107,7 +130,7 @@ def resize( image: np.ndarray, size: SizeDict, patch_size: SizeDict, - resample: "PILImageResampling | int | None" = None, + resample: "PILImageResampling | None" = None, **kwargs, ) -> np.ndarray: """ @@ -159,7 +182,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/poolformer/image_processing_pil_poolformer.py b/src/transformers/models/poolformer/image_processing_pil_poolformer.py index ad1426cefb59..e261cd9e0791 100644 --- a/src/transformers/models/poolformer/image_processing_pil_poolformer.py +++ b/src/transformers/models/poolformer/image_processing_pil_poolformer.py @@ -65,7 +65,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: "PILImageResampling | int | None" = None, + resample: "PILImageResampling | None" = None, crop_pct: float | None = None, **kwargs, ) -> np.ndarray: @@ -97,7 +97,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py index 0badb5a2a9f3..c7b66ba0b321 100644 --- a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py @@ -136,7 +136,7 @@ def resize_with_aspect_ratio( size: SizeDict, keep_aspect_ratio: bool = False, ensure_multiple_of: int = 1, - resample: PILImageResampling | int | None = None, + resample: PILImageResampling | None = None, ) -> np.ndarray: """ Resize an image to target size while optionally maintaining aspect ratio and ensuring dimensions are multiples. @@ -251,7 +251,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py b/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py index 50a83d1957a1..eccfce542a0e 100644 --- a/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py +++ b/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py @@ -13,6 +13,7 @@ # limitations under the License. """PIL Image processor class for Qwen2-VL.""" +import math from collections.abc import Iterable import numpy as np @@ -143,7 +144,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py index adb783d4c203..314403032ccf 100644 --- a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py @@ -412,7 +412,7 @@ def _preprocess( masks_path: str | pathlib.Path | None, do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/rt_detr/modular_rt_detr.py b/src/transformers/models/rt_detr/modular_rt_detr.py index d5783a83f3f9..6e67845e2655 100644 --- a/src/transformers/models/rt_detr/modular_rt_detr.py +++ b/src/transformers/models/rt_detr/modular_rt_detr.py @@ -247,7 +247,7 @@ def _preprocess( masks_path: str | pathlib.Path | None, do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, @@ -480,7 +480,7 @@ def _preprocess( masks_path: str | pathlib.Path | None, do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/sam/image_processing_pil_sam.py b/src/transformers/models/sam/image_processing_pil_sam.py index 3d914d4f7cc0..c65bd649684f 100644 --- a/src/transformers/models/sam/image_processing_pil_sam.py +++ b/src/transformers/models/sam/image_processing_pil_sam.py @@ -17,7 +17,7 @@ from collections.abc import Iterable from copy import deepcopy from itertools import product -from typing import Any, Optional +from typing import TYPE_CHECKING, Any, Optional import numpy as np @@ -40,6 +40,13 @@ if is_vision_available(): import PIL +if is_torch_available(): + import torch + +if TYPE_CHECKING: + import torch + + def get_resize_output_image_size( input_image: np.ndarray, longest_edge: int, @@ -138,7 +145,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: PILImageResampling | int | None = None, + resample: PILImageResampling | None = None, **kwargs, ) -> np.ndarray: """ @@ -220,7 +227,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, @@ -417,6 +424,9 @@ def post_process_masks( """ if not is_torch_available(): raise ImportError("PyTorch is required for post_process_masks") + import torch + import torch.nn.functional as F + pad_size = self.pad_size if pad_size is None else pad_size target_image_size = (pad_size["height"], pad_size["width"]) if isinstance(original_sizes, (torch.Tensor, np.ndarray)): @@ -713,8 +723,8 @@ def _post_process_for_mask_generation(rle_masks, iou_scores, mask_boxes, amg_cro if not is_torch_available(): raise ImportError("PyTorch is required for post_process_for_mask_generation") -if is_torch_available(): import torch + from torchvision.ops.boxes import batched_nms keep_by_nms = batched_nms( boxes=mask_boxes.float(), diff --git a/src/transformers/models/segformer/image_processing_pil_segformer.py b/src/transformers/models/segformer/image_processing_pil_segformer.py index 3ffb6b49f3c4..b26059259292 100644 --- a/src/transformers/models/segformer/image_processing_pil_segformer.py +++ b/src/transformers/models/segformer/image_processing_pil_segformer.py @@ -149,7 +149,7 @@ def _preprocess( self, images: list["np.ndarray"], do_reduce_labels: bool, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | None", do_resize: bool, do_rescale: bool, do_normalize: bool, diff --git a/src/transformers/models/segformer/modular_segformer.py b/src/transformers/models/segformer/modular_segformer.py index 1f2e80a7cc00..414dc58e8c52 100644 --- a/src/transformers/models/segformer/modular_segformer.py +++ b/src/transformers/models/segformer/modular_segformer.py @@ -102,7 +102,7 @@ def _preprocess( self, images: list["torch.Tensor"], do_reduce_labels: bool, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | None", do_resize: bool, do_rescale: bool, do_normalize: bool, @@ -209,7 +209,7 @@ def _preprocess( self, images: list["np.ndarray"], do_reduce_labels: bool, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | None", do_resize: bool, do_rescale: bool, do_normalize: bool, diff --git a/src/transformers/models/seggpt/image_processing_pil_seggpt.py b/src/transformers/models/seggpt/image_processing_pil_seggpt.py index 06b93236d933..b48cce71edcc 100644 --- a/src/transformers/models/seggpt/image_processing_pil_seggpt.py +++ b/src/transformers/models/seggpt/image_processing_pil_seggpt.py @@ -209,7 +209,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/siglip2/image_processing_pil_siglip2.py b/src/transformers/models/siglip2/image_processing_pil_siglip2.py index ff3567c74637..18f080b47fe5 100644 --- a/src/transformers/models/siglip2/image_processing_pil_siglip2.py +++ b/src/transformers/models/siglip2/image_processing_pil_siglip2.py @@ -145,7 +145,7 @@ def _preprocess( do_resize: bool, patch_size: int, max_num_patches: int, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py index e2025eb466e5..a8d61e41a2c9 100644 --- a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py +++ b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py @@ -20,6 +20,7 @@ # limitations under the License. import math +from typing import TYPE_CHECKING import numpy as np @@ -38,6 +39,10 @@ from ...utils import TensorType, auto_docstring +if TYPE_CHECKING: + import torch + + def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: """Make pixel mask: 1=valid, 0=padding. Images are CHW.""" h, w = image.shape[-2:] @@ -230,7 +235,7 @@ def split_images( self, image: np.ndarray, max_image_size: dict[str, int], - resample: "PILImageResampling | int | None" = None, + resample: "PILImageResampling | None" = None, ): """Split an image into patches (mirrors TorchvisionBackend.split_images). Images are always CHW.""" num_channels, height, width = image.shape @@ -265,7 +270,7 @@ def resize_for_vision_encoder( self, image: np.ndarray, vision_encoder_max_size: int, - resample: "PILImageResampling | int | None" = None, + resample: "PILImageResampling | None" = None, ): """Resize images to be multiples of vision_encoder_max_size. Images are always CHW.""" height, width = image.shape[-2:] @@ -319,7 +324,7 @@ def _preprocess( images: list[list[np.ndarray]], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/superglue/image_processing_pil_superglue.py b/src/transformers/models/superglue/image_processing_pil_superglue.py index efa531754dcb..5326863a5687 100644 --- a/src/transformers/models/superglue/image_processing_pil_superglue.py +++ b/src/transformers/models/superglue/image_processing_pil_superglue.py @@ -20,7 +20,16 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature -from ...image_utils import ImageInput, PILImageResampling, SizeDict, to_numpy_array +from ...image_utils import ( + ImageInput, + ImageType, + PILImageResampling, + SizeDict, + get_image_type, + is_pil_image, + is_valid_image, + to_numpy_array, +) from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring from ...utils.import_utils import requires @@ -130,7 +139,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_rescale: bool, rescale_factor: float, return_tensors: str | TensorType | None, diff --git a/src/transformers/models/superpoint/image_processing_pil_superpoint.py b/src/transformers/models/superpoint/image_processing_pil_superpoint.py index 0a6bb7e421f7..2788df829f22 100644 --- a/src/transformers/models/superpoint/image_processing_pil_superpoint.py +++ b/src/transformers/models/superpoint/image_processing_pil_superpoint.py @@ -86,7 +86,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_rescale: bool, rescale_factor: float, return_tensors: str | TensorType | None, diff --git a/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py b/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py index 2f95b12dac2f..96388b3a1d41 100644 --- a/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py +++ b/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py @@ -88,7 +88,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/textnet/image_processing_pil_textnet.py b/src/transformers/models/textnet/image_processing_pil_textnet.py index 15ad7a025210..b10e88c215da 100644 --- a/src/transformers/models/textnet/image_processing_pil_textnet.py +++ b/src/transformers/models/textnet/image_processing_pil_textnet.py @@ -71,7 +71,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", size_divisor: int = 32, **kwargs, ) -> np.ndarray: @@ -102,7 +102,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/tvp/image_processing_pil_tvp.py b/src/transformers/models/tvp/image_processing_pil_tvp.py index 01ab76f31ab0..e085a588a345 100644 --- a/src/transformers/models/tvp/image_processing_pil_tvp.py +++ b/src/transformers/models/tvp/image_processing_pil_tvp.py @@ -203,7 +203,7 @@ def _preprocess( images: list[list[np.ndarray]], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py index f42787e0aa9c..bb90a43b09e6 100644 --- a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py +++ b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py @@ -18,6 +18,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import math from collections.abc import Iterable import numpy as np @@ -146,7 +147,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/video_llama_3/modular_video_llama_3.py b/src/transformers/models/video_llama_3/modular_video_llama_3.py index 038422591fc0..c4a9e40bc8f0 100644 --- a/src/transformers/models/video_llama_3/modular_video_llama_3.py +++ b/src/transformers/models/video_llama_3/modular_video_llama_3.py @@ -1128,7 +1128,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/videomae/image_processing_pil_videomae.py b/src/transformers/models/videomae/image_processing_pil_videomae.py index b268c3aefa91..6fffe2e143bf 100644 --- a/src/transformers/models/videomae/image_processing_pil_videomae.py +++ b/src/transformers/models/videomae/image_processing_pil_videomae.py @@ -63,7 +63,7 @@ def _preprocess( images: list[list[np.ndarray]], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/vilt/image_processing_pil_vilt.py b/src/transformers/models/vilt/image_processing_pil_vilt.py index 0502e419bac4..14341b1ecab5 100644 --- a/src/transformers/models/vilt/image_processing_pil_vilt.py +++ b/src/transformers/models/vilt/image_processing_pil_vilt.py @@ -125,7 +125,7 @@ def resize( self, image: np.ndarray, size: SizeDict, - resample: "PILImageResampling | int | None" = None, + resample: "PILImageResampling | None" = None, size_divisor: int | None = None, ) -> np.ndarray: """ @@ -207,7 +207,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/vitpose/image_processing_pil_vitpose.py b/src/transformers/models/vitpose/image_processing_pil_vitpose.py index 680c09db0a8f..3deaf4f4e2df 100644 --- a/src/transformers/models/vitpose/image_processing_pil_vitpose.py +++ b/src/transformers/models/vitpose/image_processing_pil_vitpose.py @@ -14,6 +14,7 @@ """Image processor class for VitPose.""" import itertools +import math from typing import TYPE_CHECKING import numpy as np @@ -34,7 +35,9 @@ if is_scipy_available(): - pass + from scipy.ndimage import affine_transform, gaussian_filter + +inv = np.linalg.inv if TYPE_CHECKING: from .modeling_vitpose import VitPoseEstimatorOutput @@ -393,7 +396,7 @@ def _preprocess( images: list[np.ndarray], do_resize: bool, size: SizeDict, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/yolos/image_processing_pil_yolos.py b/src/transformers/models/yolos/image_processing_pil_yolos.py index d1493773d0a0..6965cbdbff0a 100644 --- a/src/transformers/models/yolos/image_processing_pil_yolos.py +++ b/src/transformers/models/yolos/image_processing_pil_yolos.py @@ -578,7 +578,7 @@ def _preprocess( masks_path: str | pathlib.Path | None, do_resize: bool, size: SizeDict, - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py index cfda324199c3..3c871243f6c4 100644 --- a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py +++ b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py @@ -206,7 +206,7 @@ def _preprocess( size: SizeDict, keep_aspect_ratio: bool | None, ensure_multiple_of: int | None, - resample: PILImageResampling | int | None, + resample: PILImageResampling | None, do_pad: bool, do_rescale: bool, rescale_factor: float | None, From 5a0e02d7aa85c0b0b807bb6bc4391fdf0159912f Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 16:56:16 +0100 Subject: [PATCH 16/30] more --- .../image_processing_pil_deepseek_vl_hybrid.py | 2 +- .../models/deepseek_vl_hybrid/modular_deepseek_vl_hybrid.py | 2 +- .../ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py | 1 + .../models/idefics3/image_processing_pil_idefics3.py | 2 +- .../models/layoutlmv2/image_processing_pil_layoutlmv2.py | 2 +- src/transformers/models/owlv2/image_processing_pil_owlv2.py | 2 +- src/transformers/models/rt_detr/image_processing_pil_rt_detr.py | 2 +- src/transformers/models/rt_detr/modular_rt_detr.py | 1 - 8 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py index 2523b255f388..ef877cffe2b3 100644 --- a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py +++ b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py @@ -39,7 +39,7 @@ if is_torch_available(): - import torch + pass # Copied from transformers.models.deepseek_vl_hybrid.image_processing_deepseek_vl_hybrid.DeepseekVLHybridImageProcessorKwargs diff --git a/src/transformers/models/deepseek_vl_hybrid/modular_deepseek_vl_hybrid.py b/src/transformers/models/deepseek_vl_hybrid/modular_deepseek_vl_hybrid.py index 082524aa550d..7a0b5dca38e0 100644 --- a/src/transformers/models/deepseek_vl_hybrid/modular_deepseek_vl_hybrid.py +++ b/src/transformers/models/deepseek_vl_hybrid/modular_deepseek_vl_hybrid.py @@ -530,7 +530,7 @@ def _standardize_kwargs( def _preprocess( self, - images: list["torch.Tensor"], + images: list[np.ndarray], do_resize: bool, size: SizeDict, high_res_size: SizeDict, diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py index 0d8c9ba745e2..4fd76c8e47af 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py @@ -28,6 +28,7 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, logging + logger = logging.get_logger(__name__) diff --git a/src/transformers/models/idefics3/image_processing_pil_idefics3.py b/src/transformers/models/idefics3/image_processing_pil_idefics3.py index 4e98b5fe4d1d..200dccde373b 100644 --- a/src/transformers/models/idefics3/image_processing_pil_idefics3.py +++ b/src/transformers/models/idefics3/image_processing_pil_idefics3.py @@ -34,7 +34,7 @@ if TYPE_CHECKING: - import torch + pass def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: diff --git a/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py b/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py index 34aee542a0d2..8dbad5a20973 100644 --- a/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py +++ b/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py @@ -31,7 +31,7 @@ if TYPE_CHECKING: - import torch + pass try: import pytesseract diff --git a/src/transformers/models/owlv2/image_processing_pil_owlv2.py b/src/transformers/models/owlv2/image_processing_pil_owlv2.py index a892217df468..a6d50484539c 100644 --- a/src/transformers/models/owlv2/image_processing_pil_owlv2.py +++ b/src/transformers/models/owlv2/image_processing_pil_owlv2.py @@ -92,7 +92,7 @@ def box_iou(boxes1, boxes2): if is_torch_available(): import torch if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF + pass def _preprocess_resize_output_shape(image, output_shape): diff --git a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py index 314403032ccf..61c45e44790d 100644 --- a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py @@ -60,7 +60,7 @@ if is_torch_available(): import torch if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF + pass SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) diff --git a/src/transformers/models/rt_detr/modular_rt_detr.py b/src/transformers/models/rt_detr/modular_rt_detr.py index 6e67845e2655..cd4e8faf3fc2 100644 --- a/src/transformers/models/rt_detr/modular_rt_detr.py +++ b/src/transformers/models/rt_detr/modular_rt_detr.py @@ -18,7 +18,6 @@ import numpy as np import torch import torch.nn.functional as F -import torchvision.transforms.v2.functional as tvF from torch import nn from ... import initialization as init From e8ea722d9c1db0d17d549866f27f44a366c367e3 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 16:57:45 +0100 Subject: [PATCH 17/30] fix repo --- .../models/aria/image_processing_pil_aria.py | 1 + .../models/beit/image_processing_pil_beit.py | 1 + .../image_processing_pil_bridgetower.py | 3 +- .../image_processing_pil_conditional_detr.py | 30 +++++-------- .../convnext/image_processing_pil_convnext.py | 1 + .../image_processing_pil_deepseek_vl.py | 16 ++----- .../image_processing_deepseek_vl_hybrid.py | 4 +- ...image_processing_pil_deepseek_vl_hybrid.py | 38 ++-------------- .../modular_deepseek_vl_hybrid.py | 1 + .../image_processing_pil_deformable_detr.py | 24 ++++------- .../models/detr/image_processing_pil_detr.py | 9 ++++ .../donut/image_processing_pil_donut.py | 1 + .../models/dpt/image_processing_pil_dpt.py | 2 + .../image_processing_pil_efficientloftr.py | 19 +++----- .../image_processing_pil_efficientnet.py | 1 + .../models/eomt/image_processing_pil_eomt.py | 7 +++ .../image_processing_pil_ernie4_5_vl_moe.py | 21 ++------- .../flava/image_processing_pil_flava.py | 3 ++ .../models/fuyu/image_processing_pil_fuyu.py | 4 ++ .../gemma3/image_processing_pil_gemma3.py | 1 + .../glm46v/image_processing_pil_glm46v.py | 21 ++------- .../glm4v/image_processing_pil_glm4v.py | 2 + .../image_processing_pil_glm_image.py | 26 ++--------- .../models/glm_image/processing_glm_image.py | 6 +-- .../models/glpn/image_processing_pil_glpn.py | 1 + .../got_ocr2/image_processing_pil_got_ocr2.py | 2 + .../image_processing_pil_grounding_dino.py | 26 +++++------ .../idefics2/image_processing_pil_idefics2.py | 4 ++ .../idefics3/image_processing_pil_idefics3.py | 7 +++ .../imagegpt/image_processing_pil_imagegpt.py | 3 ++ .../janus/image_processing_pil_janus.py | 1 + .../image_processing_pil_kosmos2_5.py | 4 ++ .../image_processing_pil_layoutlmv2.py | 2 + .../image_processing_pil_layoutlmv3.py | 2 + .../image_processing_pil_lightglue.py | 19 +++----- .../image_processing_pil_llava_next.py | 1 + .../image_processing_llava_onevision.py | 2 +- .../image_processing_pil_llava_onevision.py | 17 ++------ .../image_processing_pil_mask2former.py | 42 ++++-------------- .../image_processing_pil_maskformer.py | 7 +++ .../mllama/image_processing_pil_mllama.py | 9 ++++ .../image_processing_pil_mobilenet_v2.py | 1 + .../image_processing_pil_mobilevit.py | 1 + .../nougat/image_processing_pil_nougat.py | 1 + .../image_processing_pil_oneformer.py | 8 ++++ .../ovis2/image_processing_pil_ovis2.py | 3 ++ .../models/owlv2/image_processing_owlv2.py | 2 +- .../owlv2/image_processing_pil_owlv2.py | 34 ++++++--------- .../image_processing_pil_paddleocr_vl.py | 22 ++-------- .../image_processing_pil_pix2struct.py | 9 +++- .../pixtral/image_processing_pil_pixtral.py | 2 + .../image_processing_pil_poolformer.py | 1 + ...ge_processing_pil_prompt_depth_anything.py | 3 ++ .../qwen2_vl/image_processing_pil_qwen2_vl.py | 2 + .../rt_detr/image_processing_pil_rt_detr.py | 33 +++----------- .../rt_detr/image_processing_rt_detr.py | 4 +- .../models/sam/image_processing_pil_sam.py | 15 +++++++ .../image_processing_pil_segformer.py | 20 +++------ .../segformer/image_processing_segformer.py | 2 +- .../seggpt/image_processing_pil_seggpt.py | 2 + .../siglip2/image_processing_pil_siglip2.py | 3 ++ .../smolvlm/image_processing_pil_smolvlm.py | 43 ++++++------------- .../image_processing_pil_superglue.py | 4 ++ .../image_processing_pil_superpoint.py | 3 ++ .../swin2sr/image_processing_pil_swin2sr.py | 1 + .../textnet/image_processing_pil_textnet.py | 1 + .../models/tvp/image_processing_pil_tvp.py | 2 + .../image_processing_pil_video_llama_3.py | 26 ++--------- .../models/vilt/image_processing_pil_vilt.py | 4 ++ .../vitmatte/image_processing_pil_vitmatte.py | 1 + .../vitpose/image_processing_pil_vitpose.py | 8 ++++ .../yolos/image_processing_pil_yolos.py | 25 ++++------- .../zoedepth/image_processing_pil_zoedepth.py | 2 + 73 files changed, 284 insertions(+), 395 deletions(-) diff --git a/src/transformers/models/aria/image_processing_pil_aria.py b/src/transformers/models/aria/image_processing_pil_aria.py index 778c6f0b8af2..ba40f184fab7 100644 --- a/src/transformers/models/aria/image_processing_pil_aria.py +++ b/src/transformers/models/aria/image_processing_pil_aria.py @@ -226,4 +226,5 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non num_patches = 1 if not split_image else resized_height // max_image_size * resized_width // max_image_size return num_patches + __all__ = ["AriaImageProcessorPil"] diff --git a/src/transformers/models/beit/image_processing_pil_beit.py b/src/transformers/models/beit/image_processing_pil_beit.py index 77868a08809d..de05a3c889d4 100644 --- a/src/transformers/models/beit/image_processing_pil_beit.py +++ b/src/transformers/models/beit/image_processing_pil_beit.py @@ -205,4 +205,5 @@ def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] return semantic_segmentation + __all__ = ["BeitImageProcessorPil"] diff --git a/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py b/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py index 97f5853a3e7c..f939cb027960 100644 --- a/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py +++ b/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py @@ -13,7 +13,6 @@ # limitations under the License. """Image processor class for BridgeTower.""" - import numpy as np from ...image_processing_backends import PilBackend @@ -44,6 +43,7 @@ class BridgeTowerImageProcessorKwargs(ImagesKwargs, total=False): size_divisor: int + # Copied from transformers.models.bridgetower.image_processing_bridgetower.get_resize_output_image_size def get_resize_output_image_size( input_image: np.ndarray | torch.Tensor, @@ -173,4 +173,5 @@ def _preprocess( return BatchFeature(data=data, tensor_type=return_tensors) + __all__ = ["BridgeTowerImageProcessorPil"] diff --git a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py index 2f9ae8858d16..41b770e6f1f2 100644 --- a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py +++ b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py @@ -48,9 +48,10 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, logging, requires_backends from ...utils.import_utils import requires +from .image_processing_conditional_detr import ConditionalDetrImageProcessorKwargs if is_vision_available(): @@ -63,6 +64,7 @@ SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) + # inspired by https://github.com/facebookresearch/conditional_detr/blob/master/datasets/coco.py#L33 def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndarray: """ @@ -97,6 +99,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar return masks + # inspired by https://github.com/facebookresearch/conditional_detr/blob/master/datasets/coco.py#L50 def prepare_coco_detection_annotation( image, @@ -157,6 +160,7 @@ def prepare_coco_detection_annotation( return new_target + def masks_to_boxes(masks: np.ndarray) -> np.ndarray: """ Compute the bounding boxes around the provided panoptic segmentation masks. @@ -190,6 +194,7 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: return np.stack([x_min, y_min, x_max, y_max], 1) + # 2 functions below adapted from https://github.com/cocodataset/panopticapi/blob/master/panopticapi/utils.py # Copyright (c) 2018, Alexander Kirillov # All rights reserved. @@ -203,6 +208,7 @@ def rgb_to_id(color): return color[:, :, 0] + 256 * color[:, :, 1] + 256 * 256 * color[:, :, 2] return int(color[0] + 256 * color[1] + 256 * 256 * color[2]) + def prepare_coco_panoptic_annotation( image: np.ndarray, target: dict, @@ -244,21 +250,6 @@ def prepare_coco_panoptic_annotation( return new_target -# Copied from transformers.models.conditional_detr.image_processing_conditional_detr.ConditionalDetrImageProcessorKwargs -class ConditionalDetrImageProcessorKwargs(ImagesKwargs, total=False): - r""" - format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): - Data format of the annotations. One of "coco_detection" or "coco_panoptic". - do_convert_annotations (`bool`, *optional*, defaults to `True`): - Controls whether to convert the annotations to the format expected by the CONDITIONAL_DETR model. Converts the - bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. - Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. - """ - - format: str | AnnotationFormat - do_convert_annotations: bool - -# Copied from transformers.models.conditional_detr.image_processing_conditional_detr.binary_mask_to_rle def binary_mask_to_rle(mask): """ Converts given binary mask of shape `(height, width)` to the run-length encoding (RLE) format. @@ -283,7 +274,6 @@ def binary_mask_to_rle(mask): return list(runs) -# Copied from transformers.models.conditional_detr.image_processing_conditional_detr.check_segment_validity def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overlap_mask_area_threshold=0.8): # Get the mask associated with the k class mask_k = mask_labels == k @@ -302,7 +292,6 @@ def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overl return mask_exists, mask_k -# Copied from transformers.models.conditional_detr.image_processing_conditional_detr.compute_segments def compute_segments( mask_probs, pred_scores, @@ -365,7 +354,7 @@ def compute_segments( return segmentation, segments -# Copied from transformers.models.conditional_detr.image_processing_conditional_detr.convert_segmentation_to_rle + def convert_segmentation_to_rle(segmentation): """ Converts given segmentation map of shape `(height, width)` to the run-length encoding (RLE) format. @@ -388,7 +377,7 @@ def convert_segmentation_to_rle(segmentation): return run_length_encodings -# Copied from transformers.models.conditional_detr.image_processing_conditional_detr.remove_low_and_no_objects + def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): """ Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and @@ -1124,4 +1113,5 @@ def post_process_panoptic_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results + __all__ = ["ConditionalDetrImageProcessorPil"] diff --git a/src/transformers/models/convnext/image_processing_pil_convnext.py b/src/transformers/models/convnext/image_processing_pil_convnext.py index 0293cb4daa1e..0e9691d2cce7 100644 --- a/src/transformers/models/convnext/image_processing_pil_convnext.py +++ b/src/transformers/models/convnext/image_processing_pil_convnext.py @@ -132,4 +132,5 @@ def _preprocess( return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) + __all__ = ["ConvNextImageProcessorPil"] diff --git a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py index 3f5e554fd22a..d29296535277 100644 --- a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py +++ b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py @@ -18,6 +18,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + import numpy as np from ...image_processing_backends import PilBackend @@ -31,19 +32,9 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring - - -# Copied from transformers.models.deepseek_vl.image_processing_deepseek_vl.DeepseekVLImageProcessorKwargs -class DeepseekVLImageProcessorKwargs(ImagesKwargs, total=False): - r""" - min_size (`int`, *optional*, defaults to 14): - The minimum allowed size for the resized image. Ensures that neither the height nor width - falls below this value after resizing. - """ - - min_size: int +from .image_processing_deepseek_vl import DeepseekVLImageProcessorKwargs @auto_docstring @@ -170,4 +161,5 @@ def postprocess(self): """Applies post-processing to the decoded image tokens by reversing transformations applied during preprocessing.""" raise AttributeError("Not needed for DeepseekVL") + __all__ = ["DeepseekVLImageProcessorPil"] diff --git a/src/transformers/models/deepseek_vl_hybrid/image_processing_deepseek_vl_hybrid.py b/src/transformers/models/deepseek_vl_hybrid/image_processing_deepseek_vl_hybrid.py index e9b87784a241..867d61aeb230 100644 --- a/src/transformers/models/deepseek_vl_hybrid/image_processing_deepseek_vl_hybrid.py +++ b/src/transformers/models/deepseek_vl_hybrid/image_processing_deepseek_vl_hybrid.py @@ -173,8 +173,8 @@ def _preprocess( size: SizeDict, high_res_size: SizeDict, min_size: int, - resample: "PILImageResampling | int | None", - high_res_resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", + high_res_resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py index ef877cffe2b3..c7ef92dce05f 100644 --- a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py +++ b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py @@ -19,7 +19,6 @@ # limitations under the License. from collections.abc import Iterable -from typing import Union import numpy as np @@ -34,39 +33,9 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available - - -if is_torch_available(): - pass - - -# Copied from transformers.models.deepseek_vl_hybrid.image_processing_deepseek_vl_hybrid.DeepseekVLHybridImageProcessorKwargs -class DeepseekVLHybridImageProcessorKwargs(ImagesKwargs, total=False): - r""" - min_size (`int`, *optional*, defaults to 14): - The minimum allowed size for the resized image. Ensures that neither the height nor width - falls below this value after resizing. - high_res_size (`dict`, *optional*, defaults to `{"height": 1024, "width": 1024}`): - Size of the high resolution output image after resizing. Can be overridden by the `high_res_size` parameter in the `preprocess` - method. - high_res_resample (`PILImageResampling`, *optional*, defaults to `Resampling.BICUBIC`): - Resampling filter to use if resizing the image. Only has an effect if `do_resize` is set to `True`. Can be - overridden by the `high_res_resample` parameter in the `preprocess` method. - high_res_image_mean (`float` or `list[float]`, *optional*, defaults to `OPENAI_CLIP_MEAN`): - Mean to use if normalizing the high resolution image. This is a float or list of floats the length of the number of - channels in the image. Can be overridden by the `high_res_image_mean` parameter in the `preprocess` method. - high_res_image_std (`float` or `list[float]`, *optional*, defaults to `OPENAI_CLIP_STD`): - Standard deviation to use if normalizing the high resolution image. This is a float or list of floats the length of the - number of channels in the image. Can be overridden by the `high_res_image_std` parameter in the `preprocess` method. - """ - - min_size: int - high_res_size: dict - high_res_resample: Union["PILImageResampling", int] - high_res_image_mean: float | list[float] | tuple[float, ...] - high_res_image_std: float | list[float] | tuple[float, ...] +from ...processing_utils import Unpack +from ...utils import TensorType, auto_docstring +from .image_processing_deepseek_vl_hybrid import DeepseekVLHybridImageProcessorKwargs @auto_docstring @@ -262,4 +231,5 @@ def _standardize_kwargs( return kwargs + __all__ = ["DeepseekVLHybridImageProcessorPil"] diff --git a/src/transformers/models/deepseek_vl_hybrid/modular_deepseek_vl_hybrid.py b/src/transformers/models/deepseek_vl_hybrid/modular_deepseek_vl_hybrid.py index 7a0b5dca38e0..99d24c163562 100644 --- a/src/transformers/models/deepseek_vl_hybrid/modular_deepseek_vl_hybrid.py +++ b/src/transformers/models/deepseek_vl_hybrid/modular_deepseek_vl_hybrid.py @@ -16,6 +16,7 @@ from dataclasses import dataclass from typing import Union +import numpy as np import torch import torch.nn as nn from huggingface_hub.dataclasses import strict diff --git a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py index f4dceff5966a..dd66876deca4 100644 --- a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py +++ b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py @@ -47,9 +47,10 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available from ...utils.import_utils import requires, requires_backends +from .image_processing_deformable_detr import DeformableDetrImageProcessorKwargs if is_vision_available(): @@ -59,6 +60,7 @@ SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) + # inspired by https://github.com/facebookresearch/deformable_detr/blob/master/datasets/coco.py#L33 def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndarray: """ @@ -93,6 +95,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar return masks + # inspired by https://github.com/facebookresearch/deformable_detr/blob/master/datasets/coco.py#L50 def prepare_coco_detection_annotation( image, @@ -153,6 +156,7 @@ def prepare_coco_detection_annotation( return new_target + def masks_to_boxes(masks: np.ndarray) -> np.ndarray: """ Compute the bounding boxes around the provided panoptic segmentation masks. @@ -186,6 +190,7 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: return np.stack([x_min, y_min, x_max, y_max], 1) + # 2 functions below adapted from https://github.com/cocodataset/panopticapi/blob/master/panopticapi/utils.py # Copyright (c) 2018, Alexander Kirillov # All rights reserved. @@ -199,6 +204,7 @@ def rgb_to_id(color): return color[:, :, 0] + 256 * color[:, :, 1] + 256 * 256 * color[:, :, 2] return int(color[0] + 256 * color[1] + 256 * 256 * color[2]) + def prepare_coco_panoptic_annotation( image: np.ndarray, target: dict, @@ -240,21 +246,6 @@ def prepare_coco_panoptic_annotation( return new_target -# Copied from transformers.models.deformable_detr.image_processing_deformable_detr.DeformableDetrImageProcessorKwargs -class DeformableDetrImageProcessorKwargs(ImagesKwargs, total=False): - r""" - format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): - Data format of the annotations. One of "coco_detection" or "coco_panoptic". - do_convert_annotations (`bool`, *optional*, defaults to `True`): - Controls whether to convert the annotations to the format expected by the DEFORMABLE_DETR model. Converts the - bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. - Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. - """ - - format: str | AnnotationFormat - do_convert_annotations: bool - - @auto_docstring class DeformableDetrImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR @@ -730,4 +721,5 @@ def post_process_object_detection( return results + __all__ = ["DeformableDetrImageProcessorPil"] diff --git a/src/transformers/models/detr/image_processing_pil_detr.py b/src/transformers/models/detr/image_processing_pil_detr.py index 2a1dc588801c..264ba4c10bf4 100644 --- a/src/transformers/models/detr/image_processing_pil_detr.py +++ b/src/transformers/models/detr/image_processing_pil_detr.py @@ -61,6 +61,7 @@ SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) + # inspired by https://github.com/facebookresearch/detr/blob/master/datasets/coco.py#L33 def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndarray: """ @@ -95,6 +96,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar return masks + # inspired by https://github.com/facebookresearch/detr/blob/master/datasets/coco.py#L50 def prepare_coco_detection_annotation( image, @@ -155,6 +157,7 @@ def prepare_coco_detection_annotation( return new_target + def masks_to_boxes(masks: np.ndarray) -> np.ndarray: """ Compute the bounding boxes around the provided panoptic segmentation masks. @@ -188,6 +191,7 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: return np.stack([x_min, y_min, x_max, y_max], 1) + # 2 functions below adapted from https://github.com/cocodataset/panopticapi/blob/master/panopticapi/utils.py # Copyright (c) 2018, Alexander Kirillov # All rights reserved. @@ -201,6 +205,7 @@ def rgb_to_id(color): return color[:, :, 0] + 256 * color[:, :, 1] + 256 * 256 * color[:, :, 2] return int(color[0] + 256 * color[1] + 256 * 256 * color[2]) + def prepare_coco_panoptic_annotation( image: np.ndarray, target: dict, @@ -256,6 +261,7 @@ class DetrImageProcessorKwargs(ImagesKwargs, total=False): format: str | AnnotationFormat do_convert_annotations: bool + # Copied from transformers.models.detr.image_processing_detr.binary_mask_to_rle def binary_mask_to_rle(mask): """ @@ -363,6 +369,7 @@ def compute_segments( return segmentation, segments + # Copied from transformers.models.detr.image_processing_detr.convert_segmentation_to_rle def convert_segmentation_to_rle(segmentation): """ @@ -386,6 +393,7 @@ def convert_segmentation_to_rle(segmentation): return run_length_encodings + # Copied from transformers.models.detr.image_processing_detr.remove_low_and_no_objects def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): """ @@ -1124,4 +1132,5 @@ def post_process_panoptic_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results + __all__ = ["DetrImageProcessorPil"] diff --git a/src/transformers/models/donut/image_processing_pil_donut.py b/src/transformers/models/donut/image_processing_pil_donut.py index d74f43be5734..d693a9f9e855 100644 --- a/src/transformers/models/donut/image_processing_pil_donut.py +++ b/src/transformers/models/donut/image_processing_pil_donut.py @@ -208,4 +208,5 @@ def _preprocess( return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) + __all__ = ["DonutImageProcessorPil"] diff --git a/src/transformers/models/dpt/image_processing_pil_dpt.py b/src/transformers/models/dpt/image_processing_pil_dpt.py index e6ac3ccbde32..f418df82b763 100644 --- a/src/transformers/models/dpt/image_processing_pil_dpt.py +++ b/src/transformers/models/dpt/image_processing_pil_dpt.py @@ -61,6 +61,7 @@ class DPTImageProcessorKwargs(ImagesKwargs, total=False): keep_aspect_ratio: bool do_reduce_labels: bool + # Copied from transformers.models.dpt.image_processing_dpt.get_resize_output_image_size def get_resize_output_image_size( input_image: np.ndarray, @@ -307,4 +308,5 @@ def post_process_depth_estimation( results.append({"predicted_depth": depth}) return results + __all__ = ["DPTImageProcessorPil"] diff --git a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py index 1479ae3795dc..a8a1af8b3df0 100644 --- a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py @@ -5,7 +5,6 @@ # modular_efficientloftr.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 -from typing import TYPE_CHECKING import numpy as np from PIL import Image, ImageDraw @@ -22,22 +21,23 @@ is_valid_image, to_numpy_array, ) -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, is_torch_available from ...utils.import_utils import requires +from .image_processing_efficientloftr import EfficientLoFTRImageProcessorKwargs +from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput if is_torch_available(): import torch -if TYPE_CHECKING: - from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput def is_grayscale(image: np.ndarray): if image.shape[0] == 1: return True return np.all(image[0, ...] == image[1, ...]) and np.all(image[1, ...] == image[2, ...]) + def convert_to_grayscale(image: ImageInput) -> ImageInput: """ Converts an image to grayscale format using the NTSC formula. Only support numpy and PIL Image. @@ -66,16 +66,6 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: return image -# Copied from transformers.models.efficientloftr.image_processing_efficientloftr.EfficientLoFTRImageProcessorKwargs -class EfficientLoFTRImageProcessorKwargs(ImagesKwargs, total=False): - r""" - do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`): - Whether to convert the image to grayscale. Can be overridden by `do_grayscale` in the `preprocess` method. - """ - - do_grayscale: bool - -# Copied from transformers.models.efficientloftr.image_processing_efficientloftr.validate_and_format_image_pairs def validate_and_format_image_pairs(images: ImageInput): error_message = ( "Input images must be a one of the following :", @@ -277,4 +267,5 @@ def _get_color(self, score): b = 0 return (r, g, b) + __all__ = ["EfficientLoFTRImageProcessorPil"] diff --git a/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py b/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py index 552648fd16ba..043414c25c74 100644 --- a/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py +++ b/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py @@ -110,4 +110,5 @@ def _preprocess( processed_images.append(image) return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) + __all__ = ["EfficientNetImageProcessorPil"] diff --git a/src/transformers/models/eomt/image_processing_pil_eomt.py b/src/transformers/models/eomt/image_processing_pil_eomt.py index b5bccc825b73..6fa4a7d85484 100644 --- a/src/transformers/models/eomt/image_processing_pil_eomt.py +++ b/src/transformers/models/eomt/image_processing_pil_eomt.py @@ -38,6 +38,7 @@ if is_torch_available(): import torch + # Adapted from transformers.models.maskformer.image_processing_maskformer.convert_segmentation_map_to_binary_masks def convert_segmentation_map_to_binary_masks( segmentation_map: np.ndarray, @@ -75,6 +76,7 @@ def convert_segmentation_map_to_binary_masks( return binary_masks.astype(np.float32), labels.astype(np.int64) + # Copied from transformers.models.eomt.image_processing_eomt.check_segment_validity def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overlap_mask_area_threshold=0.8): # Get the mask associated with the k class @@ -113,6 +115,7 @@ class EomtImageProcessorKwargs(ImagesKwargs, total=False): do_split_image: bool ignore_index: int | None + # Copied from transformers.models.eomt.image_processing_eomt.compute_segments def compute_segments( mask_probs, @@ -167,6 +170,7 @@ def compute_segments( current_segment_id += 1 return segmentation, segments + # Copied from transformers.models.eomt.image_processing_eomt.get_target_size def get_target_size(size_dict: dict[str, int]) -> tuple[int, int]: """Returns the height and width from a size dict.""" @@ -175,6 +179,7 @@ def get_target_size(size_dict: dict[str, int]) -> tuple[int, int]: return target_height, target_width + # Copied from transformers.models.eomt.image_processing_eomt.remove_low_and_no_objects def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): """ @@ -203,6 +208,7 @@ def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_ return masks[to_keep], scores[to_keep], labels[to_keep] + @auto_docstring @requires(backends=("torch",)) class EomtImageProcessorPil(PilBackend): @@ -632,4 +638,5 @@ def post_process_instance_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results + __all__ = ["EomtImageProcessorPil"] diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py index 4fd76c8e47af..8aed9c816627 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py @@ -25,29 +25,14 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, logging +from .image_processing_ernie4_5_vl_moe import Ernie4_5_VLMoeImageProcessorKwargs logger = logging.get_logger(__name__) -# Copied from transformers.models.ernie4_5_vl_moe.image_processing_ernie4_5_vl_moe.Ernie4_5_VLMoeImageProcessorKwargs -class Ernie4_5_VLMoeImageProcessorKwargs(ImagesKwargs, total=False): - r""" - patch_size (`int`, *optional*, defaults to 14): - The spatial patch size of the vision encoder. - temporal_patch_size (`int`, *optional*): - The temporal patch size of the vision encoder. Unused in the image processor, only used for videos. - merge_size (`int`, *optional*, defaults to 2): - The merge size of the vision encoder to llm encoder. - """ - - patch_size: int - temporal_patch_size: int - merge_size: int - -# Copied from transformers.models.ernie4_5_vl_moe.image_processing_ernie4_5_vl_moe.smart_resize def smart_resize( height: int, width: int, factor: int = 28, min_pixels: int = 56 * 56, max_pixels: int = 14 * 14 * 4 * 1280 ): @@ -234,6 +219,7 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non grid_h, grid_w = resized_height // patch_size, resized_width // patch_size return grid_h * grid_w + class Ernie4_5_VL_MoeImageProcessorPil(Ernie4_5_VLMoeImageProcessorPil): def __init__(self, *args, **kwargs): logger.warning_once( @@ -241,4 +227,5 @@ def __init__(self, *args, **kwargs): ) super().__init__(*args, **kwargs) + __all__ = ["Ernie4_5_VL_MoeImageProcessorPil", "Ernie4_5_VLMoeImageProcessorPil"] diff --git a/src/transformers/models/flava/image_processing_pil_flava.py b/src/transformers/models/flava/image_processing_pil_flava.py index ba03da8d1c1c..65635ca479a8 100644 --- a/src/transformers/models/flava/image_processing_pil_flava.py +++ b/src/transformers/models/flava/image_processing_pil_flava.py @@ -59,6 +59,7 @@ # Copied from transformers.models.flava.image_processing_flava.LOGIT_LAPLACE_EPS LOGIT_LAPLACE_EPS: float = 0.1 + # Copied from transformers.models.flava.image_processing_flava.FlavaImageProcessorKwargs class FlavaImageProcessorKwargs(ImagesKwargs, total=False): """ @@ -144,6 +145,7 @@ class FlavaImageProcessorKwargs(ImagesKwargs, total=False): codebook_image_mean: float | Iterable[float] codebook_image_std: float | Iterable[float] + # Copied from transformers.models.flava.image_processing_flava.FlavaMaskingGenerator # Inspired from https://github.com/microsoft/unilm/blob/master/beit/masking_generator.py class FlavaMaskingGenerator: @@ -467,4 +469,5 @@ def _preprocess( return BatchFeature(data=data, tensor_type=return_tensors) + __all__ = ["FlavaImageProcessorPil"] diff --git a/src/transformers/models/fuyu/image_processing_pil_fuyu.py b/src/transformers/models/fuyu/image_processing_pil_fuyu.py index f4bc7bd671f5..199e5903d5fc 100644 --- a/src/transformers/models/fuyu/image_processing_pil_fuyu.py +++ b/src/transformers/models/fuyu/image_processing_pil_fuyu.py @@ -151,6 +151,7 @@ def _to(elem): self.data = new_data return self + # Copied from transformers.models.fuyu.image_processing_fuyu.FuyuImagesKwargs class FuyuImagesKwargs(ImagesKwargs, total=False): r""" @@ -166,6 +167,7 @@ class FuyuImagesKwargs(ImagesKwargs, total=False): padding_value: float padding_mode: str + # Copied from transformers.models.fuyu.image_processing_fuyu.make_list_of_list_of_images def make_list_of_list_of_images( images: list[list[ImageInput]] | list[ImageInput] | ImageInput, @@ -181,6 +183,7 @@ def make_list_of_list_of_images( raise ValueError("images must be a list of list of images or a list of images or an image.") + @auto_docstring @requires(backends=("torch",)) class FuyuImageProcessorPil(PilBackend): @@ -573,4 +576,5 @@ def _standardize_kwargs(self, patch_size: dict[str, int] | SizeDict | None = Non kwargs["patch_size"] = patch_size return kwargs + __all__ = ["FuyuImageProcessorPil"] diff --git a/src/transformers/models/gemma3/image_processing_pil_gemma3.py b/src/transformers/models/gemma3/image_processing_pil_gemma3.py index 2225e65325cb..4e8462870467 100644 --- a/src/transformers/models/gemma3/image_processing_pil_gemma3.py +++ b/src/transformers/models/gemma3/image_processing_pil_gemma3.py @@ -221,4 +221,5 @@ def _preprocess( data={"pixel_values": processed_images, "num_crops": num_crops}, tensor_type=return_tensors ) + __all__ = ["Gemma3ImageProcessorPil"] diff --git a/src/transformers/models/glm46v/image_processing_pil_glm46v.py b/src/transformers/models/glm46v/image_processing_pil_glm46v.py index e9bd5a239e80..034566192431 100644 --- a/src/transformers/models/glm46v/image_processing_pil_glm46v.py +++ b/src/transformers/models/glm46v/image_processing_pil_glm46v.py @@ -18,6 +18,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + import math import numpy as np @@ -25,26 +26,11 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring +from .image_processing_glm46v import Glm46VImageProcessorKwargs -# Copied from transformers.models.glm46v.image_processing_glm46v.Glm46VImageProcessorKwargs -class Glm46VImageProcessorKwargs(ImagesKwargs, total=False): - """ - patch_size (`int`, *optional*, defaults to 14): - The spatial patch size of the vision encoder. - temporal_patch_size (`int`, *optional*, defaults to 2): - The temporal patch size of the vision encoder. - merge_size (`int`, *optional*, defaults to 2): - The merge size of the vision encoder to llm encoder. - """ - - patch_size: int - temporal_patch_size: int - merge_size: int - -# Copied from transformers.models.glm46v.image_processing_glm46v.smart_resize def smart_resize( num_frames: int, height: int, @@ -258,4 +244,5 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non grid_h, grid_w = resized_height // patch_size, resized_width // patch_size return grid_h * grid_w + __all__ = ["Glm46VImageProcessorPil"] diff --git a/src/transformers/models/glm4v/image_processing_pil_glm4v.py b/src/transformers/models/glm4v/image_processing_pil_glm4v.py index f296b23e0f18..0be34b98efd4 100644 --- a/src/transformers/models/glm4v/image_processing_pil_glm4v.py +++ b/src/transformers/models/glm4v/image_processing_pil_glm4v.py @@ -84,6 +84,7 @@ class Glm4vImageProcessorKwargs(ImagesKwargs, total=False): temporal_patch_size: int merge_size: int + @auto_docstring class Glm4vImageProcessorPil(PilBackend): do_resize = True @@ -261,4 +262,5 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non grid_h, grid_w = resized_height // patch_size, resized_width // patch_size return grid_h * grid_w + __all__ = ["Glm4vImageProcessorPil"] diff --git a/src/transformers/models/glm_image/image_processing_pil_glm_image.py b/src/transformers/models/glm_image/image_processing_pil_glm_image.py index eb3b701e3322..0aaf95a9aaea 100644 --- a/src/transformers/models/glm_image/image_processing_pil_glm_image.py +++ b/src/transformers/models/glm_image/image_processing_pil_glm_image.py @@ -26,32 +26,11 @@ from ...feature_extraction_utils import BatchFeature from ...image_processing_backends import PilBackend from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring +from .image_processing_glm_image import GlmImageImageProcessorKwargs -# Copied from transformers.models.glm_image.image_processing_glm_image.GlmImageImageProcessorKwargs -class GlmImageImageProcessorKwargs(ImagesKwargs, total=False): - r""" - min_pixels (`int`, *optional*, defaults to `56 * 56`): - The min pixels of the image to resize the image. - max_pixels (`int`, *optional*, defaults to `28 * 28 * 1280`): - The max pixels of the image to resize the image. - patch_size (`int`, *optional*, defaults to 14): - The spatial patch size of the vision encoder. - temporal_patch_size (`int`, *optional*, defaults to 2): - The temporal patch size of the vision encoder. - merge_size (`int`, *optional*, defaults to 2): - The merge size of the vision encoder to llm encoder. - """ - - min_pixels: int - max_pixels: int - patch_size: int - temporal_patch_size: int - merge_size: int - -# Copied from transformers.models.glm_image.image_processing_glm_image.smart_resize def smart_resize( height: int, width: int, @@ -262,4 +241,5 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non grid_h, grid_w = resized_height // patch_size, resized_width // patch_size return grid_h * grid_w + __all__ = ["GlmImageImageProcessorPil"] diff --git a/src/transformers/models/glm_image/processing_glm_image.py b/src/transformers/models/glm_image/processing_glm_image.py index 3c89d1e908c5..07f4103a10a4 100644 --- a/src/transformers/models/glm_image/processing_glm_image.py +++ b/src/transformers/models/glm_image/processing_glm_image.py @@ -20,6 +20,8 @@ import math +import torch + from ...feature_extraction_utils import BatchFeature from ...image_utils import ImageInput from ...processing_utils import ImagesKwargs, ProcessingKwargs, ProcessorMixin, Unpack @@ -185,8 +187,6 @@ def __call__( is_text_to_image=is_text_to_image, ) ) - import torch - image_inputs["image_grid_thw"] = torch.cat(all_grids, dim=0) # Store images_per_sample for later use (add target images count) @@ -237,8 +237,6 @@ def _build_target_image_grid_thw( prev_token_w: int, is_text_to_image: bool = True, ): - import torch - if is_text_to_image: # Text-to-image: 2 target grids (large + small preview) return torch.tensor( diff --git a/src/transformers/models/glpn/image_processing_pil_glpn.py b/src/transformers/models/glpn/image_processing_pil_glpn.py index d66cb3818573..964f426e01e4 100644 --- a/src/transformers/models/glpn/image_processing_pil_glpn.py +++ b/src/transformers/models/glpn/image_processing_pil_glpn.py @@ -136,4 +136,5 @@ def post_process_depth_estimation( results.append({"predicted_depth": depth}) return results + __all__ = ["GLPNImageProcessorPil"] diff --git a/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py b/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py index cf311198297a..8d2395b35d14 100644 --- a/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py +++ b/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py @@ -54,6 +54,7 @@ class GotOcr2ImageProcessorKwargs(ImagesKwargs, total=False): min_patches: int max_patches: int + # Copied from transformers.models.got_ocr2.image_processing_got_ocr2.get_all_supported_aspect_ratios @lru_cache(maxsize=10) def get_all_supported_aspect_ratios(min_image_tiles: int, max_image_tiles: int) -> list[tuple[int, int]]: @@ -305,4 +306,5 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non return num_patches + __all__ = ["GotOcr2ImageProcessorPil"] diff --git a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py index bf7f34087d60..fbdbef4110b4 100644 --- a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py +++ b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py @@ -53,14 +53,16 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, requires_backends from ...utils.import_utils import requires +from .image_processing_grounding_dino import GroundingDinoImageProcessorKwargs if TYPE_CHECKING: from .modeling_grounding_dino import GroundingDinoObjectDetectionOutput + if is_vision_available(): import PIL.Image if is_torch_available(): @@ -68,6 +70,7 @@ SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) + # inspired by https://github.com/facebookresearch/grounding_dino/blob/master/datasets/coco.py#L33 def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndarray: """ @@ -102,6 +105,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar return masks + # inspired by https://github.com/facebookresearch/grounding_dino/blob/master/datasets/coco.py#L50 def prepare_coco_detection_annotation( image, @@ -162,6 +166,7 @@ def prepare_coco_detection_annotation( return new_target + def masks_to_boxes(masks: np.ndarray) -> np.ndarray: """ Compute the bounding boxes around the provided panoptic segmentation masks. @@ -195,6 +200,7 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: return np.stack([x_min, y_min, x_max, y_max], 1) + # 2 functions below adapted from https://github.com/cocodataset/panopticapi/blob/master/panopticapi/utils.py # Copyright (c) 2018, Alexander Kirillov # All rights reserved. @@ -208,6 +214,7 @@ def rgb_to_id(color): return color[:, :, 0] + 256 * color[:, :, 1] + 256 * 256 * color[:, :, 2] return int(color[0] + 256 * color[1] + 256 * 256 * color[2]) + def prepare_coco_panoptic_annotation( image: np.ndarray, target: dict, @@ -248,6 +255,7 @@ def prepare_coco_panoptic_annotation( return new_target + def _scale_boxes(boxes, target_sizes): """ Scale batch of bounding boxes to the target sizes. @@ -276,21 +284,6 @@ def _scale_boxes(boxes, target_sizes): return boxes -# Copied from transformers.models.grounding_dino.image_processing_grounding_dino.GroundingDinoImageProcessorKwargs -class GroundingDinoImageProcessorKwargs(ImagesKwargs, total=False): - r""" - format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): - Data format of the annotations. One of "coco_detection" or "coco_panoptic". - do_convert_annotations (`bool`, *optional*, defaults to `True`): - Controls whether to convert the annotations to the format expected by the GROUNDING_DINO model. Converts the - bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. - Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. - """ - - format: str | AnnotationFormat - do_convert_annotations: bool - - @auto_docstring class GroundingDinoImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR @@ -760,4 +753,5 @@ def post_process_object_detection( return results + __all__ = ["GroundingDinoImageProcessorPil"] diff --git a/src/transformers/models/idefics2/image_processing_pil_idefics2.py b/src/transformers/models/idefics2/image_processing_pil_idefics2.py index ec50dc02d5bd..26d276dbd33c 100644 --- a/src/transformers/models/idefics2/image_processing_pil_idefics2.py +++ b/src/transformers/models/idefics2/image_processing_pil_idefics2.py @@ -54,6 +54,7 @@ class Idefics2ImageProcessorKwargs(ImagesKwargs, total=False): do_image_splitting: bool + # Copied from transformers.models.idefics2.image_processing_idefics2.convert_to_rgb def convert_to_rgb(image: ImageInput) -> ImageInput: """ @@ -72,6 +73,7 @@ def convert_to_rgb(image: ImageInput) -> ImageInput: alpha_composite = alpha_composite.convert("RGB") return alpha_composite + # Copied from transformers.models.idefics2.image_processing_idefics2.get_max_height_width def get_max_height_width(images_list: list[list["torch.Tensor|np.ndarray"]]) -> tuple[int, int]: """ @@ -86,6 +88,7 @@ def get_max_height_width(images_list: list[list["torch.Tensor|np.ndarray"]]) -> max_width = max(size[1] for size in image_sizes) return (max_height, max_width) + # Copied from transformers.models.idefics2.image_processing_idefics2.get_resize_output_image_size def get_resize_output_image_size(image, size: SizeDict) -> tuple[int, int]: """ @@ -271,4 +274,5 @@ def _preprocess( return BatchFeature(data=data, tensor_type=return_tensors) + __all__ = ["Idefics2ImageProcessorPil"] diff --git a/src/transformers/models/idefics3/image_processing_pil_idefics3.py b/src/transformers/models/idefics3/image_processing_pil_idefics3.py index 200dccde373b..304fb1befa34 100644 --- a/src/transformers/models/idefics3/image_processing_pil_idefics3.py +++ b/src/transformers/models/idefics3/image_processing_pil_idefics3.py @@ -48,6 +48,7 @@ def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndar # Copied from transformers.models.idefics3.image_processing_idefics3.MAX_IMAGE_SIZE MAX_IMAGE_SIZE = 4096 # 4k resolution as absolute maximum + # Copied from transformers.models.idefics3.image_processing_idefics3.Idefics3ImageProcessorKwargs class Idefics3ImageProcessorKwargs(ImagesKwargs, total=False): """ @@ -64,6 +65,7 @@ class Idefics3ImageProcessorKwargs(ImagesKwargs, total=False): max_image_size: dict[str, int] return_row_col_info: bool + # Copied from transformers.models.idefics3.image_processing_idefics3._resize_output_size_rescale_to_max_len def _resize_output_size_rescale_to_max_len( height: int, width: int, min_len: int | None = 1, max_len: int | None = None @@ -101,6 +103,7 @@ def _resize_output_size_rescale_to_max_len( width = max(width, min_len) return height, width + # Copied from transformers.models.idefics3.image_processing_idefics3._resize_output_size_scale_below_upper_bound def _resize_output_size_scale_below_upper_bound( height: int, width: int, max_len: dict[str, int] | None = None @@ -132,6 +135,7 @@ def _resize_output_size_scale_below_upper_bound( width = max(width, 1) return height, width + def get_max_height_width(images_list: list[list[np.ndarray]]) -> tuple[int, int]: """ Get the maximum height and width across all images in a batch. @@ -145,6 +149,7 @@ def get_max_height_width(images_list: list[list[np.ndarray]]) -> tuple[int, int] max_width = max(size[1] for size in image_sizes) return (max_height, max_width) + def get_num_channels(images_list: list[list[np.ndarray]]) -> int: """ Get the number of channels across all images in a batch. Handle empty sublists like in [[], [image]]. @@ -155,6 +160,7 @@ def get_num_channels(images_list: list[list[np.ndarray]]) -> int: raise ValueError("No images found in the batch.") + def get_resize_output_image_size( image: np.ndarray, resolution_max_side: int, @@ -470,4 +476,5 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs: di return num_patches, num_rows, num_cols + __all__ = ["Idefics3ImageProcessorPil"] diff --git a/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py b/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py index 3cd904da53b4..5213a6a10d4a 100644 --- a/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py +++ b/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py @@ -34,6 +34,7 @@ if is_torch_available(): import torch + def squared_euclidean_distance(a, b): b = b.T a2 = np.sum(np.square(a), axis=1) @@ -42,6 +43,7 @@ def squared_euclidean_distance(a, b): d = a2[:, None] - 2 * ab + b2[None, :] return d + def color_quantize(x, clusters): x = x.reshape(-1, 3) d = squared_euclidean_distance(x, clusters) @@ -149,4 +151,5 @@ def to_dict(self): return output + __all__ = ["ImageGPTImageProcessorPil"] diff --git a/src/transformers/models/janus/image_processing_pil_janus.py b/src/transformers/models/janus/image_processing_pil_janus.py index 60a6a3f57bbc..e3c44f37f7e3 100644 --- a/src/transformers/models/janus/image_processing_pil_janus.py +++ b/src/transformers/models/janus/image_processing_pil_janus.py @@ -267,4 +267,5 @@ def unnormalize( ) return image + __all__ = ["JanusImageProcessorPil"] diff --git a/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py b/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py index 77e2d94d5521..84695ff2f24c 100644 --- a/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py +++ b/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py @@ -28,6 +28,7 @@ if is_torch_available(): import torch + # Copied from transformers.models.kosmos2_5.image_processing_kosmos2_5.Kosmos2_5ImageProcessorKwargs class Kosmos2_5ImageProcessorKwargs(ImagesKwargs, total=False): r""" @@ -41,6 +42,7 @@ class Kosmos2_5ImageProcessorKwargs(ImagesKwargs, total=False): patch_size: SizeDict | None max_patches: int + # Copied from transformers.models.kosmos2_5.image_processing_kosmos2_5.torch_extract_patches # Similar to transformers.models.pix2struct.image_processing_pix2struct.torch_extract_patches but dealing with a batch of images directly. def torch_extract_patches(image_tensor, patch_height, patch_width): @@ -66,6 +68,7 @@ def torch_extract_patches(image_tensor, patch_height, patch_width): ) return patches + @auto_docstring @requires(backends=("torch",)) class Kosmos2_5ImageProcessorPil(PilBackend): @@ -249,4 +252,5 @@ def _standardize_kwargs(self, patch_size: dict[str, int] | SizeDict | None = Non kwargs["patch_size"] = patch_size return kwargs + __all__ = ["Kosmos2_5ImageProcessorPil"] diff --git a/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py b/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py index 8dbad5a20973..a5f2cbd882a9 100644 --- a/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py +++ b/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py @@ -58,6 +58,7 @@ class LayoutLMv2ImageProcessorKwargs(ImagesKwargs, total=False): ocr_lang: str | None tesseract_config: str | None + # Copied from transformers.models.layoutlmv2.image_processing_layoutlmv2.normalize_box def normalize_box(box, width, height): return [ @@ -176,4 +177,5 @@ def _preprocess( return data + __all__ = ["LayoutLMv2ImageProcessorPil"] diff --git a/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py b/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py index 421be1056b55..895f569892f7 100644 --- a/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py +++ b/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py @@ -60,6 +60,7 @@ class LayoutLMv3ImageProcessorKwargs(ImagesKwargs, total=False): ocr_lang: str | None tesseract_config: str | None + # Copied from transformers.models.layoutlmv3.image_processing_layoutlmv3.normalize_box def normalize_box(box, width, height): return [ @@ -190,4 +191,5 @@ def _preprocess( return data + __all__ = ["LayoutLMv3ImageProcessorPil"] diff --git a/src/transformers/models/lightglue/image_processing_pil_lightglue.py b/src/transformers/models/lightglue/image_processing_pil_lightglue.py index ad1c9c8eec2c..84504fd97b6f 100644 --- a/src/transformers/models/lightglue/image_processing_pil_lightglue.py +++ b/src/transformers/models/lightglue/image_processing_pil_lightglue.py @@ -35,9 +35,10 @@ is_valid_image, to_numpy_array, ) -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, is_torch_available from ...utils.import_utils import requires +from .image_processing_lightglue import LightGlueImageProcessorKwargs if TYPE_CHECKING: @@ -45,11 +46,13 @@ if is_torch_available(): import torch + def is_grayscale(image: np.ndarray): if image.shape[0] == 1: return True return np.all(image[0, ...] == image[1, ...]) and np.all(image[1, ...] == image[2, ...]) + def convert_to_grayscale(image: ImageInput) -> ImageInput: """ Converts an image to grayscale format using the NTSC formula. Only support numpy and PIL Image. @@ -77,19 +80,7 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: image = image.convert("L") return image -@requires(backends=("torch",)) - -# Copied from transformers.models.lightglue.image_processing_lightglue.LightGlueImageProcessorKwargs -class LightGlueImageProcessorKwargs(ImagesKwargs, total=False): - r""" - do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`): - Whether to convert the image to grayscale. Can be overridden by `do_grayscale` in the `preprocess` method. - """ - - do_grayscale: bool - -# Copied from transformers.models.lightglue.image_processing_lightglue.validate_and_format_image_pairs def validate_and_format_image_pairs(images: ImageInput): error_message = ( "Input images must be a one of the following :", @@ -118,6 +109,7 @@ def _is_valid_image(image): raise ValueError(error_message) +@requires(backends=("torch",)) class LightGlueImageProcessorPil(PilBackend): valid_kwargs = LightGlueImageProcessorKwargs resample = PILImageResampling.BILINEAR @@ -299,4 +291,5 @@ def _get_color(self, score): b = 0 return (r, g, b) + __all__ = ["LightGlueImageProcessorPil"] diff --git a/src/transformers/models/llava_next/image_processing_pil_llava_next.py b/src/transformers/models/llava_next/image_processing_pil_llava_next.py index f11e034080f1..c29f05f1ea41 100644 --- a/src/transformers/models/llava_next/image_processing_pil_llava_next.py +++ b/src/transformers/models/llava_next/image_processing_pil_llava_next.py @@ -235,4 +235,5 @@ def _preprocess( data={"pixel_values": processed_images, "image_sizes": image_sizes}, tensor_type=return_tensors ) + __all__ = ["LlavaNextImageProcessorPil"] diff --git a/src/transformers/models/llava_onevision/image_processing_llava_onevision.py b/src/transformers/models/llava_onevision/image_processing_llava_onevision.py index 8d745802fc00..3dc2650bcb44 100644 --- a/src/transformers/models/llava_onevision/image_processing_llava_onevision.py +++ b/src/transformers/models/llava_onevision/image_processing_llava_onevision.py @@ -172,7 +172,7 @@ def _preprocess( do_resize: bool, size: SizeDict, image_grid_pinpoints: list[list[int]], - resample: "PILImageResampling | int | None", + resample: "PILImageResampling | None", do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, diff --git a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py index 18a381dbf221..b894b72025b9 100644 --- a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py +++ b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py @@ -18,6 +18,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + import numpy as np from ...image_processing_backends import PilBackend @@ -31,20 +32,9 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring - - -# Copied from transformers.models.llava_onevision.image_processing_llava_onevision.LlavaOnevisionImageProcessorKwargs -class LlavaOnevisionImageProcessorKwargs(ImagesKwargs, total=False): - r""" - image_grid_pinpoints (`list[list[int]]`, *optional*): - A list of possible resolutions to use for processing high resolution images. The best resolution is selected - based on the original size of the image. Can be overridden by `image_grid_pinpoints` in the `preprocess` - method. - """ - - image_grid_pinpoints: list[list[int]] +from .image_processing_llava_onevision import LlavaOnevisionImageProcessorKwargs @auto_docstring @@ -303,4 +293,5 @@ def pad_to_square( return result + __all__ = ["LlavaOnevisionImageProcessorPil"] diff --git a/src/transformers/models/mask2former/image_processing_pil_mask2former.py b/src/transformers/models/mask2former/image_processing_pil_mask2former.py index 7f352bab42f7..5d5a4cd211ba 100644 --- a/src/transformers/models/mask2former/image_processing_pil_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_pil_mask2former.py @@ -18,6 +18,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + import math from typing import Any @@ -38,9 +39,10 @@ get_image_size_for_max_height_width, get_max_height_width, ) -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, is_torch_available, logging, requires_backends from ...utils.import_utils import requires +from .image_processing_mask2former import Mask2FormerImageProcessorKwargs if is_torch_available(): @@ -49,6 +51,7 @@ logger = logging.get_logger(__name__) + def convert_segmentation_map_to_binary_masks( segmentation_map: np.ndarray, instance_id_to_semantic_id: dict[int, int] | None = None, @@ -84,36 +87,7 @@ def convert_segmentation_map_to_binary_masks( labels = all_labels.astype(np.int64) return binary_masks.astype(np.float32), labels -@requires(backends=("torch",)) - - -# Copied from transformers.models.mask2former.image_processing_mask2former.Mask2FormerImageProcessorKwargs -class Mask2FormerImageProcessorKwargs(ImagesKwargs, total=False): - r""" - ignore_index (`int`, *optional*): - Label to be assigned to background pixels in segmentation maps. If provided, segmentation map pixels - denoted with 0 (background) will be replaced with `ignore_index`. - do_reduce_labels (`bool`, *optional*, defaults to `False`): - Whether or not to decrement all label values of segmentation maps by 1. Usually used for datasets where 0 - is used for background, and background itself is not included in all classes of a dataset (e.g. ADE20k). - The background label will be replaced by `ignore_index`. - num_labels (`int`, *optional*): - The number of labels in the segmentation map. - size_divisor (`int`, *optional*, defaults to `32`): - Some backbones need images divisible by a certain number. If not passed, it defaults to the value used in - Swin Transformer. - pad_size (`SizeDict`, *optional*): - The size to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` - is not provided, images will be padded to the largest height and width in the batch. - """ - ignore_index: int | None - do_reduce_labels: bool - num_labels: int | None - size_divisor: int - pad_size: SizeDict | None - -# Copied from transformers.models.mask2former.image_processing_mask2former.binary_mask_to_rle def binary_mask_to_rle(mask): """ Converts given binary mask of shape `(height, width)` to the run-length encoding (RLE) format. @@ -138,7 +112,6 @@ def binary_mask_to_rle(mask): return list(runs) -# Copied from transformers.models.mask2former.image_processing_mask2former.check_segment_validity def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overlap_mask_area_threshold=0.8): # Get the mask associated with the k class mask_k = mask_labels == k @@ -157,7 +130,6 @@ def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overl return mask_exists, mask_k -# Copied from transformers.models.mask2former.image_processing_mask2former.compute_segments def compute_segments( mask_probs, pred_scores, @@ -217,7 +189,7 @@ def compute_segments( return segmentation, segments -# Copied from transformers.models.mask2former.image_processing_mask2former.convert_segmentation_to_rle + def convert_segmentation_to_rle(segmentation): """ Converts given segmentation map of shape `(height, width)` to the run-length encoding (RLE) format. @@ -238,7 +210,7 @@ def convert_segmentation_to_rle(segmentation): return run_length_encodings -# Copied from transformers.models.mask2former.image_processing_mask2former.remove_low_and_no_objects + def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): """ Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and @@ -267,6 +239,7 @@ def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_ return masks[to_keep], scores[to_keep], labels[to_keep] +@requires(backends=("torch",)) class Mask2FormerImageProcessorPil(PilBackend): valid_kwargs = Mask2FormerImageProcessorKwargs resample = PILImageResampling.BILINEAR @@ -854,4 +827,5 @@ def post_process_panoptic_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results + __all__ = ["Mask2FormerImageProcessorPil"] diff --git a/src/transformers/models/maskformer/image_processing_pil_maskformer.py b/src/transformers/models/maskformer/image_processing_pil_maskformer.py index 68c50471be55..25369a606822 100644 --- a/src/transformers/models/maskformer/image_processing_pil_maskformer.py +++ b/src/transformers/models/maskformer/image_processing_pil_maskformer.py @@ -44,6 +44,7 @@ logger = logging.get_logger(__name__) + def convert_segmentation_map_to_binary_masks( segmentation_map: np.ndarray, instance_id_to_semantic_id: dict[int, int] | None = None, @@ -79,6 +80,7 @@ def convert_segmentation_map_to_binary_masks( labels = all_labels.astype(np.int64) return binary_masks.astype(np.float32), labels + # Copied from transformers.models.maskformer.image_processing_maskformer.MaskFormerImageProcessorKwargs class MaskFormerImageProcessorKwargs(ImagesKwargs, total=False): r""" @@ -105,6 +107,7 @@ class MaskFormerImageProcessorKwargs(ImagesKwargs, total=False): size_divisor: int pad_size: SizeDict | None + # Copied from transformers.models.maskformer.image_processing_maskformer.binary_mask_to_rle def binary_mask_to_rle(mask): """ @@ -209,6 +212,7 @@ def compute_segments( return segmentation, segments + # Copied from transformers.models.maskformer.image_processing_maskformer.convert_segmentation_to_rle def convert_segmentation_to_rle(segmentation): """ @@ -230,6 +234,7 @@ def convert_segmentation_to_rle(segmentation): return run_length_encodings + # Copied from transformers.models.maskformer.image_processing_maskformer.remove_low_and_no_objects def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): """ @@ -258,6 +263,7 @@ def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_ return masks[to_keep], scores[to_keep], labels[to_keep] + @auto_docstring @requires(backends=("torch",)) class MaskFormerImageProcessorPil(PilBackend): @@ -835,4 +841,5 @@ def post_process_panoptic_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results + __all__ = ["MaskFormerImageProcessorPil"] diff --git a/src/transformers/models/mllama/image_processing_pil_mllama.py b/src/transformers/models/mllama/image_processing_pil_mllama.py index fc02dbdb381b..f9411a8944b5 100644 --- a/src/transformers/models/mllama/image_processing_pil_mllama.py +++ b/src/transformers/models/mllama/image_processing_pil_mllama.py @@ -48,6 +48,7 @@ def split_to_tiles_np(image: np.ndarray, num_tiles_height: int, num_tiles_width: return np.ascontiguousarray(image) + def build_aspect_ratio_mask_np(aspect_ratios: list[list[tuple[int, int]]], max_image_tiles: int) -> np.ndarray: """ Build aspect ratio mask (numpy version). @@ -79,6 +80,7 @@ def build_aspect_ratio_mask_np(aspect_ratios: list[list[tuple[int, int]]], max_i return aspect_ratio_mask + def pack_images( batch_images: list[list[np.ndarray]], max_image_tiles: int, @@ -130,6 +132,7 @@ def pack_images( return stacked_images, all_num_tiles + def convert_aspect_ratios_to_ids_np(aspect_ratios: list[list[tuple[int, int]]], max_image_tiles: int) -> np.ndarray: """ Convert aspect ratio tuples to ids (numpy version). @@ -169,6 +172,7 @@ class MllamaImageProcessorKwargs(ImagesKwargs, total=False): max_image_tiles: int + # Copied from transformers.models.mllama.image_processing_mllama._validate_size def _validate_size(size: SizeDict) -> None: if not (size.height and size.width): @@ -187,6 +191,7 @@ def _validate_mllama_preprocess_arguments(do_resize, size, do_pad, max_image_til raise ValueError(f"MllamaImageProcessor `max_image_tiles` must be a positive integer, got {max_image_tiles}.") _validate_size(size) + # Copied from transformers.models.idefics2.image_processing_idefics2.convert_to_rgb def convert_to_rgb(image: ImageInput) -> ImageInput: """ @@ -205,6 +210,7 @@ def convert_to_rgb(image: ImageInput) -> ImageInput: alpha_composite = alpha_composite.convert("RGB") return alpha_composite + # Copied from transformers.models.mllama.image_processing_mllama.get_all_supported_aspect_ratios @lru_cache(maxsize=10) def get_all_supported_aspect_ratios(max_image_tiles: int) -> list[tuple[int, int]]: @@ -235,6 +241,7 @@ def get_all_supported_aspect_ratios(max_image_tiles: int) -> list[tuple[int, int aspect_ratios.append((width, height)) return aspect_ratios + # Copied from transformers.models.mllama.image_processing_mllama.get_image_size_fit_to_canvas def get_image_size_fit_to_canvas( image_height: int, @@ -286,6 +293,7 @@ def get_image_size_fit_to_canvas( return new_height, new_width + # Copied from transformers.models.mllama.image_processing_mllama.get_optimal_tiled_canvas @lru_cache(maxsize=100) def get_optimal_tiled_canvas( @@ -529,4 +537,5 @@ def _preprocess( return encoded_inputs + __all__ = ["MllamaImageProcessorPil"] diff --git a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py index ae37ab2ae067..b4626c960fe1 100644 --- a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py +++ b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py @@ -199,4 +199,5 @@ def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] semantic_segmentation = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])] return semantic_segmentation + __all__ = ["MobileNetV2ImageProcessorPil"] diff --git a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py index 0d3ecfddf185..385971c0bba3 100644 --- a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py +++ b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py @@ -208,4 +208,5 @@ def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] semantic_segmentation = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])] return semantic_segmentation + __all__ = ["MobileViTImageProcessorPil"] diff --git a/src/transformers/models/nougat/image_processing_pil_nougat.py b/src/transformers/models/nougat/image_processing_pil_nougat.py index c30d399fc40e..fbfebc3cd690 100644 --- a/src/transformers/models/nougat/image_processing_pil_nougat.py +++ b/src/transformers/models/nougat/image_processing_pil_nougat.py @@ -289,4 +289,5 @@ def _preprocess( return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) + __all__ = ["NougatImageProcessorPil"] diff --git a/src/transformers/models/oneformer/image_processing_pil_oneformer.py b/src/transformers/models/oneformer/image_processing_pil_oneformer.py index dd3a7bb5f469..d4c4bb046795 100644 --- a/src/transformers/models/oneformer/image_processing_pil_oneformer.py +++ b/src/transformers/models/oneformer/image_processing_pil_oneformer.py @@ -69,6 +69,7 @@ def make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarr mask[:input_height, :input_width] = 1 return mask + # Copied from transformers.models.oneformer.image_processing_oneformer.OneFormerImageProcessorKwargs class OneFormerImageProcessorKwargs(ImagesKwargs, total=False): r""" @@ -93,6 +94,7 @@ class OneFormerImageProcessorKwargs(ImagesKwargs, total=False): ignore_index: int | None do_reduce_labels: bool + # Copied from transformers.models.oneformer.image_processing_oneformer.binary_mask_to_rle def binary_mask_to_rle(mask): """ @@ -195,6 +197,7 @@ def compute_segments( return segmentation, segments + # Copied from transformers.models.oneformer.image_processing_oneformer.convert_segmentation_to_rle def convert_segmentation_to_rle(segmentation): """ @@ -216,6 +219,7 @@ def convert_segmentation_to_rle(segmentation): return run_length_encodings + # Copied from transformers.models.oneformer.image_processing_oneformer.load_metadata def load_metadata(repo_id, class_info_file): fname = os.path.join("" if repo_id is None else repo_id, class_info_file) @@ -238,6 +242,7 @@ def load_metadata(repo_id, class_info_file): return class_info + # Copied from transformers.models.oneformer.image_processing_oneformer.prepare_metadata def prepare_metadata(class_info): metadata = {} @@ -252,6 +257,7 @@ def prepare_metadata(class_info): metadata["class_names"] = class_names return metadata + # Copied from transformers.models.oneformer.image_processing_oneformer.remove_low_and_no_objects def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): """ @@ -280,6 +286,7 @@ def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_ return masks[to_keep], scores[to_keep], labels[to_keep] + @auto_docstring @requires(backends=("torch",)) class OneFormerImageProcessorPil(PilBackend): @@ -997,4 +1004,5 @@ def post_process_panoptic_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results + __all__ = ["OneFormerImageProcessorPil"] diff --git a/src/transformers/models/ovis2/image_processing_pil_ovis2.py b/src/transformers/models/ovis2/image_processing_pil_ovis2.py index db21675807b9..62641735aef8 100644 --- a/src/transformers/models/ovis2/image_processing_pil_ovis2.py +++ b/src/transformers/models/ovis2/image_processing_pil_ovis2.py @@ -56,6 +56,7 @@ class Ovis2ImageProcessorKwargs(ImagesKwargs, total=False): max_patches: int use_covering_area_grid: bool + # Copied from transformers.models.ovis2.image_processing_ovis2.get_all_supported_aspect_ratios @lru_cache(maxsize=10) def get_all_supported_aspect_ratios(min_image_tiles: int, max_image_tiles: int) -> list[tuple[int, int]]: @@ -122,6 +123,7 @@ def get_min_tile_covering_grid( return min(sufficient_covering_grids, key=lambda x: (x[0][0] * x[0][1], -x[1]))[0] return min(evaluated_grids, key=lambda x: (-x[1], x[0][0] * x[0][1]))[0] + # Copied from transformers.models.ovis2.image_processing_ovis2.get_optimal_tiled_canvas @lru_cache(maxsize=100) def get_optimal_tiled_canvas( @@ -291,4 +293,5 @@ def _preprocess( return BatchFeature(data={"pixel_values": processed_images, "grids": grids}, tensor_type=return_tensors) + __all__ = ["Ovis2ImageProcessorPil"] diff --git a/src/transformers/models/owlv2/image_processing_owlv2.py b/src/transformers/models/owlv2/image_processing_owlv2.py index aa577beb4644..ecf947519755 100644 --- a/src/transformers/models/owlv2/image_processing_owlv2.py +++ b/src/transformers/models/owlv2/image_processing_owlv2.py @@ -359,7 +359,7 @@ def _preprocess( images: list["torch.Tensor"], do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | None", do_pad: bool, do_rescale: bool, rescale_factor: float, diff --git a/src/transformers/models/owlv2/image_processing_pil_owlv2.py b/src/transformers/models/owlv2/image_processing_pil_owlv2.py index a6d50484539c..081a9b2521a0 100644 --- a/src/transformers/models/owlv2/image_processing_pil_owlv2.py +++ b/src/transformers/models/owlv2/image_processing_pil_owlv2.py @@ -28,17 +28,20 @@ from ...image_transforms import center_to_corners_format, pad, to_channel_dimension_format from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ChannelDimension, PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack -from ...utils import ( - TensorType, - auto_docstring, - is_scipy_available, - is_torch_available, - is_torchvision_available, - requires_backends, -) +from ...utils import TensorType, auto_docstring, is_scipy_available, is_torch_available, requires_backends from ...utils.import_utils import requires +if is_scipy_available(): + from scipy import ndimage as ndi + + +if TYPE_CHECKING: + from .modeling_owlv2 import Owlv2ObjectDetectionOutput +if is_torch_available(): + import torch + + def _upcast(t): # Protects from numerical overflows in multiplications by upcasting to the equivalent higher type import torch @@ -64,7 +67,6 @@ def box_area(boxes): return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) -# Copied from transformers.models.owlv2.image_processing_owlv2.box_iou def box_iou(boxes1, boxes2): import torch @@ -83,18 +85,6 @@ def box_iou(boxes1, boxes2): return iou, union -if is_scipy_available(): - from scipy import ndimage as ndi - - -if TYPE_CHECKING: - from .modeling_owlv2 import Owlv2ObjectDetectionOutput -if is_torch_available(): - import torch -if is_torchvision_available(): - pass - - def _preprocess_resize_output_shape(image, output_shape): """Validate resize output shape according to input image. @@ -359,7 +349,7 @@ def post_process_image_guided_detection(self, outputs, threshold=0.0, nms_thresh return results - def pad(self, image: "np.ndarray", constant_value: float = 0.0) -> "np.ndarray": + def pad(self, image: np.ndarray, constant_value: float = 0.0) -> np.ndarray: """ Pad an image with zeros to the given size. """ diff --git a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py index d8281998449a..560b7869ddb9 100644 --- a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py @@ -31,28 +31,11 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring +from .image_processing_paddleocr_vl import PaddleOCRVLImageProcessorKwargs -# Copied from transformers.models.paddleocr_vl.image_processing_paddleocr_vl.PaddleOCRVLImageProcessorKwargs -class PaddleOCRVLImageProcessorKwargs(ImagesKwargs, total=False): - r""" - patch_size (`int`, *optional*, defaults to 14): - The spatial patch size of the vision encoder. - temporal_patch_size (`int`, *optional*, defaults to 1): - The temporal patch size of the vision encoder. - merge_size (`int`, *optional*, defaults to 2): - The merge size of the vision encoder to llm encoder. - """ - - min_pixels: int - max_pixels: int - patch_size: int - temporal_patch_size: int - merge_size: int - -# Copied from transformers.models.paddleocr_vl.image_processing_paddleocr_vl.smart_resize def smart_resize( height: int, width: int, @@ -248,4 +231,5 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non grid_h, grid_w = resized_height // patch_size, resized_width // patch_size return grid_h * grid_w + __all__ = ["PaddleOCRVLImageProcessorPil"] diff --git a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py index 12631c890927..222b99a3e33b 100644 --- a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py +++ b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py @@ -36,6 +36,7 @@ DEFAULT_FONT_PATH = "ybelkada/fonts" + # Copied from transformers.models.pix2struct.image_processing_pix2struct.Pix2StructImageProcessorKwargs class Pix2StructImageProcessorKwargs(ImagesKwargs, total=False): """ @@ -55,6 +56,7 @@ class Pix2StructImageProcessorKwargs(ImagesKwargs, total=False): is_vqa: bool header_text: list[str] | str | None + # Copied from transformers.models.pix2struct.image_processing_pix2struct.render_text # Adapted from https://github.com/google-research/pix2struct/blob/0e1779af0f4db4b652c1d92b3bbd2550a7399123/pix2struct/preprocessing/preprocessing_utils.py#L106 def render_text( @@ -126,6 +128,7 @@ def render_text( return img + # Copied from transformers.models.pix2struct.image_processing_pix2struct.torch_extract_patches if is_torch_available(): import torch @@ -145,13 +148,16 @@ def torch_extract_patches(image_tensor, patch_height, patch_width): Width of patches to extract. """ batch_size, channels, height, width = image_tensor.shape - patches = torch.nn.functional.unfold(image_tensor, (patch_height, patch_width), stride=(patch_height, patch_width)) + patches = torch.nn.functional.unfold( + image_tensor, (patch_height, patch_width), stride=(patch_height, patch_width) + ) patches = patches.reshape(batch_size, channels, patch_height, patch_width, -1) patches = patches.permute(0, 4, 2, 3, 1).reshape( batch_size, height // patch_height, width // patch_width, channels * patch_height * patch_width ) return patches + @auto_docstring @requires(backends=("torch",)) class Pix2StructImageProcessorPil(PilBackend): @@ -398,4 +404,5 @@ def _preprocess( tensor_type=return_tensors, ) + __all__ = ["Pix2StructImageProcessorPil"] diff --git a/src/transformers/models/pixtral/image_processing_pil_pixtral.py b/src/transformers/models/pixtral/image_processing_pil_pixtral.py index 7d4ba7469bec..c833d7b66198 100644 --- a/src/transformers/models/pixtral/image_processing_pil_pixtral.py +++ b/src/transformers/models/pixtral/image_processing_pil_pixtral.py @@ -40,6 +40,7 @@ class PixtralImageProcessorKwargs(ImagesKwargs, total=False): patch_size: dict[str, int] | int + # Copied from transformers.models.pixtral.image_processing_pixtral._num_image_tokens def _num_image_tokens(image_size: tuple[int, int], patch_size: tuple[int, int]) -> int: """ @@ -222,4 +223,5 @@ def _preprocess( data={"pixel_values": padded_images, "image_sizes": batch_image_sizes}, tensor_type=return_tensors ) + __all__ = ["PixtralImageProcessorPil"] diff --git a/src/transformers/models/poolformer/image_processing_pil_poolformer.py b/src/transformers/models/poolformer/image_processing_pil_poolformer.py index e261cd9e0791..deb22ecdc240 100644 --- a/src/transformers/models/poolformer/image_processing_pil_poolformer.py +++ b/src/transformers/models/poolformer/image_processing_pil_poolformer.py @@ -129,4 +129,5 @@ def _preprocess( return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) + __all__ = ["PoolFormerImageProcessorPil"] diff --git a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py index c7b66ba0b321..d40d53394986 100644 --- a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py @@ -39,6 +39,7 @@ if TYPE_CHECKING: from ...modeling_outputs import DepthEstimatorOutput + def _constrain_to_multiple_of(val, multiple, min_val=0, max_val=None): """Constrain a value to be a multiple of another value.""" x = round(val / multiple) * multiple @@ -51,6 +52,7 @@ def _constrain_to_multiple_of(val, multiple, min_val=0, max_val=None): return x + def _get_resize_output_image_size( input_image: np.ndarray, output_size: tuple[int, int], keep_aspect_ratio: bool, multiple: int ) -> tuple[int, int]: @@ -327,4 +329,5 @@ def post_process_depth_estimation( return results + __all__ = ["PromptDepthAnythingImageProcessorPil"] diff --git a/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py b/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py index eccfce542a0e..489bb267173f 100644 --- a/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py +++ b/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py @@ -52,6 +52,7 @@ class Qwen2VLImageProcessorKwargs(ImagesKwargs, total=False): temporal_patch_size: int merge_size: int + # Copied from transformers.models.qwen2_vl.image_processing_qwen2_vl.smart_resize def smart_resize( height: int, width: int, factor: int = 28, min_pixels: int = 56 * 56, max_pixels: int = 14 * 14 * 4 * 1280 @@ -251,4 +252,5 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non grid_h, grid_w = resized_height // patch_size, resized_width // patch_size return grid_h * grid_w + __all__ = ["Qwen2VLImageProcessorPil"] diff --git a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py index 61c45e44790d..606b5640602c 100644 --- a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py @@ -46,24 +46,18 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import ImagesKwargs, Unpack -from ...utils import ( - TensorType, - auto_docstring, - is_torch_available, - is_torchvision_available, - requires_backends, -) +from ...processing_utils import Unpack +from ...utils import TensorType, auto_docstring, is_torch_available, requires_backends from ...utils.import_utils import requires +from .image_processing_rt_detr import RTDetrImageProcessorKwargs if is_torch_available(): import torch -if is_torchvision_available(): - pass SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) + def prepare_coco_detection_annotation_pil( image, target, @@ -118,24 +112,8 @@ def prepare_coco_detection_annotation_pil( return new_target -@requires(backends=("torch",)) - - -# Copied from transformers.models.rt_detr.image_processing_rt_detr.RTDetrImageProcessorKwargs -class RTDetrImageProcessorKwargs(ImagesKwargs, total=False): - r""" - format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): - Data format of the annotations. One of "coco_detection" or "coco_panoptic". - do_convert_annotations (`bool`, *optional*, defaults to `True`): - Controls whether to convert the annotations to the format expected by the RT_DETR model. Converts the - bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. - Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. - """ - - format: str | AnnotationFormat - do_convert_annotations: bool - +@requires(backends=("torch",)) class RTDetrImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR image_mean = IMAGENET_DEFAULT_MEAN @@ -586,4 +564,5 @@ def post_process_object_detection( return results + __all__ = ["RTDetrImageProcessorPil"] diff --git a/src/transformers/models/rt_detr/image_processing_rt_detr.py b/src/transformers/models/rt_detr/image_processing_rt_detr.py index d89394ad0cab..0aadd7ece2e9 100644 --- a/src/transformers/models/rt_detr/image_processing_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_rt_detr.py @@ -21,7 +21,7 @@ from typing import Any, Optional import torch -import torchvision.transforms.v2.functional as tvF +from torchvision.transforms.v2 import functional as tvF from ...image_processing_backends import TorchvisionBackend from ...image_processing_utils import BatchFeature, SizeDict @@ -381,7 +381,7 @@ def _preprocess( masks_path: str | pathlib.Path | None, do_resize: bool, size: SizeDict, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | None", do_rescale: bool, rescale_factor: float, do_normalize: bool, diff --git a/src/transformers/models/sam/image_processing_pil_sam.py b/src/transformers/models/sam/image_processing_pil_sam.py index c65bd649684f..74ce88caec0b 100644 --- a/src/transformers/models/sam/image_processing_pil_sam.py +++ b/src/transformers/models/sam/image_processing_pil_sam.py @@ -63,6 +63,7 @@ def get_resize_output_image_size( new_height = int(new_width * ratio) return (new_height, new_width) + # Copied from transformers.models.sam.image_processing_sam.SamImageProcessorKwargs class SamImageProcessorKwargs(ImagesKwargs, total=False): r""" @@ -76,6 +77,7 @@ class SamImageProcessorKwargs(ImagesKwargs, total=False): mask_size: dict[str, int] mask_pad_size: dict[str, int] + @auto_docstring @requires(backends=("torch",)) class SamImageProcessorPil(PilBackend): @@ -465,6 +467,7 @@ def post_process_for_mask_generation(self, all_masks, all_scores, all_boxes, cro """ return _post_process_for_mask_generation(all_masks, all_scores, all_boxes, crops_nms_thresh) + def _compute_stability_score(masks: "torch.Tensor", mask_threshold: float, stability_score_offset: int): # One mask is always contained inside the other. # Save memory by preventing unnecessary cast to torch.int64 @@ -475,6 +478,7 @@ def _compute_stability_score(masks: "torch.Tensor", mask_threshold: float, stabi stability_scores = intersections / unions return stability_scores + def _batched_mask_to_box(masks: "torch.Tensor"): """ Computes the bounding boxes around the given input masks. The bounding boxes are in the XYXY format which @@ -523,6 +527,7 @@ def _batched_mask_to_box(masks: "torch.Tensor"): out = out.reshape(*shape[:-2], 4) return out + def _is_box_near_crop_edge(boxes, crop_box, orig_box, atol=20.0): """Filter masks at the edge of a crop, but not at the edge of the original image.""" crop_box_torch = torch.as_tensor(crop_box, dtype=torch.float, device=boxes.device) @@ -540,6 +545,7 @@ def _is_box_near_crop_edge(boxes, crop_box, orig_box, atol=20.0): near_crop_edge = torch.logical_and(near_crop_edge, ~near_image_edge) return torch.any(near_crop_edge, dim=1) + def _pad_masks(masks, crop_box: list[int], orig_height: int, orig_width: int): left, top, right, bottom = crop_box if left == 0 and top == 0 and right == orig_width and bottom == orig_height: @@ -549,6 +555,7 @@ def _pad_masks(masks, crop_box: list[int], orig_height: int, orig_width: int): pad = (left, pad_x - left, top, pad_y - top) return torch.nn.functional.pad(masks, pad, value=0) + def _generate_crop_boxes( image, target_size: int, @@ -600,6 +607,7 @@ def _generate_crop_boxes( return crop_boxes, points_per_crop, cropped_images, input_labels + def _generate_per_layer_crops(crop_n_layers, overlap_ratio, original_size): """ Generates 2 ** (layers idx + 1) crops for each crop_n_layers. Crops are in the XYWH format : The XYWH format @@ -633,6 +641,7 @@ def _generate_per_layer_crops(crop_n_layers, overlap_ratio, original_size): return crop_boxes, layer_idxs + def _build_point_grid(n_per_side: int) -> np.ndarray: """Generates a 2D grid of points evenly spaced in [0,1]x[0,1].""" offset = 1 / (2 * n_per_side) @@ -642,6 +651,7 @@ def _build_point_grid(n_per_side: int) -> np.ndarray: points = np.stack([points_x, points_y], axis=-1).reshape(-1, 2) return points + def _generate_crop_images( crop_boxes, image, points_grid, layer_idxs, target_size, original_size, input_data_format=None ): @@ -666,6 +676,7 @@ def _generate_crop_images( return cropped_images, total_points_per_crop + def _normalize_coordinates( target_size: int, coords: np.ndarray, original_size: tuple[int, int], is_bounding_box=False ) -> np.ndarray: @@ -693,6 +704,7 @@ def _normalize_coordinates( return coords + def _rle_to_mask(rle: dict[str, Any]) -> np.ndarray: """Compute a binary mask from an uncompressed RLE.""" height, width = rle["size"] @@ -706,6 +718,7 @@ def _rle_to_mask(rle: dict[str, Any]) -> np.ndarray: mask = mask.reshape(width, height) return mask.transpose() # Reshape to original shape + def _post_process_for_mask_generation(rle_masks, iou_scores, mask_boxes, amg_crops_nms_thresh=0.7): """ Perform NMS (Non Maximum Suppression) on the outputs. @@ -740,6 +753,7 @@ def _post_process_for_mask_generation(rle_masks, iou_scores, mask_boxes, amg_cro return masks, iou_scores, rle_masks, mask_boxes + def _mask_to_rle(input_mask: "torch.Tensor"): """ Encodes masks the run-length encoding (RLE), in the format expected by pycoco tools. @@ -770,4 +784,5 @@ def _mask_to_rle(input_mask: "torch.Tensor"): out.append({"size": [height, width], "counts": counts}) return out + __all__ = ["SamImageProcessorPil"] diff --git a/src/transformers/models/segformer/image_processing_pil_segformer.py b/src/transformers/models/segformer/image_processing_pil_segformer.py index b26059259292..3fe8e38bec6f 100644 --- a/src/transformers/models/segformer/image_processing_pil_segformer.py +++ b/src/transformers/models/segformer/image_processing_pil_segformer.py @@ -18,6 +18,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + import numpy as np from ...image_processing_backends import PilBackend @@ -30,9 +31,10 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, is_torch_available, is_torchvision_available from ...utils.import_utils import requires +from .image_processing_segformer import SegformerImageProcessorKwargs if is_torch_available(): @@ -40,21 +42,8 @@ if is_torchvision_available(): import torchvision.transforms.v2.functional as tvF -@requires(backends=("torch", "torchvision")) - - -# Copied from transformers.models.segformer.image_processing_segformer.SegformerImageProcessorKwargs -class SegformerImageProcessorKwargs(ImagesKwargs, total=False): - r""" - do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): - Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 - is used for background, and background itself is not included in all classes of a dataset (e.g. - ADE20k). The background label will be replaced by 255. - """ - - do_reduce_labels: bool - +@requires(backends=("torch", "torchvision")) class SegformerImageProcessorPil(PilBackend): """PIL backend for Segformer with reduce_label support.""" @@ -220,4 +209,5 @@ def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] return semantic_segmentation + __all__ = ["SegformerImageProcessorPil"] diff --git a/src/transformers/models/segformer/image_processing_segformer.py b/src/transformers/models/segformer/image_processing_segformer.py index 4f00674bcf82..efc8c312953e 100644 --- a/src/transformers/models/segformer/image_processing_segformer.py +++ b/src/transformers/models/segformer/image_processing_segformer.py @@ -148,7 +148,7 @@ def _preprocess( self, images: list["torch.Tensor"], do_reduce_labels: bool, - resample: "PILImageResampling | tvF.InterpolationMode | int | None", + resample: "PILImageResampling | None", do_resize: bool, do_rescale: bool, do_normalize: bool, diff --git a/src/transformers/models/seggpt/image_processing_pil_seggpt.py b/src/transformers/models/seggpt/image_processing_pil_seggpt.py index b48cce71edcc..a6281453f90f 100644 --- a/src/transformers/models/seggpt/image_processing_pil_seggpt.py +++ b/src/transformers/models/seggpt/image_processing_pil_seggpt.py @@ -42,6 +42,7 @@ class SegGptImageProcessorKwargs(ImagesKwargs, total=False): num_labels: int + # Copied from transformers.models.seggpt.image_processing_seggpt.build_palette # See https://huggingface.co/papers/2212.02499 at 3.1 Redefining Output Spaces as "Images" - Semantic Segmentation # Taken from https://github.com/Abdullah-Meda/Painter/blob/main/Painter/data/coco_semseg/gen_color_coco_panoptic_segm.py#L31 @@ -290,4 +291,5 @@ def post_process_semantic_segmentation( return semantic_segmentation + __all__ = ["SegGptImageProcessorPil"] diff --git a/src/transformers/models/siglip2/image_processing_pil_siglip2.py b/src/transformers/models/siglip2/image_processing_pil_siglip2.py index 18f080b47fe5..d131423c7c43 100644 --- a/src/transformers/models/siglip2/image_processing_pil_siglip2.py +++ b/src/transformers/models/siglip2/image_processing_pil_siglip2.py @@ -38,6 +38,7 @@ def convert_image_to_patches(image: np.ndarray, patch_size: int) -> np.ndarray: patched_image = patched_image.reshape(num_patches_height * num_patches_width, -1) return patched_image + def pad_along_first_dim(array: np.ndarray, target_length: int, pad_value: int = 0) -> tuple[np.ndarray, np.ndarray]: """ Pad the array along the first dimension. @@ -65,6 +66,7 @@ class Siglip2ImageProcessorKwargs(ImagesKwargs, total=False): patch_size: int max_num_patches: int + # Copied from transformers.models.siglip2.image_processing_siglip2.get_image_size_for_max_num_patches def get_image_size_for_max_num_patches( image_height: int, image_width: int, patch_size: int, max_num_patches: int, eps: float = 1e-5 @@ -195,4 +197,5 @@ def _preprocess( ) return batch_feature + __all__ = ["Siglip2ImageProcessorPil"] diff --git a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py index a8d61e41a2c9..63a2a69b77eb 100644 --- a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py +++ b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py @@ -19,8 +19,8 @@ # See the License for the specific language governing permissions and # limitations under the License. + import math -from typing import TYPE_CHECKING import numpy as np @@ -35,12 +35,9 @@ SizeDict, make_nested_list_of_images, ) -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring - - -if TYPE_CHECKING: - import torch +from .image_processing_smolvlm import SmolVLMImageProcessorKwargs def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: @@ -51,26 +48,9 @@ def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndar return mask -# Copied from transformers.models.smolvlm.image_processing_smolvlm.MAX_IMAGE_SIZE MAX_IMAGE_SIZE = 4096 # 4k resolution as absolute maximum -# Copied from transformers.models.smolvlm.image_processing_smolvlm.SmolVLMImageProcessorKwargs -class SmolVLMImageProcessorKwargs(ImagesKwargs, total=False): - """ - do_image_splitting (`bool`, *optional*, defaults to `True`): - Whether to split the image into sub-images concatenated with the original image. They are split into patches - such that each patch has a size of `max_image_size["height"]` x `max_image_size["width"]`. - max_image_size (`Dict`, *optional*, defaults to `{"longest_edge": 364}`): - Maximum resolution of the patches of images accepted by the model. This is a dictionary containing the key "longest_edge". - return_row_col_info (`bool`, *optional*, defaults to `False`): - Whether to return the row and column information of the images. - """ - - do_image_splitting: bool - max_image_size: dict[str, int] - return_row_col_info: bool -# Copied from transformers.models.smolvlm.image_processing_smolvlm._resize_output_size_rescale_to_max_len def _resize_output_size_rescale_to_max_len( height: int, width: int, min_len: int | None = 1, max_len: int | None = None ) -> tuple[int, int]: @@ -107,7 +87,7 @@ def _resize_output_size_rescale_to_max_len( width = max(width, min_len) return height, width -# Copied from transformers.models.smolvlm.image_processing_smolvlm._resize_output_size_scale_below_upper_bound + def _resize_output_size_scale_below_upper_bound( height: int, width: int, max_len: dict[str, int] | None = None ) -> tuple[int, int]: @@ -138,8 +118,8 @@ def _resize_output_size_scale_below_upper_bound( width = max(width, 1) return height, width -# Copied from transformers.models.smolvlm.image_processing_smolvlm.get_max_height_width -def get_max_height_width(images_list: list[list["torch.Tensor|np.ndarray"]]) -> tuple[int, int]: + +def get_max_height_width(images_list: list[list[np.ndarray]]) -> tuple[int, int]: """ Get the maximum height and width across all images in a batch. """ @@ -152,8 +132,8 @@ def get_max_height_width(images_list: list[list["torch.Tensor|np.ndarray"]]) -> max_width = max(size[1] for size in image_sizes) return (max_height, max_width) -# Copied from transformers.models.smolvlm.image_processing_smolvlm.get_num_channels -def get_num_channels(images_list: list[list["torch.Tensor|np.ndarray"]]) -> int: + +def get_num_channels(images_list: list[list[np.ndarray]]) -> int: """ Get the number of channels across all images in a batch. Handle empty sublists like in [[], [image]]. """ @@ -163,15 +143,15 @@ def get_num_channels(images_list: list[list["torch.Tensor|np.ndarray"]]) -> int: raise ValueError("No images found in the batch.") -# Copied from transformers.models.smolvlm.image_processing_smolvlm.get_resize_output_image_size + def get_resize_output_image_size( - image: "torch.Tensor", + image: np.ndarray, resolution_max_side: int, ) -> tuple[int, int]: """ Get the output size of the image after resizing given a dictionary specifying the max and min sizes. Args: - image (`torch.Tensor`): + image (`np.ndarray`): Image to resize. resolution_max_side (`int`): The longest edge of the image will be resized to this value. The shortest edge will be resized to keep the @@ -479,4 +459,5 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs: di return num_patches, num_rows, num_cols + __all__ = ["SmolVLMImageProcessorPil"] diff --git a/src/transformers/models/superglue/image_processing_pil_superglue.py b/src/transformers/models/superglue/image_processing_pil_superglue.py index 5326863a5687..e0be6412ce83 100644 --- a/src/transformers/models/superglue/image_processing_pil_superglue.py +++ b/src/transformers/models/superglue/image_processing_pil_superglue.py @@ -40,11 +40,13 @@ from .modeling_superglue import SuperGlueKeypointMatchingOutput + def is_grayscale(image: np.ndarray): if image.shape[0] == 1: return True return np.all(image[0, ...] == image[1, ...]) and np.all(image[1, ...] == image[2, ...]) + def convert_to_grayscale(image: ImageInput) -> ImageInput: """ Converts an image to grayscale format using the NTSC formula. Only support numpy and PIL Image. @@ -110,6 +112,7 @@ class SuperGlueImageProcessorKwargs(ImagesKwargs, total=False): do_grayscale: bool + @auto_docstring class SuperGlueImageProcessorPil(PilBackend): valid_kwargs = SuperGlueImageProcessorKwargs @@ -292,4 +295,5 @@ def _get_color(self, score): b = 0 return (r, g, b) + __all__ = ["SuperGlueImageProcessorPil"] diff --git a/src/transformers/models/superpoint/image_processing_pil_superpoint.py b/src/transformers/models/superpoint/image_processing_pil_superpoint.py index 2788df829f22..ecd1bb8fac6e 100644 --- a/src/transformers/models/superpoint/image_processing_pil_superpoint.py +++ b/src/transformers/models/superpoint/image_processing_pil_superpoint.py @@ -30,12 +30,14 @@ from .modeling_superpoint import SuperPointKeypointDescriptionOutput + def is_grayscale(image: np.ndarray) -> bool: """Checks if an image is grayscale (all RGB channels are identical).""" if image.shape[0] == 1: return True return np.all(image[0, ...] == image[1, ...]) and np.all(image[1, ...] == image[2, ...]) + def convert_to_grayscale(image: np.ndarray) -> np.ndarray: """ Converts an image to grayscale format using the NTSC formula. Only support numpy arrays. @@ -160,4 +162,5 @@ def post_process_keypoint_detection( return results + __all__ = ["SuperPointImageProcessorPil"] diff --git a/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py b/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py index 96388b3a1d41..442fe8bd4992 100644 --- a/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py +++ b/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py @@ -112,4 +112,5 @@ def _preprocess( processed_images.append(image) return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) + __all__ = ["Swin2SRImageProcessorPil"] diff --git a/src/transformers/models/textnet/image_processing_pil_textnet.py b/src/transformers/models/textnet/image_processing_pil_textnet.py index b10e88c215da..232162884c9d 100644 --- a/src/transformers/models/textnet/image_processing_pil_textnet.py +++ b/src/transformers/models/textnet/image_processing_pil_textnet.py @@ -130,4 +130,5 @@ def _preprocess( processed_images.append(image) return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) + __all__ = ["TextNetImageProcessorPil"] diff --git a/src/transformers/models/tvp/image_processing_pil_tvp.py b/src/transformers/models/tvp/image_processing_pil_tvp.py index e085a588a345..c9577858ed56 100644 --- a/src/transformers/models/tvp/image_processing_pil_tvp.py +++ b/src/transformers/models/tvp/image_processing_pil_tvp.py @@ -36,6 +36,7 @@ if is_torchvision_available(): pass + def get_resize_output_image_size( input_image: np.ndarray, max_size: int = 448, @@ -259,4 +260,5 @@ def _preprocess( return BatchFeature(data={"pixel_values": processed_videos}, tensor_type=return_tensors) + __all__ = ["TvpImageProcessorPil"] diff --git a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py index bb90a43b09e6..46f1cbb7d25d 100644 --- a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py +++ b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py @@ -26,32 +26,11 @@ from ...feature_extraction_utils import BatchFeature from ...image_processing_backends import PilBackend from ...image_utils import IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ImageInput, PILImageResampling, SizeDict -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring +from .image_processing_video_llama_3 import VideoLlama3ImageProcessorKwargs -# Copied from transformers.models.video_llama_3.image_processing_video_llama_3.VideoLlama3ImageProcessorKwargs -class VideoLlama3ImageProcessorKwargs(ImagesKwargs, total=False): - r""" - min_pixels (`int`, *optional*, defaults to `56 * 56`): - The min pixels of the image to resize the image. - max_pixels (`int`, *optional*, defaults to `28 * 28 * 1280`): - The max pixels of the image to resize the image. - patch_size (`int`, *optional*, defaults to 14): - The spatial patch size of the vision encoder. - temporal_patch_size (`int`, *optional*, defaults to 2): - The temporal patch size of the vision encoder. - merge_size (`int`, *optional*, defaults to 2): - The merge size of the vision encoder to llm encoder. - """ - - min_pixels: int - max_pixels: int - patch_size: int - temporal_patch_size: int - merge_size: int - -# Copied from transformers.models.video_llama_3.image_processing_video_llama_3.smart_resize def smart_resize( height: int, width: int, factor: int = 28, min_pixels: int = 56 * 56, max_pixels: int = 14 * 14 * 4 * 1280 ): @@ -260,4 +239,5 @@ def get_number_of_image_patches(self, height: int, width: int, images_kwargs=Non grid_h, grid_w = resized_height // patch_size, resized_width // patch_size return grid_h * grid_w + __all__ = ["VideoLlama3ImageProcessorPil"] diff --git a/src/transformers/models/vilt/image_processing_pil_vilt.py b/src/transformers/models/vilt/image_processing_pil_vilt.py index 14341b1ecab5..cc4315b93824 100644 --- a/src/transformers/models/vilt/image_processing_pil_vilt.py +++ b/src/transformers/models/vilt/image_processing_pil_vilt.py @@ -41,12 +41,14 @@ MAX_LONGER_EDGE = 1333 MAX_SHORTER_EDGE = 800 + def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ return [max(values_i) for values_i in zip(*values)] + def make_pixel_mask( image: np.ndarray, output_size: tuple[int, int], input_data_format: str | ChannelDimension | None = None ) -> np.ndarray: @@ -64,6 +66,7 @@ def make_pixel_mask( mask[:input_height, :input_width] = 1 return mask + def get_resize_output_image_size( input_image: np.ndarray, shorter: int = 800, @@ -238,4 +241,5 @@ def _preprocess( return BatchFeature(data=data, tensor_type=return_tensors) + __all__ = ["ViltImageProcessorPil"] diff --git a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py index 4837e0ddbd77..0e34534aa90c 100644 --- a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py +++ b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py @@ -155,4 +155,5 @@ def _preprocess( return BatchFeature(data={"pixel_values": processed_images}, tensor_type=return_tensors) + __all__ = ["VitMatteImageProcessorPil"] diff --git a/src/transformers/models/vitpose/image_processing_pil_vitpose.py b/src/transformers/models/vitpose/image_processing_pil_vitpose.py index 3deaf4f4e2df..76b264409577 100644 --- a/src/transformers/models/vitpose/image_processing_pil_vitpose.py +++ b/src/transformers/models/vitpose/image_processing_pil_vitpose.py @@ -57,6 +57,7 @@ class VitPoseImageProcessorKwargs(ImagesKwargs, total=False): do_affine_transform: bool | None normalize_factor: float | None + # Copied from transformers.models.vitpose.image_processing_vitpose.box_to_center_and_scale # inspired by https://github.com/ViTAE-Transformer/ViTPose/blob/d5216452796c90c6bc29f5c5ec0bdba94366768a/mmpose/datasets/datasets/base/kpt_2d_sview_rgb_img_top_down_dataset.py#L132 def box_to_center_and_scale( @@ -102,6 +103,7 @@ def box_to_center_and_scale( return center, scale + # Copied from transformers.models.vitpose.image_processing_vitpose.coco_to_pascal_voc def coco_to_pascal_voc(bboxes: np.ndarray) -> np.ndarray: """ @@ -122,6 +124,7 @@ def coco_to_pascal_voc(bboxes: np.ndarray) -> np.ndarray: return bboxes + # Copied from transformers.models.vitpose.image_processing_vitpose.get_keypoint_predictions def get_keypoint_predictions(heatmaps: np.ndarray) -> tuple[np.ndarray, np.ndarray]: """Get keypoint predictions from score maps. @@ -155,6 +158,7 @@ def get_keypoint_predictions(heatmaps: np.ndarray) -> tuple[np.ndarray, np.ndarr preds = np.where(np.tile(scores, (1, 1, 2)) > 0.0, preds, -1) return preds, scores + # Copied from transformers.models.vitpose.image_processing_vitpose.get_warp_matrix def get_warp_matrix(theta: float, size_input: np.ndarray, size_dst: np.ndarray, size_target: np.ndarray): """ @@ -192,6 +196,7 @@ def get_warp_matrix(theta: float, size_input: np.ndarray, size_dst: np.ndarray, ) return matrix + # Copied from transformers.models.vitpose.image_processing_vitpose.post_dark_unbiased_data_processing def post_dark_unbiased_data_processing(coords: np.ndarray, batch_heatmaps: np.ndarray, kernel: int = 3) -> np.ndarray: """DARK post-pocessing. Implemented by unbiased_data_processing. @@ -252,6 +257,7 @@ def post_dark_unbiased_data_processing(coords: np.ndarray, batch_heatmaps: np.nd coords -= np.einsum("ijmn,ijnk->ijmk", hessian, derivative).squeeze() return coords + # Copied from transformers.models.vitpose.image_processing_vitpose.scipy_warp_affine def scipy_warp_affine(src, M, size): """ @@ -278,6 +284,7 @@ def scipy_warp_affine(src, M, size): new_src = np.stack(new_src, axis=-1) return new_src + # Copied from transformers.models.vitpose.image_processing_vitpose.transform_preds def transform_preds(coords: np.ndarray, center: np.ndarray, scale: np.ndarray, output_size: np.ndarray) -> np.ndarray: """Get final keypoint predictions from heatmaps and apply scaling and @@ -516,4 +523,5 @@ def post_process_pose_estimation( results.append(image_results) return results + __all__ = ["VitPoseImageProcessorPil"] diff --git a/src/transformers/models/yolos/image_processing_pil_yolos.py b/src/transformers/models/yolos/image_processing_pil_yolos.py index 6965cbdbff0a..7f5b8385d8b9 100644 --- a/src/transformers/models/yolos/image_processing_pil_yolos.py +++ b/src/transformers/models/yolos/image_processing_pil_yolos.py @@ -33,9 +33,10 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import ImagesKwargs, Unpack +from ...processing_utils import Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, requires_backends from ...utils.import_utils import requires +from .image_processing_yolos import YolosImageProcessorKwargs if is_vision_available(): @@ -46,6 +47,7 @@ SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) + # inspired by https://github.com/facebookresearch/yolos/blob/master/datasets/coco.py#L33 def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndarray: """ @@ -80,6 +82,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar return masks + # inspired by https://github.com/facebookresearch/yolos/blob/master/datasets/coco.py#L50 def prepare_coco_detection_annotation( image, @@ -140,6 +143,7 @@ def prepare_coco_detection_annotation( return new_target + def masks_to_boxes(masks: np.ndarray) -> np.ndarray: """ Compute the bounding boxes around the provided panoptic segmentation masks. @@ -173,6 +177,7 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: return np.stack([x_min, y_min, x_max, y_max], 1) + # 2 functions below adapted from https://github.com/cocodataset/panopticapi/blob/master/panopticapi/utils.py # Copyright (c) 2018, Alexander Kirillov # All rights reserved. @@ -186,6 +191,7 @@ def rgb_to_id(color): return color[:, :, 0] + 256 * color[:, :, 1] + 256 * 256 * color[:, :, 2] return int(color[0] + 256 * color[1] + 256 * 256 * color[2]) + def prepare_coco_panoptic_annotation( image: np.ndarray, target: dict, @@ -226,6 +232,7 @@ def prepare_coco_panoptic_annotation( return new_target + def get_size_with_aspect_ratio_yolos( image_size: tuple[int, int], size: int, max_size: int | None = None, mod_size: int = 16 ) -> tuple[int, int]: @@ -267,21 +274,6 @@ def get_size_with_aspect_ratio_yolos( return (oh, ow) -# Copied from transformers.models.yolos.image_processing_yolos.YolosImageProcessorKwargs -class YolosImageProcessorKwargs(ImagesKwargs, total=False): - r""" - format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): - Data format of the annotations. One of "coco_detection" or "coco_panoptic". - do_convert_annotations (`bool`, *optional*, defaults to `True`): - Controls whether to convert the annotations to the format expected by the YOLOS model. Converts the - bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. - Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. - """ - - format: str | AnnotationFormat - do_convert_annotations: bool - - @auto_docstring class YolosImageProcessorPil(PilBackend): resample = PILImageResampling.BILINEAR @@ -751,4 +743,5 @@ def post_process_object_detection( return results + __all__ = ["YolosImageProcessorPil"] diff --git a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py index 3c871243f6c4..dc4454d70dc9 100644 --- a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py +++ b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py @@ -67,6 +67,7 @@ class ZoeDepthImageProcessorKwargs(ImagesKwargs, total=False): keep_aspect_ratio: bool ensure_multiple_of: int + # Copied from transformers.models.zoedepth.image_processing_zoedepth.get_resize_output_image_size def get_resize_output_image_size( input_image: "torch.Tensor | np.ndarray", @@ -339,4 +340,5 @@ def post_process_depth_estimation( return results + __all__ = ["ZoeDepthImageProcessorPil"] From b15369eaf8c110be166c7ee9d9f422dd5a2f0f3a Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 17:11:05 +0100 Subject: [PATCH 18/30] up --- .../models/beit/image_processing_pil_beit.py | 2 +- .../image_processing_pil_bridgetower.py | 17 ++++------------- .../colmodernvbert/processing_colmodernvbert.py | 7 ++++++- .../models/glm_image/modular_glm_image.py | 2 ++ .../models/glm_image/processing_glm_image.py | 9 +++++++-- src/transformers/models/pi0/processing_pi0.py | 7 ++++++- .../models/sam2_video/processing_sam2_video.py | 7 ++++++- .../sam3_tracker/processing_sam3_tracker.py | 7 ++++++- .../processing_sam3_tracker_video.py | 7 ++++++- .../models/sam3_video/processing_sam3_video.py | 6 +++++- 10 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/transformers/models/beit/image_processing_pil_beit.py b/src/transformers/models/beit/image_processing_pil_beit.py index de05a3c889d4..2daa20c9172c 100644 --- a/src/transformers/models/beit/image_processing_pil_beit.py +++ b/src/transformers/models/beit/image_processing_pil_beit.py @@ -63,7 +63,7 @@ class BeitImageProcessorPil(PilBackend): def __init__(self, **kwargs: Unpack[BeitImageProcessorKwargs]): super().__init__(**kwargs) - # @auto_docstring + @auto_docstring def preprocess( self, images: ImageInput, diff --git a/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py b/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py index f939cb027960..c18f12d6662f 100644 --- a/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py +++ b/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py @@ -26,12 +26,7 @@ get_image_size, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring, is_torch_available -from ...utils.import_utils import requires - - -if is_torch_available(): - import torch +from ...utils import TensorType, auto_docstring # Copied from transformers.models.bridgetower.image_processing_bridgetower.BridgeTowerImageProcessorKwargs @@ -44,18 +39,15 @@ class BridgeTowerImageProcessorKwargs(ImagesKwargs, total=False): size_divisor: int -# Copied from transformers.models.bridgetower.image_processing_bridgetower.get_resize_output_image_size +# adapted from transformers.models.bridgetower.image_processing_bridgetower.get_resize_output_image_size def get_resize_output_image_size( - input_image: np.ndarray | torch.Tensor, + input_image: np.ndarray, shorter: int = 800, longer: int = 1333, size_divisor: int = 32, ) -> tuple[int, int]: """Get output image size after resizing with size_divisor.""" - if isinstance(input_image, torch.Tensor): - input_height, input_width = input_image.shape[-2:] - else: - input_height, input_width = get_image_size(input_image, channel_dim=ChannelDimension.FIRST) + input_height, input_width = get_image_size(input_image, channel_dim=ChannelDimension.FIRST) min_size, max_size = shorter, longer scale = min_size / min(input_height, input_width) @@ -80,7 +72,6 @@ def get_resize_output_image_size( @auto_docstring -@requires(backends=("vision", "torch")) class BridgeTowerImageProcessorPil(PilBackend): """PIL backend for BridgeTower with custom resize and center_crop.""" diff --git a/src/transformers/models/colmodernvbert/processing_colmodernvbert.py b/src/transformers/models/colmodernvbert/processing_colmodernvbert.py index de9a81205682..f9c34aaa9263 100755 --- a/src/transformers/models/colmodernvbert/processing_colmodernvbert.py +++ b/src/transformers/models/colmodernvbert/processing_colmodernvbert.py @@ -23,7 +23,12 @@ from typing import TYPE_CHECKING, Optional, Union import numpy as np -import torch + +from ...utils import is_torch_available + + +if is_torch_available(): + import torch from ...feature_extraction_utils import BatchFeature from ...image_utils import ImageInput, is_valid_image, load_image diff --git a/src/transformers/models/glm_image/modular_glm_image.py b/src/transformers/models/glm_image/modular_glm_image.py index 2da4c882521d..e72aede3da66 100644 --- a/src/transformers/models/glm_image/modular_glm_image.py +++ b/src/transformers/models/glm_image/modular_glm_image.py @@ -32,6 +32,7 @@ from ...tokenization_utils_base import PreTokenizedInput, TextInput from ...utils import TransformersKwargs, auto_docstring, can_return_tuple, logging from ...utils.generic import merge_with_config_defaults +from ...utils.import_utils import requires from ...utils.output_capturing import capture_outputs from ..chameleon.modeling_chameleon import ChameleonVQVAE, ChameleonVQVAEModelOutput, ChameleonVQVAEVectorQuantizer from ..glm4v.configuration_glm4v import Glm4vTextConfig, Glm4vVisionConfig @@ -1273,6 +1274,7 @@ class GlmImageProcessorKwargs(Qwen2VLProcessorKwargs): } +@requires(backends=("torch",)) class GlmImageProcessor(ProcessorMixin): r""" Constructs a GLM-Image processor which wraps a GLM-Image image processor and a GLM-Image tokenizer into a single processor. diff --git a/src/transformers/models/glm_image/processing_glm_image.py b/src/transformers/models/glm_image/processing_glm_image.py index 07f4103a10a4..e418de936dec 100644 --- a/src/transformers/models/glm_image/processing_glm_image.py +++ b/src/transformers/models/glm_image/processing_glm_image.py @@ -20,12 +20,16 @@ import math -import torch - from ...feature_extraction_utils import BatchFeature from ...image_utils import ImageInput from ...processing_utils import ImagesKwargs, ProcessingKwargs, ProcessorMixin, Unpack from ...tokenization_utils_base import PreTokenizedInput, TextInput +from ...utils import is_torch_available +from ...utils.import_utils import requires + + +if is_torch_available(): + import torch class GlmImageImagesKwargs(ImagesKwargs, total=False): @@ -54,6 +58,7 @@ class GlmImageProcessorKwargs(ProcessingKwargs, total=False): images_kwargs: GlmImageImagesKwargs +@requires(backends=("torch",)) class GlmImageProcessor(ProcessorMixin): r""" Constructs a GLM-Image processor which wraps a GLM-Image image processor and a GLM-Image tokenizer into a single processor. diff --git a/src/transformers/models/pi0/processing_pi0.py b/src/transformers/models/pi0/processing_pi0.py index 6fe2d94ff08c..9cc186ee8894 100644 --- a/src/transformers/models/pi0/processing_pi0.py +++ b/src/transformers/models/pi0/processing_pi0.py @@ -19,7 +19,12 @@ # limitations under the License. import numpy as np -import torch + +from ...utils import is_torch_available + + +if is_torch_available(): + import torch import torch.nn.functional as F from ...feature_extraction_utils import BatchFeature diff --git a/src/transformers/models/sam2_video/processing_sam2_video.py b/src/transformers/models/sam2_video/processing_sam2_video.py index 416c5c543fe2..2ad20d85ede0 100644 --- a/src/transformers/models/sam2_video/processing_sam2_video.py +++ b/src/transformers/models/sam2_video/processing_sam2_video.py @@ -21,7 +21,12 @@ from typing import Union import numpy as np -import torch + +from ...utils import is_torch_available + + +if is_torch_available(): + import torch from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin diff --git a/src/transformers/models/sam3_tracker/processing_sam3_tracker.py b/src/transformers/models/sam3_tracker/processing_sam3_tracker.py index d9beb6ee65da..91ec30b5f0e0 100644 --- a/src/transformers/models/sam3_tracker/processing_sam3_tracker.py +++ b/src/transformers/models/sam3_tracker/processing_sam3_tracker.py @@ -22,7 +22,12 @@ from copy import deepcopy import numpy as np -import torch + +from ...utils import is_torch_available + + +if is_torch_available(): + import torch from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin diff --git a/src/transformers/models/sam3_tracker_video/processing_sam3_tracker_video.py b/src/transformers/models/sam3_tracker_video/processing_sam3_tracker_video.py index 708be51bd5e3..26abb2a7bfb7 100644 --- a/src/transformers/models/sam3_tracker_video/processing_sam3_tracker_video.py +++ b/src/transformers/models/sam3_tracker_video/processing_sam3_tracker_video.py @@ -23,7 +23,12 @@ from typing import Union import numpy as np -import torch + +from ...utils import is_torch_available + + +if is_torch_available(): + import torch from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin diff --git a/src/transformers/models/sam3_video/processing_sam3_video.py b/src/transformers/models/sam3_video/processing_sam3_video.py index 6f7b1c238cfe..4d8277095dc1 100644 --- a/src/transformers/models/sam3_video/processing_sam3_video.py +++ b/src/transformers/models/sam3_video/processing_sam3_video.py @@ -13,7 +13,11 @@ # limitations under the License. from typing import Union -import torch +from ...utils import is_torch_available + + +if is_torch_available(): + import torch from ...utils import is_torchvision_available From a5c748104d7c042f023af6695c7f176935aa4303 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 17:54:13 +0100 Subject: [PATCH 19/30] update --- .../models/colmodernvbert/processing_colmodernvbert.py | 7 +------ src/transformers/models/glm_image/processing_glm_image.py | 7 ++----- src/transformers/models/pi0/processing_pi0.py | 7 +------ .../models/sam2_video/processing_sam2_video.py | 7 +------ .../models/sam3_tracker/processing_sam3_tracker.py | 7 +------ .../sam3_tracker_video/processing_sam3_tracker_video.py | 7 +------ .../models/segformer/image_processing_pil_segformer.py | 4 ++-- 7 files changed, 9 insertions(+), 37 deletions(-) diff --git a/src/transformers/models/colmodernvbert/processing_colmodernvbert.py b/src/transformers/models/colmodernvbert/processing_colmodernvbert.py index f9c34aaa9263..de9a81205682 100755 --- a/src/transformers/models/colmodernvbert/processing_colmodernvbert.py +++ b/src/transformers/models/colmodernvbert/processing_colmodernvbert.py @@ -23,12 +23,7 @@ from typing import TYPE_CHECKING, Optional, Union import numpy as np - -from ...utils import is_torch_available - - -if is_torch_available(): - import torch +import torch from ...feature_extraction_utils import BatchFeature from ...image_utils import ImageInput, is_valid_image, load_image diff --git a/src/transformers/models/glm_image/processing_glm_image.py b/src/transformers/models/glm_image/processing_glm_image.py index e418de936dec..1b117f2ced4e 100644 --- a/src/transformers/models/glm_image/processing_glm_image.py +++ b/src/transformers/models/glm_image/processing_glm_image.py @@ -20,18 +20,15 @@ import math +import torch + from ...feature_extraction_utils import BatchFeature from ...image_utils import ImageInput from ...processing_utils import ImagesKwargs, ProcessingKwargs, ProcessorMixin, Unpack from ...tokenization_utils_base import PreTokenizedInput, TextInput -from ...utils import is_torch_available from ...utils.import_utils import requires -if is_torch_available(): - import torch - - class GlmImageImagesKwargs(ImagesKwargs, total=False): """ target_h (`int`): diff --git a/src/transformers/models/pi0/processing_pi0.py b/src/transformers/models/pi0/processing_pi0.py index 9cc186ee8894..6fe2d94ff08c 100644 --- a/src/transformers/models/pi0/processing_pi0.py +++ b/src/transformers/models/pi0/processing_pi0.py @@ -19,12 +19,7 @@ # limitations under the License. import numpy as np - -from ...utils import is_torch_available - - -if is_torch_available(): - import torch +import torch import torch.nn.functional as F from ...feature_extraction_utils import BatchFeature diff --git a/src/transformers/models/sam2_video/processing_sam2_video.py b/src/transformers/models/sam2_video/processing_sam2_video.py index 2ad20d85ede0..416c5c543fe2 100644 --- a/src/transformers/models/sam2_video/processing_sam2_video.py +++ b/src/transformers/models/sam2_video/processing_sam2_video.py @@ -21,12 +21,7 @@ from typing import Union import numpy as np - -from ...utils import is_torch_available - - -if is_torch_available(): - import torch +import torch from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin diff --git a/src/transformers/models/sam3_tracker/processing_sam3_tracker.py b/src/transformers/models/sam3_tracker/processing_sam3_tracker.py index 91ec30b5f0e0..d9beb6ee65da 100644 --- a/src/transformers/models/sam3_tracker/processing_sam3_tracker.py +++ b/src/transformers/models/sam3_tracker/processing_sam3_tracker.py @@ -22,12 +22,7 @@ from copy import deepcopy import numpy as np - -from ...utils import is_torch_available - - -if is_torch_available(): - import torch +import torch from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin diff --git a/src/transformers/models/sam3_tracker_video/processing_sam3_tracker_video.py b/src/transformers/models/sam3_tracker_video/processing_sam3_tracker_video.py index 26abb2a7bfb7..708be51bd5e3 100644 --- a/src/transformers/models/sam3_tracker_video/processing_sam3_tracker_video.py +++ b/src/transformers/models/sam3_tracker_video/processing_sam3_tracker_video.py @@ -23,12 +23,7 @@ from typing import Union import numpy as np - -from ...utils import is_torch_available - - -if is_torch_available(): - import torch +import torch from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin diff --git a/src/transformers/models/segformer/image_processing_pil_segformer.py b/src/transformers/models/segformer/image_processing_pil_segformer.py index 3fe8e38bec6f..77514873c59a 100644 --- a/src/transformers/models/segformer/image_processing_pil_segformer.py +++ b/src/transformers/models/segformer/image_processing_pil_segformer.py @@ -32,7 +32,7 @@ SizeDict, ) from ...processing_utils import Unpack -from ...utils import TensorType, is_torch_available, is_torchvision_available +from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available from ...utils.import_utils import requires from .image_processing_segformer import SegformerImageProcessorKwargs @@ -64,7 +64,7 @@ class SegformerImageProcessorPil(PilBackend): def __init__(self, **kwargs: Unpack[SegformerImageProcessorKwargs]): super().__init__(**kwargs) - # @auto_docstring + @auto_docstring def preprocess( self, images: ImageInput, From b7473083b001bb3270f9174cd43f862a59c32a21 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 18:11:28 +0100 Subject: [PATCH 20/30] fix imports --- .../image_processing_pil_conditional_detr.py | 17 +++++++++-- .../image_processing_pil_deepseek_vl.py | 13 +++++++-- ...image_processing_pil_deepseek_vl_hybrid.py | 25 ++++++++++++++-- .../image_processing_pil_deformable_detr.py | 17 +++++++++-- .../image_processing_pil_efficientloftr.py | 19 ++++++++++-- .../image_processing_pil_ernie4_5_vl_moe.py | 18 ++++++++++-- .../glm46v/image_processing_pil_glm46v.py | 18 ++++++++++-- .../image_processing_pil_glm_image.py | 24 +++++++++++++-- .../image_processing_pil_grounding_dino.py | 17 +++++++++-- .../image_processing_pil_lightglue.py | 12 ++++++-- .../image_processing_pil_llava_onevision.py | 14 +++++++-- .../image_processing_pil_mask2former.py | 29 +++++++++++++++++-- .../image_processing_pil_paddleocr_vl.py | 20 +++++++++++-- .../rt_detr/image_processing_pil_rt_detr.py | 17 +++++++++-- .../image_processing_pil_segformer.py | 14 +++++++-- .../smolvlm/image_processing_pil_smolvlm.py | 19 ++++++++++-- .../image_processing_pil_video_llama_3.py | 24 +++++++++++++-- .../yolos/image_processing_pil_yolos.py | 17 +++++++++-- 18 files changed, 297 insertions(+), 37 deletions(-) diff --git a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py index 41b770e6f1f2..e7e9753227c4 100644 --- a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py +++ b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py @@ -48,10 +48,23 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, logging, requires_backends from ...utils.import_utils import requires -from .image_processing_conditional_detr import ConditionalDetrImageProcessorKwargs +class ConditionalDetrImageProcessorKwargs(ImagesKwargs, total=False): + r""" + format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): + Data format of the annotations. One of "coco_detection" or "coco_panoptic". + do_convert_annotations (`bool`, *optional*, defaults to `True`): + Controls whether to convert the annotations to the format expected by the CONDITIONAL_DETR model. Converts the + bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. + Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. + """ + + format: str | AnnotationFormat + do_convert_annotations: bool + + if is_vision_available(): diff --git a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py index d29296535277..283d10907b99 100644 --- a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py +++ b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py @@ -32,9 +32,18 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_deepseek_vl import DeepseekVLImageProcessorKwargs +class DeepseekVLImageProcessorKwargs(ImagesKwargs, total=False): + r""" + min_size (`int`, *optional*, defaults to 14): + The minimum allowed size for the resized image. Ensures that neither the height nor width + falls below this value after resizing. + """ + + min_size: int + + @auto_docstring diff --git a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py index c7ef92dce05f..25187581bd1f 100644 --- a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py +++ b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py @@ -33,9 +33,30 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_deepseek_vl_hybrid import DeepseekVLHybridImageProcessorKwargs +class DeepseekVLHybridImageProcessorKwargs(ImagesKwargs, total=False): + r""" + min_size (`int`, *optional*, defaults to 14): + The minimum allowed size for the resized image. Ensures that neither the height nor width + falls below this value after resizing. + high_res_size (`dict`, *optional*, defaults to `{"height": 1024, "width": 1024}`): + Size of the high resolution output image after resizing. + high_res_resample (`PILImageResampling`, *optional*, defaults to `Resampling.BICUBIC`): + Resampling filter to use if resizing the image. + high_res_image_mean (`float` or `list[float]`, *optional*, defaults to `OPENAI_CLIP_MEAN`): + Mean to use if normalizing the high resolution image. + high_res_image_std (`float` or `list[float]`, *optional*, defaults to `OPENAI_CLIP_STD`): + Standard deviation to use if normalizing the high resolution image. + """ + + min_size: int + high_res_size: dict + high_res_resample: "PILImageResampling | None" + high_res_image_mean: float | list[float] | tuple[float, ...] + high_res_image_std: float | list[float] | tuple[float, ...] + + @auto_docstring diff --git a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py index dd66876deca4..e0949198823e 100644 --- a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py +++ b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py @@ -47,10 +47,23 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available from ...utils.import_utils import requires, requires_backends -from .image_processing_deformable_detr import DeformableDetrImageProcessorKwargs +class DeformableDetrImageProcessorKwargs(ImagesKwargs, total=False): + r""" + format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): + Data format of the annotations. One of "coco_detection" or "coco_panoptic". + do_convert_annotations (`bool`, *optional*, defaults to `True`): + Controls whether to convert the annotations to the format expected by the DEFORMABLE_DETR model. Converts the + bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. + Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. + """ + + format: str | AnnotationFormat + do_convert_annotations: bool + + if is_vision_available(): diff --git a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py index a8a1af8b3df0..4768ec7c9e8d 100644 --- a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py @@ -21,17 +21,30 @@ is_valid_image, to_numpy_array, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available from ...utils.import_utils import requires -from .image_processing_efficientloftr import EfficientLoFTRImageProcessorKwargs -from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput if is_torch_available(): import torch +class EfficientLoFTRImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`): + Whether to convert the image to grayscale. Can be overridden by `do_grayscale` in the `preprocess` method. + """ + + do_grayscale: bool + + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput + + def is_grayscale(image: np.ndarray): if image.shape[0] == 1: return True diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py index 8aed9c816627..bdff7fa1f489 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py @@ -25,9 +25,23 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, logging -from .image_processing_ernie4_5_vl_moe import Ernie4_5_VLMoeImageProcessorKwargs +class Ernie4_5_VLMoeImageProcessorKwargs(ImagesKwargs, total=False): + r""" + patch_size (`int`, *optional*, defaults to 14): + The spatial patch size of the vision encoder. + temporal_patch_size (`int`, *optional*): + The temporal patch size of the vision encoder. Unused in the image processor, only used for videos. + merge_size (`int`, *optional*, defaults to 2): + The merge size of the vision encoder to llm encoder. + """ + + patch_size: int + temporal_patch_size: int + merge_size: int + + logger = logging.get_logger(__name__) diff --git a/src/transformers/models/glm46v/image_processing_pil_glm46v.py b/src/transformers/models/glm46v/image_processing_pil_glm46v.py index 034566192431..de14d5527e73 100644 --- a/src/transformers/models/glm46v/image_processing_pil_glm46v.py +++ b/src/transformers/models/glm46v/image_processing_pil_glm46v.py @@ -26,9 +26,23 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_glm46v import Glm46VImageProcessorKwargs +class Glm46VImageProcessorKwargs(ImagesKwargs, total=False): + """ + patch_size (`int`, *optional*, defaults to 14): + The spatial patch size of the vision encoder. + temporal_patch_size (`int`, *optional*, defaults to 2): + The temporal patch size of the vision encoder. + merge_size (`int`, *optional*, defaults to 2): + The merge size of the vision encoder to llm encoder. + """ + + patch_size: int + temporal_patch_size: int + merge_size: int + + def smart_resize( diff --git a/src/transformers/models/glm_image/image_processing_pil_glm_image.py b/src/transformers/models/glm_image/image_processing_pil_glm_image.py index 0aaf95a9aaea..a674ed4c0844 100644 --- a/src/transformers/models/glm_image/image_processing_pil_glm_image.py +++ b/src/transformers/models/glm_image/image_processing_pil_glm_image.py @@ -26,9 +26,29 @@ from ...feature_extraction_utils import BatchFeature from ...image_processing_backends import PilBackend from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_glm_image import GlmImageImageProcessorKwargs +class GlmImageImageProcessorKwargs(ImagesKwargs, total=False): + r""" + min_pixels (`int`, *optional*, defaults to `56 * 56`): + The min pixels of the image to resize the image. + max_pixels (`int`, *optional*, defaults to `28 * 28 * 1280`): + The max pixels of the image to resize the image. + patch_size (`int`, *optional*, defaults to 14): + The spatial patch size of the vision encoder. + temporal_patch_size (`int`, *optional*, defaults to 2): + The temporal patch size of the vision encoder. + merge_size (`int`, *optional*, defaults to 2): + The merge size of the vision encoder to llm encoder. + """ + + min_pixels: int + max_pixels: int + patch_size: int + temporal_patch_size: int + merge_size: int + + def smart_resize( diff --git a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py index fbdbef4110b4..1c60ec5c4bdc 100644 --- a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py +++ b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py @@ -53,10 +53,23 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, requires_backends from ...utils.import_utils import requires -from .image_processing_grounding_dino import GroundingDinoImageProcessorKwargs +class GroundingDinoImageProcessorKwargs(ImagesKwargs, total=False): + r""" + format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): + Data format of the annotations. One of "coco_detection" or "coco_panoptic". + do_convert_annotations (`bool`, *optional*, defaults to `True`): + Controls whether to convert the annotations to the format expected by the GROUNDING_DINO model. Converts the + bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. + Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. + """ + + format: str | AnnotationFormat + do_convert_annotations: bool + + if TYPE_CHECKING: diff --git a/src/transformers/models/lightglue/image_processing_pil_lightglue.py b/src/transformers/models/lightglue/image_processing_pil_lightglue.py index 84504fd97b6f..974ca1e831ee 100644 --- a/src/transformers/models/lightglue/image_processing_pil_lightglue.py +++ b/src/transformers/models/lightglue/image_processing_pil_lightglue.py @@ -35,10 +35,18 @@ is_valid_image, to_numpy_array, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available from ...utils.import_utils import requires -from .image_processing_lightglue import LightGlueImageProcessorKwargs +class LightGlueImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`): + Whether to convert the image to grayscale. Can be overridden by `do_grayscale` in the `preprocess` method. + """ + + do_grayscale: bool + + if TYPE_CHECKING: diff --git a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py index b894b72025b9..81fd109861b9 100644 --- a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py +++ b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py @@ -32,9 +32,19 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_llava_onevision import LlavaOnevisionImageProcessorKwargs +class LlavaOnevisionImageProcessorKwargs(ImagesKwargs, total=False): + r""" + image_grid_pinpoints (`list[list[int]]`, *optional*): + A list of possible resolutions to use for processing high resolution images. The best resolution is selected + based on the original size of the image. Can be overridden by `image_grid_pinpoints` in the `preprocess` + method. + """ + + image_grid_pinpoints: list[list[int]] + + @auto_docstring diff --git a/src/transformers/models/mask2former/image_processing_pil_mask2former.py b/src/transformers/models/mask2former/image_processing_pil_mask2former.py index 5d5a4cd211ba..7afc89fac40d 100644 --- a/src/transformers/models/mask2former/image_processing_pil_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_pil_mask2former.py @@ -39,10 +39,35 @@ get_image_size_for_max_height_width, get_max_height_width, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, logging, requires_backends from ...utils.import_utils import requires -from .image_processing_mask2former import Mask2FormerImageProcessorKwargs +class Mask2FormerImageProcessorKwargs(ImagesKwargs, total=False): + r""" + ignore_index (`int`, *optional*): + Label to be assigned to background pixels in segmentation maps. If provided, segmentation map pixels + denoted with 0 (background) will be replaced with `ignore_index`. + do_reduce_labels (`bool`, *optional*, defaults to `False`): + Whether or not to decrement all label values of segmentation maps by 1. Usually used for datasets where 0 + is used for background, and background itself is not included in all classes of a dataset (e.g. ADE20k). + The background label will be replaced by `ignore_index`. + num_labels (`int`, *optional*): + The number of labels in the segmentation map. + size_divisor (`int`, *optional*, defaults to `32`): + Some backbones need images divisible by a certain number. If not passed, it defaults to the value used in + Swin Transformer. + pad_size (`SizeDict`, *optional*): + The size to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` + is not provided, images will be padded to the largest height and width in the batch. + """ + + ignore_index: int | None + do_reduce_labels: bool + num_labels: int | None + size_divisor: int + pad_size: SizeDict | None + + if is_torch_available(): diff --git a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py index 560b7869ddb9..e278a09dec84 100644 --- a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py @@ -31,9 +31,25 @@ from ...image_processing_backends import PilBackend from ...image_processing_utils import BatchFeature from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_paddleocr_vl import PaddleOCRVLImageProcessorKwargs +class PaddleOCRVLImageProcessorKwargs(ImagesKwargs, total=False): + r""" + patch_size (`int`, *optional*, defaults to 14): + The spatial patch size of the vision encoder. + temporal_patch_size (`int`, *optional*, defaults to 1): + The temporal patch size of the vision encoder. + merge_size (`int`, *optional*, defaults to 2): + The merge size of the vision encoder to llm encoder. + """ + + min_pixels: int + max_pixels: int + patch_size: int + temporal_patch_size: int + merge_size: int + + def smart_resize( diff --git a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py index 606b5640602c..b355ef4eb5a0 100644 --- a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py @@ -46,10 +46,23 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, requires_backends from ...utils.import_utils import requires -from .image_processing_rt_detr import RTDetrImageProcessorKwargs +class RTDetrImageProcessorKwargs(ImagesKwargs, total=False): + r""" + format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): + Data format of the annotations. One of "coco_detection" or "coco_panoptic". + do_convert_annotations (`bool`, *optional*, defaults to `True`): + Controls whether to convert the annotations to the format expected by the RT_DETR model. Converts the + bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. + Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. + """ + + format: str | AnnotationFormat + do_convert_annotations: bool + + if is_torch_available(): diff --git a/src/transformers/models/segformer/image_processing_pil_segformer.py b/src/transformers/models/segformer/image_processing_pil_segformer.py index 77514873c59a..ab4f7c53f462 100644 --- a/src/transformers/models/segformer/image_processing_pil_segformer.py +++ b/src/transformers/models/segformer/image_processing_pil_segformer.py @@ -31,10 +31,20 @@ PILImageResampling, SizeDict, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available from ...utils.import_utils import requires -from .image_processing_segformer import SegformerImageProcessorKwargs +class SegformerImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): + Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 + is used for background, and background itself is not included in all classes of a dataset (e.g. + ADE20k). The background label will be replaced by 255. + """ + + do_reduce_labels: bool + + if is_torch_available(): diff --git a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py index 63a2a69b77eb..08abad5bd762 100644 --- a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py +++ b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py @@ -35,9 +35,24 @@ SizeDict, make_nested_list_of_images, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_smolvlm import SmolVLMImageProcessorKwargs +class SmolVLMImageProcessorKwargs(ImagesKwargs, total=False): + """ + do_image_splitting (`bool`, *optional*, defaults to `True`): + Whether to split the image into sub-images concatenated with the original image. They are split into patches + such that each patch has a size of `max_image_size["height"]` x `max_image_size["width"]`. + max_image_size (`Dict`, *optional*, defaults to `{"longest_edge": 364}`): + Maximum resolution of the patches of images accepted by the model. This is a dictionary containing the key "longest_edge". + return_row_col_info (`bool`, *optional*, defaults to `False`): + Whether to return the row and column information of the images. + """ + + do_image_splitting: bool + max_image_size: dict[str, int] + return_row_col_info: bool + + def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: diff --git a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py index 46f1cbb7d25d..73d3f5afd42e 100644 --- a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py +++ b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py @@ -26,9 +26,29 @@ from ...feature_extraction_utils import BatchFeature from ...image_processing_backends import PilBackend from ...image_utils import IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ImageInput, PILImageResampling, SizeDict -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring -from .image_processing_video_llama_3 import VideoLlama3ImageProcessorKwargs +class VideoLlama3ImageProcessorKwargs(ImagesKwargs, total=False): + r""" + min_pixels (`int`, *optional*, defaults to `56 * 56`): + The min pixels of the image to resize the image. + max_pixels (`int`, *optional*, defaults to `28 * 28 * 1280`): + The max pixels of the image to resize the image. + patch_size (`int`, *optional*, defaults to 14): + The spatial patch size of the vision encoder. + temporal_patch_size (`int`, *optional*, defaults to 2): + The temporal patch size of the vision encoder. + merge_size (`int`, *optional*, defaults to 2): + The merge size of the vision encoder to llm encoder. + """ + + min_pixels: int + max_pixels: int + patch_size: int + temporal_patch_size: int + merge_size: int + + def smart_resize( diff --git a/src/transformers/models/yolos/image_processing_pil_yolos.py b/src/transformers/models/yolos/image_processing_pil_yolos.py index 7f5b8385d8b9..12207a1a89bc 100644 --- a/src/transformers/models/yolos/image_processing_pil_yolos.py +++ b/src/transformers/models/yolos/image_processing_pil_yolos.py @@ -33,10 +33,23 @@ get_max_height_width, validate_annotations, ) -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, requires_backends from ...utils.import_utils import requires -from .image_processing_yolos import YolosImageProcessorKwargs +class YolosImageProcessorKwargs(ImagesKwargs, total=False): + r""" + format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): + Data format of the annotations. One of "coco_detection" or "coco_panoptic". + do_convert_annotations (`bool`, *optional*, defaults to `True`): + Controls whether to convert the annotations to the format expected by the YOLOS model. Converts the + bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. + Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. + """ + + format: str | AnnotationFormat + do_convert_annotations: bool + + if is_vision_available(): From 58ddf7f07b95ae259fac879b3d59dcaa9d889ffb Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 18:12:06 +0100 Subject: [PATCH 21/30] style --- .../image_processing_pil_conditional_detr.py | 4 ++-- .../deepseek_vl/image_processing_pil_deepseek_vl.py | 4 ++-- .../image_processing_pil_deepseek_vl_hybrid.py | 4 ++-- .../image_processing_pil_deformable_detr.py | 4 ++-- .../image_processing_pil_efficientloftr.py | 4 ++-- .../models/efficientloftr/modular_efficientloftr.py | 12 +++++++++--- .../image_processing_pil_ernie4_5_vl_moe.py | 4 ++-- .../models/glm46v/image_processing_pil_glm46v.py | 4 ++-- .../glm_image/image_processing_pil_glm_image.py | 4 ++-- .../image_processing_pil_grounding_dino.py | 4 ++-- .../lightglue/image_processing_pil_lightglue.py | 4 ++-- .../image_processing_pil_llava_onevision.py | 4 ++-- .../mask2former/image_processing_pil_mask2former.py | 4 ++-- .../image_processing_pil_paddleocr_vl.py | 4 ++-- .../models/rt_detr/image_processing_pil_rt_detr.py | 4 ++-- .../segformer/image_processing_pil_segformer.py | 4 ++-- .../models/smolvlm/image_processing_pil_smolvlm.py | 4 ++-- .../image_processing_pil_video_llama_3.py | 4 ++-- .../models/yolos/image_processing_pil_yolos.py | 4 ++-- 19 files changed, 45 insertions(+), 39 deletions(-) diff --git a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py index e7e9753227c4..02fcfcf0af15 100644 --- a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py +++ b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py @@ -51,6 +51,8 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, logging, requires_backends from ...utils.import_utils import requires + + class ConditionalDetrImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): @@ -65,8 +67,6 @@ class ConditionalDetrImageProcessorKwargs(ImagesKwargs, total=False): do_convert_annotations: bool - - if is_vision_available(): import PIL.Image if is_torch_available(): diff --git a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py index 283d10907b99..e868830b0220 100644 --- a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py +++ b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py @@ -34,6 +34,8 @@ ) from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring + + class DeepseekVLImageProcessorKwargs(ImagesKwargs, total=False): r""" min_size (`int`, *optional*, defaults to 14): @@ -44,8 +46,6 @@ class DeepseekVLImageProcessorKwargs(ImagesKwargs, total=False): min_size: int - - @auto_docstring class DeepseekVLImageProcessorPil(PilBackend): resample = PILImageResampling.BICUBIC diff --git a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py index 25187581bd1f..6eddb273e567 100644 --- a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py +++ b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py @@ -35,6 +35,8 @@ ) from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring + + class DeepseekVLHybridImageProcessorKwargs(ImagesKwargs, total=False): r""" min_size (`int`, *optional*, defaults to 14): @@ -57,8 +59,6 @@ class DeepseekVLHybridImageProcessorKwargs(ImagesKwargs, total=False): high_res_image_std: float | list[float] | tuple[float, ...] - - @auto_docstring class DeepseekVLHybridImageProcessorPil(PilBackend): resample = PILImageResampling.BICUBIC diff --git a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py index e0949198823e..78c4829b8d40 100644 --- a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py +++ b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py @@ -50,6 +50,8 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available from ...utils.import_utils import requires, requires_backends + + class DeformableDetrImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): @@ -64,8 +66,6 @@ class DeformableDetrImageProcessorKwargs(ImagesKwargs, total=False): do_convert_annotations: bool - - if is_vision_available(): import PIL.Image if is_torch_available(): diff --git a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py index 4768ec7c9e8d..0f47785f0ad5 100644 --- a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py @@ -6,6 +6,8 @@ # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 +from typing import TYPE_CHECKING + import numpy as np from PIL import Image, ImageDraw @@ -39,8 +41,6 @@ class EfficientLoFTRImageProcessorKwargs(ImagesKwargs, total=False): do_grayscale: bool -from typing import TYPE_CHECKING - if TYPE_CHECKING: from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput diff --git a/src/transformers/models/efficientloftr/modular_efficientloftr.py b/src/transformers/models/efficientloftr/modular_efficientloftr.py index f2d8e037e735..86d8d34eba70 100644 --- a/src/transformers/models/efficientloftr/modular_efficientloftr.py +++ b/src/transformers/models/efficientloftr/modular_efficientloftr.py @@ -1,10 +1,16 @@ -import torch +from typing import TYPE_CHECKING -from ...utils import TensorType +from ...utils import TensorType, is_torch_available from ...utils.import_utils import requires from ..superglue.image_processing_pil_superglue import SuperGlueImageProcessorPil from ..superglue.image_processing_superglue import SuperGlueImageProcessor -from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput + + +if is_torch_available(): + import torch + +if TYPE_CHECKING: + from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput class EfficientLoFTRImageProcessor(SuperGlueImageProcessor): diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py index bdff7fa1f489..e13ae9efe3c7 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py @@ -27,6 +27,8 @@ from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, logging + + class Ernie4_5_VLMoeImageProcessorKwargs(ImagesKwargs, total=False): r""" patch_size (`int`, *optional*, defaults to 14): @@ -42,8 +44,6 @@ class Ernie4_5_VLMoeImageProcessorKwargs(ImagesKwargs, total=False): merge_size: int - - logger = logging.get_logger(__name__) diff --git a/src/transformers/models/glm46v/image_processing_pil_glm46v.py b/src/transformers/models/glm46v/image_processing_pil_glm46v.py index de14d5527e73..261c921e1ce8 100644 --- a/src/transformers/models/glm46v/image_processing_pil_glm46v.py +++ b/src/transformers/models/glm46v/image_processing_pil_glm46v.py @@ -28,6 +28,8 @@ from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring + + class Glm46VImageProcessorKwargs(ImagesKwargs, total=False): """ patch_size (`int`, *optional*, defaults to 14): @@ -43,8 +45,6 @@ class Glm46VImageProcessorKwargs(ImagesKwargs, total=False): merge_size: int - - def smart_resize( num_frames: int, height: int, diff --git a/src/transformers/models/glm_image/image_processing_pil_glm_image.py b/src/transformers/models/glm_image/image_processing_pil_glm_image.py index a674ed4c0844..355bb04adb67 100644 --- a/src/transformers/models/glm_image/image_processing_pil_glm_image.py +++ b/src/transformers/models/glm_image/image_processing_pil_glm_image.py @@ -28,6 +28,8 @@ from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring + + class GlmImageImageProcessorKwargs(ImagesKwargs, total=False): r""" min_pixels (`int`, *optional*, defaults to `56 * 56`): @@ -49,8 +51,6 @@ class GlmImageImageProcessorKwargs(ImagesKwargs, total=False): merge_size: int - - def smart_resize( height: int, width: int, diff --git a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py index 1c60ec5c4bdc..146a331d2eda 100644 --- a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py +++ b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py @@ -56,6 +56,8 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, requires_backends from ...utils.import_utils import requires + + class GroundingDinoImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): @@ -70,8 +72,6 @@ class GroundingDinoImageProcessorKwargs(ImagesKwargs, total=False): do_convert_annotations: bool - - if TYPE_CHECKING: from .modeling_grounding_dino import GroundingDinoObjectDetectionOutput diff --git a/src/transformers/models/lightglue/image_processing_pil_lightglue.py b/src/transformers/models/lightglue/image_processing_pil_lightglue.py index 974ca1e831ee..3349933ff066 100644 --- a/src/transformers/models/lightglue/image_processing_pil_lightglue.py +++ b/src/transformers/models/lightglue/image_processing_pil_lightglue.py @@ -38,6 +38,8 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available from ...utils.import_utils import requires + + class LightGlueImageProcessorKwargs(ImagesKwargs, total=False): r""" do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`): @@ -47,8 +49,6 @@ class LightGlueImageProcessorKwargs(ImagesKwargs, total=False): do_grayscale: bool - - if TYPE_CHECKING: from .modeling_lightglue import LightGlueKeypointMatchingOutput if is_torch_available(): diff --git a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py index 81fd109861b9..23534a65d70f 100644 --- a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py +++ b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py @@ -34,6 +34,8 @@ ) from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring + + class LlavaOnevisionImageProcessorKwargs(ImagesKwargs, total=False): r""" image_grid_pinpoints (`list[list[int]]`, *optional*): @@ -45,8 +47,6 @@ class LlavaOnevisionImageProcessorKwargs(ImagesKwargs, total=False): image_grid_pinpoints: list[list[int]] - - @auto_docstring class LlavaOnevisionImageProcessorPil(PilBackend): model_input_names = ["pixel_values", "image_sizes", "batch_num_images"] diff --git a/src/transformers/models/mask2former/image_processing_pil_mask2former.py b/src/transformers/models/mask2former/image_processing_pil_mask2former.py index 7afc89fac40d..ba5e91bb323e 100644 --- a/src/transformers/models/mask2former/image_processing_pil_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_pil_mask2former.py @@ -42,6 +42,8 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, logging, requires_backends from ...utils.import_utils import requires + + class Mask2FormerImageProcessorKwargs(ImagesKwargs, total=False): r""" ignore_index (`int`, *optional*): @@ -68,8 +70,6 @@ class Mask2FormerImageProcessorKwargs(ImagesKwargs, total=False): pad_size: SizeDict | None - - if is_torch_available(): import torch from torch import nn diff --git a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py index e278a09dec84..ac639892640f 100644 --- a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py @@ -33,6 +33,8 @@ from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring + + class PaddleOCRVLImageProcessorKwargs(ImagesKwargs, total=False): r""" patch_size (`int`, *optional*, defaults to 14): @@ -50,8 +52,6 @@ class PaddleOCRVLImageProcessorKwargs(ImagesKwargs, total=False): merge_size: int - - def smart_resize( height: int, width: int, diff --git a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py index b355ef4eb5a0..9ef8d43a5971 100644 --- a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py @@ -49,6 +49,8 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, requires_backends from ...utils.import_utils import requires + + class RTDetrImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): @@ -63,8 +65,6 @@ class RTDetrImageProcessorKwargs(ImagesKwargs, total=False): do_convert_annotations: bool - - if is_torch_available(): import torch diff --git a/src/transformers/models/segformer/image_processing_pil_segformer.py b/src/transformers/models/segformer/image_processing_pil_segformer.py index ab4f7c53f462..85dc85636a70 100644 --- a/src/transformers/models/segformer/image_processing_pil_segformer.py +++ b/src/transformers/models/segformer/image_processing_pil_segformer.py @@ -34,6 +34,8 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_torchvision_available from ...utils.import_utils import requires + + class SegformerImageProcessorKwargs(ImagesKwargs, total=False): r""" do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): @@ -45,8 +47,6 @@ class SegformerImageProcessorKwargs(ImagesKwargs, total=False): do_reduce_labels: bool - - if is_torch_available(): pass if is_torchvision_available(): diff --git a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py index 08abad5bd762..c63c0060545e 100644 --- a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py +++ b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py @@ -37,6 +37,8 @@ ) from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring + + class SmolVLMImageProcessorKwargs(ImagesKwargs, total=False): """ do_image_splitting (`bool`, *optional*, defaults to `True`): @@ -53,8 +55,6 @@ class SmolVLMImageProcessorKwargs(ImagesKwargs, total=False): return_row_col_info: bool - - def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarray: """Make pixel mask: 1=valid, 0=padding. Images are CHW.""" h, w = image.shape[-2:] diff --git a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py index 73d3f5afd42e..5272c7465b2b 100644 --- a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py +++ b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py @@ -28,6 +28,8 @@ from ...image_utils import IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, ImageInput, PILImageResampling, SizeDict from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring + + class VideoLlama3ImageProcessorKwargs(ImagesKwargs, total=False): r""" min_pixels (`int`, *optional*, defaults to `56 * 56`): @@ -49,8 +51,6 @@ class VideoLlama3ImageProcessorKwargs(ImagesKwargs, total=False): merge_size: int - - def smart_resize( height: int, width: int, factor: int = 28, min_pixels: int = 56 * 56, max_pixels: int = 14 * 14 * 4 * 1280 ): diff --git a/src/transformers/models/yolos/image_processing_pil_yolos.py b/src/transformers/models/yolos/image_processing_pil_yolos.py index 12207a1a89bc..63a5553f02ed 100644 --- a/src/transformers/models/yolos/image_processing_pil_yolos.py +++ b/src/transformers/models/yolos/image_processing_pil_yolos.py @@ -36,6 +36,8 @@ from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, is_torch_available, is_vision_available, requires_backends from ...utils.import_utils import requires + + class YolosImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): @@ -50,8 +52,6 @@ class YolosImageProcessorKwargs(ImagesKwargs, total=False): do_convert_annotations: bool - - if is_vision_available(): import PIL.Image if is_torch_available(): From f5956fded0246650dff1d9694163802db958e9fa Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 19:33:36 +0100 Subject: [PATCH 22/30] fix check copies --- .../models/aria/image_processing_pil_aria.py | 2 +- .../models/beit/image_processing_pil_beit.py | 2 +- .../image_processing_pil_bridgetower.py | 2 +- .../convnext/image_processing_pil_convnext.py | 2 +- .../models/detr/image_processing_pil_detr.py | 12 ++++++------ .../models/donut/image_processing_pil_donut.py | 2 +- .../models/dpt/image_processing_pil_dpt.py | 4 ++-- .../image_processing_pil_efficientnet.py | 2 +- .../models/eomt/image_processing_pil_eomt.py | 10 +++++----- .../models/flava/image_processing_pil_flava.py | 14 +++++++------- .../models/fuyu/image_processing_pil_fuyu.py | 6 +++--- .../gemma3/image_processing_pil_gemma3.py | 2 +- .../models/glm4v/image_processing_pil_glm4v.py | 2 +- .../models/glpn/image_processing_pil_glpn.py | 2 +- .../got_ocr2/image_processing_pil_got_ocr2.py | 6 +++--- .../idefics/image_processing_pil_idefics.py | 6 +++--- .../idefics2/image_processing_pil_idefics2.py | 8 ++++---- .../idefics3/image_processing_pil_idefics3.py | 8 ++++---- .../imagegpt/image_processing_pil_imagegpt.py | 2 +- .../models/janus/image_processing_pil_janus.py | 2 +- .../image_processing_pil_kosmos2_5.py | 4 ++-- .../image_processing_pil_layoutlmv2.py | 6 +++--- .../image_processing_pil_layoutlmv3.py | 6 +++--- .../image_processing_pil_llava_next.py | 2 +- .../image_processing_pil_maskformer.py | 14 +++++++------- .../mllama/image_processing_pil_mllama.py | 14 +++++++------- .../image_processing_pil_mobilenet_v2.py | 2 +- .../image_processing_pil_mobilevit.py | 2 +- .../nougat/image_processing_pil_nougat.py | 2 +- .../image_processing_pil_oneformer.py | 18 +++++++++--------- .../models/ovis2/image_processing_pil_ovis2.py | 12 ++++++------ .../owlvit/image_processing_pil_owlvit.py | 4 ++-- .../image_processing_pil_pix2struct.py | 6 +++--- .../pixtral/image_processing_pil_pixtral.py | 6 +++--- .../image_processing_pil_poolformer.py | 2 +- ...age_processing_pil_prompt_depth_anything.py | 2 +- .../qwen2_vl/image_processing_pil_qwen2_vl.py | 4 ++-- .../models/sam/image_processing_pil_sam.py | 2 +- .../seggpt/image_processing_pil_seggpt.py | 4 ++-- .../siglip2/image_processing_pil_siglip2.py | 4 ++-- .../image_processing_pil_superglue.py | 2 +- .../image_processing_pil_superpoint.py | 2 +- .../swin2sr/image_processing_pil_swin2sr.py | 2 +- .../textnet/image_processing_pil_textnet.py | 2 +- .../models/tvp/image_processing_pil_tvp.py | 2 +- .../models/vilt/image_processing_pil_vilt.py | 2 +- .../vitmatte/image_processing_pil_vitmatte.py | 2 +- .../vitpose/image_processing_pil_vitpose.py | 16 ++++++++-------- .../zoedepth/image_processing_pil_zoedepth.py | 4 ++-- 49 files changed, 123 insertions(+), 123 deletions(-) diff --git a/src/transformers/models/aria/image_processing_pil_aria.py b/src/transformers/models/aria/image_processing_pil_aria.py index ba40f184fab7..cd7233e9648c 100644 --- a/src/transformers/models/aria/image_processing_pil_aria.py +++ b/src/transformers/models/aria/image_processing_pil_aria.py @@ -28,7 +28,7 @@ from ...utils import TensorType, auto_docstring -# Copied from transformers.models.aria.image_processing_aria.AriaImageProcessorKwargs +# Adapted from transformers.models.aria.image_processing_aria.AriaImageProcessorKwargs class AriaImageProcessorKwargs(ImagesKwargs, total=False): r""" max_image_size (`int`, *optional*, defaults to `self.max_image_size`): diff --git a/src/transformers/models/beit/image_processing_pil_beit.py b/src/transformers/models/beit/image_processing_pil_beit.py index 2daa20c9172c..e3ccf12e909b 100644 --- a/src/transformers/models/beit/image_processing_pil_beit.py +++ b/src/transformers/models/beit/image_processing_pil_beit.py @@ -30,7 +30,7 @@ from ...utils.import_utils import requires -# Copied from transformers.models.beit.image_processing_beit.BeitImageProcessorKwargs +# Adapted from transformers.models.beit.image_processing_beit.BeitImageProcessorKwargs class BeitImageProcessorKwargs(ImagesKwargs, total=False): r""" do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): diff --git a/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py b/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py index c18f12d6662f..e6d92a4b3f13 100644 --- a/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py +++ b/src/transformers/models/bridgetower/image_processing_pil_bridgetower.py @@ -29,7 +29,7 @@ from ...utils import TensorType, auto_docstring -# Copied from transformers.models.bridgetower.image_processing_bridgetower.BridgeTowerImageProcessorKwargs +# Adapted from transformers.models.bridgetower.image_processing_bridgetower.BridgeTowerImageProcessorKwargs class BridgeTowerImageProcessorKwargs(ImagesKwargs, total=False): r""" size_divisor (`int`, *optional*, defaults to `self.size_divisor`): diff --git a/src/transformers/models/convnext/image_processing_pil_convnext.py b/src/transformers/models/convnext/image_processing_pil_convnext.py index 0e9691d2cce7..37c656d00c6b 100644 --- a/src/transformers/models/convnext/image_processing_pil_convnext.py +++ b/src/transformers/models/convnext/image_processing_pil_convnext.py @@ -29,7 +29,7 @@ from ...utils import TensorType, auto_docstring -# Copied from transformers.models.convnext.image_processing_convnext.ConvNextImageProcessorKwargs +# Adapted from transformers.models.convnext.image_processing_convnext.ConvNextImageProcessorKwargs class ConvNextImageProcessorKwargs(ImagesKwargs, total=False): r""" crop_pct (`float`, *optional*, defaults to `self.crop_pct`): diff --git a/src/transformers/models/detr/image_processing_pil_detr.py b/src/transformers/models/detr/image_processing_pil_detr.py index 264ba4c10bf4..14c5769549d8 100644 --- a/src/transformers/models/detr/image_processing_pil_detr.py +++ b/src/transformers/models/detr/image_processing_pil_detr.py @@ -247,7 +247,7 @@ def prepare_coco_panoptic_annotation( return new_target -# Copied from transformers.models.detr.image_processing_detr.DetrImageProcessorKwargs +# Adapted from transformers.models.detr.image_processing_detr.DetrImageProcessorKwargs class DetrImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): @@ -262,7 +262,7 @@ class DetrImageProcessorKwargs(ImagesKwargs, total=False): do_convert_annotations: bool -# Copied from transformers.models.detr.image_processing_detr.binary_mask_to_rle +# Adapted from transformers.models.detr.image_processing_detr.binary_mask_to_rle def binary_mask_to_rle(mask): """ Converts given binary mask of shape `(height, width)` to the run-length encoding (RLE) format. @@ -287,7 +287,7 @@ def binary_mask_to_rle(mask): return list(runs) -# Copied from transformers.models.detr.image_processing_detr.check_segment_validity +# Adapted from transformers.models.detr.image_processing_detr.check_segment_validity def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overlap_mask_area_threshold=0.8): # Get the mask associated with the k class mask_k = mask_labels == k @@ -306,7 +306,7 @@ def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overl return mask_exists, mask_k -# Copied from transformers.models.detr.image_processing_detr.compute_segments +# Adapted from transformers.models.detr.image_processing_detr.compute_segments def compute_segments( mask_probs, pred_scores, @@ -370,7 +370,7 @@ def compute_segments( return segmentation, segments -# Copied from transformers.models.detr.image_processing_detr.convert_segmentation_to_rle +# Adapted from transformers.models.detr.image_processing_detr.convert_segmentation_to_rle def convert_segmentation_to_rle(segmentation): """ Converts given segmentation map of shape `(height, width)` to the run-length encoding (RLE) format. @@ -394,7 +394,7 @@ def convert_segmentation_to_rle(segmentation): return run_length_encodings -# Copied from transformers.models.detr.image_processing_detr.remove_low_and_no_objects +# Adapted from transformers.models.detr.image_processing_detr.remove_low_and_no_objects def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): """ Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and diff --git a/src/transformers/models/donut/image_processing_pil_donut.py b/src/transformers/models/donut/image_processing_pil_donut.py index d693a9f9e855..b2865b556bc8 100644 --- a/src/transformers/models/donut/image_processing_pil_donut.py +++ b/src/transformers/models/donut/image_processing_pil_donut.py @@ -29,7 +29,7 @@ from ...utils import TensorType, auto_docstring -# Copied from transformers.models.donut.image_processing_donut.DonutImageProcessorKwargs +# Adapted from transformers.models.donut.image_processing_donut.DonutImageProcessorKwargs class DonutImageProcessorKwargs(ImagesKwargs, total=False): r""" do_thumbnail (`bool`, *optional*, defaults to `self.do_thumbnail`): diff --git a/src/transformers/models/dpt/image_processing_pil_dpt.py b/src/transformers/models/dpt/image_processing_pil_dpt.py index f418df82b763..6f770cac4e5f 100644 --- a/src/transformers/models/dpt/image_processing_pil_dpt.py +++ b/src/transformers/models/dpt/image_processing_pil_dpt.py @@ -41,7 +41,7 @@ logger = logging.get_logger(__name__) -# Copied from transformers.models.dpt.image_processing_dpt.DPTImageProcessorKwargs +# Adapted from transformers.models.dpt.image_processing_dpt.DPTImageProcessorKwargs class DPTImageProcessorKwargs(ImagesKwargs, total=False): r""" ensure_multiple_of (`int`, *optional*, defaults to 1): @@ -62,7 +62,7 @@ class DPTImageProcessorKwargs(ImagesKwargs, total=False): do_reduce_labels: bool -# Copied from transformers.models.dpt.image_processing_dpt.get_resize_output_image_size +# Adapted from transformers.models.dpt.image_processing_dpt.get_resize_output_image_size def get_resize_output_image_size( input_image: np.ndarray, output_size: int | Iterable[int], diff --git a/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py b/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py index 043414c25c74..867a67d24582 100644 --- a/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py +++ b/src/transformers/models/efficientnet/image_processing_pil_efficientnet.py @@ -27,7 +27,7 @@ from ...utils import TensorType, auto_docstring -# Copied from transformers.models.efficientnet.image_processing_efficientnet.EfficientNetImageProcessorKwargs +# Adapted from transformers.models.efficientnet.image_processing_efficientnet.EfficientNetImageProcessorKwargs class EfficientNetImageProcessorKwargs(ImagesKwargs, total=False): """ rescale_offset (`bool`, *optional*, defaults to `self.rescale_offset`): diff --git a/src/transformers/models/eomt/image_processing_pil_eomt.py b/src/transformers/models/eomt/image_processing_pil_eomt.py index 6fa4a7d85484..841537838a09 100644 --- a/src/transformers/models/eomt/image_processing_pil_eomt.py +++ b/src/transformers/models/eomt/image_processing_pil_eomt.py @@ -77,7 +77,7 @@ def convert_segmentation_map_to_binary_masks( return binary_masks.astype(np.float32), labels.astype(np.int64) -# Copied from transformers.models.eomt.image_processing_eomt.check_segment_validity +# Adapted from transformers.models.eomt.image_processing_eomt.check_segment_validity def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overlap_mask_area_threshold=0.8): # Get the mask associated with the k class mask_k = mask_labels == k @@ -100,7 +100,7 @@ def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overl return mask_exists, final_mask -# Copied from transformers.models.eomt.image_processing_eomt.EomtImageProcessorKwargs +# Adapted from transformers.models.eomt.image_processing_eomt.EomtImageProcessorKwargs class EomtImageProcessorKwargs(ImagesKwargs, total=False): r""" do_split_image (`bool`, *optional*, defaults to `self.do_split_image`): @@ -116,7 +116,7 @@ class EomtImageProcessorKwargs(ImagesKwargs, total=False): ignore_index: int | None -# Copied from transformers.models.eomt.image_processing_eomt.compute_segments +# Adapted from transformers.models.eomt.image_processing_eomt.compute_segments def compute_segments( mask_probs, pred_scores, @@ -171,7 +171,7 @@ def compute_segments( return segmentation, segments -# Copied from transformers.models.eomt.image_processing_eomt.get_target_size +# Adapted from transformers.models.eomt.image_processing_eomt.get_target_size def get_target_size(size_dict: dict[str, int]) -> tuple[int, int]: """Returns the height and width from a size dict.""" target_height = size_dict["shortest_edge"] @@ -180,7 +180,7 @@ def get_target_size(size_dict: dict[str, int]) -> tuple[int, int]: return target_height, target_width -# Copied from transformers.models.eomt.image_processing_eomt.remove_low_and_no_objects +# Adapted from transformers.models.eomt.image_processing_eomt.remove_low_and_no_objects def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): """ Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and diff --git a/src/transformers/models/flava/image_processing_pil_flava.py b/src/transformers/models/flava/image_processing_pil_flava.py index 65635ca479a8..ef2228d6ddf1 100644 --- a/src/transformers/models/flava/image_processing_pil_flava.py +++ b/src/transformers/models/flava/image_processing_pil_flava.py @@ -43,24 +43,24 @@ import torch -# Copied from transformers.models.flava.image_processing_flava.FLAVA_CODEBOOK_MEAN +# Adapted from transformers.models.flava.image_processing_flava.FLAVA_CODEBOOK_MEAN FLAVA_CODEBOOK_MEAN = [0.0, 0.0, 0.0] -# Copied from transformers.models.flava.image_processing_flava.FLAVA_CODEBOOK_STD +# Adapted from transformers.models.flava.image_processing_flava.FLAVA_CODEBOOK_STD FLAVA_CODEBOOK_STD = [1.0, 1.0, 1.0] -# Copied from transformers.models.flava.image_processing_flava.FLAVA_IMAGE_MEAN +# Adapted from transformers.models.flava.image_processing_flava.FLAVA_IMAGE_MEAN # These values are taken from CLIP FLAVA_IMAGE_MEAN = OPENAI_CLIP_MEAN -# Copied from transformers.models.flava.image_processing_flava.FLAVA_IMAGE_STD +# Adapted from transformers.models.flava.image_processing_flava.FLAVA_IMAGE_STD FLAVA_IMAGE_STD = OPENAI_CLIP_STD -# Copied from transformers.models.flava.image_processing_flava.LOGIT_LAPLACE_EPS +# Adapted from transformers.models.flava.image_processing_flava.LOGIT_LAPLACE_EPS LOGIT_LAPLACE_EPS: float = 0.1 -# Copied from transformers.models.flava.image_processing_flava.FlavaImageProcessorKwargs +# Adapted from transformers.models.flava.image_processing_flava.FlavaImageProcessorKwargs class FlavaImageProcessorKwargs(ImagesKwargs, total=False): """ return_image_mask (`bool`, *optional*, defaults to `False`): @@ -146,7 +146,7 @@ class FlavaImageProcessorKwargs(ImagesKwargs, total=False): codebook_image_std: float | Iterable[float] -# Copied from transformers.models.flava.image_processing_flava.FlavaMaskingGenerator +# Adapted from transformers.models.flava.image_processing_flava.FlavaMaskingGenerator # Inspired from https://github.com/microsoft/unilm/blob/master/beit/masking_generator.py class FlavaMaskingGenerator: def __init__( diff --git a/src/transformers/models/fuyu/image_processing_pil_fuyu.py b/src/transformers/models/fuyu/image_processing_pil_fuyu.py index 199e5903d5fc..4e4787f4b774 100644 --- a/src/transformers/models/fuyu/image_processing_pil_fuyu.py +++ b/src/transformers/models/fuyu/image_processing_pil_fuyu.py @@ -40,7 +40,7 @@ import torch -# Copied from transformers.models.fuyu.image_processing_fuyu.FuyuBatchFeature +# Adapted from transformers.models.fuyu.image_processing_fuyu.FuyuBatchFeature class FuyuBatchFeature(BatchFeature): """ BatchFeature class for Fuyu image processor and processor. @@ -152,7 +152,7 @@ def _to(elem): return self -# Copied from transformers.models.fuyu.image_processing_fuyu.FuyuImagesKwargs +# Adapted from transformers.models.fuyu.image_processing_fuyu.FuyuImagesKwargs class FuyuImagesKwargs(ImagesKwargs, total=False): r""" patch_size (`dict[str, int]`, *optional*, defaults to `{"height": 30, "width": 30}`): @@ -168,7 +168,7 @@ class FuyuImagesKwargs(ImagesKwargs, total=False): padding_mode: str -# Copied from transformers.models.fuyu.image_processing_fuyu.make_list_of_list_of_images +# Adapted from transformers.models.fuyu.image_processing_fuyu.make_list_of_list_of_images def make_list_of_list_of_images( images: list[list[ImageInput]] | list[ImageInput] | ImageInput, ) -> list[list[ImageInput]]: diff --git a/src/transformers/models/gemma3/image_processing_pil_gemma3.py b/src/transformers/models/gemma3/image_processing_pil_gemma3.py index 4e8462870467..f8580585a062 100644 --- a/src/transformers/models/gemma3/image_processing_pil_gemma3.py +++ b/src/transformers/models/gemma3/image_processing_pil_gemma3.py @@ -35,7 +35,7 @@ ) -# Copied from transformers.models.gemma3.image_processing_gemma3.Gemma3ImageProcessorKwargs +# Adapted from transformers.models.gemma3.image_processing_gemma3.Gemma3ImageProcessorKwargs class Gemma3ImageProcessorKwargs(ImagesKwargs, total=False): """ do_pan_and_scan (`bool`, *optional*): diff --git a/src/transformers/models/glm4v/image_processing_pil_glm4v.py b/src/transformers/models/glm4v/image_processing_pil_glm4v.py index 0be34b98efd4..64ebfee422cf 100644 --- a/src/transformers/models/glm4v/image_processing_pil_glm4v.py +++ b/src/transformers/models/glm4v/image_processing_pil_glm4v.py @@ -33,7 +33,7 @@ logger = logging.get_logger(__name__) -# Copied from transformers.models.glm4v.image_processing_glm4v.smart_resize +# Adapted from transformers.models.glm4v.image_processing_glm4v.smart_resize def smart_resize( num_frames: int, height: int, diff --git a/src/transformers/models/glpn/image_processing_pil_glpn.py b/src/transformers/models/glpn/image_processing_pil_glpn.py index 964f426e01e4..fbfcfd8569d7 100644 --- a/src/transformers/models/glpn/image_processing_pil_glpn.py +++ b/src/transformers/models/glpn/image_processing_pil_glpn.py @@ -31,7 +31,7 @@ logger = logging.get_logger(__name__) -# Copied from transformers.models.glpn.image_processing_glpn.GLPNImageProcessorKwargs +# Adapted from transformers.models.glpn.image_processing_glpn.GLPNImageProcessorKwargs class GLPNImageProcessorKwargs(ImagesKwargs, total=False): """ size_divisor (`int`, *optional*, defaults to 32): diff --git a/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py b/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py index 8d2395b35d14..e016a390f5ec 100644 --- a/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py +++ b/src/transformers/models/got_ocr2/image_processing_pil_got_ocr2.py @@ -36,7 +36,7 @@ ) -# Copied from transformers.models.got_ocr2.image_processing_got_ocr2.GotOcr2ImageProcessorKwargs +# Adapted from transformers.models.got_ocr2.image_processing_got_ocr2.GotOcr2ImageProcessorKwargs class GotOcr2ImageProcessorKwargs(ImagesKwargs, total=False): r""" crop_to_patches (`bool`, *optional*, defaults to `self.crop_to_patches`): @@ -55,7 +55,7 @@ class GotOcr2ImageProcessorKwargs(ImagesKwargs, total=False): max_patches: int -# Copied from transformers.models.got_ocr2.image_processing_got_ocr2.get_all_supported_aspect_ratios +# Adapted from transformers.models.got_ocr2.image_processing_got_ocr2.get_all_supported_aspect_ratios @lru_cache(maxsize=10) def get_all_supported_aspect_ratios(min_image_tiles: int, max_image_tiles: int) -> list[tuple[int, int]]: """ @@ -91,7 +91,7 @@ def get_all_supported_aspect_ratios(min_image_tiles: int, max_image_tiles: int) return aspect_ratios -# Copied from transformers.models.got_ocr2.image_processing_got_ocr2.get_optimal_tiled_canvas +# Adapted from transformers.models.got_ocr2.image_processing_got_ocr2.get_optimal_tiled_canvas @lru_cache(maxsize=100) def get_optimal_tiled_canvas( original_image_size: tuple[int, int], diff --git a/src/transformers/models/idefics/image_processing_pil_idefics.py b/src/transformers/models/idefics/image_processing_pil_idefics.py index 37eba9839eb2..d74ade839489 100644 --- a/src/transformers/models/idefics/image_processing_pil_idefics.py +++ b/src/transformers/models/idefics/image_processing_pil_idefics.py @@ -21,14 +21,14 @@ from ...utils import auto_docstring, is_torch_available -# Copied from transformers.models.idefics.image_processing_idefics.IDEFICS_STANDARD_MEAN +# Adapted from transformers.models.idefics.image_processing_idefics.IDEFICS_STANDARD_MEAN IDEFICS_STANDARD_MEAN = [0.48145466, 0.4578275, 0.40821073] -# Copied from transformers.models.idefics.image_processing_idefics.IDEFICS_STANDARD_STD +# Adapted from transformers.models.idefics.image_processing_idefics.IDEFICS_STANDARD_STD IDEFICS_STANDARD_STD = [0.26862954, 0.26130258, 0.27577711] -# Copied from transformers.models.idefics.image_processing_idefics.IdeficsImageProcessorKwargs +# Adapted from transformers.models.idefics.image_processing_idefics.IdeficsImageProcessorKwargs class IdeficsImageProcessorKwargs(ImagesKwargs, total=False): r""" transform (`Callable`, *optional*, defaults to `None`): diff --git a/src/transformers/models/idefics2/image_processing_pil_idefics2.py b/src/transformers/models/idefics2/image_processing_pil_idefics2.py index 26d276dbd33c..641bdf0d2682 100644 --- a/src/transformers/models/idefics2/image_processing_pil_idefics2.py +++ b/src/transformers/models/idefics2/image_processing_pil_idefics2.py @@ -45,7 +45,7 @@ def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndar return mask -# Copied from transformers.models.idefics2.image_processing_idefics2.Idefics2ImageProcessorKwargs +# Adapted from transformers.models.idefics2.image_processing_idefics2.Idefics2ImageProcessorKwargs class Idefics2ImageProcessorKwargs(ImagesKwargs, total=False): r""" do_image_splitting (`bool`, *optional*, defaults to `self.do_image_splitting`): @@ -55,7 +55,7 @@ class Idefics2ImageProcessorKwargs(ImagesKwargs, total=False): do_image_splitting: bool -# Copied from transformers.models.idefics2.image_processing_idefics2.convert_to_rgb +# Adapted from transformers.models.idefics2.image_processing_idefics2.convert_to_rgb def convert_to_rgb(image: ImageInput) -> ImageInput: """ Converts an image to RGB format. Only converts if the image is of type PIL.Image.Image, otherwise returns the image @@ -74,7 +74,7 @@ def convert_to_rgb(image: ImageInput) -> ImageInput: return alpha_composite -# Copied from transformers.models.idefics2.image_processing_idefics2.get_max_height_width +# Adapted from transformers.models.idefics2.image_processing_idefics2.get_max_height_width def get_max_height_width(images_list: list[list["torch.Tensor|np.ndarray"]]) -> tuple[int, int]: """ Get the maximum height and width across all images in a batch. @@ -89,7 +89,7 @@ def get_max_height_width(images_list: list[list["torch.Tensor|np.ndarray"]]) -> return (max_height, max_width) -# Copied from transformers.models.idefics2.image_processing_idefics2.get_resize_output_image_size +# Adapted from transformers.models.idefics2.image_processing_idefics2.get_resize_output_image_size def get_resize_output_image_size(image, size: SizeDict) -> tuple[int, int]: """ Get the output size of the image after resizing given a dictionary specifying the max and min sizes. diff --git a/src/transformers/models/idefics3/image_processing_pil_idefics3.py b/src/transformers/models/idefics3/image_processing_pil_idefics3.py index 304fb1befa34..7f2ffaf50892 100644 --- a/src/transformers/models/idefics3/image_processing_pil_idefics3.py +++ b/src/transformers/models/idefics3/image_processing_pil_idefics3.py @@ -45,11 +45,11 @@ def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndar return mask -# Copied from transformers.models.idefics3.image_processing_idefics3.MAX_IMAGE_SIZE +# Adapted from transformers.models.idefics3.image_processing_idefics3.MAX_IMAGE_SIZE MAX_IMAGE_SIZE = 4096 # 4k resolution as absolute maximum -# Copied from transformers.models.idefics3.image_processing_idefics3.Idefics3ImageProcessorKwargs +# Adapted from transformers.models.idefics3.image_processing_idefics3.Idefics3ImageProcessorKwargs class Idefics3ImageProcessorKwargs(ImagesKwargs, total=False): """ do_image_splitting (`bool`, *optional*, defaults to `True`): @@ -66,7 +66,7 @@ class Idefics3ImageProcessorKwargs(ImagesKwargs, total=False): return_row_col_info: bool -# Copied from transformers.models.idefics3.image_processing_idefics3._resize_output_size_rescale_to_max_len +# Adapted from transformers.models.idefics3.image_processing_idefics3._resize_output_size_rescale_to_max_len def _resize_output_size_rescale_to_max_len( height: int, width: int, min_len: int | None = 1, max_len: int | None = None ) -> tuple[int, int]: @@ -104,7 +104,7 @@ def _resize_output_size_rescale_to_max_len( return height, width -# Copied from transformers.models.idefics3.image_processing_idefics3._resize_output_size_scale_below_upper_bound +# Adapted from transformers.models.idefics3.image_processing_idefics3._resize_output_size_scale_below_upper_bound def _resize_output_size_scale_below_upper_bound( height: int, width: int, max_len: dict[str, int] | None = None ) -> tuple[int, int]: diff --git a/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py b/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py index 5213a6a10d4a..cc287faa3510 100644 --- a/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py +++ b/src/transformers/models/imagegpt/image_processing_pil_imagegpt.py @@ -50,7 +50,7 @@ def color_quantize(x, clusters): return np.argmin(d, axis=1) -# Copied from transformers.models.imagegpt.image_processing_imagegpt.ImageGPTImageProcessorKwargs +# Adapted from transformers.models.imagegpt.image_processing_imagegpt.ImageGPTImageProcessorKwargs class ImageGPTImageProcessorKwargs(ImagesKwargs, total=False): r""" clusters (`np.ndarray` or `list[list[int]]` or `torch.Tensor`, *optional*, defaults to `self.clusters`): diff --git a/src/transformers/models/janus/image_processing_pil_janus.py b/src/transformers/models/janus/image_processing_pil_janus.py index e3c44f37f7e3..8a1350f3366b 100644 --- a/src/transformers/models/janus/image_processing_pil_janus.py +++ b/src/transformers/models/janus/image_processing_pil_janus.py @@ -40,7 +40,7 @@ ) -# Copied from transformers.models.janus.image_processing_janus.JanusImageProcessorKwargs +# Adapted from transformers.models.janus.image_processing_janus.JanusImageProcessorKwargs class JanusImageProcessorKwargs(ImagesKwargs, total=False): r""" min_size (`int`, *optional*, defaults to 14): diff --git a/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py b/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py index 84695ff2f24c..bf420fa78daa 100644 --- a/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py +++ b/src/transformers/models/kosmos2_5/image_processing_pil_kosmos2_5.py @@ -29,7 +29,7 @@ import torch -# Copied from transformers.models.kosmos2_5.image_processing_kosmos2_5.Kosmos2_5ImageProcessorKwargs +# Adapted from transformers.models.kosmos2_5.image_processing_kosmos2_5.Kosmos2_5ImageProcessorKwargs class Kosmos2_5ImageProcessorKwargs(ImagesKwargs, total=False): r""" patch_size (`Dict[str, int]`, *optional*, defaults to `{"height": 16, "width": 16}`): @@ -43,7 +43,7 @@ class Kosmos2_5ImageProcessorKwargs(ImagesKwargs, total=False): max_patches: int -# Copied from transformers.models.kosmos2_5.image_processing_kosmos2_5.torch_extract_patches +# Adapted from transformers.models.kosmos2_5.image_processing_kosmos2_5.torch_extract_patches # Similar to transformers.models.pix2struct.image_processing_pix2struct.torch_extract_patches but dealing with a batch of images directly. def torch_extract_patches(image_tensor, patch_height, patch_width): """ diff --git a/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py b/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py index a5f2cbd882a9..5d6cad0ac1a1 100644 --- a/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py +++ b/src/transformers/models/layoutlmv2/image_processing_pil_layoutlmv2.py @@ -39,7 +39,7 @@ pytesseract = None -# Copied from transformers.models.layoutlmv2.image_processing_layoutlmv2.LayoutLMv2ImageProcessorKwargs +# Adapted from transformers.models.layoutlmv2.image_processing_layoutlmv2.LayoutLMv2ImageProcessorKwargs class LayoutLMv2ImageProcessorKwargs(ImagesKwargs, total=False): r""" apply_ocr (`bool`, *optional*, defaults to `self.apply_ocr`): @@ -59,7 +59,7 @@ class LayoutLMv2ImageProcessorKwargs(ImagesKwargs, total=False): tesseract_config: str | None -# Copied from transformers.models.layoutlmv2.image_processing_layoutlmv2.normalize_box +# Adapted from transformers.models.layoutlmv2.image_processing_layoutlmv2.normalize_box def normalize_box(box, width, height): return [ int(1000 * (box[0] / width)), @@ -69,7 +69,7 @@ def normalize_box(box, width, height): ] -# Copied from transformers.models.layoutlmv2.image_processing_layoutlmv2.apply_tesseract +# Adapted from transformers.models.layoutlmv2.image_processing_layoutlmv2.apply_tesseract def apply_tesseract( image: np.ndarray, lang: str | None, diff --git a/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py b/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py index 895f569892f7..34b75d236c84 100644 --- a/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py +++ b/src/transformers/models/layoutlmv3/image_processing_pil_layoutlmv3.py @@ -41,7 +41,7 @@ pytesseract = None -# Copied from transformers.models.layoutlmv3.image_processing_layoutlmv3.LayoutLMv3ImageProcessorKwargs +# Adapted from transformers.models.layoutlmv3.image_processing_layoutlmv3.LayoutLMv3ImageProcessorKwargs class LayoutLMv3ImageProcessorKwargs(ImagesKwargs, total=False): r""" apply_ocr (`bool`, *optional*, defaults to `True`): @@ -61,7 +61,7 @@ class LayoutLMv3ImageProcessorKwargs(ImagesKwargs, total=False): tesseract_config: str | None -# Copied from transformers.models.layoutlmv3.image_processing_layoutlmv3.normalize_box +# Adapted from transformers.models.layoutlmv3.image_processing_layoutlmv3.normalize_box def normalize_box(box, width, height): return [ int(1000 * (box[0] / width)), @@ -71,7 +71,7 @@ def normalize_box(box, width, height): ] -# Copied from transformers.models.layoutlmv3.image_processing_layoutlmv3.apply_tesseract +# Adapted from transformers.models.layoutlmv3.image_processing_layoutlmv3.apply_tesseract def apply_tesseract( image: "np.ndarray | torch.Tensor", lang: str | None, diff --git a/src/transformers/models/llava_next/image_processing_pil_llava_next.py b/src/transformers/models/llava_next/image_processing_pil_llava_next.py index c29f05f1ea41..8e742529c2d0 100644 --- a/src/transformers/models/llava_next/image_processing_pil_llava_next.py +++ b/src/transformers/models/llava_next/image_processing_pil_llava_next.py @@ -34,7 +34,7 @@ from ...utils import TensorType, auto_docstring -# Copied from transformers.models.llava_next.image_processing_llava_next.LlavaNextImageProcessorKwargs +# Adapted from transformers.models.llava_next.image_processing_llava_next.LlavaNextImageProcessorKwargs class LlavaNextImageProcessorKwargs(ImagesKwargs, total=False): r""" image_grid_pinpoints (`list[list[int]]`, *optional*): diff --git a/src/transformers/models/maskformer/image_processing_pil_maskformer.py b/src/transformers/models/maskformer/image_processing_pil_maskformer.py index 25369a606822..e1217b78d53a 100644 --- a/src/transformers/models/maskformer/image_processing_pil_maskformer.py +++ b/src/transformers/models/maskformer/image_processing_pil_maskformer.py @@ -81,7 +81,7 @@ def convert_segmentation_map_to_binary_masks( return binary_masks.astype(np.float32), labels -# Copied from transformers.models.maskformer.image_processing_maskformer.MaskFormerImageProcessorKwargs +# Adapted from transformers.models.maskformer.image_processing_maskformer.MaskFormerImageProcessorKwargs class MaskFormerImageProcessorKwargs(ImagesKwargs, total=False): r""" ignore_index (`int`, *optional*): @@ -108,7 +108,7 @@ class MaskFormerImageProcessorKwargs(ImagesKwargs, total=False): pad_size: SizeDict | None -# Copied from transformers.models.maskformer.image_processing_maskformer.binary_mask_to_rle +# Adapted from transformers.models.maskformer.image_processing_maskformer.binary_mask_to_rle def binary_mask_to_rle(mask): """ Converts given binary mask of shape `(height, width)` to the run-length encoding (RLE) format. @@ -133,7 +133,7 @@ def binary_mask_to_rle(mask): return list(runs) -# Copied from transformers.models.maskformer.image_processing_maskformer.check_segment_validity +# Adapted from transformers.models.maskformer.image_processing_maskformer.check_segment_validity def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overlap_mask_area_threshold=0.8): # Get the mask associated with the k class mask_k = mask_labels == k @@ -152,7 +152,7 @@ def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overl return mask_exists, mask_k -# Copied from transformers.models.maskformer.image_processing_maskformer.compute_segments +# Adapted from transformers.models.maskformer.image_processing_maskformer.compute_segments def compute_segments( mask_probs, pred_scores, @@ -213,7 +213,7 @@ def compute_segments( return segmentation, segments -# Copied from transformers.models.maskformer.image_processing_maskformer.convert_segmentation_to_rle +# Adapted from transformers.models.maskformer.image_processing_maskformer.convert_segmentation_to_rle def convert_segmentation_to_rle(segmentation): """ Converts given segmentation map of shape `(height, width)` to the run-length encoding (RLE) format. @@ -235,7 +235,7 @@ def convert_segmentation_to_rle(segmentation): return run_length_encodings -# Copied from transformers.models.maskformer.image_processing_maskformer.remove_low_and_no_objects +# Adapted from transformers.models.maskformer.image_processing_maskformer.remove_low_and_no_objects def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): """ Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and @@ -750,7 +750,7 @@ def post_process_instance_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results - # Copied from transformers.models.maskformer.image_processing_maskformer.MaskFormerImageProcessor.post_process_panoptic_segmentation + # Adapted from transformers.models.maskformer.image_processing_maskformer.MaskFormerImageProcessor.post_process_panoptic_segmentation def post_process_panoptic_segmentation( self, outputs, diff --git a/src/transformers/models/mllama/image_processing_pil_mllama.py b/src/transformers/models/mllama/image_processing_pil_mllama.py index f9411a8944b5..397590c2b00c 100644 --- a/src/transformers/models/mllama/image_processing_pil_mllama.py +++ b/src/transformers/models/mllama/image_processing_pil_mllama.py @@ -163,7 +163,7 @@ def convert_aspect_ratios_to_ids_np(aspect_ratios: list[list[tuple[int, int]]], return aspect_ratios_ids -# Copied from transformers.models.mllama.image_processing_mllama.MllamaImageProcessorKwargs +# Adapted from transformers.models.mllama.image_processing_mllama.MllamaImageProcessorKwargs class MllamaImageProcessorKwargs(ImagesKwargs, total=False): """ max_image_tiles (`int`, *optional*): @@ -173,7 +173,7 @@ class MllamaImageProcessorKwargs(ImagesKwargs, total=False): max_image_tiles: int -# Copied from transformers.models.mllama.image_processing_mllama._validate_size +# Adapted from transformers.models.mllama.image_processing_mllama._validate_size def _validate_size(size: SizeDict) -> None: if not (size.height and size.width): raise ValueError(f"Argument `size` must be a dictionary with keys 'height' and 'width'. Got: {size}") @@ -181,7 +181,7 @@ def _validate_size(size: SizeDict) -> None: raise ValueError(f"Argument `size` must have the same height and width, got {size}") -# Copied from transformers.models.mllama.image_processing_mllama._validate_mllama_preprocess_arguments +# Adapted from transformers.models.mllama.image_processing_mllama._validate_mllama_preprocess_arguments def _validate_mllama_preprocess_arguments(do_resize, size, do_pad, max_image_tiles): if not do_pad: raise ValueError("MllamaImageProcessor doesn't support `do_pad=False` mode.") @@ -192,7 +192,7 @@ def _validate_mllama_preprocess_arguments(do_resize, size, do_pad, max_image_til _validate_size(size) -# Copied from transformers.models.idefics2.image_processing_idefics2.convert_to_rgb +# Adapted from transformers.models.idefics2.image_processing_idefics2.convert_to_rgb def convert_to_rgb(image: ImageInput) -> ImageInput: """ Converts an image to RGB format. Only converts if the image is of type PIL.Image.Image, otherwise returns the image @@ -211,7 +211,7 @@ def convert_to_rgb(image: ImageInput) -> ImageInput: return alpha_composite -# Copied from transformers.models.mllama.image_processing_mllama.get_all_supported_aspect_ratios +# Adapted from transformers.models.mllama.image_processing_mllama.get_all_supported_aspect_ratios @lru_cache(maxsize=10) def get_all_supported_aspect_ratios(max_image_tiles: int) -> list[tuple[int, int]]: """ @@ -242,7 +242,7 @@ def get_all_supported_aspect_ratios(max_image_tiles: int) -> list[tuple[int, int return aspect_ratios -# Copied from transformers.models.mllama.image_processing_mllama.get_image_size_fit_to_canvas +# Adapted from transformers.models.mllama.image_processing_mllama.get_image_size_fit_to_canvas def get_image_size_fit_to_canvas( image_height: int, image_width: int, @@ -294,7 +294,7 @@ def get_image_size_fit_to_canvas( return new_height, new_width -# Copied from transformers.models.mllama.image_processing_mllama.get_optimal_tiled_canvas +# Adapted from transformers.models.mllama.image_processing_mllama.get_optimal_tiled_canvas @lru_cache(maxsize=100) def get_optimal_tiled_canvas( image_height: int, diff --git a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py index b4626c960fe1..109de1a53ec6 100644 --- a/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py +++ b/src/transformers/models/mobilenet_v2/image_processing_pil_mobilenet_v2.py @@ -36,7 +36,7 @@ from ...utils.import_utils import requires -# Copied from transformers.models.mobilenet_v2.image_processing_mobilenet_v2.MobileNetV2ImageProcessorKwargs +# Adapted from transformers.models.mobilenet_v2.image_processing_mobilenet_v2.MobileNetV2ImageProcessorKwargs class MobileNetV2ImageProcessorKwargs(ImagesKwargs, total=False): """ do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): diff --git a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py index 385971c0bba3..893e27fe4ccf 100644 --- a/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py +++ b/src/transformers/models/mobilevit/image_processing_pil_mobilevit.py @@ -40,7 +40,7 @@ logger = logging.get_logger(__name__) -# Copied from transformers.models.mobilevit.image_processing_mobilevit.MobileVitImageProcessorKwargs +# Adapted from transformers.models.mobilevit.image_processing_mobilevit.MobileVitImageProcessorKwargs class MobileVitImageProcessorKwargs(ImagesKwargs, total=False): """ do_flip_channel_order (`bool`, *optional*, defaults to `self.do_flip_channel_order`): diff --git a/src/transformers/models/nougat/image_processing_pil_nougat.py b/src/transformers/models/nougat/image_processing_pil_nougat.py index fbfebc3cd690..a4c240229b32 100644 --- a/src/transformers/models/nougat/image_processing_pil_nougat.py +++ b/src/transformers/models/nougat/image_processing_pil_nougat.py @@ -39,7 +39,7 @@ ) -# Copied from transformers.models.nougat.image_processing_nougat.NougatImageProcessorKwargs +# Adapted from transformers.models.nougat.image_processing_nougat.NougatImageProcessorKwargs class NougatImageProcessorKwargs(ImagesKwargs, total=False): r""" do_crop_margin (`bool`, *optional*, defaults to `self.do_crop_margin`): diff --git a/src/transformers/models/oneformer/image_processing_pil_oneformer.py b/src/transformers/models/oneformer/image_processing_pil_oneformer.py index d4c4bb046795..bed17ab5f4eb 100644 --- a/src/transformers/models/oneformer/image_processing_pil_oneformer.py +++ b/src/transformers/models/oneformer/image_processing_pil_oneformer.py @@ -70,7 +70,7 @@ def make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndarr return mask -# Copied from transformers.models.oneformer.image_processing_oneformer.OneFormerImageProcessorKwargs +# Adapted from transformers.models.oneformer.image_processing_oneformer.OneFormerImageProcessorKwargs class OneFormerImageProcessorKwargs(ImagesKwargs, total=False): r""" repo_path (`str`, *optional*, defaults to `shi-labs/oneformer_demo`): @@ -95,7 +95,7 @@ class OneFormerImageProcessorKwargs(ImagesKwargs, total=False): do_reduce_labels: bool -# Copied from transformers.models.oneformer.image_processing_oneformer.binary_mask_to_rle +# Adapted from transformers.models.oneformer.image_processing_oneformer.binary_mask_to_rle def binary_mask_to_rle(mask): """ Converts given binary mask of shape `(height, width)` to the run-length encoding (RLE) format. @@ -120,7 +120,7 @@ def binary_mask_to_rle(mask): return list(runs) -# Copied from transformers.models.oneformer.image_processing_oneformer.check_segment_validity +# Adapted from transformers.models.oneformer.image_processing_oneformer.check_segment_validity def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overlap_mask_area_threshold=0.8): # Get the mask associated with the k class mask_k = mask_labels == k @@ -139,7 +139,7 @@ def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overl return mask_exists, mask_k -# Copied from transformers.models.oneformer.image_processing_oneformer.compute_segments +# Adapted from transformers.models.oneformer.image_processing_oneformer.compute_segments def compute_segments( mask_probs, pred_scores, @@ -198,7 +198,7 @@ def compute_segments( return segmentation, segments -# Copied from transformers.models.oneformer.image_processing_oneformer.convert_segmentation_to_rle +# Adapted from transformers.models.oneformer.image_processing_oneformer.convert_segmentation_to_rle def convert_segmentation_to_rle(segmentation): """ Converts given segmentation map of shape `(height, width)` to the run-length encoding (RLE) format. @@ -220,7 +220,7 @@ def convert_segmentation_to_rle(segmentation): return run_length_encodings -# Copied from transformers.models.oneformer.image_processing_oneformer.load_metadata +# Adapted from transformers.models.oneformer.image_processing_oneformer.load_metadata def load_metadata(repo_id, class_info_file): fname = os.path.join("" if repo_id is None else repo_id, class_info_file) @@ -243,7 +243,7 @@ def load_metadata(repo_id, class_info_file): return class_info -# Copied from transformers.models.oneformer.image_processing_oneformer.prepare_metadata +# Adapted from transformers.models.oneformer.image_processing_oneformer.prepare_metadata def prepare_metadata(class_info): metadata = {} class_names = [] @@ -258,7 +258,7 @@ def prepare_metadata(class_info): return metadata -# Copied from transformers.models.oneformer.image_processing_oneformer.remove_low_and_no_objects +# Adapted from transformers.models.oneformer.image_processing_oneformer.remove_low_and_no_objects def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): """ Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and @@ -913,7 +913,7 @@ def post_process_instance_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results - # Copied from transformers.models.maskformer.image_processing_maskformer.MaskFormerImageProcessor.post_process_panoptic_segmentation + # Adapted from transformers.models.maskformer.image_processing_maskformer.MaskFormerImageProcessor.post_process_panoptic_segmentation def post_process_panoptic_segmentation( self, outputs, diff --git a/src/transformers/models/ovis2/image_processing_pil_ovis2.py b/src/transformers/models/ovis2/image_processing_pil_ovis2.py index 62641735aef8..73772ec65846 100644 --- a/src/transformers/models/ovis2/image_processing_pil_ovis2.py +++ b/src/transformers/models/ovis2/image_processing_pil_ovis2.py @@ -33,7 +33,7 @@ from ...utils import TensorType, auto_docstring -# Copied from transformers.models.ovis2.image_processing_ovis2.Ovis2ImageProcessorKwargs +# Adapted from transformers.models.ovis2.image_processing_ovis2.Ovis2ImageProcessorKwargs class Ovis2ImageProcessorKwargs(ImagesKwargs, total=False): """ crop_to_patches (`bool`, *optional*, defaults to `False`): @@ -57,7 +57,7 @@ class Ovis2ImageProcessorKwargs(ImagesKwargs, total=False): use_covering_area_grid: bool -# Copied from transformers.models.ovis2.image_processing_ovis2.get_all_supported_aspect_ratios +# Adapted from transformers.models.ovis2.image_processing_ovis2.get_all_supported_aspect_ratios @lru_cache(maxsize=10) def get_all_supported_aspect_ratios(min_image_tiles: int, max_image_tiles: int) -> list[tuple[int, int]]: """Computes all allowed aspect ratios for a given minimum and maximum number of input tiles.""" @@ -69,7 +69,7 @@ def get_all_supported_aspect_ratios(min_image_tiles: int, max_image_tiles: int) return sorted(aspect_ratios, key=lambda x: x[0] * x[1]) -# Copied from transformers.models.ovis2.image_processing_ovis2.compute_patch_covering_area +# Adapted from transformers.models.ovis2.image_processing_ovis2.compute_patch_covering_area def compute_patch_covering_area(left: int, upper: int, right: int, lower: int, side: int) -> float: w = right - left h = lower - upper @@ -80,7 +80,7 @@ def compute_patch_covering_area(left: int, upper: int, right: int, lower: int, s return w * h -# Copied from transformers.models.ovis2.image_processing_ovis2.split_image_into_grid +# Adapted from transformers.models.ovis2.image_processing_ovis2.split_image_into_grid def split_image_into_grid(h: int, w: int, grid: tuple[int, int]) -> list[tuple[int, int, int, int]]: row_height = h // grid[0] col_width = w // grid[1] @@ -96,7 +96,7 @@ def split_image_into_grid(h: int, w: int, grid: tuple[int, int]) -> list[tuple[i ] -# Copied from transformers.models.ovis2.image_processing_ovis2.get_min_tile_covering_grid +# Adapted from transformers.models.ovis2.image_processing_ovis2.get_min_tile_covering_grid @lru_cache(maxsize=100) def get_min_tile_covering_grid( image_size: tuple[int, int], @@ -124,7 +124,7 @@ def get_min_tile_covering_grid( return min(evaluated_grids, key=lambda x: (-x[1], x[0][0] * x[0][1]))[0] -# Copied from transformers.models.ovis2.image_processing_ovis2.get_optimal_tiled_canvas +# Adapted from transformers.models.ovis2.image_processing_ovis2.get_optimal_tiled_canvas @lru_cache(maxsize=100) def get_optimal_tiled_canvas( original_image_size: tuple[int, int], diff --git a/src/transformers/models/owlvit/image_processing_pil_owlvit.py b/src/transformers/models/owlvit/image_processing_pil_owlvit.py index b34af3051dfa..d688aa8d4f6a 100644 --- a/src/transformers/models/owlvit/image_processing_pil_owlvit.py +++ b/src/transformers/models/owlvit/image_processing_pil_owlvit.py @@ -33,7 +33,7 @@ def _upcast(t): return t if t.dtype in (torch.int32, torch.int64) else t.int() -# Copied from transformers.models.owlvit.image_processing_owlvit._scale_boxes +# Adapted from transformers.models.owlvit.image_processing_owlvit._scale_boxes def _scale_boxes(boxes, target_sizes): """ Scale batch of bounding boxes to the target sizes. @@ -78,7 +78,7 @@ def box_area(boxes): return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) -# Copied from transformers.models.owlvit.image_processing_owlvit.box_iou +# Adapted from transformers.models.owlvit.image_processing_owlvit.box_iou def box_iou(boxes1, boxes2): import torch diff --git a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py index 222b99a3e33b..82c127abb29a 100644 --- a/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py +++ b/src/transformers/models/pix2struct/image_processing_pil_pix2struct.py @@ -37,7 +37,7 @@ DEFAULT_FONT_PATH = "ybelkada/fonts" -# Copied from transformers.models.pix2struct.image_processing_pix2struct.Pix2StructImageProcessorKwargs +# Adapted from transformers.models.pix2struct.image_processing_pix2struct.Pix2StructImageProcessorKwargs class Pix2StructImageProcessorKwargs(ImagesKwargs, total=False): """ max_patches (`int`, *optional*): @@ -57,7 +57,7 @@ class Pix2StructImageProcessorKwargs(ImagesKwargs, total=False): header_text: list[str] | str | None -# Copied from transformers.models.pix2struct.image_processing_pix2struct.render_text +# Adapted from transformers.models.pix2struct.image_processing_pix2struct.render_text # Adapted from https://github.com/google-research/pix2struct/blob/0e1779af0f4db4b652c1d92b3bbd2550a7399123/pix2struct/preprocessing/preprocessing_utils.py#L106 def render_text( text: str, @@ -129,7 +129,7 @@ def render_text( return img -# Copied from transformers.models.pix2struct.image_processing_pix2struct.torch_extract_patches +# Adapted from transformers.models.pix2struct.image_processing_pix2struct.torch_extract_patches if is_torch_available(): import torch diff --git a/src/transformers/models/pixtral/image_processing_pil_pixtral.py b/src/transformers/models/pixtral/image_processing_pil_pixtral.py index c833d7b66198..2c3a5cfbfef5 100644 --- a/src/transformers/models/pixtral/image_processing_pil_pixtral.py +++ b/src/transformers/models/pixtral/image_processing_pil_pixtral.py @@ -31,7 +31,7 @@ from ...utils import TensorType, auto_docstring -# Copied from transformers.models.pixtral.image_processing_pixtral.PixtralImageProcessorKwargs +# Adapted from transformers.models.pixtral.image_processing_pixtral.PixtralImageProcessorKwargs class PixtralImageProcessorKwargs(ImagesKwargs, total=False): """ patch_size (`Union[dict[str, int], int]` *optional*, defaults to `{"height": 16, "width": 16}`): @@ -41,7 +41,7 @@ class PixtralImageProcessorKwargs(ImagesKwargs, total=False): patch_size: dict[str, int] | int -# Copied from transformers.models.pixtral.image_processing_pixtral._num_image_tokens +# Adapted from transformers.models.pixtral.image_processing_pixtral._num_image_tokens def _num_image_tokens(image_size: tuple[int, int], patch_size: tuple[int, int]) -> int: """ Calculate the number of image tokens given the image size and patch size. @@ -62,7 +62,7 @@ def _num_image_tokens(image_size: tuple[int, int], patch_size: tuple[int, int]) return num_height_tokens, num_width_tokens -# Copied from transformers.models.pixtral.image_processing_pixtral.get_resize_output_image_size +# Adapted from transformers.models.pixtral.image_processing_pixtral.get_resize_output_image_size def get_resize_output_image_size( input_image: ImageInput, size: int | tuple[int, int] | list[int] | tuple[int], diff --git a/src/transformers/models/poolformer/image_processing_pil_poolformer.py b/src/transformers/models/poolformer/image_processing_pil_poolformer.py index deb22ecdc240..b2e5fef4e464 100644 --- a/src/transformers/models/poolformer/image_processing_pil_poolformer.py +++ b/src/transformers/models/poolformer/image_processing_pil_poolformer.py @@ -29,7 +29,7 @@ from ...utils import TensorType, auto_docstring -# Copied from transformers.models.poolformer.image_processing_poolformer.PoolFormerImageProcessorKwargs +# Adapted from transformers.models.poolformer.image_processing_poolformer.PoolFormerImageProcessorKwargs class PoolFormerImageProcessorKwargs(ImagesKwargs, total=False): r""" crop_pct (`float`, *optional*, defaults to `self.crop_pct`): diff --git a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py index d40d53394986..26f31249462a 100644 --- a/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/image_processing_pil_prompt_depth_anything.py @@ -79,7 +79,7 @@ def _get_resize_output_image_size( return (new_height, new_width) -# Copied from transformers.models.prompt_depth_anything.image_processing_prompt_depth_anything.PromptDepthAnythingImageProcessorKwargs +# Adapted from transformers.models.prompt_depth_anything.image_processing_prompt_depth_anything.PromptDepthAnythingImageProcessorKwargs class PromptDepthAnythingImageProcessorKwargs(ImagesKwargs, total=False): r""" keep_aspect_ratio (`bool`, *optional*): diff --git a/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py b/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py index 489bb267173f..5c55fbb0de18 100644 --- a/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py +++ b/src/transformers/models/qwen2_vl/image_processing_pil_qwen2_vl.py @@ -31,7 +31,7 @@ from ...utils import TensorType, auto_docstring -# Copied from transformers.models.qwen2_vl.image_processing_qwen2_vl.Qwen2VLImageProcessorKwargs +# Adapted from transformers.models.qwen2_vl.image_processing_qwen2_vl.Qwen2VLImageProcessorKwargs class Qwen2VLImageProcessorKwargs(ImagesKwargs, total=False): r""" min_pixels (`int`, *optional*, defaults to `56 * 56`): @@ -53,7 +53,7 @@ class Qwen2VLImageProcessorKwargs(ImagesKwargs, total=False): merge_size: int -# Copied from transformers.models.qwen2_vl.image_processing_qwen2_vl.smart_resize +# Adapted from transformers.models.qwen2_vl.image_processing_qwen2_vl.smart_resize def smart_resize( height: int, width: int, factor: int = 28, min_pixels: int = 56 * 56, max_pixels: int = 14 * 14 * 4 * 1280 ): diff --git a/src/transformers/models/sam/image_processing_pil_sam.py b/src/transformers/models/sam/image_processing_pil_sam.py index 74ce88caec0b..c127e7ef0c32 100644 --- a/src/transformers/models/sam/image_processing_pil_sam.py +++ b/src/transformers/models/sam/image_processing_pil_sam.py @@ -64,7 +64,7 @@ def get_resize_output_image_size( return (new_height, new_width) -# Copied from transformers.models.sam.image_processing_sam.SamImageProcessorKwargs +# Adapted from transformers.models.sam.image_processing_sam.SamImageProcessorKwargs class SamImageProcessorKwargs(ImagesKwargs, total=False): r""" mask_size (`dict[str, int]`, *optional*): diff --git a/src/transformers/models/seggpt/image_processing_pil_seggpt.py b/src/transformers/models/seggpt/image_processing_pil_seggpt.py index a6281453f90f..e9365c304e00 100644 --- a/src/transformers/models/seggpt/image_processing_pil_seggpt.py +++ b/src/transformers/models/seggpt/image_processing_pil_seggpt.py @@ -30,7 +30,7 @@ from ...utils.import_utils import requires -# Copied from transformers.models.seggpt.image_processing_seggpt.SegGptImageProcessorKwargs +# Adapted from transformers.models.seggpt.image_processing_seggpt.SegGptImageProcessorKwargs class SegGptImageProcessorKwargs(ImagesKwargs, total=False): r""" num_labels (`int`, *optional*): @@ -43,7 +43,7 @@ class SegGptImageProcessorKwargs(ImagesKwargs, total=False): num_labels: int -# Copied from transformers.models.seggpt.image_processing_seggpt.build_palette +# Adapted from transformers.models.seggpt.image_processing_seggpt.build_palette # See https://huggingface.co/papers/2212.02499 at 3.1 Redefining Output Spaces as "Images" - Semantic Segmentation # Taken from https://github.com/Abdullah-Meda/Painter/blob/main/Painter/data/coco_semseg/gen_color_coco_panoptic_segm.py#L31 def build_palette(num_labels: int) -> list[tuple[int, int, int]]: diff --git a/src/transformers/models/siglip2/image_processing_pil_siglip2.py b/src/transformers/models/siglip2/image_processing_pil_siglip2.py index d131423c7c43..8ae926dbe04d 100644 --- a/src/transformers/models/siglip2/image_processing_pil_siglip2.py +++ b/src/transformers/models/siglip2/image_processing_pil_siglip2.py @@ -53,7 +53,7 @@ def pad_along_first_dim(array: np.ndarray, target_length: int, pad_value: int = return array, mask -# Copied from transformers.models.siglip2.image_processing_siglip2.Siglip2ImageProcessorKwargs +# Adapted from transformers.models.siglip2.image_processing_siglip2.Siglip2ImageProcessorKwargs class Siglip2ImageProcessorKwargs(ImagesKwargs, total=False): r""" patch_size (`int`, *optional*, defaults to `self.patch_size`): @@ -67,7 +67,7 @@ class Siglip2ImageProcessorKwargs(ImagesKwargs, total=False): max_num_patches: int -# Copied from transformers.models.siglip2.image_processing_siglip2.get_image_size_for_max_num_patches +# Adapted from transformers.models.siglip2.image_processing_siglip2.get_image_size_for_max_num_patches def get_image_size_for_max_num_patches( image_height: int, image_width: int, patch_size: int, max_num_patches: int, eps: float = 1e-5 ) -> tuple[int, int]: diff --git a/src/transformers/models/superglue/image_processing_pil_superglue.py b/src/transformers/models/superglue/image_processing_pil_superglue.py index e0be6412ce83..613b49182733 100644 --- a/src/transformers/models/superglue/image_processing_pil_superglue.py +++ b/src/transformers/models/superglue/image_processing_pil_superglue.py @@ -75,7 +75,7 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: return image -# Copied from transformers.models.superglue.image_processing_superglue.validate_and_format_image_pairs +# Adapted from transformers.models.superglue.image_processing_superglue.validate_and_format_image_pairs def validate_and_format_image_pairs(images: ImageInput): error_message = ( "Input images must be a one of the following :", diff --git a/src/transformers/models/superpoint/image_processing_pil_superpoint.py b/src/transformers/models/superpoint/image_processing_pil_superpoint.py index ecd1bb8fac6e..e34d250c41b3 100644 --- a/src/transformers/models/superpoint/image_processing_pil_superpoint.py +++ b/src/transformers/models/superpoint/image_processing_pil_superpoint.py @@ -58,7 +58,7 @@ def convert_to_grayscale(image: np.ndarray) -> np.ndarray: return gray_image -# Copied from transformers.models.superpoint.image_processing_superpoint.SuperPointImageProcessorKwargs +# Adapted from transformers.models.superpoint.image_processing_superpoint.SuperPointImageProcessorKwargs class SuperPointImageProcessorKwargs(ImagesKwargs, total=False): r""" do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`): diff --git a/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py b/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py index 442fe8bd4992..6b1b5c566a57 100644 --- a/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py +++ b/src/transformers/models/swin2sr/image_processing_pil_swin2sr.py @@ -28,7 +28,7 @@ from ...utils import TensorType, auto_docstring -# Copied from transformers.models.swin2sr.image_processing_swin2sr.Swin2SRImageProcessorKwargs +# Adapted from transformers.models.swin2sr.image_processing_swin2sr.Swin2SRImageProcessorKwargs class Swin2SRImageProcessorKwargs(ImagesKwargs, total=False): """ size_divisor (`int`, *optional*, defaults to `self.size_divisor`): diff --git a/src/transformers/models/textnet/image_processing_pil_textnet.py b/src/transformers/models/textnet/image_processing_pil_textnet.py index 232162884c9d..5efeeb4d6eb1 100644 --- a/src/transformers/models/textnet/image_processing_pil_textnet.py +++ b/src/transformers/models/textnet/image_processing_pil_textnet.py @@ -31,7 +31,7 @@ from ...utils import TensorType, auto_docstring -# Copied from transformers.models.textnet.image_processing_textnet.TextNetImageProcessorKwargs +# Adapted from transformers.models.textnet.image_processing_textnet.TextNetImageProcessorKwargs class TextNetImageProcessorKwargs(ImagesKwargs, total=False): """ size_divisor (`int`, *optional*, defaults to `self.size_divisor`): diff --git a/src/transformers/models/tvp/image_processing_pil_tvp.py b/src/transformers/models/tvp/image_processing_pil_tvp.py index c9577858ed56..41fe34f14147 100644 --- a/src/transformers/models/tvp/image_processing_pil_tvp.py +++ b/src/transformers/models/tvp/image_processing_pil_tvp.py @@ -56,7 +56,7 @@ def get_resize_output_image_size( return size -# Copied from transformers.models.tvp.image_processing_tvp.TvpImageProcessorKwargs +# Adapted from transformers.models.tvp.image_processing_tvp.TvpImageProcessorKwargs class TvpImageProcessorKwargs(ImagesKwargs, total=False): r""" do_flip_channel_order (`bool`, *optional*, defaults to `self.do_flip_channel_order`): diff --git a/src/transformers/models/vilt/image_processing_pil_vilt.py b/src/transformers/models/vilt/image_processing_pil_vilt.py index cc4315b93824..88948e0ef538 100644 --- a/src/transformers/models/vilt/image_processing_pil_vilt.py +++ b/src/transformers/models/vilt/image_processing_pil_vilt.py @@ -98,7 +98,7 @@ def get_resize_output_image_size( return new_height, new_width -# Copied from transformers.models.vilt.image_processing_vilt.ViltImageProcessorKwargs +# Adapted from transformers.models.vilt.image_processing_vilt.ViltImageProcessorKwargs class ViltImageProcessorKwargs(ImagesKwargs, total=False): r""" size_divisor (`int`, *optional*, defaults to `self.size_divisor`): diff --git a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py index 0e34534aa90c..4bd543aad0e1 100644 --- a/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py +++ b/src/transformers/models/vitmatte/image_processing_pil_vitmatte.py @@ -34,7 +34,7 @@ pass -# Copied from transformers.models.vitmatte.image_processing_vitmatte.VitMatteImageProcessorKwargs +# Adapted from transformers.models.vitmatte.image_processing_vitmatte.VitMatteImageProcessorKwargs class VitMatteImageProcessorKwargs(ImagesKwargs, total=False): r""" size_divisor (`int`, *optional*, defaults to `self.size_divisor`): diff --git a/src/transformers/models/vitpose/image_processing_pil_vitpose.py b/src/transformers/models/vitpose/image_processing_pil_vitpose.py index 76b264409577..d0394da224d7 100644 --- a/src/transformers/models/vitpose/image_processing_pil_vitpose.py +++ b/src/transformers/models/vitpose/image_processing_pil_vitpose.py @@ -45,7 +45,7 @@ logger = logging.get_logger(__name__) -# Copied from transformers.models.vitpose.image_processing_vitpose.VitPoseImageProcessorKwargs +# Adapted from transformers.models.vitpose.image_processing_vitpose.VitPoseImageProcessorKwargs class VitPoseImageProcessorKwargs(ImagesKwargs, total=False): r""" do_affine_transform (`bool`, *optional*): @@ -58,7 +58,7 @@ class VitPoseImageProcessorKwargs(ImagesKwargs, total=False): normalize_factor: float | None -# Copied from transformers.models.vitpose.image_processing_vitpose.box_to_center_and_scale +# Adapted from transformers.models.vitpose.image_processing_vitpose.box_to_center_and_scale # inspired by https://github.com/ViTAE-Transformer/ViTPose/blob/d5216452796c90c6bc29f5c5ec0bdba94366768a/mmpose/datasets/datasets/base/kpt_2d_sview_rgb_img_top_down_dataset.py#L132 def box_to_center_and_scale( box: tuple | list | np.ndarray, @@ -104,7 +104,7 @@ def box_to_center_and_scale( return center, scale -# Copied from transformers.models.vitpose.image_processing_vitpose.coco_to_pascal_voc +# Adapted from transformers.models.vitpose.image_processing_vitpose.coco_to_pascal_voc def coco_to_pascal_voc(bboxes: np.ndarray) -> np.ndarray: """ Converts bounding boxes from the COCO format to the Pascal VOC format. @@ -125,7 +125,7 @@ def coco_to_pascal_voc(bboxes: np.ndarray) -> np.ndarray: return bboxes -# Copied from transformers.models.vitpose.image_processing_vitpose.get_keypoint_predictions +# Adapted from transformers.models.vitpose.image_processing_vitpose.get_keypoint_predictions def get_keypoint_predictions(heatmaps: np.ndarray) -> tuple[np.ndarray, np.ndarray]: """Get keypoint predictions from score maps. @@ -159,7 +159,7 @@ def get_keypoint_predictions(heatmaps: np.ndarray) -> tuple[np.ndarray, np.ndarr return preds, scores -# Copied from transformers.models.vitpose.image_processing_vitpose.get_warp_matrix +# Adapted from transformers.models.vitpose.image_processing_vitpose.get_warp_matrix def get_warp_matrix(theta: float, size_input: np.ndarray, size_dst: np.ndarray, size_target: np.ndarray): """ Calculate the transformation matrix under the constraint of unbiased. Paper ref: Huang et al. The Devil is in the @@ -197,7 +197,7 @@ def get_warp_matrix(theta: float, size_input: np.ndarray, size_dst: np.ndarray, return matrix -# Copied from transformers.models.vitpose.image_processing_vitpose.post_dark_unbiased_data_processing +# Adapted from transformers.models.vitpose.image_processing_vitpose.post_dark_unbiased_data_processing def post_dark_unbiased_data_processing(coords: np.ndarray, batch_heatmaps: np.ndarray, kernel: int = 3) -> np.ndarray: """DARK post-pocessing. Implemented by unbiased_data_processing. @@ -258,7 +258,7 @@ def post_dark_unbiased_data_processing(coords: np.ndarray, batch_heatmaps: np.nd return coords -# Copied from transformers.models.vitpose.image_processing_vitpose.scipy_warp_affine +# Adapted from transformers.models.vitpose.image_processing_vitpose.scipy_warp_affine def scipy_warp_affine(src, M, size): """ This function implements cv2.warpAffine function using affine_transform in scipy. See https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.affine_transform.html and https://docs.opencv.org/4.x/d4/d61/tutorial_warp_affine.html for more details. @@ -285,7 +285,7 @@ def scipy_warp_affine(src, M, size): return new_src -# Copied from transformers.models.vitpose.image_processing_vitpose.transform_preds +# Adapted from transformers.models.vitpose.image_processing_vitpose.transform_preds def transform_preds(coords: np.ndarray, center: np.ndarray, scale: np.ndarray, output_size: np.ndarray) -> np.ndarray: """Get final keypoint predictions from heatmaps and apply scaling and translation to map them back to the image. diff --git a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py index dc4454d70dc9..6be12be7698e 100644 --- a/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py +++ b/src/transformers/models/zoedepth/image_processing_pil_zoedepth.py @@ -48,7 +48,7 @@ import torchvision.transforms.v2.functional as tvF -# Copied from transformers.models.zoedepth.image_processing_zoedepth.ZoeDepthImageProcessorKwargs +# Adapted from transformers.models.zoedepth.image_processing_zoedepth.ZoeDepthImageProcessorKwargs class ZoeDepthImageProcessorKwargs(ImagesKwargs, total=False): r""" keep_aspect_ratio (`bool`, *optional*, defaults to `self.keep_aspect_ratio`): @@ -68,7 +68,7 @@ class ZoeDepthImageProcessorKwargs(ImagesKwargs, total=False): ensure_multiple_of: int -# Copied from transformers.models.zoedepth.image_processing_zoedepth.get_resize_output_image_size +# Adapted from transformers.models.zoedepth.image_processing_zoedepth.get_resize_output_image_size def get_resize_output_image_size( input_image: "torch.Tensor | np.ndarray", output_size: int | Iterable[int], From 5ef4900f98093868f883091062c36bba4aca9891 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 21:29:50 +0100 Subject: [PATCH 23/30] arf --- .../image_processing_pil_conditional_detr.py | 23 +++++++++------ .../modular_conditional_detr.py | 17 ++++++++++- .../image_processing_pil_deepseek_vl.py | 1 + .../models/deepseek_vl/modular_deepseek_vl.py | 12 +++++++- ...image_processing_pil_deepseek_vl_hybrid.py | 13 +-------- .../image_processing_pil_deformable_detr.py | 12 ++++---- .../modular_deformable_detr.py | 17 ++++++++++- .../image_processing_efficientloftr.py | 12 ++++++-- .../image_processing_pil_efficientloftr.py | 9 ++---- .../efficientloftr/modular_efficientloftr.py | 10 +++++++ .../image_processing_pil_ernie4_5_vl_moe.py | 12 ++++---- .../modular_ernie4_5_vl_moe.py | 10 +++++-- .../glm46v/image_processing_pil_glm46v.py | 1 + .../image_processing_pil_glm_image.py | 1 + .../image_processing_pil_grounding_dino.py | 20 +++++++------ .../grounding_dino/modular_grounding_dino.py | 16 +++++++++++ .../image_processing_pil_lightglue.py | 13 +++++---- .../models/lightglue/modular_lightglue.py | 13 ++++++--- .../image_processing_pil_llava_onevision.py | 1 + .../modular_llava_onevision.py | 13 ++++++++- .../image_processing_pil_mask2former.py | 21 +++++++++----- .../models/mask2former/modular_mask2former.py | 28 +++++++++++++++++++ .../owlv2/image_processing_pil_owlv2.py | 1 + .../image_processing_pil_paddleocr_vl.py | 7 ++++- .../paddleocr_vl/modular_paddleocr_vl.py | 11 ++++++-- .../rt_detr/image_processing_pil_rt_detr.py | 8 ++++-- .../models/rt_detr/modular_rt_detr.py | 14 ++++++++++ .../image_processing_pil_segformer.py | 13 +++++---- .../models/segformer/modular_segformer.py | 12 ++++++++ .../smolvlm/image_processing_pil_smolvlm.py | 4 +++ .../models/smolvlm/modular_smolvlm.py | 18 +++++++++++- .../image_processing_pil_video_llama_3.py | 1 + .../video_llama_3/modular_video_llama_3.py | 25 ++++++++++++++--- .../yolos/image_processing_pil_yolos.py | 14 ++++++---- 34 files changed, 307 insertions(+), 96 deletions(-) diff --git a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py index 02fcfcf0af15..f8e7fb801712 100644 --- a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py +++ b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py @@ -53,6 +53,16 @@ from ...utils.import_utils import requires +if is_vision_available(): + import PIL.Image +if is_torch_available(): + import torch + from torch import nn + +logger = logging.get_logger(__name__) + + +# Adapted from transformers.models.conditional_detr.image_processing_conditional_detr.ConditionalDetrImageProcessorKwargs class ConditionalDetrImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): @@ -67,14 +77,6 @@ class ConditionalDetrImageProcessorKwargs(ImagesKwargs, total=False): do_convert_annotations: bool -if is_vision_available(): - import PIL.Image -if is_torch_available(): - import torch - from torch import nn - -logger = logging.get_logger(__name__) - SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) @@ -263,6 +265,7 @@ def prepare_coco_panoptic_annotation( return new_target +# Adapted from transformers.models.conditional_detr.image_processing_conditional_detr.binary_mask_to_rle def binary_mask_to_rle(mask): """ Converts given binary mask of shape `(height, width)` to the run-length encoding (RLE) format. @@ -287,6 +290,7 @@ def binary_mask_to_rle(mask): return list(runs) +# Adapted from transformers.models.conditional_detr.image_processing_conditional_detr.check_segment_validity def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overlap_mask_area_threshold=0.8): # Get the mask associated with the k class mask_k = mask_labels == k @@ -305,6 +309,7 @@ def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overl return mask_exists, mask_k +# Adapted from transformers.models.conditional_detr.image_processing_conditional_detr.compute_segments def compute_segments( mask_probs, pred_scores, @@ -368,6 +373,7 @@ def compute_segments( return segmentation, segments +# Adapted from transformers.models.conditional_detr.image_processing_conditional_detr.convert_segmentation_to_rle def convert_segmentation_to_rle(segmentation): """ Converts given segmentation map of shape `(height, width)` to the run-length encoding (RLE) format. @@ -391,6 +397,7 @@ def convert_segmentation_to_rle(segmentation): return run_length_encodings +# Adapted from transformers.models.conditional_detr.image_processing_conditional_detr.remove_low_and_no_objects def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): """ Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and diff --git a/src/transformers/models/conditional_detr/modular_conditional_detr.py b/src/transformers/models/conditional_detr/modular_conditional_detr.py index 2205b85c5547..ffc1e78bee01 100644 --- a/src/transformers/models/conditional_detr/modular_conditional_detr.py +++ b/src/transformers/models/conditional_detr/modular_conditional_detr.py @@ -20,12 +20,13 @@ from ...image_transforms import ( center_to_corners_format, ) +from ...image_utils import AnnotationFormat from ...masking_utils import create_bidirectional_mask from ...modeling_outputs import ( BaseModelOutput, ) from ...modeling_utils import ALL_ATTENTION_FUNCTIONS -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import ( TensorType, TransformersKwargs, @@ -65,6 +66,20 @@ logger = logging.get_logger(__name__) +class ConditionalDetrImageProcessorKwargs(ImagesKwargs, total=False): + r""" + format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): + Data format of the annotations. One of "coco_detection" or "coco_panoptic". + do_convert_annotations (`bool`, *optional*, defaults to `True`): + Controls whether to convert the annotations to the format expected by the CONDITIONAL_DETR model. Converts the + bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. + Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. + """ + + format: str | AnnotationFormat + do_convert_annotations: bool + + class ConditionalDetrImageProcessor(DetrImageProcessor): def post_process_object_detection( self, outputs, threshold: float = 0.5, target_sizes: TensorType | list[tuple] = None, top_k: int = 100 diff --git a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py index e868830b0220..7138d2f5238c 100644 --- a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py +++ b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py @@ -36,6 +36,7 @@ from ...utils import TensorType, auto_docstring +# Adapted from transformers.models.deepseek_vl.image_processing_deepseek_vl.DeepseekVLImageProcessorKwargs class DeepseekVLImageProcessorKwargs(ImagesKwargs, total=False): r""" min_size (`int`, *optional*, defaults to 14): diff --git a/src/transformers/models/deepseek_vl/modular_deepseek_vl.py b/src/transformers/models/deepseek_vl/modular_deepseek_vl.py index be955c6fd41e..a56da6f3fe0a 100644 --- a/src/transformers/models/deepseek_vl/modular_deepseek_vl.py +++ b/src/transformers/models/deepseek_vl/modular_deepseek_vl.py @@ -20,7 +20,7 @@ from ...configuration_utils import PreTrainedConfig from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput -from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack +from ...processing_utils import ImagesKwargs, ProcessingKwargs, ProcessorMixin, Unpack from ...tokenization_utils_base import ( PreTokenizedInput, TextInput, @@ -152,6 +152,16 @@ def generate(self): raise AttributeError("Not needed for DeepseekVL") +class DeepseekVLImageProcessorKwargs(ImagesKwargs, total=False): + r""" + min_size (`int`, *optional*, defaults to 14): + The minimum allowed size for the resized image. Ensures that neither the height nor width + falls below this value after resizing. + """ + + min_size: int + + class DeepseekVLImageProcessorPil(JanusImageProcessorPil): def postprocess(self): raise AttributeError("Not needed for DeepseekVL") diff --git a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py index 6eddb273e567..035eae816a72 100644 --- a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py +++ b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py @@ -37,26 +37,15 @@ from ...utils import TensorType, auto_docstring +# Adapted from transformers.models.deepseek_vl_hybrid.image_processing_deepseek_vl_hybrid.DeepseekVLHybridImageProcessorKwargs class DeepseekVLHybridImageProcessorKwargs(ImagesKwargs, total=False): r""" min_size (`int`, *optional*, defaults to 14): The minimum allowed size for the resized image. Ensures that neither the height nor width falls below this value after resizing. - high_res_size (`dict`, *optional*, defaults to `{"height": 1024, "width": 1024}`): - Size of the high resolution output image after resizing. - high_res_resample (`PILImageResampling`, *optional*, defaults to `Resampling.BICUBIC`): - Resampling filter to use if resizing the image. - high_res_image_mean (`float` or `list[float]`, *optional*, defaults to `OPENAI_CLIP_MEAN`): - Mean to use if normalizing the high resolution image. - high_res_image_std (`float` or `list[float]`, *optional*, defaults to `OPENAI_CLIP_STD`): - Standard deviation to use if normalizing the high resolution image. """ min_size: int - high_res_size: dict - high_res_resample: "PILImageResampling | None" - high_res_image_mean: float | list[float] | tuple[float, ...] - high_res_image_std: float | list[float] | tuple[float, ...] @auto_docstring diff --git a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py index 78c4829b8d40..1c6c9519e37a 100644 --- a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py +++ b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py @@ -52,6 +52,13 @@ from ...utils.import_utils import requires, requires_backends +if is_vision_available(): + import PIL.Image +if is_torch_available(): + import torch + + +# Adapted from transformers.models.deformable_detr.image_processing_deformable_detr.DeformableDetrImageProcessorKwargs class DeformableDetrImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): @@ -66,11 +73,6 @@ class DeformableDetrImageProcessorKwargs(ImagesKwargs, total=False): do_convert_annotations: bool -if is_vision_available(): - import PIL.Image -if is_torch_available(): - import torch - SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) diff --git a/src/transformers/models/deformable_detr/modular_deformable_detr.py b/src/transformers/models/deformable_detr/modular_deformable_detr.py index a4a5b4acd95a..a2f80e8236ad 100644 --- a/src/transformers/models/deformable_detr/modular_deformable_detr.py +++ b/src/transformers/models/deformable_detr/modular_deformable_detr.py @@ -23,10 +23,11 @@ from ... import initialization as init from ...backbone_utils import load_backbone from ...image_transforms import center_to_corners_format +from ...image_utils import AnnotationFormat from ...integrations import use_kernel_forward_from_hub from ...modeling_outputs import BaseModelOutput from ...modeling_utils import PreTrainedModel -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import ( ModelOutput, TensorType, @@ -60,6 +61,20 @@ logger = logging.get_logger(__name__) +class DeformableDetrImageProcessorKwargs(ImagesKwargs, total=False): + r""" + format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): + Data format of the annotations. One of "coco_detection" or "coco_panoptic". + do_convert_annotations (`bool`, *optional*, defaults to `True`): + Controls whether to convert the annotations to the format expected by the DEFORMABLE_DETR model. Converts the + bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. + Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. + """ + + format: str | AnnotationFormat + do_convert_annotations: bool + + class DeformableDetrImageProcessor(DetrImageProcessor): def post_process_object_detection( self, outputs, threshold: float = 0.5, target_sizes: TensorType | list[tuple] = None, top_k: int = 100 diff --git a/src/transformers/models/efficientloftr/image_processing_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_efficientloftr.py index 4333fd6eed10..7f2366860df2 100644 --- a/src/transformers/models/efficientloftr/image_processing_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_efficientloftr.py @@ -4,8 +4,8 @@ # the file from the modular. If any change should be done, please apply the change to the # modular_efficientloftr.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 +from typing import TYPE_CHECKING -import torch from PIL import Image, ImageDraw from torchvision.transforms.v2 import functional as tvF @@ -23,8 +23,14 @@ to_numpy_array, ) from ...processing_utils import ImagesKwargs, Unpack -from ...utils import TensorType, auto_docstring -from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput +from ...utils import TensorType, auto_docstring, is_torch_available + + +if is_torch_available(): + import torch + +if TYPE_CHECKING: + from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput class EfficientLoFTRImageProcessorKwargs(ImagesKwargs, total=False): diff --git a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py index 0f47785f0ad5..5f467c56dd4f 100644 --- a/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py +++ b/src/transformers/models/efficientloftr/image_processing_pil_efficientloftr.py @@ -4,8 +4,6 @@ # the file from the modular. If any change should be done, please apply the change to the # modular_efficientloftr.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 - - from typing import TYPE_CHECKING import numpy as np @@ -28,6 +26,8 @@ from ...utils.import_utils import requires +if TYPE_CHECKING: + from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput if is_torch_available(): import torch @@ -41,10 +41,6 @@ class EfficientLoFTRImageProcessorKwargs(ImagesKwargs, total=False): do_grayscale: bool -if TYPE_CHECKING: - from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput - - def is_grayscale(image: np.ndarray): if image.shape[0] == 1: return True @@ -79,6 +75,7 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: return image +# Adapted from transformers.models.efficientloftr.image_processing_efficientloftr.validate_and_format_image_pairs def validate_and_format_image_pairs(images: ImageInput): error_message = ( "Input images must be a one of the following :", diff --git a/src/transformers/models/efficientloftr/modular_efficientloftr.py b/src/transformers/models/efficientloftr/modular_efficientloftr.py index 86d8d34eba70..17e3e399a8df 100644 --- a/src/transformers/models/efficientloftr/modular_efficientloftr.py +++ b/src/transformers/models/efficientloftr/modular_efficientloftr.py @@ -1,5 +1,6 @@ from typing import TYPE_CHECKING +from ...processing_utils import ImagesKwargs from ...utils import TensorType, is_torch_available from ...utils.import_utils import requires from ..superglue.image_processing_pil_superglue import SuperGlueImageProcessorPil @@ -13,6 +14,15 @@ from .modeling_efficientloftr import EfficientLoFTRKeypointMatchingOutput +class EfficientLoFTRImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`): + Whether to convert the image to grayscale. Can be overridden by `do_grayscale` in the `preprocess` method. + """ + + do_grayscale: bool + + class EfficientLoFTRImageProcessor(SuperGlueImageProcessor): def post_process_keypoint_matching( self, diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py index e13ae9efe3c7..61fd71a0e997 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py @@ -29,12 +29,15 @@ from ...utils import TensorType, auto_docstring, logging +logger = logging.get_logger(__name__) + + class Ernie4_5_VLMoeImageProcessorKwargs(ImagesKwargs, total=False): - r""" + """ patch_size (`int`, *optional*, defaults to 14): The spatial patch size of the vision encoder. - temporal_patch_size (`int`, *optional*): - The temporal patch size of the vision encoder. Unused in the image processor, only used for videos. + temporal_patch_size (`int`, *optional*, defaults to 2): + The temporal patch size of the vision encoder. merge_size (`int`, *optional*, defaults to 2): The merge size of the vision encoder to llm encoder. """ @@ -44,9 +47,6 @@ class Ernie4_5_VLMoeImageProcessorKwargs(ImagesKwargs, total=False): merge_size: int -logger = logging.get_logger(__name__) - - def smart_resize( height: int, width: int, factor: int = 28, min_pixels: int = 56 * 56, max_pixels: int = 14 * 14 * 4 * 1280 ): diff --git a/src/transformers/models/ernie4_5_vl_moe/modular_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/modular_ernie4_5_vl_moe.py index ad47bc0508a3..42bbb44b70a5 100644 --- a/src/transformers/models/ernie4_5_vl_moe/modular_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/modular_ernie4_5_vl_moe.py @@ -43,7 +43,7 @@ from ...modeling_outputs import BaseModelOutputWithPooling, MoeCausalLMOutputWithPast, MoeModelOutputWithPast from ...modeling_rope_utils import dynamic_rope_update from ...modeling_utils import PreTrainedModel -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import ( TensorType, TransformersKwargs, @@ -63,7 +63,7 @@ Ernie4_5_MoeStatics, Ernie4_5_MoeTopKRouter, ) -from ..glm4v.image_processing_glm4v import Glm4vImageProcessor, Glm4vImageProcessorKwargs +from ..glm4v.image_processing_glm4v import Glm4vImageProcessor from ..glm4v.image_processing_pil_glm4v import Glm4vImageProcessorPil from ..glm4v.modeling_glm4v import Glm4vForConditionalGeneration from ..mixtral.modeling_mixtral import load_balancing_loss_func @@ -1220,7 +1220,7 @@ def forward( ) -class Ernie4_5_VLMoeImageProcessorKwargs(Glm4vImageProcessorKwargs): +class Ernie4_5_VLMoeImageProcessorKwargs(ImagesKwargs, total=False): r""" patch_size (`int`, *optional*, defaults to 14): The spatial patch size of the vision encoder. @@ -1230,6 +1230,10 @@ class Ernie4_5_VLMoeImageProcessorKwargs(Glm4vImageProcessorKwargs): The merge size of the vision encoder to llm encoder. """ + patch_size: int + temporal_patch_size: int + merge_size: int + class Ernie4_5_VLMoeImageProcessorPil(Glm4vImageProcessorPil): size = {"shortest_edge": 56 * 56, "longest_edge": 28 * 28 * 6177} diff --git a/src/transformers/models/glm46v/image_processing_pil_glm46v.py b/src/transformers/models/glm46v/image_processing_pil_glm46v.py index 261c921e1ce8..5601e732c2b3 100644 --- a/src/transformers/models/glm46v/image_processing_pil_glm46v.py +++ b/src/transformers/models/glm46v/image_processing_pil_glm46v.py @@ -45,6 +45,7 @@ class Glm46VImageProcessorKwargs(ImagesKwargs, total=False): merge_size: int +# Adapted from transformers.models.glm46v.image_processing_glm46v.smart_resize def smart_resize( num_frames: int, height: int, diff --git a/src/transformers/models/glm_image/image_processing_pil_glm_image.py b/src/transformers/models/glm_image/image_processing_pil_glm_image.py index 355bb04adb67..2dde18ef2066 100644 --- a/src/transformers/models/glm_image/image_processing_pil_glm_image.py +++ b/src/transformers/models/glm_image/image_processing_pil_glm_image.py @@ -30,6 +30,7 @@ from ...utils import TensorType, auto_docstring +# Adapted from transformers.models.glm_image.image_processing_glm_image.GlmImageImageProcessorKwargs class GlmImageImageProcessorKwargs(ImagesKwargs, total=False): r""" min_pixels (`int`, *optional*, defaults to `56 * 56`): diff --git a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py index 146a331d2eda..25a74f5658b6 100644 --- a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py +++ b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py @@ -58,6 +58,17 @@ from ...utils.import_utils import requires +if TYPE_CHECKING: + from .modeling_grounding_dino import GroundingDinoObjectDetectionOutput + + +if is_vision_available(): + import PIL.Image +if is_torch_available(): + import torch + + +# Adapted from transformers.models.grounding_dino.image_processing_grounding_dino.GroundingDinoImageProcessorKwargs class GroundingDinoImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): @@ -72,15 +83,6 @@ class GroundingDinoImageProcessorKwargs(ImagesKwargs, total=False): do_convert_annotations: bool -if TYPE_CHECKING: - from .modeling_grounding_dino import GroundingDinoObjectDetectionOutput - - -if is_vision_available(): - import PIL.Image -if is_torch_available(): - import torch - SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) diff --git a/src/transformers/models/grounding_dino/modular_grounding_dino.py b/src/transformers/models/grounding_dino/modular_grounding_dino.py index bd35fd512ffe..483ad262a602 100644 --- a/src/transformers/models/grounding_dino/modular_grounding_dino.py +++ b/src/transformers/models/grounding_dino/modular_grounding_dino.py @@ -25,6 +25,8 @@ from transformers.models.detr.image_processing_pil_detr import DetrImageProcessorPil from ...image_transforms import center_to_corners_format +from ...image_utils import AnnotationFormat +from ...processing_utils import ImagesKwargs from ...utils import ( TensorType, logging, @@ -68,6 +70,20 @@ def _scale_boxes(boxes, target_sizes): return boxes +class GroundingDinoImageProcessorKwargs(ImagesKwargs, total=False): + r""" + format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): + Data format of the annotations. One of "coco_detection" or "coco_panoptic". + do_convert_annotations (`bool`, *optional*, defaults to `True`): + Controls whether to convert the annotations to the format expected by the GROUNDING_DINO model. Converts the + bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. + Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. + """ + + format: str | AnnotationFormat + do_convert_annotations: bool + + class GroundingDinoImageProcessor(DetrImageProcessor): def post_process_object_detection( self, diff --git a/src/transformers/models/lightglue/image_processing_pil_lightglue.py b/src/transformers/models/lightglue/image_processing_pil_lightglue.py index 3349933ff066..9f43fe1bbc7a 100644 --- a/src/transformers/models/lightglue/image_processing_pil_lightglue.py +++ b/src/transformers/models/lightglue/image_processing_pil_lightglue.py @@ -40,6 +40,12 @@ from ...utils.import_utils import requires +if TYPE_CHECKING: + from .modeling_lightglue import LightGlueKeypointMatchingOutput +if is_torch_available(): + import torch + + class LightGlueImageProcessorKwargs(ImagesKwargs, total=False): r""" do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`): @@ -49,12 +55,6 @@ class LightGlueImageProcessorKwargs(ImagesKwargs, total=False): do_grayscale: bool -if TYPE_CHECKING: - from .modeling_lightglue import LightGlueKeypointMatchingOutput -if is_torch_available(): - import torch - - def is_grayscale(image: np.ndarray): if image.shape[0] == 1: return True @@ -89,6 +89,7 @@ def convert_to_grayscale(image: ImageInput) -> ImageInput: return image +# Adapted from transformers.models.lightglue.image_processing_lightglue.validate_and_format_image_pairs def validate_and_format_image_pairs(images: ImageInput): error_message = ( "Input images must be a one of the following :", diff --git a/src/transformers/models/lightglue/modular_lightglue.py b/src/transformers/models/lightglue/modular_lightglue.py index c0b4812d76e7..701d0017ec98 100644 --- a/src/transformers/models/lightglue/modular_lightglue.py +++ b/src/transformers/models/lightglue/modular_lightglue.py @@ -23,7 +23,7 @@ from ...configuration_utils import PreTrainedConfig from ...modeling_flash_attention_utils import FlashAttentionKwargs from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import ModelOutput, TensorType, auto_docstring, can_return_tuple, logging from ...utils.import_utils import requires from ..auto import CONFIG_MAPPING, AutoConfig @@ -32,7 +32,7 @@ from ..cohere.modeling_cohere import apply_rotary_pos_emb from ..llama.modeling_llama import LlamaAttention, eager_attention_forward from ..superglue.image_processing_pil_superglue import SuperGlueImageProcessorPil -from ..superglue.image_processing_superglue import SuperGlueImageProcessor, SuperGlueImageProcessorKwargs +from ..superglue.image_processing_superglue import SuperGlueImageProcessor from ..superpoint import SuperPointConfig @@ -165,8 +165,13 @@ class LightGlueKeypointMatchingOutput(ModelOutput): attentions: tuple[torch.FloatTensor] | None = None -class LightGlueImageProcessorKwargs(SuperGlueImageProcessorKwargs): - pass +class LightGlueImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`): + Whether to convert the image to grayscale. Can be overridden by `do_grayscale` in the `preprocess` method. + """ + + do_grayscale: bool class LightGlueImageProcessor(SuperGlueImageProcessor): diff --git a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py index 23534a65d70f..f70a5c124bd7 100644 --- a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py +++ b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py @@ -36,6 +36,7 @@ from ...utils import TensorType, auto_docstring +# Adapted from transformers.models.llava_onevision.image_processing_llava_onevision.LlavaOnevisionImageProcessorKwargs class LlavaOnevisionImageProcessorKwargs(ImagesKwargs, total=False): r""" image_grid_pinpoints (`list[list[int]]`, *optional*): diff --git a/src/transformers/models/llava_onevision/modular_llava_onevision.py b/src/transformers/models/llava_onevision/modular_llava_onevision.py index f44a4612cdc2..a3634aa17cba 100644 --- a/src/transformers/models/llava_onevision/modular_llava_onevision.py +++ b/src/transformers/models/llava_onevision/modular_llava_onevision.py @@ -34,7 +34,7 @@ ) from ...modeling_flash_attention_utils import FlashAttentionKwargs from ...modeling_outputs import BaseModelOutputWithPooling -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TensorType, auto_docstring, logging from ...utils.generic import can_return_tuple, merge_with_config_defaults from ..llava_next.image_processing_llava_next import LlavaNextImageProcessor, LlavaNextImageProcessorKwargs @@ -217,6 +217,17 @@ def _preprocess( ) +class LlavaOnevisionImageProcessorKwargs(ImagesKwargs, total=False): + r""" + image_grid_pinpoints (`list[list[int]]`, *optional*): + A list of possible resolutions to use for processing high resolution images. The best resolution is selected + based on the original size of the image. Can be overridden by `image_grid_pinpoints` in the `preprocess` + method. + """ + + image_grid_pinpoints: list[list[int]] + + class LlavaOnevisionImageProcessorPil(LlavaNextImageProcessorPil): resample = PILImageResampling.BICUBIC image_mean = OPENAI_CLIP_MEAN diff --git a/src/transformers/models/mask2former/image_processing_pil_mask2former.py b/src/transformers/models/mask2former/image_processing_pil_mask2former.py index ba5e91bb323e..51961c1721cd 100644 --- a/src/transformers/models/mask2former/image_processing_pil_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_pil_mask2former.py @@ -44,6 +44,14 @@ from ...utils.import_utils import requires +if is_torch_available(): + import torch + from torch import nn + +logger = logging.get_logger(__name__) + + +# Adapted from transformers.models.mask2former.image_processing_mask2former.Mask2FormerImageProcessorKwargs class Mask2FormerImageProcessorKwargs(ImagesKwargs, total=False): r""" ignore_index (`int`, *optional*): @@ -70,13 +78,6 @@ class Mask2FormerImageProcessorKwargs(ImagesKwargs, total=False): pad_size: SizeDict | None -if is_torch_available(): - import torch - from torch import nn - -logger = logging.get_logger(__name__) - - def convert_segmentation_map_to_binary_masks( segmentation_map: np.ndarray, instance_id_to_semantic_id: dict[int, int] | None = None, @@ -113,6 +114,7 @@ def convert_segmentation_map_to_binary_masks( return binary_masks.astype(np.float32), labels +# Adapted from transformers.models.mask2former.image_processing_mask2former.binary_mask_to_rle def binary_mask_to_rle(mask): """ Converts given binary mask of shape `(height, width)` to the run-length encoding (RLE) format. @@ -137,6 +139,7 @@ def binary_mask_to_rle(mask): return list(runs) +# Adapted from transformers.models.mask2former.image_processing_mask2former.check_segment_validity def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overlap_mask_area_threshold=0.8): # Get the mask associated with the k class mask_k = mask_labels == k @@ -155,6 +158,7 @@ def check_segment_validity(mask_labels, mask_probs, k, mask_threshold=0.5, overl return mask_exists, mask_k +# Adapted from transformers.models.mask2former.image_processing_mask2former.compute_segments def compute_segments( mask_probs, pred_scores, @@ -215,6 +219,7 @@ def compute_segments( return segmentation, segments +# Adapted from transformers.models.mask2former.image_processing_mask2former.convert_segmentation_to_rle def convert_segmentation_to_rle(segmentation): """ Converts given segmentation map of shape `(height, width)` to the run-length encoding (RLE) format. @@ -236,6 +241,7 @@ def convert_segmentation_to_rle(segmentation): return run_length_encodings +# Adapted from transformers.models.mask2former.image_processing_mask2former.remove_low_and_no_objects def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_labels): """ Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and @@ -757,6 +763,7 @@ def post_process_instance_segmentation( results.append({"segmentation": segmentation, "segments_info": segments}) return results + # Adapted from transformers.models.mask2former.image_processing_mask2former.Mask2FormerImageProcessor.post_process_panoptic_segmentation def post_process_panoptic_segmentation( self, outputs, diff --git a/src/transformers/models/mask2former/modular_mask2former.py b/src/transformers/models/mask2former/modular_mask2former.py index 089baffe5df7..87f2b834991f 100644 --- a/src/transformers/models/mask2former/modular_mask2former.py +++ b/src/transformers/models/mask2former/modular_mask2former.py @@ -15,6 +15,8 @@ import torch from torch import nn +from ...image_utils import SizeDict +from ...processing_utils import ImagesKwargs from ...utils import ( TensorType, logging, @@ -33,6 +35,32 @@ logger = logging.get_logger(__name__) +class Mask2FormerImageProcessorKwargs(ImagesKwargs, total=False): + r""" + ignore_index (`int`, *optional*): + Label to be assigned to background pixels in segmentation maps. If provided, segmentation map pixels + denoted with 0 (background) will be replaced with `ignore_index`. + do_reduce_labels (`bool`, *optional*, defaults to `False`): + Whether or not to decrement all label values of segmentation maps by 1. Usually used for datasets where 0 + is used for background, and background itself is not included in all classes of a dataset (e.g. ADE20k). + The background label will be replaced by `ignore_index`. + num_labels (`int`, *optional*): + The number of labels in the segmentation map. + size_divisor (`int`, *optional*, defaults to `32`): + Some backbones need images divisible by a certain number. If not passed, it defaults to the value used in + Swin Transformer. + pad_size (`SizeDict`, *optional*): + The size to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` + is not provided, images will be padded to the largest height and width in the batch. + """ + + ignore_index: int | None + do_reduce_labels: bool + num_labels: int | None + size_divisor: int + pad_size: SizeDict | None + + class Mask2FormerImageProcessor(MaskFormerImageProcessor): def post_process_semantic_segmentation( self, outputs, target_sizes: list[tuple[int, int]] | None = None diff --git a/src/transformers/models/owlv2/image_processing_pil_owlv2.py b/src/transformers/models/owlv2/image_processing_pil_owlv2.py index 081a9b2521a0..b61db3c0484e 100644 --- a/src/transformers/models/owlv2/image_processing_pil_owlv2.py +++ b/src/transformers/models/owlv2/image_processing_pil_owlv2.py @@ -67,6 +67,7 @@ def box_area(boxes): return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) +# Adapted from transformers.models.owlv2.image_processing_owlv2.box_iou def box_iou(boxes1, boxes2): import torch diff --git a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py index ac639892640f..4536ca5fa94b 100644 --- a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py @@ -35,11 +35,16 @@ from ...utils import TensorType, auto_docstring +# Adapted from transformers.models.paddleocr_vl.image_processing_paddleocr_vl.PaddleOCRVLImageProcessorKwargs class PaddleOCRVLImageProcessorKwargs(ImagesKwargs, total=False): r""" + min_pixels (`int`, *optional*, defaults to `56 * 56`): + The min pixels of the image to resize the image. + max_pixels (`int`, *optional*, defaults to `28 * 28 * 1280`): + The max pixels of the image to resize the image. patch_size (`int`, *optional*, defaults to 14): The spatial patch size of the vision encoder. - temporal_patch_size (`int`, *optional*, defaults to 1): + temporal_patch_size (`int`, *optional*, defaults to 2): The temporal patch size of the vision encoder. merge_size (`int`, *optional*, defaults to 2): The merge size of the vision encoder to llm encoder. diff --git a/src/transformers/models/paddleocr_vl/modular_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/modular_paddleocr_vl.py index 85fa2e822c75..12d935978415 100644 --- a/src/transformers/models/paddleocr_vl/modular_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/modular_paddleocr_vl.py @@ -38,8 +38,9 @@ from ...modeling_outputs import BaseModelOutput, BaseModelOutputWithPast, BaseModelOutputWithPooling from ...modeling_utils import PreTrainedModel from ...models.qwen2_vl.image_processing_pil_qwen2_vl import Qwen2VLImageProcessorPil -from ...models.qwen2_vl.image_processing_qwen2_vl import Qwen2VLImageProcessor, Qwen2VLImageProcessorKwargs +from ...models.qwen2_vl.image_processing_qwen2_vl import Qwen2VLImageProcessor from ...processing_utils import ( + ImagesKwargs, ProcessingKwargs, ProcessorMixin, Unpack, @@ -122,7 +123,7 @@ def smart_resize( return h_bar, w_bar -class PaddleOCRVLImageProcessorKwargs(Qwen2VLImageProcessorKwargs): +class PaddleOCRVLImageProcessorKwargs(ImagesKwargs, total=False): r""" patch_size (`int`, *optional*, defaults to 14): The spatial patch size of the vision encoder. @@ -132,6 +133,12 @@ class PaddleOCRVLImageProcessorKwargs(Qwen2VLImageProcessorKwargs): The merge size of the vision encoder to llm encoder. """ + min_pixels: int + max_pixels: int + patch_size: int + temporal_patch_size: int + merge_size: int + class PaddleOCRVLImageProcessorPil(Qwen2VLImageProcessorPil): size = {"shortest_edge": 384 * 384, "longest_edge": 1536 * 1536} diff --git a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py index 9ef8d43a5971..623cb5c756dc 100644 --- a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py @@ -51,6 +51,11 @@ from ...utils.import_utils import requires +if is_torch_available(): + import torch + + +# Adapted from transformers.models.rt_detr.image_processing_rt_detr.RTDetrImageProcessorKwargs class RTDetrImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): @@ -65,9 +70,6 @@ class RTDetrImageProcessorKwargs(ImagesKwargs, total=False): do_convert_annotations: bool -if is_torch_available(): - import torch - SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) diff --git a/src/transformers/models/rt_detr/modular_rt_detr.py b/src/transformers/models/rt_detr/modular_rt_detr.py index cd4e8faf3fc2..97136541d6ec 100644 --- a/src/transformers/models/rt_detr/modular_rt_detr.py +++ b/src/transformers/models/rt_detr/modular_rt_detr.py @@ -426,6 +426,20 @@ def post_process_panoptic_segmentation(self): raise NotImplementedError("Panoptic segmentation post-processing is not implemented for RT-DETR yet.") +class RTDetrImageProcessorKwargs(ImagesKwargs, total=False): + r""" + format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): + Data format of the annotations. One of "coco_detection" or "coco_panoptic". + do_convert_annotations (`bool`, *optional*, defaults to `True`): + Controls whether to convert the annotations to the format expected by the RT_DETR model. Converts the + bounding boxes to the format `(center_x, center_y, width, height)` and in the range `[0, 1]`. + Can be overridden by the `do_convert_annotations` parameter in the `preprocess` method. + """ + + format: str | AnnotationFormat + do_convert_annotations: bool + + @requires(backends=("torch",)) class RTDetrImageProcessorPil(DetrImageProcessorPil): resample = PILImageResampling.BILINEAR diff --git a/src/transformers/models/segformer/image_processing_pil_segformer.py b/src/transformers/models/segformer/image_processing_pil_segformer.py index 85dc85636a70..b5c0b70fd5e5 100644 --- a/src/transformers/models/segformer/image_processing_pil_segformer.py +++ b/src/transformers/models/segformer/image_processing_pil_segformer.py @@ -36,6 +36,13 @@ from ...utils.import_utils import requires +if is_torch_available(): + pass +if is_torchvision_available(): + import torchvision.transforms.v2.functional as tvF + + +# Adapted from transformers.models.segformer.image_processing_segformer.SegformerImageProcessorKwargs class SegformerImageProcessorKwargs(ImagesKwargs, total=False): r""" do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): @@ -47,12 +54,6 @@ class SegformerImageProcessorKwargs(ImagesKwargs, total=False): do_reduce_labels: bool -if is_torch_available(): - pass -if is_torchvision_available(): - import torchvision.transforms.v2.functional as tvF - - @requires(backends=("torch", "torchvision")) class SegformerImageProcessorPil(PilBackend): """PIL backend for Segformer with reduce_label support.""" diff --git a/src/transformers/models/segformer/modular_segformer.py b/src/transformers/models/segformer/modular_segformer.py index 414dc58e8c52..d7f339ea6e42 100644 --- a/src/transformers/models/segformer/modular_segformer.py +++ b/src/transformers/models/segformer/modular_segformer.py @@ -31,10 +31,22 @@ PILImageResampling, SizeDict, ) +from ...processing_utils import ImagesKwargs from ...utils import TensorType from ...utils.import_utils import requires +class SegformerImageProcessorKwargs(ImagesKwargs, total=False): + r""" + do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): + Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 + is used for background, and background itself is not included in all classes of a dataset (e.g. + ADE20k). The background label will be replaced by 255. + """ + + do_reduce_labels: bool + + class SegformerImageProcessor(BeitImageProcessor): resample = PILImageResampling.BILINEAR image_mean = IMAGENET_DEFAULT_MEAN diff --git a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py index c63c0060545e..353f30616f3e 100644 --- a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py +++ b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py @@ -39,6 +39,7 @@ from ...utils import TensorType, auto_docstring +# Adapted from transformers.models.smolvlm.image_processing_smolvlm.SmolVLMImageProcessorKwargs class SmolVLMImageProcessorKwargs(ImagesKwargs, total=False): """ do_image_splitting (`bool`, *optional*, defaults to `True`): @@ -63,9 +64,11 @@ def _make_pixel_mask(image: np.ndarray, output_size: tuple[int, int]) -> np.ndar return mask +# Adapted from transformers.models.smolvlm.image_processing_smolvlm.MAX_IMAGE_SIZE MAX_IMAGE_SIZE = 4096 # 4k resolution as absolute maximum +# Adapted from transformers.models.smolvlm.image_processing_smolvlm._resize_output_size_rescale_to_max_len def _resize_output_size_rescale_to_max_len( height: int, width: int, min_len: int | None = 1, max_len: int | None = None ) -> tuple[int, int]: @@ -103,6 +106,7 @@ def _resize_output_size_rescale_to_max_len( return height, width +# Adapted from transformers.models.smolvlm.image_processing_smolvlm._resize_output_size_scale_below_upper_bound def _resize_output_size_scale_below_upper_bound( height: int, width: int, max_len: dict[str, int] | None = None ) -> tuple[int, int]: diff --git a/src/transformers/models/smolvlm/modular_smolvlm.py b/src/transformers/models/smolvlm/modular_smolvlm.py index cf91863c56a7..9c572cc9d877 100644 --- a/src/transformers/models/smolvlm/modular_smolvlm.py +++ b/src/transformers/models/smolvlm/modular_smolvlm.py @@ -22,7 +22,7 @@ from ...generation import GenerationConfig from ...modeling_flash_attention_utils import FlashAttentionKwargs from ...modeling_outputs import BaseModelOutputWithPooling -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...utils import TransformersKwargs, auto_docstring, can_return_tuple, logging, torch_compilable_check from ..idefics3.configuration_idefics3 import Idefics3Config, Idefics3VisionConfig from ..idefics3.image_processing_idefics3 import Idefics3ImageProcessor @@ -91,6 +91,22 @@ class SmolVLMConfig(Idefics3Config): model_type = "smolvlm" +class SmolVLMImageProcessorKwargs(ImagesKwargs, total=False): + """ + do_image_splitting (`bool`, *optional*, defaults to `True`): + Whether to split the image into sub-images concatenated with the original image. They are split into patches + such that each patch has a size of `max_image_size["height"]` x `max_image_size["width"]`. + max_image_size (`Dict`, *optional*, defaults to `{"longest_edge": 364}`): + Maximum resolution of the patches of images accepted by the model. This is a dictionary containing the key "longest_edge". + return_row_col_info (`bool`, *optional*, defaults to `False`): + Whether to return the row and column information of the images. + """ + + do_image_splitting: bool + max_image_size: dict[str, int] + return_row_col_info: bool + + class SmolVLMImageProcessor(Idefics3ImageProcessor): pass diff --git a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py index 5272c7465b2b..814c3d5c6968 100644 --- a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py +++ b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py @@ -30,6 +30,7 @@ from ...utils import TensorType, auto_docstring +# Adapted from transformers.models.video_llama_3.image_processing_video_llama_3.VideoLlama3ImageProcessorKwargs class VideoLlama3ImageProcessorKwargs(ImagesKwargs, total=False): r""" min_pixels (`int`, *optional*, defaults to `56 * 56`): diff --git a/src/transformers/models/video_llama_3/modular_video_llama_3.py b/src/transformers/models/video_llama_3/modular_video_llama_3.py index c4a9e40bc8f0..4eef74580c87 100644 --- a/src/transformers/models/video_llama_3/modular_video_llama_3.py +++ b/src/transformers/models/video_llama_3/modular_video_llama_3.py @@ -37,7 +37,7 @@ ) from ...modeling_outputs import BaseModelOutput, BaseModelOutputWithPooling, ModelOutput from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel -from ...processing_utils import Unpack +from ...processing_utils import ImagesKwargs, Unpack from ...tokenization_utils_base import PreTokenizedInput, TextInput from ...utils import ( TensorType, @@ -55,7 +55,7 @@ from ..auto import CONFIG_MAPPING, AutoConfig from ..auto.modeling_auto import AutoModel from ..qwen2_vl.image_processing_pil_qwen2_vl import Qwen2VLImageProcessorPil -from ..qwen2_vl.image_processing_qwen2_vl import Qwen2VLImageProcessor, Qwen2VLImageProcessorKwargs, smart_resize +from ..qwen2_vl.image_processing_qwen2_vl import Qwen2VLImageProcessor, smart_resize from ..qwen2_vl.modeling_qwen2_vl import ( Qwen2VLForConditionalGeneration, Qwen2VLModel, @@ -1107,8 +1107,25 @@ def model_input_names(self): raise AttributeError("VideoLlama doesn't need to override it") -class VideoLlama3ImageProcessorKwargs(Qwen2VLImageProcessorKwargs): - pass +class VideoLlama3ImageProcessorKwargs(ImagesKwargs, total=False): + r""" + min_pixels (`int`, *optional*, defaults to `56 * 56`): + The min pixels of the image to resize the image. + max_pixels (`int`, *optional*, defaults to `28 * 28 * 1280`): + The max pixels of the image to resize the image. + patch_size (`int`, *optional*, defaults to 14): + The spatial patch size of the vision encoder. + temporal_patch_size (`int`, *optional*, defaults to 2): + The temporal patch size of the vision encoder. + merge_size (`int`, *optional*, defaults to 2): + The merge size of the vision encoder to llm encoder. + """ + + min_pixels: int + max_pixels: int + patch_size: int + temporal_patch_size: int + merge_size: int class VideoLlama3ImageProcessorPil(Qwen2VLImageProcessorPil): diff --git a/src/transformers/models/yolos/image_processing_pil_yolos.py b/src/transformers/models/yolos/image_processing_pil_yolos.py index 63a5553f02ed..219348363ea3 100644 --- a/src/transformers/models/yolos/image_processing_pil_yolos.py +++ b/src/transformers/models/yolos/image_processing_pil_yolos.py @@ -38,6 +38,14 @@ from ...utils.import_utils import requires +if is_vision_available(): + import PIL.Image +if is_torch_available(): + import torch + from torch import nn + + +# Adapted from transformers.models.yolos.image_processing_yolos.YolosImageProcessorKwargs class YolosImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): @@ -52,12 +60,6 @@ class YolosImageProcessorKwargs(ImagesKwargs, total=False): do_convert_annotations: bool -if is_vision_available(): - import PIL.Image -if is_torch_available(): - import torch - from torch import nn - SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) From 5df7f825f618dbbf27c25f76c48e873f0d1ff353 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 27 Mar 2026 21:29:56 +0100 Subject: [PATCH 24/30] converter up --- utils/modular_model_converter.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/utils/modular_model_converter.py b/utils/modular_model_converter.py index d5dc7dfe23b6..649abd5d62b2 100644 --- a/utils/modular_model_converter.py +++ b/utils/modular_model_converter.py @@ -1692,6 +1692,10 @@ class NewNameModel(LlamaModel): class_file_type = find_file_type(class_name, new_name) # In this case, we need to remove it from the dependencies and create a new import instead if class_file_type != file_type: + # image_processing_pil and image_processing must never depend on each other. + # When a PIL class needs an image_processing class, inline it instead of importing. + if file_type == "image_processing_pil" and class_file_type == "image_processing": + continue corrected_dependencies.remove(class_name) import_statement = f"from .{class_file_type}_{new_name} import {class_name}" new_imports[class_name] = cst.parse_statement(import_statement) @@ -1744,7 +1748,14 @@ class node based on the inherited classes if needed. Also returns any new import # Remove all classes explicitly defined in modular from the dependencies. Otherwise, if a class is referenced # before its new modular definition, it may be wrongly imported from elsewhere as a dependency if it matches # another class from a modeling file after renaming, even though it would be added after anyway (leading to duplicates) - new_node_dependencies -= set(modular_mapper.classes.keys()) + # Exception: for image_processing_pil files, image_processing modular classes must be inlined (not excluded), + # because these two files must never import from each other. + classes_to_exclude = set(modular_mapper.classes.keys()) + if file_type == "image_processing_pil": + classes_to_exclude -= { + k for k in classes_to_exclude if find_file_type(k, model_name) == "image_processing" + } + new_node_dependencies -= classes_to_exclude # The node was modified -> look for all recursive dependencies of the new node all_dependencies_to_add = find_all_dependencies( From 8d445e599adb3f2e3aed8e959d827f691e7d9df0 Mon Sep 17 00:00:00 2001 From: Arthur Date: Sat, 28 Mar 2026 00:33:52 +0100 Subject: [PATCH 25/30] fix? --- .../image_processing_pil_deepseek_vl_hybrid.py | 18 +++++++++++++++++- utils/modular_model_converter.py | 8 +++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py index 035eae816a72..55573c35c423 100644 --- a/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py +++ b/src/transformers/models/deepseek_vl_hybrid/image_processing_pil_deepseek_vl_hybrid.py @@ -19,6 +19,7 @@ # limitations under the License. from collections.abc import Iterable +from typing import Union import numpy as np @@ -37,15 +38,30 @@ from ...utils import TensorType, auto_docstring -# Adapted from transformers.models.deepseek_vl_hybrid.image_processing_deepseek_vl_hybrid.DeepseekVLHybridImageProcessorKwargs class DeepseekVLHybridImageProcessorKwargs(ImagesKwargs, total=False): r""" min_size (`int`, *optional*, defaults to 14): The minimum allowed size for the resized image. Ensures that neither the height nor width falls below this value after resizing. + high_res_size (`dict`, *optional*, defaults to `{"height": 1024, "width": 1024}`): + Size of the high resolution output image after resizing. Can be overridden by the `high_res_size` parameter in the `preprocess` + method. + high_res_resample (`PILImageResampling`, *optional*, defaults to `Resampling.BICUBIC`): + Resampling filter to use if resizing the image. Only has an effect if `do_resize` is set to `True`. Can be + overridden by the `high_res_resample` parameter in the `preprocess` method. + high_res_image_mean (`float` or `list[float]`, *optional*, defaults to `OPENAI_CLIP_MEAN`): + Mean to use if normalizing the high resolution image. This is a float or list of floats the length of the number of + channels in the image. Can be overridden by the `high_res_image_mean` parameter in the `preprocess` method. + high_res_image_std (`float` or `list[float]`, *optional*, defaults to `OPENAI_CLIP_STD`): + Standard deviation to use if normalizing the high resolution image. This is a float or list of floats the length of the + number of channels in the image. Can be overridden by the `high_res_image_std` parameter in the `preprocess` method. """ min_size: int + high_res_size: dict + high_res_resample: Union["PILImageResampling", int] + high_res_image_mean: float | list[float] | tuple[float, ...] + high_res_image_std: float | list[float] | tuple[float, ...] @auto_docstring diff --git a/utils/modular_model_converter.py b/utils/modular_model_converter.py index 649abd5d62b2..48dc46b8b593 100644 --- a/utils/modular_model_converter.py +++ b/utils/modular_model_converter.py @@ -1766,7 +1766,13 @@ class node based on the inherited classes if needed. Also returns any new import relative_dependency_order = mapper.compute_relative_order(all_dependencies_to_add) nodes_to_add = { - dep: (relative_dependency_order[dep], mapper.global_nodes[dep]) for dep in all_dependencies_to_add + dep: ( + relative_dependency_order[dep], + # If this dependency is explicitly defined in the modular, prefer the modular's version. + # This prevents a renamed parent class from overriding a modular-defined class of the same name. + modular_mapper.global_nodes[dep] if dep in modular_mapper.classes else mapper.global_nodes[dep], + ) + for dep in all_dependencies_to_add } # No transformers (modeling file) super class, just check functions and assignments dependencies From 0a2c4f99244ab812f0b29805506320daab6ff354 Mon Sep 17 00:00:00 2001 From: Arthur Date: Sat, 28 Mar 2026 00:37:28 +0100 Subject: [PATCH 26/30] fix copies --- .../image_processing_pil_conditional_detr.py | 1 - .../models/deepseek_vl/image_processing_pil_deepseek_vl.py | 1 - .../image_processing_pil_deformable_detr.py | 1 - .../image_processing_pil_ernie4_5_vl_moe.py | 6 +++--- .../grounding_dino/image_processing_pil_grounding_dino.py | 1 - .../image_processing_pil_llava_onevision.py | 1 - .../models/mask2former/image_processing_pil_mask2former.py | 1 - .../paddleocr_vl/image_processing_pil_paddleocr_vl.py | 7 +------ .../models/rt_detr/image_processing_pil_rt_detr.py | 1 - .../models/segformer/image_processing_pil_segformer.py | 1 - .../models/smolvlm/image_processing_pil_smolvlm.py | 1 - .../video_llama_3/image_processing_pil_video_llama_3.py | 1 - 12 files changed, 4 insertions(+), 19 deletions(-) diff --git a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py index f8e7fb801712..359c4c706f7c 100644 --- a/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py +++ b/src/transformers/models/conditional_detr/image_processing_pil_conditional_detr.py @@ -62,7 +62,6 @@ logger = logging.get_logger(__name__) -# Adapted from transformers.models.conditional_detr.image_processing_conditional_detr.ConditionalDetrImageProcessorKwargs class ConditionalDetrImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): diff --git a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py index 7138d2f5238c..e868830b0220 100644 --- a/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py +++ b/src/transformers/models/deepseek_vl/image_processing_pil_deepseek_vl.py @@ -36,7 +36,6 @@ from ...utils import TensorType, auto_docstring -# Adapted from transformers.models.deepseek_vl.image_processing_deepseek_vl.DeepseekVLImageProcessorKwargs class DeepseekVLImageProcessorKwargs(ImagesKwargs, total=False): r""" min_size (`int`, *optional*, defaults to 14): diff --git a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py index 1c6c9519e37a..fcd95fa4647f 100644 --- a/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py +++ b/src/transformers/models/deformable_detr/image_processing_pil_deformable_detr.py @@ -58,7 +58,6 @@ import torch -# Adapted from transformers.models.deformable_detr.image_processing_deformable_detr.DeformableDetrImageProcessorKwargs class DeformableDetrImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): diff --git a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py index 61fd71a0e997..7f372c3af02d 100644 --- a/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py +++ b/src/transformers/models/ernie4_5_vl_moe/image_processing_pil_ernie4_5_vl_moe.py @@ -33,11 +33,11 @@ class Ernie4_5_VLMoeImageProcessorKwargs(ImagesKwargs, total=False): - """ + r""" patch_size (`int`, *optional*, defaults to 14): The spatial patch size of the vision encoder. - temporal_patch_size (`int`, *optional*, defaults to 2): - The temporal patch size of the vision encoder. + temporal_patch_size (`int`, *optional*): + The temporal patch size of the vision encoder. Unused in the image processor, only used for videos. merge_size (`int`, *optional*, defaults to 2): The merge size of the vision encoder to llm encoder. """ diff --git a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py index 25a74f5658b6..31c59e5f3930 100644 --- a/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py +++ b/src/transformers/models/grounding_dino/image_processing_pil_grounding_dino.py @@ -68,7 +68,6 @@ import torch -# Adapted from transformers.models.grounding_dino.image_processing_grounding_dino.GroundingDinoImageProcessorKwargs class GroundingDinoImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): diff --git a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py index f70a5c124bd7..23534a65d70f 100644 --- a/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py +++ b/src/transformers/models/llava_onevision/image_processing_pil_llava_onevision.py @@ -36,7 +36,6 @@ from ...utils import TensorType, auto_docstring -# Adapted from transformers.models.llava_onevision.image_processing_llava_onevision.LlavaOnevisionImageProcessorKwargs class LlavaOnevisionImageProcessorKwargs(ImagesKwargs, total=False): r""" image_grid_pinpoints (`list[list[int]]`, *optional*): diff --git a/src/transformers/models/mask2former/image_processing_pil_mask2former.py b/src/transformers/models/mask2former/image_processing_pil_mask2former.py index 51961c1721cd..8358a3601bed 100644 --- a/src/transformers/models/mask2former/image_processing_pil_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_pil_mask2former.py @@ -51,7 +51,6 @@ logger = logging.get_logger(__name__) -# Adapted from transformers.models.mask2former.image_processing_mask2former.Mask2FormerImageProcessorKwargs class Mask2FormerImageProcessorKwargs(ImagesKwargs, total=False): r""" ignore_index (`int`, *optional*): diff --git a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py index 4536ca5fa94b..ac639892640f 100644 --- a/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py +++ b/src/transformers/models/paddleocr_vl/image_processing_pil_paddleocr_vl.py @@ -35,16 +35,11 @@ from ...utils import TensorType, auto_docstring -# Adapted from transformers.models.paddleocr_vl.image_processing_paddleocr_vl.PaddleOCRVLImageProcessorKwargs class PaddleOCRVLImageProcessorKwargs(ImagesKwargs, total=False): r""" - min_pixels (`int`, *optional*, defaults to `56 * 56`): - The min pixels of the image to resize the image. - max_pixels (`int`, *optional*, defaults to `28 * 28 * 1280`): - The max pixels of the image to resize the image. patch_size (`int`, *optional*, defaults to 14): The spatial patch size of the vision encoder. - temporal_patch_size (`int`, *optional*, defaults to 2): + temporal_patch_size (`int`, *optional*, defaults to 1): The temporal patch size of the vision encoder. merge_size (`int`, *optional*, defaults to 2): The merge size of the vision encoder to llm encoder. diff --git a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py index 623cb5c756dc..1fe55d067653 100644 --- a/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_pil_rt_detr.py @@ -55,7 +55,6 @@ import torch -# Adapted from transformers.models.rt_detr.image_processing_rt_detr.RTDetrImageProcessorKwargs class RTDetrImageProcessorKwargs(ImagesKwargs, total=False): r""" format (`str`, *optional*, defaults to `AnnotationFormat.COCO_DETECTION`): diff --git a/src/transformers/models/segformer/image_processing_pil_segformer.py b/src/transformers/models/segformer/image_processing_pil_segformer.py index b5c0b70fd5e5..f1d0bb0f627b 100644 --- a/src/transformers/models/segformer/image_processing_pil_segformer.py +++ b/src/transformers/models/segformer/image_processing_pil_segformer.py @@ -42,7 +42,6 @@ import torchvision.transforms.v2.functional as tvF -# Adapted from transformers.models.segformer.image_processing_segformer.SegformerImageProcessorKwargs class SegformerImageProcessorKwargs(ImagesKwargs, total=False): r""" do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): diff --git a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py index 353f30616f3e..3d53ed09c11f 100644 --- a/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py +++ b/src/transformers/models/smolvlm/image_processing_pil_smolvlm.py @@ -39,7 +39,6 @@ from ...utils import TensorType, auto_docstring -# Adapted from transformers.models.smolvlm.image_processing_smolvlm.SmolVLMImageProcessorKwargs class SmolVLMImageProcessorKwargs(ImagesKwargs, total=False): """ do_image_splitting (`bool`, *optional*, defaults to `True`): diff --git a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py index 814c3d5c6968..5272c7465b2b 100644 --- a/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py +++ b/src/transformers/models/video_llama_3/image_processing_pil_video_llama_3.py @@ -30,7 +30,6 @@ from ...utils import TensorType, auto_docstring -# Adapted from transformers.models.video_llama_3.image_processing_video_llama_3.VideoLlama3ImageProcessorKwargs class VideoLlama3ImageProcessorKwargs(ImagesKwargs, total=False): r""" min_pixels (`int`, *optional*, defaults to `56 * 56`): From e153efdcc1839a0a05ac6425e17edef0bba0f168 Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 30 Mar 2026 08:56:26 +0200 Subject: [PATCH 27/30] fix for func --- src/transformers/utils/import_utils.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/transformers/utils/import_utils.py b/src/transformers/utils/import_utils.py index e7a3068fe403..9f308b62fa2c 100644 --- a/src/transformers/utils/import_utils.py +++ b/src/transformers/utils/import_utils.py @@ -15,6 +15,7 @@ Import utilities: Utilities related to imports and our lazy inits. """ +import functools import importlib.machinery import importlib.metadata import importlib.util @@ -2519,8 +2520,19 @@ def requires(*, backends=()): raise ValueError(f"Backend should be defined in the BACKENDS_MAPPING. Offending backend: {backend}") def inner_fn(fun): - fun.__backends = applied_backends - return fun + if isinstance(fun, type): + # For classes, just attach the metadata — don't wrap, as that would + # turn the class into a plain function and break isinstance checks. + fun.__backends = applied_backends + return fun + + @functools.wraps(fun) + def wrapper(*args, **kwargs): + requires_backends(fun, applied_backends) + return fun(*args, **kwargs) + + wrapper.__backends = applied_backends + return wrapper return inner_fn From 2e764240a784dca073e4b00783b32468dca196e3 Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 30 Mar 2026 08:58:57 +0200 Subject: [PATCH 28/30] style --- src/transformers/models/smolvlm/processing_smolvlm.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/transformers/models/smolvlm/processing_smolvlm.py b/src/transformers/models/smolvlm/processing_smolvlm.py index e3d5360d6aa8..69eb7d6e5a61 100644 --- a/src/transformers/models/smolvlm/processing_smolvlm.py +++ b/src/transformers/models/smolvlm/processing_smolvlm.py @@ -26,13 +26,13 @@ from ...video_utils import VideoInput -# Copied from transformers.models.smolvlm.video_processing_smolvlm.DEFAULT_VIDEO_INTRO +# Adapted from transformers.models.smolvlm.video_processing_smolvlm.DEFAULT_VIDEO_INTRO DEFAULT_VIDEO_INTRO = ( "You are provided the following series of {frame_count} frames from a {video_duration} [H:MM:SS] video.\n" ) -# Copied from transformers.models.smolvlm.video_processing_smolvlm.DEFAULT_MEDIA_OUTTRO +# Adapted from transformers.models.smolvlm.video_processing_smolvlm.DEFAULT_MEDIA_OUTTRO DEFAULT_MEDIA_OUTTRO = "\n\n" -# Copied from transformers.models.smolvlm.video_processing_smolvlm.FRAME_TIMESTAMP_MESSAGE +# Adapted from transformers.models.smolvlm.video_processing_smolvlm.FRAME_TIMESTAMP_MESSAGE FRAME_TIMESTAMP_MESSAGE = "\nFrame from {timestamp}:" if TYPE_CHECKING: From c2510fe4526c5de83af39b304c83e3627b054f9b Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 30 Mar 2026 09:13:05 +0200 Subject: [PATCH 29/30] ignore --- src/transformers/utils/import_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/utils/import_utils.py b/src/transformers/utils/import_utils.py index 9f308b62fa2c..a0b5c7a31eeb 100644 --- a/src/transformers/utils/import_utils.py +++ b/src/transformers/utils/import_utils.py @@ -2531,7 +2531,7 @@ def wrapper(*args, **kwargs): requires_backends(fun, applied_backends) return fun(*args, **kwargs) - wrapper.__backends = applied_backends + wrapper.__backends = applied_backends # type: ignore [unresolved-attribute] return wrapper return inner_fn From 93239c1db73d66dc4652a8da6ad82fe1685b2483 Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 30 Mar 2026 09:13:29 +0200 Subject: [PATCH 30/30] type --- src/transformers/utils/import_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/utils/import_utils.py b/src/transformers/utils/import_utils.py index a0b5c7a31eeb..66d29e221525 100644 --- a/src/transformers/utils/import_utils.py +++ b/src/transformers/utils/import_utils.py @@ -2531,7 +2531,7 @@ def wrapper(*args, **kwargs): requires_backends(fun, applied_backends) return fun(*args, **kwargs) - wrapper.__backends = applied_backends # type: ignore [unresolved-attribute] + wrapper.__backends = applied_backends # type: ignore [unresolved-attribute] return wrapper return inner_fn