Skip to content

Exploring use of kwargs for timm model and transforms creation #35819

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rwightman
Copy link
Contributor

What does this PR do?

Allow kwargs to be passed through to timm create_model and create_transforms fns via the TimmWrapperModel and TimmWrapperImageProcessor classes respectively.

Current exploring use and not ready to finalize. Would like to allow flexibility to configure model and transforms at runtime for added flexibility.

Examples:

Allow dynamic image size for vits
model = AutoModelForImageClassification.from_pretrained("timm/vit_base_patch16_224.augreg2_in21k_ft_in1k", dynamic_img_size=True)

Change image size of pre-processor

model = AutoModel.from_pretrained("timm/vit_large_patch14_dinov2.lvd142m", dynamic_img_size=True)
proc = transformers.AutoImageProcessor.from_pretrained("timm/vit_large_patch14_dinov2.lvd142m", img_size=448)

Customize image transforms, this will enable auto_augment and rand-erasing in train transforms and use the train resolution instead of test target size for eval transform

proc = transformers.AutoImageProcessor.from_pretrained("timm/resnet50.a1_in1k", use_train_size=True, auto_augment='rand-m9-inc1-mstd101', re_prob=0.3)

@rwightman rwightman marked this pull request as draft January 21, 2025 16:54
@rwightman
Copy link
Contributor Author

I though this could also be neat to wire through in the image classification example script, though not quite sure how to add the args to HfArgumentParser, would be least obtrusive if a --model-kwargs and --preprocessor-kwargs could be added that'd pass through lists of key-value pairs as kwargs to the AutoModel and AutoImageProcessor...

@rwightman rwightman requested a review from qubvel January 21, 2025 16:58
@rwightman
Copy link
Contributor Author

Not sure who else would have opinions re the example scripts, this could allow all the advanced timm augmentations, and also enabling of some model regularization like stochastic depth (e.g. drop_path=0.2) for training through transformers. @merveenoyan @NielsRogge

Copy link
Member

@qubvel qubvel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating! Can you please add test cases?

}

# Merge kwargs that should be passed through to timm transform factory into image_processor_dict
for k in _DATA_ARG_KEYS + tuple(inspect.signature(timm.data.create_transform).parameters.keys()):
Copy link
Member

@qubvel qubvel Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to have a variable for tuple(inspect.signature(timm.data.create_transform).parameters.keys()), easier to read and debug

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qubvel changed that code a little in last commit

* default to train input size (less surprising)
* add properties to mimic .size .crop_size .image_mean .image_std attributes in many Transformers image preproc (works with autotrain now)
* try to make key check / inspect code more clear
@rwightman
Copy link
Contributor Author

I added properties to the image processor so use cases where people were read image_processor.size, image_processor.image_mean, etc will work. autotrain relied on this and now it works with timm models.

@qubvel
Copy link
Member

qubvel commented Jan 23, 2025

Great! Can you please add some test cases to tests/models/timm_wrapper/? And would be great to add some code snippets to the docs timm_wrapper.md to make sure it's clear how to modify image processor/model parameters.

@rwightman
Copy link
Contributor Author

Great! Can you please add some test cases to tests/models/timm_wrapper/? And would be great to add some code snippets to the docs timm_wrapper.md to make sure it's clear how to modify image processor/model parameters.

I'll get to this eventually, I wanted to explore the use cases a bit more with some notebooks, train runs and see if any other changes are worthwhile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants