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

import error while using torch.hub.load #11472

Closed
2 tasks done
PushpakBhoge512 opened this issue May 2, 2023 · 8 comments
Closed
2 tasks done

import error while using torch.hub.load #11472

PushpakBhoge512 opened this issue May 2, 2023 · 8 comments
Labels
bug Something isn't working Stale

Comments

@PushpakBhoge512
Copy link

Search before asking

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

YOLOv5 Component

PyTorch Hub

Bug

getting the following error when loading via torch.hub.load(), it appears to be coming only for this weights file. my other weights file work fine

snippet

import torch

weights_path = "./runs/train/exp14/weights/best.pt"
model = torch.hub.load("ultralytics/yolov5", "custom", weights_path, force_reload=True)

Error

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
File ~/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py:49, in _create(name, pretrained, channels, classes, autoshape, verbose, device)
     48 try:
---> 49     model = DetectMultiBackend(path, device=device, fuse=autoshape)  # detection model
     50     if autoshape:

File /tmp/yolov5/models/common.py:344, in DetectMultiBackend.__init__(self, weights, device, dnn, data, fp16, fuse)
    343 if pt:  # PyTorch
--> 344     model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse)
    345     stride = max(int(model.stride.max()), 32)  # model stride

File /tmp/yolov5/models/experimental.py:79, in attempt_load(weights, device, inplace, fuse)
     78 for w in weights if isinstance(weights, list) else [weights]:
---> 79     ckpt = torch.load(attempt_download(w), map_location='cpu')  # load
     80     ckpt = (ckpt.get('ema') or ckpt['model']).to(device).float()  # FP32 model

File ~/miniconda3/envs/prod/lib/python3.9/site-packages/torch/serialization.py:607, in load(f, map_location, pickle_module, **pickle_load_args)
    606             return torch.jit.load(opened_file)
--> 607         return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
    608 return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)

File ~/miniconda3/envs/prod/lib/python3.9/site-packages/torch/serialization.py:882, in _load(zip_file, map_location, pickle_module, pickle_file, **pickle_load_args)
    881 unpickler.persistent_load = persistent_load
--> 882 result = unpickler.load()
    884 torch._utils._validate_loaded_sparse_tensors()

File ~/miniconda3/envs/prod/lib/python3.9/site-packages/torch/serialization.py:875, in _load.<locals>.UnpicklerWrapper.find_class(self, mod_name, name)
    874 mod_name = load_module_mapping.get(mod_name, mod_name)
--> 875 return super().find_class(mod_name, name)

File ~/yolov5/models/common.py:30
     29 from utils.dataloaders import exif_transpose, letterbox
---> 30 from utils.general import (LOGGER, ROOT, Profile, check_requirements, check_suffix, check_version, colorstr,
     31                            increment_path, is_notebook, make_divisible, non_max_suppression, scale_boxes, xywh2xyxy,
     32                            xyxy2xywh, yaml_load)
     33 from utils.plots import Annotator, colors, save_one_box

ImportError: cannot import name 'is_notebook' from 'utils.general' (/tmp/yolov5/utils/general.py)

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
File ~/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py:60, in _create(name, pretrained, channels, classes, autoshape, verbose, device)
     59     except Exception:
---> 60         model = attempt_load(path, device=device, fuse=False)  # arbitrary model
     61 else:

File /tmp/yolov5/models/experimental.py:79, in attempt_load(weights, device, inplace, fuse)
     78 for w in weights if isinstance(weights, list) else [weights]:
---> 79     ckpt = torch.load(attempt_download(w), map_location='cpu')  # load
     80     ckpt = (ckpt.get('ema') or ckpt['model']).to(device).float()  # FP32 model

File ~/miniconda3/envs/prod/lib/python3.9/site-packages/torch/serialization.py:607, in load(f, map_location, pickle_module, **pickle_load_args)
    606             return torch.jit.load(opened_file)
--> 607         return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
    608 return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)

File ~/miniconda3/envs/prod/lib/python3.9/site-packages/torch/serialization.py:882, in _load(zip_file, map_location, pickle_module, pickle_file, **pickle_load_args)
    881 unpickler.persistent_load = persistent_load
--> 882 result = unpickler.load()
    884 torch._utils._validate_loaded_sparse_tensors()

File ~/miniconda3/envs/prod/lib/python3.9/site-packages/torch/serialization.py:875, in _load.<locals>.UnpicklerWrapper.find_class(self, mod_name, name)
    874 mod_name = load_module_mapping.get(mod_name, mod_name)
--> 875 return super().find_class(mod_name, name)

File ~/yolov5/models/common.py:30
     29 from utils.dataloaders import exif_transpose, letterbox
