Skip to content
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

improving TimmBackbone to support FrozenBatchNorm2d #27160

Merged

Conversation

rafaelpadilla
Copy link
Contributor

What does this PR do?

The TimmBackbone offers various customizable parameters, such as num_channels, features_only, and out_indices.

Currently, we lack direct support for timm's freeze_batch_norm_2d(...) function, which substitutes the backbone's BatchNorm2d layers with FrozenBatchNorm2d.

For models like RTDetr, Detr, ConditionalDetr, DeformableDetr, etc that require freezing of batch norm layers, one currently needs to either reimplement and modify the backbone manually or use the timm.layers.freeze_batch_norm_2d(...) function, like so:

my_backbone = TimmBackbone.from_pretrained("resnet50d")
my_backbone = timm.layers.freeze_batch_norm_2d(my_backbone)

To simplify this, this PR introduces the TimmBackboneConfig.freeze_batch_norm_2d parameter, which defaults to False for backward compatibility. When creating the backbone, if this parameter is True, it will apply timm's freeze_batch_norm_2d(...) function.

This update simplifies the process of creating backbone models. For models requiring a switch from BatchNorm2d to FrozenBatchNorm2d, the TimmBackbone can be used directly, eliminating additional steps.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@amyeroberts

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Oct 30, 2023

The documentation is not available anymore as the PR was closed or merged.

Rafael Padilla and others added 2 commits October 30, 2023 22:18
Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Nice!

Great addition to the backbone class. Just a comment on adding it as a method instead and including the unfreeze option too.

Copy link
Collaborator

@amyeroberts amyeroberts 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 adding!

Just a note on the backwards compatibility of the model when loading in pretrained checkpoints.

Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com>
Copy link
Collaborator

@amyeroberts amyeroberts 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 adding this!

@rafaelpadilla rafaelpadilla merged commit 1e32b05 into huggingface:main Nov 1, 2023
21 checks passed
@rafaelpadilla rafaelpadilla deleted the optional_freeze_batch_norm_2d branch November 1, 2023 15:58
EduardoPach pushed a commit to EduardoPach/transformers that referenced this pull request Nov 19, 2023
* supporting freeze_batch_norm_2d

* supporting freeze_batch_norm_2d

* including unfreeze + separate into methods

* fix typo

* calling unfreeze

* lint

* Update src/transformers/models/timm_backbone/modeling_timm_backbone.py

Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com>

---------

Co-authored-by: Rafael Padilla <rafael.padilla@huggingface.co>
Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com>
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.

3 participants