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

Yolo v5 loading using torch 2.6 #13513

Open
1 task done
abcjoh opened this issue Feb 18, 2025 · 2 comments
Open
1 task done

Yolo v5 loading using torch 2.6 #13513

abcjoh opened this issue Feb 18, 2025 · 2 comments
Labels
dependencies Dependencies and packages detect Object Detection issues, PR's question Further information is requested

Comments

@abcjoh
Copy link

abcjoh commented Feb 18, 2025

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 in torch.load from False to True. Re-running torch.load with weights_only set to False 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 the torch.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

@abcjoh abcjoh added the question Further information is requested label Feb 18, 2025
@UltralyticsAssistant UltralyticsAssistant added dependencies Dependencies and packages detect Object Detection issues, PR's labels Feb 18, 2025
@UltralyticsAssistant
Copy link
Member

👋 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:

  • The exact steps or script you’re using to reproduce the issue
  • Any model weights or trained checkpoints (if possible, without sensitive/private data)
  • The logs of errors you're encountering and the command used to run your script

This will help us investigate further.

Requirements

YOLOv5 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:

Documentation

YOLOv5 relies on the torch.load method for model weights loading. In your case, the changes in PyTorch 2.6 (such as the weights_only default behavior) seem to trigger the error. Ensuring up-to-date compatibility or downgrading PyTorch to 2.5 might temporarily resolve your issue 🤔.

👉 Pro Tip: Try adding torch.serialization.add_safe_globals([DetectionModel]) in your script before loading the model, as suggested by the PyTorch error message. This explicitly allows the model class for serialization.

An Ultralytics engineer will take a closer look at this soon. Thank you for your patience! 😊

@pderrenger
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Dependencies and packages detect Object Detection issues, PR's question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants