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 c575770a65f2..4fe9a48cb36f 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..c8b75c73d984 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(): @@ -241,6 +245,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 is_torchvision_available() and issubclass(image_processing_class, BaseImageProcessorFast): + 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)