-
Notifications
You must be signed in to change notification settings - Fork 28.2k
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
[Backbone
] Use load_backbone
instead of AutoBackbone.from_config
#28661
Merged
amyeroberts
merged 8 commits into
huggingface:main
from
amyeroberts:update-modeling-backbone-loading
Jan 30, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
01f30ae
Enable instantiating model with pretrained backbone weights
amyeroberts 058ac86
Remove doc updates until changes made in modeling code
amyeroberts 273ba28
Use load_backbone instead
amyeroberts 47c5c2a
Add use_timm_backbone to the model configs
amyeroberts 356eafa
Add missing imports and arguments
amyeroberts 4cae432
Update docstrings
amyeroberts 83ff616
Make sure test is properly configured
amyeroberts a3e6d7d
Include recent DPT updates
amyeroberts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,6 +117,9 @@ class DPTConfig(PretrainedConfig): | |
is `False`, this loads the backbone's config and uses that to initialize the backbone with random weights. | ||
use_pretrained_backbone (`bool`, *optional*, defaults to `False`): | ||
Whether to use pretrained weights for the backbone. | ||
use_timm_backbone (`bool`, *optional*, defaults to `False`): | ||
Whether to load `backbone` from the timm library. If `False`, the backbone is loaded from the transformers | ||
library. | ||
|
||
Example: | ||
|
||
|
@@ -169,6 +172,7 @@ def __init__( | |
backbone_config=None, | ||
backbone=None, | ||
use_pretrained_backbone=False, | ||
use_timm_backbone=False, | ||
**kwargs, | ||
): | ||
super().__init__(**kwargs) | ||
|
@@ -179,9 +183,6 @@ def __init__( | |
if use_pretrained_backbone: | ||
raise ValueError("Pretrained backbones are not supported yet.") | ||
|
||
if backbone_config is not None and backbone is not None: | ||
raise ValueError("You can't specify both `backbone` and `backbone_config`.") | ||
|
||
use_autobackbone = False | ||
if self.is_hybrid: | ||
if backbone_config is None and backbone is None: | ||
|
@@ -193,17 +194,17 @@ def __init__( | |
"out_features": ["stage1", "stage2", "stage3"], | ||
"embedding_dynamic_padding": True, | ||
} | ||
self.backbone_config = BitConfig(**backbone_config) | ||
backbone_config = BitConfig(**backbone_config) | ||
elif isinstance(backbone_config, dict): | ||
logger.info("Initializing the config with a `BiT` backbone.") | ||
self.backbone_config = BitConfig(**backbone_config) | ||
backbone_config = BitConfig(**backbone_config) | ||
elif isinstance(backbone_config, PretrainedConfig): | ||
self.backbone_config = backbone_config | ||
backbone_config = backbone_config | ||
else: | ||
raise ValueError( | ||
f"backbone_config must be a dictionary or a `PretrainedConfig`, got {backbone_config.__class__}." | ||
) | ||
|
||
self.backbone_config = backbone_config | ||
self.backbone_featmap_shape = backbone_featmap_shape | ||
self.neck_ignore_stages = neck_ignore_stages | ||
|
||
|
@@ -221,14 +222,17 @@ def __init__( | |
self.backbone_config = backbone_config | ||
self.backbone_featmap_shape = None | ||
self.neck_ignore_stages = [] | ||
|
||
else: | ||
self.backbone_config = backbone_config | ||
self.backbone_featmap_shape = None | ||
self.neck_ignore_stages = [] | ||
|
||
if use_autobackbone and backbone_config is not None and backbone is not None: | ||
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. Moved to account for the recent DPT updates where backbone and backbone_config can be passed in, but aren't necessary. |
||
raise ValueError("You can't specify both `backbone` and `backbone_config`.") | ||
|
||
self.backbone = backbone | ||
self.use_pretrained_backbone = use_pretrained_backbone | ||
self.use_timm_backbone = use_timm_backbone | ||
self.num_hidden_layers = None if use_autobackbone else num_hidden_layers | ||
self.num_attention_heads = None if use_autobackbone else num_attention_heads | ||
self.intermediate_size = None if use_autobackbone else intermediate_size | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
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.
We need to have the
use_timm_backbone
included in all model configs that contain backbone information to differentiate between loading a transformers or timm pretrained backbone.