-
-
Notifications
You must be signed in to change notification settings - Fork 16.7k
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
Yolo v5 loading using torch 2.6 #13513
Comments
👋 Hello @abcjoh, thank you for your interest in YOLOv5 🚀 and for reporting this issue! Let's get this resolved. Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for tasks like Custom Model Loading and more. From your description, it appears the issue may be related to 🐛 loading YOLOv5 weights with PyTorch 2.6. To help debug this, could you kindly provide a minimum reproducible example (MRE)? For example:
This will help us investigate further. RequirementsYOLOv5 requires Python>=3.8.0 with all requirements.txt installed, including PyTorch>=1.8. Please verify your environment setup: git clone https://github.com/ultralytics/yolov5 # clone repo
cd yolov5
pip install -r requirements.txt # install packages For compatibility updates with new PyTorch versions (like 2.6), verifying all dependencies within an environment from scratch may help. Quickstart Environments 🌟YOLOv5 can also be run in any of these up-to-date verified environments for hassle-free setups:
DocumentationYOLOv5 relies on the torch.load method for model weights loading. In your case, the changes in PyTorch 2.6 (such as the 👉 Pro Tip: Try adding An Ultralytics engineer will take a closer look at this soon. Thank you for your patience! 😊 |
To resolve the PyTorch 2.6 compatibility issue with YOLOv5, please ensure you're using the latest YOLOv5 code from the master branch (not the v7.0 release tag). Update with: pip install -U git+https://github.com/ultralytics/yolov5.git Then try loading your model again. This update includes compatibility improvements for newer PyTorch versions. If issues persist, confirm your model file is uncorrupted and from a trusted source. For reference, YOLOv5 actively maintains PyTorch compatibility as shown in our release notes: v7.0 updates. |
Search before asking
Question
Code
model = torch.hub.load("ultralytics/yolov5:v7.0", 'custom', model_path, force_reload=True)
Error
/torch/hub/ultralytics_yolov5_v7.0/hubconf.py", line 60, in _create
model = attempt_load(path, device=device, fuse=False) # arbitrary model
File "/home/.cache/torch/hub/ultralytics_yolov5_v7.0/models/experimental.py", line 79, in attempt_load
ckpt = torch.load(attempt_download(w), map_location='cpu') # load
File "/home/venv/lib/python3.10/site-packages/torch/serialization.py", line 1470, in load
raise pickle.UnpicklingError(_get_wo_message(str(e))) from None
_pickle.UnpicklingError: Weights only load failed. This file can still be loaded, to do so you have two options, �[1mdo those steps only if you trust the source of the checkpoint�[0m.
(1) In PyTorch 2.6, we changed the default value of the
weights_only
argument intorch.load
fromFalse
toTrue
. Re-runningtorch.load
withweights_only
set toFalse
will likely succeed, but it can result in arbitrary code execution. Do it only if you got the file from a trusted source.(2) Alternatively, to load with
weights_only=True
please check the recommended steps in the following error message.WeightsUnpickler error: Unsupported global: GLOBAL models.yolo.DetectionModel was not an allowed global by default. Please use
torch.serialization.add_safe_globals([DetectionModel])
or thetorch.serialization.safe_globals([DetectionModel])
context manager to allowlist this global if you trust this class/function.Check the documentation of torch.load to learn more about types accepted by default with weights_only https://pytorch.org/docs/stable/generated/torch.load.html.
Question
Can you please help me to resolve this I have tried to also add the weights_only = False but it is not working. Torch version is 2.6.
Is yolov5 not compatible with this torch 2.6 version or am I missing something.
Additional
No response
The text was updated successfully, but these errors were encountered: