-
Notifications
You must be signed in to change notification settings - Fork 7.1k
The test_detection_model_trainable_backbone_layers
test shouldn't download the pretrained_backbone weights
#4660
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
Comments
I was initially worried that the weight download would be a problem for fbcode since we can't have internet access, but it looks like it's working fine, I think the manifold weights are properly picked up (D31758310). However these tests are very long and add up for an extra 8 minutes on the run (https://www.internalfb.com/intern/testinfra/testconsole/testrun/844425139291544/): I wonder if it's the same on the CircleCI side? We might want to try to reduce the time |
@NicolasHug thanks for checking. Yes the weights are always added in the manifold so this won't be a problem. The speed is not as bad on CircleCI. We checked during merge (see here) and only 1 test appeared on the top slowest with execution time 14-15 secs. All others executed in less than 8 sec:
The problem can be solved quite easily using the new weights API, because you can easily patch the model loading method of the weights during tests and avoid their actual downloading. That's a bit harder using the old EDIT: I checked the new proposed API and patching it won't be as easy as I remembered. I think we need to make some additional minor adjustments. Here is how the weights are typically loaded based on the current proposal: if weights is not None:
model.load_state_dict(weights.state_dict(progress=progress)) One could easily patch the |
Dumb question: Could we just set the expected values of the test such that they don't depend on
Would it be enough to patch |
Unfortunately the expected values depend on
Yes. We can easily patch Edit: To clarify what I meant with "the model isn't initialized yet". model = resnet50(weights=ResNet50Weights.ImageNet1k_RefV1) the loading of the weights happens within the method. So we can't yet overwrite the |
I think it should be possible to patch @pytest.mark.parametrize("model_name", get_available_detection_models())
def test_mock_method(model_name, mocker):
mocker.patch('torch.nn.Module.load_state_dict')
mocker.patch('torch.hub.load_state_dict_from_url')
model = torchvision.models.detection.__dict__[model_name](
pretrained=False, pretrained_backbone=True, trainable_backbone_layers=4,
) I haven't checked, but this shouldn't do any network call -- this can be verified with pytest-sockets https://github.com/miketheman/pytest-socket |
I've tried: mocked = [
mocker.patch('torch.nn.Module.load_state_dict'),
mocker.patch('torchvision._internally_replaced_utils.load_state_dict_from_url'),
mocker.patch('torch.hub.load_state_dict_from_url'),
]
# test code goes here
assert all(m.call_count > 0 for m in mocked) Though the load_state_dict is called, the other two aren't. I know that the weights are being downloaded. Any thoughts? |
I think this is because
from https://docs.python.org/3/library/unittest.mock.html#where-to-patch So we probably need to patch This seems to be enough: mocker.patch('torchvision.models.resnet.load_state_dict_from_url')
mocker.patch('torchvision.models.mobilenetv3.load_state_dict_from_url')
mocker.patch('torchvision.models.vgg.load_state_dict_from_url')
mocker.patch('torchvision.models.detection.ssd.load_state_dict_from_url')
mocker.patch('torch.nn.Module.load_state_dict') but it's a bit depressing that we have to manually write all these. |
@NicolasHug Oh darn... This can easily be fixed on the new API where the entire loading is taken care by a single method, but the old one is problematic. @pmeier Any idea if there is a better way to patch the calls on load_state_dict_from_url globally on the tests? We basically want to no-op both |
Example of flakiness caused by downloading the weights. |
Uh oh!
There was an error while loading. Please reload this page.
Feature Improvement
The
test_detection_model_trainable_backbone_layers
test currently downloads the weights of the backbone:vision/test/test_models.py
Line 786 in 6530546
Setting the value
pretrained_backbone=True
is necessary because the number of trainable layers depends on this value. Unfortunately downloading pre-trained weights can lead to flakiness and slow tests and should be avoided. Until we setup a cache to store the weights locally on the CI, we should find a way to skip the actual downloading of weights during the test execution.cc @datumbox @pmeier
The text was updated successfully, but these errors were encountered: