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

Simplified PyTorch hub custom models #1677

Merged
merged 1 commit into from
Dec 12, 2020
Merged

Simplified PyTorch hub custom models #1677

merged 1 commit into from
Dec 12, 2020

Conversation

glenn-jocher
Copy link
Member

@glenn-jocher glenn-jocher commented Dec 12, 2020

This PR simplifies the process of loading a custom YOLOv5-based model (of any architecture) in PyTorch Hub. The new syntax is very simple:

import torch

model = torch.hub.load('ultralytics/yolov5', 'custom', path_or_model='path/to/custom_model.pt')
model = model.autoshape()  # for PIL/cv2/np inputs and NMS

# OR if a custom model is already loaded in the python workspace:
model = torch.hub.load('ultralytics/yolov5', 'custom', path_or_model=custom_model)
model = model.autoshape()  # for PIL/cv2/np inputs and NMS

🛠️ PR Summary

Made with ❤️ by Ultralytics Actions

🌟 Summary

New support for loading custom YOLOv5 models directly from user-specified paths.

📊 Key Changes

  • Added custom() function to hubconf.py allowing users to load their own trained YOLOv5 models.
  • Supports three input formats:
    • A string path to the model .pt file.
    • A dictionary object obtained from torch.load().
    • An nn.Module object from the loaded checkpoint.
  • Automatically transfers the model to the same device as its parameters.
  • Loads the model's state_dict and class names (names).
  • Example usage commented out in the __main__ block.

🎯 Purpose & Impact

  • 🚀 Enables users to easily deploy their custom-trained YOLOv5 models for inference.
  • 💻 Improves flexibility by supporting different methods for providing the model.
  • 🔌 Potentially expands the use cases of YOLOv5 to custom datasets and specialized applications.
  • 🌐 Non-expert users benefit from a straightforward way to implement their own models, improving the overall user experience.

@glenn-jocher glenn-jocher merged commit 87ca35b into master Dec 12, 2020
@glenn-jocher glenn-jocher deleted the hub_custom branch December 12, 2020 19:16
@glenn-jocher glenn-jocher linked an issue Dec 12, 2020 that may be closed by this pull request
@NanoCode012
Copy link
Contributor

Hello, is there a reason why auto shape is not done automatically(inside the functions)? Is there a case where users don’t need auto shape?

@glenn-jocher
Copy link
Member Author

@NanoCode012 that's a good question. The only reason not to use .autoshape() would be if you want to use the model for training (with your own custom training code) rather than for inference. I'd assume very few people are doing this though, as most would probably just use this repository to train and then place the model in their project for inference.

The problem is that the autoshape action is not reversible, mostly because it contains a call to .fuse(), so if the model is provided by default with autoshape then it would not be possible to train with it.

@NanoCode012
Copy link
Contributor

@glenn-jocher , perhaps you can add an argument like autoshape=False or trainable=False and set autoshape to True by default?

@glenn-jocher
Copy link
Member Author

@NanoCode012 actually that's a good idea. Then instead of 99% of the people being confused it will invert and the 1% that want to train will be confused, which should work a lot better for the majority. Ok I'll add a TODO here for that.

TODO: think about enabling autoshape by default on all PyTorch Hub model.

@glenn-jocher
Copy link
Member Author

Removing TODO as PR #1692 is now open.

@glenn-jocher glenn-jocher removed the TODO label Dec 15, 2020
burglarhobbit pushed a commit to burglarhobbit/yolov5 that referenced this pull request Jan 1, 2021
burglarhobbit pushed a commit to burglarhobbit/yolov5 that referenced this pull request Jan 1, 2021
burglarhobbit pushed a commit to burglarhobbit/yolov5 that referenced this pull request Jan 1, 2021
KMint1819 pushed a commit to KMint1819/yolov5 that referenced this pull request May 12, 2021
KMint1819 pushed a commit to KMint1819/yolov5 that referenced this pull request May 12, 2021
KMint1819 pushed a commit to KMint1819/yolov5 that referenced this pull request May 12, 2021
BjarneKuehl pushed a commit to fhkiel-mlaip/yolov5 that referenced this pull request Aug 26, 2022
BjarneKuehl pushed a commit to fhkiel-mlaip/yolov5 that referenced this pull request Aug 26, 2022
BjarneKuehl pushed a commit to fhkiel-mlaip/yolov5 that referenced this pull request Aug 26, 2022
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.

No module named 'models'
2 participants