-
Notifications
You must be signed in to change notification settings - Fork 34k
fix(models): Forward timm model kwargs to timm.create_model for OmDet-Turbo #44611
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
Changes from 1 commit
357a5cf
414ee30
94ce638
45ce043
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -149,8 +149,7 @@ def __init__( | |
| # Init timm backbone with hardcoded values for BC | ||
| timm_default_kwargs = { | ||
| "out_indices": [1, 2, 3], | ||
| "img_size": image_size, | ||
| "always_partition": True, | ||
| "timm_model_kwargs": {"img_size": image_size, "always_partition": True}, | ||
| } | ||
| backbone_config, kwargs = consolidate_backbone_kwargs_to_config( | ||
| backbone_config=backbone_config, | ||
|
|
@@ -161,6 +160,16 @@ def __init__( | |
| **kwargs, | ||
| ) | ||
|
|
||
| if getattr(backbone_config, "model_type", None) == "timm_backbone" and not getattr( | ||
| backbone_config, "timm_model_kwargs", None | ||
| ): | ||
| timm_extra = {} | ||
| for attr in ("img_size", "always_partition"): | ||
| if hasattr(backbone_config, attr): | ||
| timm_extra[attr] = getattr(backbone_config, attr) | ||
| if timm_extra: | ||
| backbone_config.timm_model_kwargs = timm_extra | ||
|
|
||
|
Comment on lines
+165
to
+170
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's add a comment on why this is needed pls
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added :) |
||
| if text_config is None: | ||
| logger.info("`text_config` is `None`. Initializing the config with the default `clip_text_model`") | ||
| text_config = CONFIG_MAPPING["clip_text_model"]() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,8 @@ class TimmBackboneConfig(BackboneConfigMixin, PreTrainedConfig): | |
| Whether to output only the features or also the logits. | ||
| freeze_batch_norm_2d (`bool`, *optional*, defaults to `False`): | ||
| Converts all `BatchNorm2d` and `SyncBatchNorm` layers of provided module into `FrozenBatchNorm2d`. | ||
| timm_model_kwargs (`dict`, *optional*): | ||
| Additional keyword arguments to pass to `timm.create_model` (e.g. `{"img_size": 640}`). | ||
|
|
||
| Example: | ||
| ```python | ||
|
|
@@ -57,6 +59,7 @@ def __init__( | |
| out_indices=None, | ||
| freeze_batch_norm_2d=False, | ||
| output_stride=None, | ||
| timm_model_kwargs=None, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not really what we want for now. The above fix you did with passing it as
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense; reverted :) |
||
| **kwargs, | ||
| ): | ||
| self.backbone = backbone | ||
|
|
@@ -65,6 +68,7 @@ def __init__( | |
| self.out_indices = out_indices if out_indices is not None else [-1] | ||
| self.output_stride = output_stride | ||
| self.freeze_batch_norm_2d = freeze_batch_norm_2d | ||
| self.timm_model_kwargs = timm_model_kwargs if timm_model_kwargs is not None else {} | ||
|
|
||
| super().__init__(**kwargs) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ def __init__(self, config, **kwargs): | |
| out_indices = config.out_indices if getattr(config, "out_indices", None) is not None else (-1,) | ||
| pretrained = kwargs.pop("pretrained", False) | ||
| in_chans = kwargs.pop("in_chans", config.num_channels) | ||
| timm_model_kwargs = getattr(config, "timm_model_kwargs", {}) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unpacking
|
||
|
|
||
| backbone = timm.create_model( | ||
| config.backbone, | ||
|
|
@@ -58,6 +59,7 @@ def __init__(self, config, **kwargs): | |
| in_chans=in_chans, | ||
| out_indices=out_indices, | ||
| output_stride=config.output_stride, | ||
| **timm_model_kwargs, | ||
| **kwargs, | ||
| ) | ||
|
|
||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, the
consolidate_backbone_kwargs_to_configutility should work ootb. If not, can you check why?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, the omdet-turbo-swin-tiny-hf/config.json has
backbone_config:nullandbackbone_kwargs: {"img_size": 640, "always_partition": true, ...}.→
consolidate_backbone_kwargs_to_configtakes this path becbackbone_kwargsis non-empty, the timm_default_kwargs path is skipped entirely, soimg_size/always_partitionend up as direct attrs with notimm_model_kwargs. I've added a corresponding comment in the code as well on why the block is required (happy to know if there's a better direction). Soconsolidate_backbone_kwargs_to_configworks for fresh configs OOTB, butomdet-turbo-swin-tiny-hfseems to require the BC check :)→ I thought about whether to change
consolidate_backbone_kwargs_to_configor write the BC block, but writing a check ofTimmBackboneConfigparams vstimm.create_modelparams might be fragile, like, DETR passesuse_pretrained_backboneintimm_default_kwargs, which is neither aTimmBackboneConfigparam nor a validtimm.create_modelkwarg, so it would get incorrectly forwarded totimm.create_modeland break.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahhh right, TimmBackbone doesn't accept arbitrary kwargs like
TimmWrapperdoes so it gets popped, even if we pass it when creating the configGood catch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I am working on subsequent PR here (#44252). It is blocked currently by a few other required fixes, so until then let's patch OmdetTurbo model code
We can change:
transformers/src/transformers/models/omdet_turbo/modeling_omdet_turbo.py
Line 282 in aad13b8
to smth like:
And make sure that
timm_kwargsisn't saved when serializing the config. We don't want it in hub checkpoints, it'll cause more headacheThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
after unification we'll have more freedom to pass any timm kwargs when creating the model by
config.model_argsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your time @zucchini-nlp; attempted to resolve it accordingly 🤗