---> 30 from utils.general import (LOGGER, ROOT, Profile, check_requirements, check_suffix, check_version, colorstr,
     31                            increment_path, is_notebook, make_divisible, non_max_suppression, scale_boxes, xywh2xyxy,
     32                            xyxy2xywh, yaml_load)
     33 from utils.plots import Annotator, colors, save_one_box

ImportError: cannot import name 'is_notebook' from 'utils.general' (/tmp/yolov5/utils/general.py)

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

Exception                                 Traceback (most recent call last)
Cell In[5], line 1
----> 1 model = torch.hub.load("ultralytics/yolov5", "custom", weights_path)

File ~/miniconda3/envs/prod/lib/python3.9/site-packages/torch/hub.py:399, in load(repo_or_dir, model, source, force_reload, verbose, skip_validation, *args, **kwargs)
    396 if source == 'github':
    397     repo_or_dir = _get_cache_or_reload(repo_or_dir, force_reload, verbose, skip_validation)
--> 399 model = _load_local(repo_or_dir, model, *args, **kwargs)
    400 return model

File ~/miniconda3/envs/prod/lib/python3.9/site-packages/torch/hub.py:428, in _load_local(hubconf_dir, model, *args, **kwargs)
    425 hub_module = import_module(MODULE_HUBCONF, hubconf_path)
    427 entry = _load_entry_from_hubconf(hub_module, model)
--> 428 model = entry(*args, **kwargs)
    430 sys.path.remove(hubconf_dir)
    432 return model

File ~/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py:83, in custom(path, autoshape, _verbose, device)
     81 def custom(path='path/to/model.pt', autoshape=True, _verbose=True, device=None):
     82     # YOLOv5 custom or local model
---> 83     return _create(path, autoshape=autoshape, verbose=_verbose, device=device)

File ~/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py:78, in _create(name, pretrained, channels, classes, autoshape, verbose, device)
     76 help_url = 'https://github.com/ultralytics/yolov5/issues/36'
     77 s = f'{e}. Cache may be out of date, try `force_reload=True` or see {help_url} for help.'
---> 78 raise Exception(s) from e

Exception: cannot import name 'is_notebook' from 'utils.general' (/tmp/yolov5/utils/general.py). Cache may be out of date, try `force_reload=True` or see https://github.com/ultralytics/yolov5/issues/36 for help.

Environment

Yolov5 latest main branch
python 3.9
RTX 3080 Ti cu 11.3 wsl2

Minimal Reproducible Example

import torch

weights_path = "./runs/train/exp14/weights/best.pt"
model = torch.hub.load("ultralytics/yolov5", "custom", weights_path, force_reload=True)

Additional

why do we save model objects directly as pickles and not weights OrderedDict ?
is it possible this error is coming because of this?

environment this was trained on might be slightly different

Are you willing to submit a PR?

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

github-actions bot commented May 2, 2023

👋 Hello @PushpakBhoge512, 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 a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

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

YOLOv5 CI

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

Introducing YOLOv8 🚀

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 🚀!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics

@glenn-jocher
Copy link
Member

@PushpakBhoge512 hi there! Thank you for reaching out and providing all the necessary details. The error seems to be coming from the utils.general module where is_notebook cannot be imported. This might be due to a cache issue. Can you try adding force_reload=True to your torch.hub.load() call and see if that resolves the issue? Like this:

model = torch.hub.load("ultralytics/yolov5", "custom", weights_path, force_reload=True)

Let me know if that helps. To answer your additional question, saving as pickle directly might be due to project-specific requirements and could depend on the structure of the model object. As for your environment being slightly different from the one trained on, that might also be a factor. Hope this helps!

@PushpakBhoge512
Copy link
Author

force_reload=True is not working I have tried it

@PushpakBhoge512
Copy link
Author

@glenn-jocher so if I trained in one environment and tried to load that model on some other environment where some unrelated things were different will that cause an issue? should I try retraining on exact same environment ?

@amir-sbg
Copy link

amir-sbg commented May 4, 2023

I have the same issue.

@PushpakBhoge512
Copy link
Author

@glenn-jocher found the error turns out the model was trained with yolov5 (pip package) hence it was not working
@amir-sbg this might be true in your case check once

@glenn-jocher
Copy link
Member

Thank you for the update, @PushpakBhoge512. It's good to hear that you found the cause of the error. Please note that the model training environment can impact the model's performance, and training the model on the same environment could help maintain consistency and accuracy. As for the issue with yolov5 package, thank you for sharing this with the community. This information could help other users who might be experiencing similar issues. Let us know if you need further assistance.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 4, 2023

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO 🚀 and Vision AI ⭐

@github-actions github-actions bot added the Stale label Jun 4, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale
Projects
None yet
Development

No branches or pull requests

3 participants