yolov5
yolov5 copied to clipboard
Inflexibility to Folder Name Changes. How to Save and Load using "state_dict(torch.load(...))" Methods.
Search before asking
- [X] I have searched the YOLOv5 issues and found no similar bug report.
YOLOv5 Component
Other
Bug
I require my "models" folder to be a different name; therefore, I run into the issue when running attempt_load() "models" is not found. I am using a custom model and require my GitHub repo to be private, so the torch.hub method won't work.
I have looked at other issues, to see what I can do. The links below a few of the ones I have gone through: https://github.com/ultralytics/yolov5/issues/6396 https://github.com/ultralytics/yolov5/issues/353 https://github.com/ultralytics/yolov5/issues/6610
I assume on line https://github.com/ultralytics/yolov5/blob/master/train.py#:~:text=torch.save(ckpt%2C%20best) in train.py I need to change it to something like torch.save(model.state_dict(), 'model.pt')
.
And something along the lines of the following to load it.
# Load model model = Ensemble() model.load_state_dict(torch.load('/ssd/Downloads/161222-best_49e-pose.pt', map_location="cuda:0"),strict=True) model.to(device) model.eval()
I have also tried to convert my prior trained model by reloading it, saving it and loading it with the above code and get the following results. This loading method fails with the following: raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format( RuntimeError: Error(s) in loading state_dict for Ensemble:
If I run it with strict=False, I get the error: AttributeError: 'Ensemble' object has no attribute 'stride' I also used the 'Model' object instead of 'Ensemble'. But this also fails with strict=True. It works with strict=False but the model performs very poorly compared to the default loading method during testing.
If anyone is able to help/shed some light on what I need to look at.
Environment
Python 3.8.13 Ubuntu 20.04
Minimal Reproducible Example
Change the name of the Models directory and the imports that require this module. Once done run an inference with a local model.
Additional
No response
Are you willing to submit a PR?
- [ ] Yes I'd like to help by submitting a PR!
👋 Hello @jitaxis, 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 [email protected].
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):
-
Notebooks with free GPU:
- Google Cloud Deep Learning VM. See GCP Quickstart Guide
- Amazon Deep Learning AMI. See AWS Quickstart Guide
-
Docker Image. See Docker Quickstart Guide
Status
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.
👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.
Access additional YOLOv5 🚀 resources:
- Wiki – https://github.com/ultralytics/yolov5/wiki
- Tutorials – https://docs.ultralytics.com/yolov5
- Docs – https://docs.ultralytics.com
Access additional Ultralytics ⚡ resources:
- Ultralytics HUB – https://ultralytics.com/hub
- Vision API – https://ultralytics.com/yolov5
- About Us – https://ultralytics.com/about
- Join Our Team – https://ultralytics.com/work
- Contact Us – https://ultralytics.com/contact
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 YOLOv5 🚀 and Vision AI ⭐!
@jitaxis hello! It seems like you are encountering trouble with renaming the "models" folder and saving/loading your custom model state dict. This isn't a bug, but a customization request.
To rename the "models" folder, you'd need to update references throughout the code. Also, for custom model saving/loading, it may require modifying your model.py script. While this isn't officially supported, others have done similar customizations successfully, and I recommend browsing related discussions on our Docs and GitHub issues for guidance.
Regarding your loading issues, using torch.save(model.state_dict(), 'model.pt')
and model.load_state_dict(torch.load('model.pt'))
gets my vote. Ensure strict=False if encountering attribute errors, and review model architecture compatibility.
Finally, have you considered deriving from YOLOv5's train.py to ensure consistent performance? It could simplify model handling.
Let us know your progress – the YOLO community and our team are here to assist you!