Handle and warn on incompatible kwargs during model creation to enhance the robustness and flexibility of create_model #2516
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
When using
timm
as a backbone library, users often switch between different model architectures, such as CNNs and Vision Transformers. These models frequently have different__init__
signatures. For instance, a ViT might accept animg_size
argument, while many CNN models do not.Currently, passing an unsupported keyword argument to
timm.create_model
results in aTypeError
, causing the program to crash. This forces users to write boilerplate conditional logic to manage keyword arguments for each model type, which undermines the simple, abstract interface thatcreate_model
is designed to provide.This PR aims to enhance the robustness and flexibility of
create_model
by allowing it to handle varied model arguments gracefully, thus improving the user experience.Changes
Improved
_ignore_kwargs
Utility: The_ignore_kwargs
utility function intimm.models._builder
has been updated. It now identifies and logs a warning message specifying which keyword arguments are being filtered out because they are not accepted by the target function (model_cls.__init__
).Proactive Kwarg Filtering: The
build_model_with_cfg
function was modified to call_ignore_kwargs
immediately before the model class is instantiated. This preemptively removes any extraneous arguments from thekwargs
dictionary that would otherwise cause aTypeError
.Outcome
With these changes,
create_model
now behaves as follows instead of crashing with aTypeError
:This improves compatibility across different model types and strengthens the abstraction of the
create_model
factory. As a result, users can more easily experiment with various architectures without writing cumbersome boilerplate code.Test Code
log