Skip to content
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

Add timm_wrapper support to AutoFeatureExtractor #35764

Open
wants to merge 3 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
6 changes: 5 additions & 1 deletion src/transformers/image_processing_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs):
"""
image_processor_dict = image_processor_dict.copy()
return_unused_kwargs = kwargs.pop("return_unused_kwargs", False)
is_timm = kwargs.pop("is_timm", False)

# The `size` parameter is a dict and was previously an int or tuple in feature extractors.
# We set `size` here directly to the `image_processor_dict` so that it is converted to the appropriate
Expand All @@ -420,7 +421,10 @@ def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs):
if "crop_size" in kwargs and "crop_size" in image_processor_dict:
image_processor_dict["crop_size"] = kwargs.pop("crop_size")

image_processor = cls(**image_processor_dict)
if is_timm:
image_processor = cls(image_processor_dict)
else:
image_processor = cls(**image_processor_dict)

# Update image_processor with kwargs if needed
to_remove = []
Expand Down
1 change: 1 addition & 0 deletions src/transformers/models/auto/feature_extraction_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
("swinv2", "ViTFeatureExtractor"),
("table-transformer", "DetrFeatureExtractor"),
("timesformer", "VideoMAEFeatureExtractor"),
("timm_wrapper", "TimmWrapperImageProcessor"),
("tvlt", "TvltFeatureExtractor"),
("unispeech", "Wav2Vec2FeatureExtractor"),
("unispeech-sat", "Wav2Vec2FeatureExtractor"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ def get_image_processor_dict(
pretrained_model_name_or_path, image_processor_filename=image_processor_filename, **kwargs
)

@classmethod
def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs):
"""
Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is
created using from_dict and kwargs e.g. `TimmWrapperImageProcessor.from_pretrained(checkpoint)`
"""
kwargs.update({"is_timm": True})
return super().from_dict(image_processor_dict, **kwargs)

def preprocess(
self,
images: ImageInput,
Expand Down