-
Notifications
You must be signed in to change notification settings - Fork 7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Weights classes for Resnet classification models (#4655)
* adding Weights classes for Resnet classification models * Replacing BasicBlock by Bottleneck in all but 3 model contructors * adding tests for prototype models * fixing typo in environment variable * Update test/test_prototype_models.py Co-authored-by: Vasilis Vryniotis <[email protected]> * changing default value for PYTORCH_TEST_WITH_PROTOTYPE * adding checks to compare outputs of the prototype vs old models * refactoring prototype tests * removing unused imports * applying ufmt * Update test/test_prototype_models.py Co-authored-by: Vasilis Vryniotis <[email protected]> * Update test/test_prototype_models.py Co-authored-by: Vasilis Vryniotis <[email protected]> * Update test/test_prototype_models.py Co-authored-by: Vasilis Vryniotis <[email protected]> * Update test/test_prototype_models.py Co-authored-by: Vasilis Vryniotis <[email protected]> * Update test/test_prototype_models.py Co-authored-by: Vasilis Vryniotis <[email protected]> * Update test/test_prototype_models.py Co-authored-by: Vasilis Vryniotis <[email protected]> * Update test/test_prototype_models.py Co-authored-by: Vasilis Vryniotis <[email protected]> Co-authored-by: Vasilis Vryniotis <[email protected]>
- Loading branch information
Showing
2 changed files
with
261 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,56 @@ | ||
import os | ||
|
||
import pytest | ||
import torch | ||
from common_utils import set_rng_seed, cpu_and_gpu | ||
from test_models import _assert_expected, _model_params | ||
from torchvision import models as original_models | ||
from torchvision.prototype import models | ||
|
||
|
||
def get_available_classification_models(): | ||
return [k for k, v in models.__dict__.items() if callable(v) and k[0].lower() == k[0] and k[0] != "_"] | ||
|
||
|
||
@pytest.mark.parametrize("model_name", get_available_classification_models()) | ||
@pytest.mark.parametrize("dev", cpu_and_gpu()) | ||
@pytest.mark.skipif(os.getenv("PYTORCH_TEST_WITH_PROTOTYPE", "0") == "0", reason="Prototype code tests are disabled") | ||
def test_classification_model(model_name, dev): | ||
set_rng_seed(0) | ||
defaults = { | ||
"num_classes": 50, | ||
"input_shape": (1, 3, 224, 224), | ||
} | ||
kwargs = {**defaults, **_model_params.get(model_name, {})} | ||
input_shape = kwargs.pop("input_shape") | ||
model = models.__dict__[model_name](**kwargs) | ||
model.eval().to(device=dev) | ||
x = torch.rand(input_shape).to(device=dev) | ||
out = model(x) | ||
_assert_expected(out.cpu(), model_name, prec=0.1) | ||
assert out.shape[-1] == 50 | ||
|
||
|
||
@pytest.mark.parametrize("model_name", get_available_classification_models()) | ||
@pytest.mark.parametrize("dev", cpu_and_gpu()) | ||
@pytest.mark.skipif(os.getenv("PYTORCH_TEST_WITH_PROTOTYPE", "0") == "0", reason="Prototype code tests are disabled") | ||
def test_old_vs_new_classification_factory(model_name, dev): | ||
defaults = { | ||
"pretrained": True, | ||
"input_shape": (1, 3, 224, 224), | ||
} | ||
kwargs = {**defaults, **_model_params.get(model_name, {})} | ||
input_shape = kwargs.pop("input_shape") | ||
model_old = original_models.__dict__[model_name](**kwargs) | ||
model_old.eval().to(device=dev) | ||
x = torch.rand(input_shape).to(device=dev) | ||
out_old = model_old(x) | ||
# compare with new model builder parameterized in the old fashion way | ||
model_new = models.__dict__[model_name](**kwargs) | ||
model_new.eval().to(device=dev) | ||
out_new = model_new(x) | ||
torch.testing.assert_close(out_new, out_old, rtol=0.0, atol=0.0, check_dtype=False) | ||
|
||
|
||
def test_smoke(): | ||
import torchvision.prototype.models # noqa: F401 |
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