Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/transformers/image_processing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/transformers/image_processing_utils_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 15 additions & 1 deletion tests/test_image_processing_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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)
Expand Down