Add Fast Image Processor for MobileNetV1 (#37111)

* fast image processor template for MobileNetV1 via transformers-cli

* Add fast image processors and unify tests for slow/fast image processor classes

* added loop over image_processor_list for all tests and removed boilerplate comments.

---------

Co-authored-by: Yoni Gozlan <74535834+yonigozlan@users.noreply.github.com>
This commit is contained in:
Daksh Maheshwari 2025-04-23 15:55:41 -04:00 committed by GitHub
parent dea1919be4
commit b6d65e40b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 72 additions and 13 deletions

View File

@ -77,6 +77,11 @@ If you're interested in submitting a resource to be included here, please feel f
[[autodoc]] MobileNetV1ImageProcessor
- preprocess
## MobileNetV1ImageProcessorFast
[[autodoc]] MobileNetV1ImageProcessorFast
- preprocess
## MobileNetV1Model
[[autodoc]] MobileNetV1Model

View File

@ -117,7 +117,7 @@ else:
("mistral3", ("PixtralImageProcessor", "PixtralImageProcessorFast")),
("mlcd", ("CLIPImageProcessor", "CLIPImageProcessorFast")),
("mllama", ("MllamaImageProcessor",)),
("mobilenet_v1", ("MobileNetV1ImageProcessor",)),
("mobilenet_v1", ("MobileNetV1ImageProcessor", "MobileNetV1ImageProcessorFast")),
("mobilenet_v2", ("MobileNetV2ImageProcessor", "MobileNetV2ImageProcessorFast")),
("mobilevit", ("MobileViTImageProcessor",)),
("mobilevitv2", ("MobileViTImageProcessor",)),

View File

@ -21,6 +21,7 @@ if TYPE_CHECKING:
from .configuration_mobilenet_v1 import *
from .feature_extraction_mobilenet_v1 import *
from .image_processing_mobilenet_v1 import *
from .image_processing_mobilenet_v1_fast import *
from .modeling_mobilenet_v1 import *
else:
import sys

View File

@ -0,0 +1,47 @@
# 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 MobileNetV1."""
from ...image_processing_utils_fast import (
BASE_IMAGE_PROCESSOR_FAST_DOCSTRING,
BaseImageProcessorFast,
DefaultFastImageProcessorKwargs,
Unpack,
)
from ...image_utils import IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, PILImageResampling
from ...utils import add_start_docstrings
@add_start_docstrings(
"Constructs a fast MobileNetV1 image processor.",
BASE_IMAGE_PROCESSOR_FAST_DOCSTRING,
)
class MobileNetV1ImageProcessorFast(BaseImageProcessorFast):
resample = PILImageResampling.BILINEAR
image_mean = IMAGENET_STANDARD_MEAN
image_std = IMAGENET_STANDARD_STD
size = {"shortest_edge": 256}
default_to_square = False
crop_size = {"height": 224, "width": 224}
do_resize = True
do_center_crop = True
do_rescale = True
do_normalize = True
def __init__(self, **kwargs: Unpack[DefaultFastImageProcessorKwargs]) -> None:
super().__init__(**kwargs)
__all__ = ["MobileNetV1ImageProcessorFast"]

View File

@ -16,7 +16,7 @@
import unittest
from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_vision_available
from transformers.utils import is_torchvision_available, is_vision_available
from ...test_image_processing_common import ImageProcessingTestMixin, prepare_image_inputs
@ -24,6 +24,9 @@ from ...test_image_processing_common import ImageProcessingTestMixin, prepare_im
if is_vision_available():
from transformers import MobileNetV1ImageProcessor
if is_torchvision_available():
from transformers import MobileNetV1ImageProcessorFast
class MobileNetV1ImageProcessingTester:
def __init__(
@ -79,6 +82,7 @@ class MobileNetV1ImageProcessingTester:
@require_vision
class MobileNetV1ImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
image_processing_class = MobileNetV1ImageProcessor if is_vision_available() else None
fast_image_processing_class = MobileNetV1ImageProcessorFast if is_torchvision_available() else None
def setUp(self):
super().setUp()
@ -89,17 +93,19 @@ class MobileNetV1ImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase
return self.image_processor_tester.prepare_image_processor_dict()
def test_image_processor_properties(self):
image_processing = self.image_processing_class(**self.image_processor_dict)
self.assertTrue(hasattr(image_processing, "do_resize"))
self.assertTrue(hasattr(image_processing, "size"))
self.assertTrue(hasattr(image_processing, "do_center_crop"))
self.assertTrue(hasattr(image_processing, "center_crop"))
for image_processing_class in self.image_processor_list:
image_processing = image_processing_class(**self.image_processor_dict)
self.assertTrue(hasattr(image_processing, "do_resize"))
self.assertTrue(hasattr(image_processing, "size"))
self.assertTrue(hasattr(image_processing, "do_center_crop"))
self.assertTrue(hasattr(image_processing, "center_crop"))
def test_image_processor_from_dict_with_kwargs(self):
image_processor = self.image_processing_class.from_dict(self.image_processor_dict)
self.assertEqual(image_processor.size, {"shortest_edge": 20})
self.assertEqual(image_processor.crop_size, {"height": 18, "width": 18})
for image_processing_class in self.image_processor_list:
image_processor = image_processing_class.from_dict(self.image_processor_dict)
self.assertEqual(image_processor.size, {"shortest_edge": 20})
self.assertEqual(image_processor.crop_size, {"height": 18, "width": 18})
image_processor = self.image_processing_class.from_dict(self.image_processor_dict, size=42, crop_size=84)
self.assertEqual(image_processor.size, {"shortest_edge": 42})
self.assertEqual(image_processor.crop_size, {"height": 84, "width": 84})
image_processor = image_processing_class.from_dict(self.image_processor_dict, size=42, crop_size=84)
self.assertEqual(image_processor.size, {"shortest_edge": 42})
self.assertEqual(image_processor.crop_size, {"height": 84, "width": 84})