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 the model #4970

Closed
pnama opened this issue Sep 28, 2021 · 13 comments · Fixed by #4974 or #4978
Closed

Cannot load the model #4970

pnama opened this issue Sep 28, 2021 · 13 comments · Fixed by #4974 or #4978
Labels
question Further information is requested

Comments

@pnama
Copy link

pnama commented Sep 28, 2021

I get an error when I run this code torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/path/last.pt', force_reload=True)
It was working until yesterday and now I receive an error "raise ValueError("{!r} does not start with {!r}"
ValueError: 'C:\Users\aaa\.cache\torch\hub\ultralytics_yolov5_master' does not start with 'C:\Users\aaa\PycharmProjects\project\proejct1'". I have removed the files inside the cache folder but it doesn't fix the error...
Any suggestions will be appreciated. Thank you

@pnama pnama added the question Further information is requested label Sep 28, 2021
@Basti110
Copy link

Basti110 commented Sep 28, 2021

I have the same error. Two days ago, the code worked for me.

@chainyo
Copy link

chainyo commented Sep 28, 2021

It seems linked to these recent commits: 5ed2860 and 938b39b

@pnama
Copy link
Author

pnama commented Sep 28, 2021

I've tried both the old version and new version, but still having the same error :(

@yuriikulish
Copy link

The problem is here 5ed2860#diff-2cd118cbb69c9ca7b5544f4187b11335fc3addbaf2c3c5bb45435cac648c957bR18

It fails to create relative path to two paths with no common root path

@chainyo
Copy link

chainyo commented Sep 28, 2021

It fails to create relative path to two paths with no common root path

yes, the condition statement if... else... was removed.

- ROOT = ROOT.relative_to(Path.cwd()) if ROOT.is_relative_to(Path.cwd()) else ROOT # relative

+ ROOT = ROOT.relative_to(Path.cwd())

Check this commit: 938b39b

@pnama
Copy link
Author

pnama commented Sep 28, 2021

Is there a way to fix it? I tried to edit but hub.load keeps removing my fix and runs with the newest version...

@chainyo
Copy link

chainyo commented Sep 28, 2021

Is there a way to fix it? I tried to edit but hub.load keeps removing my fix and runs with the newest version...

You can specify a local link to the repository when loading the model via torch.hub.load()
Check: https://pytorch.org/docs/stable/hub.html#torch.hub.load
I think you have to git clone yourself the repo of yolov5 and fill the path to find it in your loading function.

You can also fork the version you want of the YoloV5 repo and change ultralytics/yolov5 by your_github/yolov5.

@pnama
Copy link
Author

pnama commented Sep 28, 2021

Ok. It seems like it's working for now. Thank you so much everyone. Amazing help

@pnama pnama closed this as completed Sep 28, 2021
@chainyo
Copy link

chainyo commented Sep 28, 2021

@pnama Could you describe what worked for you ?

Edit: This is what worked for me.

  • Fork YoloV5 repository
  • I cloned the new forked repo on local git clone https://github.com/ChainYo/yolov5
  • cd yolov5 to enter in the cloned repo
  • git reset --hard a820b43aca3816c9552e9beaf14a77955742b0ec to restore the repo before the path change
  • git push -f to make a forced push of the restored repo on my own github
  • Replace ultralytics/yolov5 by ChainYo/yolov5 in my torch.hub.load() function.

This is what my loading function looks like:

model = torch.hub.load("ChainYo/yolov5", "custom", path="path/to/yolov5.pt", force_reload=True)

@pnama
Copy link
Author

pnama commented Sep 28, 2021

Yeap. Looks like you have the perfect answer. In my case, I've already had the yolo model so I just changed the path to the local folder and it worked :)

@glenn-jocher
Copy link
Member

@pnama @Basti110 @chainyo good news 😃! Your original issue may now be fixed ✅ in PR #4974. This PR comments out relative ROOT attempts in yolo.py and tf.py which may be imported by PyTorch Hub. We did not revert to using Path().is_relative() anymore because this was only introduced in python 3.8 and we want python >3.6 compatability.

To receive this update:

  • Gitgit pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub – Force-reload with model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks – View updated notebooks Open In Colab Open In Kaggle
  • Dockersudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

Thank you for spotting this issue and informing us of the problem. 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 🚀!

@glenn-jocher
Copy link
Member

glenn-jocher commented Sep 28, 2021

TODO: Add import custom model with PyTorch Hub to CI tests.

torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/path/last.pt', force_reload=True)

@glenn-jocher glenn-jocher linked a pull request Sep 28, 2021 that will close this issue
@glenn-jocher
Copy link
Member

@pnama @Basti110 @chainyo CI tests have been updated in PR #4978 to include PyTorch Hub custom model loading as part of CI. This should hopefully help us catch any bugs of this type going forward before merging PRs.

Removing TODO, task complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
5 participants