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
timmas 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_sizeargument, while many CNN models do not.Currently, passing an unsupported keyword argument to
timm.create_modelresults 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_modelis designed to provide.This PR aims to enhance the robustness and flexibility of
create_modelby allowing it to handle varied model arguments gracefully, thus improving the user experience.Changes
Improved
_ignore_kwargsUtility: The_ignore_kwargsutility function intimm.models._builderhas 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_cfgfunction was modified to call_ignore_kwargsimmediately before the model class is instantiated. This preemptively removes any extraneous arguments from thekwargsdictionary that would otherwise cause aTypeError.Outcome
With these changes,
create_modelnow behaves as follows instead of crashing with aTypeError:This improves compatibility across different model types and strengthens the abstraction of the
create_modelfactory. As a result, users can more easily experiment with various architectures without writing cumbersome boilerplate code.Test Code
log