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

FutureWarning: You are using torch.load with weights_only=False #1297

Open
Brainstorrrm opened this issue Aug 23, 2024 · 0 comments
Open

FutureWarning: You are using torch.load with weights_only=False #1297

Brainstorrrm opened this issue Aug 23, 2024 · 0 comments

Comments

@Brainstorrrm
Copy link

FutureWarning: You are using torch.load with weights_only=False (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details).

In a future release, the default value for weights_only will be flipped to True. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via torch.serialization.add_safe_globals. We recommend you start setting weights_only=True for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.

Current recognition.py
net.load_state_dict(copyStateDict(torch.load(trained_model, map_location=device)))
model.load_state_dict(torch.load(model_path, map_location=device))

Change to:
state_dict = torch.load(model_path, map_location=device, weights_only=True)
model.load_state_dict(torch.load(model_path, map_location=device, weights_only=True))

Considerations
Compatibility: Ensure that your version of PyTorch supports the weights_only=True parameter. This feature is available starting from PyTorch 1.10 as a security measure for preventing the execution of arbitrary code upon loading a model. If your PyTorch version is older, consider upgrading to a supported version to use this feature.

Handling State Dictionaries: When setting weights_only=True, ensure that the model’s state dictionary keys align correctly with the keys in the loaded state dictionary. Sometimes prefixes need to be adjusted, as seen with new_key = key[7:], which is common when moving from DataParallel models to standard models.

Quantization: The try-except block around model quantization is a good practice, especially when experimenting with performance optimizations that may not be supported across all model configurations.

Next Steps
Once you've modified the code, it's essential to test the loading process thoroughly to ensure that the model behaves as expected with the new security settings. If you encounter errors related to key mismatches or other issues, you might need to adjust how you manipulate the keys in the state dictionary or reconsider other aspects of the model loading process.

Thanks for great work.

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

No branches or pull requests

1 participant