Skip to content

[Fast Processor] OWLv2 #37289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions docs/source/en/model_doc/owlv2.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ Usage of OWLv2 is identical to [OWL-ViT](owlvit) with a new, updated image proce
- post_process_object_detection
- post_process_image_guided_detection

## Owlv2ImageProcessorFast

[[autodoc]] Owlv2ImageProcessorFast
- preprocess

## Owlv2Processor

[[autodoc]] Owlv2Processor
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,7 @@
_import_structure["models.llava"].append("LlavaImageProcessorFast")
_import_structure["models.llava_next"].append("LlavaNextImageProcessorFast")
_import_structure["models.llava_onevision"].append("LlavaOnevisionImageProcessorFast")
_import_structure["models.owlv2"].append("Owlv2ImageProcessorFast")
_import_structure["models.phi4_multimodal"].append("Phi4MultimodalImageProcessorFast")
_import_structure["models.pixtral"].append("PixtralImageProcessorFast")
_import_structure["models.qwen2_vl"].append("Qwen2VLImageProcessorFast")
Expand Down Expand Up @@ -6649,6 +6650,7 @@
from .models.llava import LlavaImageProcessorFast
from .models.llava_next import LlavaNextImageProcessorFast
from .models.llava_onevision import LlavaOnevisionImageProcessorFast
from .models.owlv2 import Owlv2ImageProcessorFast
from .models.phi4_multimodal import Phi4MultimodalImageProcessorFast
from .models.pixtral import PixtralImageProcessorFast
from .models.qwen2_vl import Qwen2VLImageProcessorFast
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/models/auto/image_processing_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
("nat", ("ViTImageProcessor", "ViTImageProcessorFast")),
("nougat", ("NougatImageProcessor",)),
("oneformer", ("OneFormerImageProcessor",)),
("owlv2", ("Owlv2ImageProcessor",)),
("owlv2", ("Owlv2ImageProcessor", "Owlv2ImageProcessorFast")),
("owlvit", ("OwlViTImageProcessor",)),
("paligemma", ("SiglipImageProcessor", "SiglipImageProcessorFast")),
("perceiver", ("PerceiverImageProcessor",)),
Expand Down
1 change: 1 addition & 0 deletions src/transformers/models/owlv2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
if TYPE_CHECKING:
from .configuration_owlv2 import *
from .image_processing_owlv2 import *
from .image_processing_owlv2_fast import *
from .modeling_owlv2 import *
from .processing_owlv2 import *
else:
Expand Down
58 changes: 58 additions & 0 deletions src/transformers/models/owlv2/image_processing_owlv2_fast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# coding=utf-8
# Copyright 2025 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.
"""Fast Image processor class for OWLv2."""

from ...image_processing_utils_fast import (
BASE_IMAGE_PROCESSOR_FAST_DOCSTRING,
BaseImageProcessorFast,
DefaultFastImageProcessorKwargs,
Unpack,
)
from ...image_utils import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, PILImageResampling
from ...utils import add_start_docstrings


@add_start_docstrings(
"Constructs a fast Owlv2 image processor.",
BASE_IMAGE_PROCESSOR_FAST_DOCSTRING,
)
class Owlv2ImageProcessorFast(BaseImageProcessorFast):
# This generated class can be used as a starting point for the fast image processor.
# if the image processor is only used for simple augmentations, such as resizing, center cropping, rescaling, or normalizing,
# only the default values should be set in the class.
# If the image processor requires more complex augmentations, methods from BaseImageProcessorFast can be overridden.
# In most cases, only the `_preprocess` method should be overridden.

# For an example of a fast image processor requiring more complex augmentations, see `LlavaNextImageProcessorFast`.

# Default values should be checked against the slow image processor
# None values left after checking can be removed
resample = PILImageResampling.BILINEAR
image_mean = OPENAI_CLIP_MEAN
image_std = OPENAI_CLIP_STD
size = {"height": 960, "width": 960}
default_to_square = None
crop_size = None
do_resize = True
do_center_crop = None
do_rescale = True
do_normalize = True
do_convert_rgb = None

def __init__(self, **kwargs: Unpack[DefaultFastImageProcessorKwargs]) -> None:
super().__init__(**kwargs)


__all__ = ["Owlv2ImageProcessorFast"]
7 changes: 7 additions & 0 deletions src/transformers/utils/dummy_torchvision_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ def __init__(self, *args, **kwargs):
requires_backends(self, ["torchvision"])


class Owlv2ImageProcessorFast(metaclass=DummyObject):
_backends = ["torchvision"]

def __init__(self, *args, **kwargs):
requires_backends(self, ["torchvision"])


class Phi4MultimodalImageProcessorFast(metaclass=DummyObject):
_backends = ["torchvision"]

Expand Down
5 changes: 3 additions & 2 deletions tests/models/owlv2/test_image_processing_owlv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
import unittest

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available
from transformers.utils import is_torch_available, is_torchvision_available, is_vision_available

from ...test_image_processing_common import ImageProcessingTestMixin, prepare_image_inputs


if is_vision_available():
from PIL import Image

from transformers import AutoProcessor, Owlv2ForObjectDetection, Owlv2ImageProcessor
from transformers import AutoProcessor, Owlv2ForObjectDetection, Owlv2ImageProcessor, Owlv2ImageProcessorFast

if is_torch_available():
import torch
Expand Down Expand Up @@ -88,6 +88,7 @@ def prepare_image_inputs(self, equal_resolution=False, numpify=False, torchify=F
@require_vision
class Owlv2ImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
image_processing_class = Owlv2ImageProcessor if is_vision_available() else None
fast_image_processing_class = Owlv2ImageProcessorFast if is_torchvision_available() else None

def setUp(self):
super().setUp()
Expand Down