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

Exploring vit features_only via new forward_intermediates() API, inspired by #2131 #2136

Merged
merged 5 commits into from
Apr 11, 2024

Conversation

rwightman
Copy link
Collaborator

No description provided.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

* forward_intermediates() added to beit, deit, eva, mvitv2, twins, vit, vit_sam
* add features_only to forward intermediates to allow just intermediate features
* fix #2060
* fix #1374
* fix #657
@rwightman
Copy link
Collaborator Author

design changed a bit,

making the focal method forward_intermediates() ... it's a variant of forward_features() that focuses on collecting relevant intermediates and processing them for consisten NCHW spatial feature output.

Instead of trying to build this in to foward_features() I opted to keep it separate for now, to lower risk of regressions and prevent some complexities with torchscript / typing, etc.

FeatureGetterNet is a new feature extraction wrapper that uses forward_intermediates to return feature maps using the features_only=True attribute. This wrapper calls forward_intermediates() with the features_only=True arg to just return the intermediate features.

Models that have forward_intermediates() can also be used as below, splitting forward to extract both the intermediates and get the class predictions.

final_feat, intermediates = model.forward_intermediates(input) 
output = model.forward_head(final_feat)  # pooling + classifier head

…f people manually replacing .head module having issues
@rwightman rwightman changed the title Exploring vit features_only using get_intermediate_layers() as per #2131 Exploring vit features_only via new forward_intermediates() API, inspired by #2131 Apr 10, 2024
* include relpos vit
* refactor reduction / size calcs so hybrid vits work and dynamic_img_size works
* fix -ve feature indices when pruning
* fix mvitv2 w/ class token
* refine naming
* add tests
@rwightman rwightman merged commit d6b9552 into main Apr 11, 2024
24 checks passed
@isaaccorley
Copy link

Looking forward to testing this!

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.

None yet

3 participants