From 86fcbca9a8521c6c03678d8cafbe636856fb0167 Mon Sep 17 00:00:00 2001 From: Suhwan Choi Date: Wed, 23 Jul 2025 11:16:09 +0000 Subject: [PATCH 1/4] feat: add `is_fast` to ImageProcessor --- src/transformers/image_processing_utils.py | 7 +++++++ src/transformers/image_processing_utils_fast.py | 7 +++++++ tests/test_image_processing_common.py | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/src/transformers/image_processing_utils.py b/src/transformers/image_processing_utils.py index d3086de1cce4..52b798c09f84 100644 --- a/src/transformers/image_processing_utils.py +++ b/src/transformers/image_processing_utils.py @@ -39,6 +39,13 @@ class BaseImageProcessor(ImageProcessingMixin): def __init__(self, **kwargs): super().__init__(**kwargs) + @property + def is_fast(self) -> bool: + """ + `bool`: Whether or not this image processor is a fast processor (backed by PyTorch and TorchVision). + """ + return False + def __call__(self, images, **kwargs) -> BatchFeature: """Preprocess an image or a batch of images.""" return self.preprocess(images, **kwargs) diff --git a/src/transformers/image_processing_utils_fast.py b/src/transformers/image_processing_utils_fast.py index 55bd84cf2798..7c53b5cac088 100644 --- a/src/transformers/image_processing_utils_fast.py +++ b/src/transformers/image_processing_utils_fast.py @@ -235,6 +235,13 @@ def __init__( # get valid kwargs names self._valid_kwargs_names = list(self.valid_kwargs.__annotations__.keys()) + @property + def is_fast(self) -> bool: + """ + `bool`: Whether or not this image processor is a fast processor (backed by PyTorch and TorchVision). + """ + return True + def resize( self, image: "torch.Tensor", diff --git a/tests/test_image_processing_common.py b/tests/test_image_processing_common.py index 8f159045e68c..fc6a9c80fc39 100644 --- a/tests/test_image_processing_common.py +++ b/tests/test_image_processing_common.py @@ -241,6 +241,16 @@ def measure_time(image_processor, image): self.assertLessEqual(fast_time, slow_time) + def test_is_fast(self): + for image_processing_class in self.image_processor_list: + image_processor = image_processing_class(**self.image_processor_dict) + + # Check is_fast is set correctly + if "Fast" in image_processing_class.__name__: + self.assertTrue(image_processor.is_fast) + else: + self.assertFalse(image_processor.is_fast) + def test_image_processor_to_json_string(self): for image_processing_class in self.image_processor_list: image_processor = image_processing_class(**self.image_processor_dict) From 843e56c91cc7a2694d0d74427079d898093a52dc Mon Sep 17 00:00:00 2001 From: MilkClouds Date: Thu, 24 Jul 2025 00:04:35 +0900 Subject: [PATCH 2/4] =?UTF-8?q?test=5Fimage=5Fprocessing=5Fcommon.py=20?= =?UTF-8?q?=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Yoni Gozlan <74535834+yonigozlan@users.noreply.github.com> --- tests/test_image_processing_common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_image_processing_common.py b/tests/test_image_processing_common.py index fc6a9c80fc39..b71febf38a90 100644 --- a/tests/test_image_processing_common.py +++ b/tests/test_image_processing_common.py @@ -246,7 +246,7 @@ def test_is_fast(self): image_processor = image_processing_class(**self.image_processor_dict) # Check is_fast is set correctly - if "Fast" in image_processing_class.__name__: + if isinstance(image_processing_class, BaseImageProcessorFast): self.assertTrue(image_processor.is_fast) else: self.assertFalse(image_processor.is_fast) From d3caec2796a5882d712fe9dc05d0e2e1c7492434 Mon Sep 17 00:00:00 2001 From: Suhwan Choi Date: Tue, 29 Jul 2025 07:35:14 +0000 Subject: [PATCH 3/4] feat: add missing BaseImageProcessorFast import --- tests/test_image_processing_common.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_image_processing_common.py b/tests/test_image_processing_common.py index b71febf38a90..cd742f79da42 100644 --- a/tests/test_image_processing_common.py +++ b/tests/test_image_processing_common.py @@ -36,7 +36,11 @@ slow, torch_device, ) -from transformers.utils import is_torch_available, is_vision_available +from transformers.utils import is_torch_available, is_torchvision_available, is_vision_available + + +if is_torchvision_available(): + from transformers.image_processing_utils_fast import BaseImageProcessorFast if is_torch_available(): From 7a191a277839640f0cb2cb920651733a3cc32a20 Mon Sep 17 00:00:00 2001 From: Suhwan Choi Date: Wed, 6 Aug 2025 16:44:04 +0000 Subject: [PATCH 4/4] fix: `issubclass` for discriminating subclass of BaseImageProcessorFast --- tests/test_image_processing_common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_image_processing_common.py b/tests/test_image_processing_common.py index cd742f79da42..c8b75c73d984 100644 --- a/tests/test_image_processing_common.py +++ b/tests/test_image_processing_common.py @@ -250,7 +250,7 @@ def test_is_fast(self): image_processor = image_processing_class(**self.image_processor_dict) # Check is_fast is set correctly - if isinstance(image_processing_class, BaseImageProcessorFast): + if is_torchvision_available() and issubclass(image_processing_class, BaseImageProcessorFast): self.assertTrue(image_processor.is_fast) else: self.assertFalse(image_processor.is_fast)