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

cannot load custom model on cpu (no gpu device available) using torch hub. #8023

Closed
1 of 2 tasks
sazzadhrz opened this issue May 28, 2022 · 5 comments · Fixed by #8024
Closed
1 of 2 tasks

cannot load custom model on cpu (no gpu device available) using torch hub. #8023

sazzadhrz opened this issue May 28, 2022 · 5 comments · Fixed by #8024
Labels
bug Something isn't working

Comments

@sazzadhrz
Copy link

sazzadhrz commented May 28, 2022

Search before asking

  • I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

PyTorch Hub

Bug

Downloading: "https://github.com/ultralytics/yolov5/archive/master.zip" to /home/sazzad/.cache/torch/hub/master.zip
YOLOv5 🚀 2022-5-28 Python-3.8.10 torch-1.11.0+cu102 CPU

Traceback (most recent call last):
  File "/home/sazzad/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 47, in _create
    model = DetectMultiBackend(path, device=device)  # download/load FP32 model
  File "/home/sazzad/.cache/torch/hub/ultralytics_yolov5_master/models/common.py", line 334, in __init__
    model = attempt_load(weights if isinstance(weights, list) else w, device=device)
  File "/home/sazzad/.cache/torch/hub/ultralytics_yolov5_master/models/experimental.py", line 80, in attempt_load
    ckpt = torch.load(attempt_download(w))
  File "/home/sazzad/venv/a2i/lib/python3.8/site-packages/torch/serialization.py", line 712, in load
    return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
  File "/home/sazzad/venv/a2i/lib/python3.8/site-packages/torch/serialization.py", line 1046, in _load
    result = unpickler.load()
  File "/home/sazzad/venv/a2i/lib/python3.8/site-packages/torch/serialization.py", line 1016, in persistent_load
    load_tensor(dtype, nbytes, key, _maybe_decode_ascii(location))
  File "/home/sazzad/venv/a2i/lib/python3.8/site-packages/torch/serialization.py", line 1001, in load_tensor
    wrap_storage=restore_location(storage, location),
  File "/home/sazzad/venv/a2i/lib/python3.8/site-packages/torch/serialization.py", line 176, in default_restore_location
    result = fn(storage, location)
  File "/home/sazzad/venv/a2i/lib/python3.8/site-packages/torch/serialization.py", line 152, in _cuda_deserialize
    device = validate_cuda_device(location)
  File "/home/sazzad/venv/a2i/lib/python3.8/site-packages/torch/serialization.py", line 136, in validate_cuda_device
    raise RuntimeError('Attempting to deserialize object on a CUDA '
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "src/yolo.py", line 20, in <module>
    model = torch.hub.load('ultralytics/yolov5', 'custom', path=weight_path, force_reload=True).autoshape()
  File "/home/sazzad/venv/a2i/lib/python3.8/site-packages/torch/hub.py", line 404, in load
    model = _load_local(repo_or_dir, model, *args, **kwargs)
  File "/home/sazzad/venv/a2i/lib/python3.8/site-packages/torch/hub.py", line 433, in _load_local
    model = entry(*args, **kwargs)
  File "/home/sazzad/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 71, in custom
    return _create(path, autoshape=autoshape, verbose=_verbose, device=device)
  File "/home/sazzad/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 66, in _create
    raise Exception(s) from e
Exception: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.. Cache may be out of date, try `force_reload=True` or see https://github.com/ultralytics/yolov5/issues/36 for help.

Environment

YOLOv5 🚀 2022-5-28 Python-3.8.10 torch-1.11.0+cu102 CPU
OS - Ubuntu 20.04
python - 3.8.10

Minimal Reproducible Example

import torch

img = 'https://ultralytics.com/images/zidane.jpg'
model_path = 'my_custom_yolov5.pt'

model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path, device='cpu', force_reload=True)
results = model(img)
results.print()

Additional

custom model is available here - https://drive.google.com/file/d/1-Xnk38QZvFXxmAO2REtmIQn6_Qnfl3fI/view?usp=sharing

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!
@sazzadhrz sazzadhrz added the bug Something isn't working label May 28, 2022
@github-actions
Copy link
Contributor

github-actions bot commented May 28, 2022

👋 Hello @sazzadhrz, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on macOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

@sazzadhrz there may just be something wrong with your model. Official models operate correctly on GPU or CPU without error using your command:

Screenshot 2022-05-28 at 22 38 32

@glenn-jocher
Copy link
Member

@sazzadhrz good news 😃! Your original issue has been added to YOLOv5 CI tests (https://github.com/ultralytics/yolov5/actions/workflows/ci-testing.yml) running every 24 hours and on every commit on every operating system. These tests verify correct operation of custom trained models with PyTorch Hub CPU in ✅ in PR #8024.

Screenshot 2022-05-28 at 22 49 30

Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀!

@sazzadhrz
Copy link
Author

Hello @glenn-jocher,

I can load other models using torch hub. This specific model is creating the issue now, and it's not resolved yet. Please note that the model was running fine for months until 27th May 2022. Could you please try to test the model I have shared?

Thanks!

@glenn-jocher
Copy link
Member

glenn-jocher commented May 29, 2022

@sazzadhrz older models load correctly with PyTorch Hub. If your model has problems and these v5.0 models that are over a year old work correctly then I'd say the source of the error is isolated to your model.

Screenshot 2022-05-29 at 18 20 45

CPU-only Colab:
Screenshot 2022-05-29 at 18 22 47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants