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

Fix missing attr model.model when loading custom yolov model #8830

Merged
merged 3 commits into from
Aug 1, 2022

Conversation

jacksonargo
Copy link
Contributor

@jacksonargo jacksonargo commented Aug 1, 2022

Loading a custom yolov model causes this line to fail. Adding a test to check if the model actually has a model.model field. With this check, I'm able to load the model no prob.

Loading model via

    model = torch.hub.load(
        'ultralytics/yolov5', 'custom', 'models/frozen_backbone_coco_unlabeled_best.onnx',
        autoshape=True, force_reload=False
    )

Causes traceback:

Traceback (most recent call last):
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/Users/jackson/Documents/GitHub/w210-capstone/api/endpoints/predictions.py", line 26, in post_predictions
    yolov_predictions = predict_bounding_boxes_for_collection(collection_id)
  File "/Users/jackson/Documents/GitHub/w210-capstone/api/predictions/predict_bounding_boxes.py", line 43, in predict_bounding_boxes_for_collection
    model = torch.hub.load(
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/hub.py", line 404, in load
    model = _load_local(repo_or_dir, model, *args, **kwargs)
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/hub.py", line 433, in _load_local
    model = entry(*args, **kwargs)
  File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 72, in custom
    return _create(path, autoshape=autoshape, verbose=_verbose, device=device)
  File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 67, in _create
    raise Exception(s) from e
Exception: 'DetectMultiBackend' object has no attribute 'model'. Cache may be out of date, try `force_reload=True` or see https://github.com/ultralytics/yolov5/issues/36 for help.
Exception on /api/v1/predictions [POST]
Traceback (most recent call last):
  File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 58, in _create
    model.model.model[-1].inplace = False  # Detect.inplace=False for safe multithread inference
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1185, in __getattr__
    raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'DetectMultiBackend' object has no attribute 'model'

🛠️ PR Summary

Made with ❤️ by Ultralytics Actions

🌟 Summary

Enhanced robustness for YOLOv5 model loading and inference in different scenarios.

📊 Key Changes

  • Added fallback loading mechanism with attempt_load for arbitrary models if the standard model loading fails.
  • Moved adjustment of the inplace attribute for the Detect layer to AutoShape class to ensure thread safety during multithreaded inference.
  • Simplified the code in _create function to avoid redundant logic.

🎯 Purpose & Impact

  • Purpose:
    • To ensure the YOLOv5 model can be loaded even when standard loading procedures fail, improving usability.
    • To maintain thread safety during inference which could benefit applications that run YOLOv5 in a multithreaded environment.
  • Impact:
    • Users will experience more reliable model loading, especially in non-standard situations, leading to fewer interruptions.
    • The inference process becomes safer for multithreaded applications, potentially improving performance without sacrificing correctness.

Loading a custom yolov model causes this line to fail. Adding a test to check if the model actually has a model.model field. With this check, I'm able to load the model no prob.

Loading model via

```py
    model = torch.hub.load(
        'ultralytics/yolov5', 'custom', 'models/frozen_backbone_coco_unlabeled_best.onnx',
        autoshape=True, force_reload=False
    )
```

Causes traceback:

```
Traceback (most recent call last):
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/Users/jackson/Documents/GitHub/w210-capstone/api/endpoints/predictions.py", line 26, in post_predictions
    yolov_predictions = predict_bounding_boxes_for_collection(collection_id)
  File "/Users/jackson/Documents/GitHub/w210-capstone/api/predictions/predict_bounding_boxes.py", line 43, in predict_bounding_boxes_for_collection
    model = torch.hub.load(
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/hub.py", line 404, in load
    model = _load_local(repo_or_dir, model, *args, **kwargs)
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/hub.py", line 433, in _load_local
    model = entry(*args, **kwargs)
  File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 72, in custom
    return _create(path, autoshape=autoshape, verbose=_verbose, device=device)
  File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 67, in _create
    raise Exception(s) from e
Exception: 'DetectMultiBackend' object has no attribute 'model'. Cache may be out of date, try `force_reload=True` or see ultralytics#36 for help.
Exception on /api/v1/predictions [POST]
Traceback (most recent call last):
  File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 58, in _create
    model.model.model[-1].inplace = False  # Detect.inplace=False for safe multithread inference
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1185, in __getattr__
    raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'DetectMultiBackend' object has no attribute 'model'
```
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 Hello @jacksonargo, thank you for submitting a YOLOv5 🚀 PR! To allow your work to be integrated as seamlessly as possible, we advise you to:

  • ✅ Verify your PR is up-to-date with upstream/master. If your PR is behind upstream/master an automatic GitHub Actions merge may be attempted by writing /rebase in a new comment, or by running the following code, replacing 'feature' with the name of your local branch:
git remote add upstream https://github.com/ultralytics/yolov5.git
git fetch upstream
# git checkout feature  # <--- replace 'feature' with local branch name
git merge upstream/master
git push -u origin -f
  • ✅ Verify all Continuous Integration (CI) checks are passing.
  • ✅ Reduce changes to the absolute minimum required for your bug fix or feature addition. "It is not daily increase but daily decrease, hack away the unessential. The closer to the source, the less wastage there is." -Bruce Lee

@glenn-jocher
Copy link
Member

@jacksonargo I just spotted this about an hour ago and created a fix to classifier branch in #8829

I'll carry over that fix here, hold on.

@glenn-jocher glenn-jocher merged commit 2e10909 into ultralytics:master Aug 1, 2022
@glenn-jocher
Copy link
Member

@jacksonargo PR is merged. Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐

@jacksonargo
Copy link
Contributor Author

Thanks! 🙇 🙇

ctjanuhowski pushed a commit to ctjanuhowski/yolov5 that referenced this pull request Sep 8, 2022
…tics#8830)

* Update hubconf.py

Loading a custom yolov model causes this line to fail. Adding a test to check if the model actually has a model.model field. With this check, I'm able to load the model no prob.

Loading model via

```py
    model = torch.hub.load(
        'ultralytics/yolov5', 'custom', 'models/frozen_backbone_coco_unlabeled_best.onnx',
        autoshape=True, force_reload=False
    )
```

Causes traceback:

```
Traceback (most recent call last):
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/Users/jackson/Documents/GitHub/w210-capstone/api/endpoints/predictions.py", line 26, in post_predictions
    yolov_predictions = predict_bounding_boxes_for_collection(collection_id)
  File "/Users/jackson/Documents/GitHub/w210-capstone/api/predictions/predict_bounding_boxes.py", line 43, in predict_bounding_boxes_for_collection
    model = torch.hub.load(
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/hub.py", line 404, in load
    model = _load_local(repo_or_dir, model, *args, **kwargs)
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/hub.py", line 433, in _load_local
    model = entry(*args, **kwargs)
  File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 72, in custom
    return _create(path, autoshape=autoshape, verbose=_verbose, device=device)
  File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 67, in _create
    raise Exception(s) from e
Exception: 'DetectMultiBackend' object has no attribute 'model'. Cache may be out of date, try `force_reload=True` or see ultralytics#36 for help.
Exception on /api/v1/predictions [POST]
Traceback (most recent call last):
  File "/Users/jackson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 58, in _create
    model.model.model[-1].inplace = False  # Detect.inplace=False for safe multithread inference
  File "/Users/jackson/Documents/GitHub/w210-capstone/.venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1185, in __getattr__
    raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'DetectMultiBackend' object has no attribute 'model'
```

* Update hubconf.py

* Update common.py

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
@glenn-jocher
Copy link
Member

@jacksonargo you're welcome! 🙌 If you have any more questions or need further assistance, feel free to ask. Keep up the 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

Successfully merging this pull request may close these issues.

None yet

2 participants