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

Fix Pretrained ResNet/ViT not working when features_only=True #2659

Merged
merged 19 commits into from
Mar 25, 2025

Conversation

lccol
Copy link
Contributor

@lccol lccol commented Mar 20, 2025

Fix the AssertionError raised when pretrained ResNet/ViT models (from timm) were created with features_only = True.

ResNet18 and Swin models were not affected by the issue (Swin models are created with torchvision instead of timm).

Closes #2640.

@github-actions github-actions bot added the models Models and pretrained weights label Mar 20, 2025
@adamjstewart adamjstewart added this to the 0.7.0 milestone Mar 20, 2025
@github-actions github-actions bot added the testing Continuous integration testing label Mar 20, 2025
@isaaccorley
Copy link
Collaborator

We will probably have to bump the min version of timm we support because looks like FeatureGetterNet and FeatureListNet aren't available in the min version. Will have to search to see when they were first added

@lccol
Copy link
Contributor Author

lccol commented Mar 20, 2025

We will probably have to bump the min version of timm we support because looks like FeatureGetterNet and FeatureListNet aren't available in the min version. Will have to search to see when they were first added

Yes. Unfortunately, in the current min version of timm, ViT model does not support the features_only param:

RuntimeError: features_only not implemented for Vision Transformer models.

EDIT: it seems that features_only was introduced for ViT in version 1.0.3 of timm. The previous one (0.9.16) still raises the error

@adamjstewart
Copy link
Collaborator

Should we skip the tests for older versions of timm? We don't necessarily need to bump the minimum version if it's very new.

@lccol
Copy link
Contributor Author

lccol commented Mar 22, 2025

I checked and timm 1.0.3 was released in April/May 2024, so the introduction of features_only on ViT is quite recent.

@isaaccorley
Copy link
Collaborator

isaaccorley commented Mar 23, 2025

You can just skip the tests using pytest.mark.skip and check what version of timm is installed.

import pytest
import timm
    
@pytest.mark.skipif(int(timm.__version__.split(".")[0]) < 1, reason="Requires timm 1.0.3 or higher")
def test_features_only():
   ...

@adamjstewart
Copy link
Collaborator

Or better:

import pytest

def test_features_only():
    pytest.importorskip('timm', minversion='1.0.3')

adamjstewart
adamjstewart previously approved these changes Mar 24, 2025
Copy link
Collaborator

@adamjstewart adamjstewart left a comment

Choose a reason for hiding this comment

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

Just a minor request to make the tests simpler, I can do this myself if you want.

Copy link
Collaborator

@adamjstewart adamjstewart left a comment

Choose a reason for hiding this comment

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

Tests look much cleaner now, thanks!

Copy link
Collaborator

@adamjstewart adamjstewart left a comment

Choose a reason for hiding this comment

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

I think this looks good now, thanks for putting up with my tedious change requests!

Note that this PR does increase the time our unit tests take to run from 11m 34s to 12m 52s (+11%). For now that's tolerable. If we add more models in the future and this becomes too much I may only run the features_only=True tests on the release branch.

Thanks again for the PR!

@adamjstewart adamjstewart enabled auto-merge (squash) March 25, 2025 10:05
@adamjstewart adamjstewart merged commit 84898f5 into microsoft:main Mar 25, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
models Models and pretrained weights testing Continuous integration testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pretrained ResNet/ViT models do not work when features_only=True
3 participants