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

Better way to deploy / ModuleNotFoundError #985

Closed
EvgeniiTitov opened this issue Sep 17, 2020 · 9 comments
Closed

Better way to deploy / ModuleNotFoundError #985

EvgeniiTitov opened this issue Sep 17, 2020 · 9 comments
Labels
question Further information is requested

Comments

@EvgeniiTitov
Copy link

EvgeniiTitov commented Sep 17, 2020

❔Question

Hello

First of all, thanks for the great work guys.

I've recently trained the small model on a custom dataset and now we'd love to test it. However, we got stuck with the same issue that has already been asked at least 2 times (issues 22 and 131). Sadly, nobody really gave any concrete answers in those tickets, so I am opening a new one with the hope to get answers.
We copied some files from yolov5/models and yolov5/utils and rewrote the rest. Now when we attempt loading the model we get the error message - ModuleNotFoundError: No module names 'models.yolo'.

What would be the best way to solve the issue? @glenn-jocher mentioned in another ticket that the entire model gets saved intentionally so that people need just the weights file to initialize the model.
a) If let's say I initialize the trained model and then save only the statedict, does it mean I will need both the *.yaml and .pt files to initialize the model in another project?
b) I know there's also an option to torch.jit.trace the model, but it didn't work for me when I tried to do so because some tensors did not match. Has it worked for anyone? Is it even possible to trace 5v models? I believe tracing has its limitations.
c) There's an option to load yolov5 models from the torch hub. However is it possible to initialize it with custom weights and not the default one?

Thank you in advance.

E

@EvgeniiTitov EvgeniiTitov added the question Further information is requested label Sep 17, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Sep 17, 2020

Hello @EvgeniiTitov, thank you for your interest in our work! Please visit our Custom Training Tutorial to get started, and see our Jupyter Notebook Open In Colab, Docker Image, and Google Cloud Quickstart Guide for example environments.

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 model or data training question, please note Ultralytics does not provide free personal support. As a leader in vision ML and AI, we do offer professional consulting, from simple expert advice up to delivery of fully customized, end-to-end production solutions for our clients, such as:

  • Cloud-based AI systems operating on hundreds of HD video streams in realtime.
  • Edge AI integrated into custom iOS and Android apps for realtime 30 FPS video inference.
  • Custom data training, hyperparameter evolution, and model exportation to any destination.

For more information please visit https://www.ultralytics.com.

@glenn-jocher
Copy link
Member

@EvgeniiTitov the intended workflow is simply:

  1. python train.py --data mydata.yaml
  2. python test.py --data mydata.yaml --weights runs/exp0/weights/best.pt

@EvgeniiTitov
Copy link
Author

EvgeniiTitov commented Sep 18, 2020

Hi @glenn-jocher,

Thanks for your reply.

Apologies if I misunderstood you, but I guess your reply doesn't really suggest how to use the model in other projects without facing the ModuleNotFound error.
Could you clarify, please? I listed several options (a, b, c) in my first message, not sure which one you were referring to. I've just studied the test.py file, it loads the model the same way it is done in the detect.py file.

Thanks in advance!
E

@glenn-jocher
Copy link
Member

@EvgeniiTitov not sure I follow. Pytorch models in general require access to modules they use, this is not specific to YOLOv5. If you want to run a native pytorch model you must also make the modules it requires available to it.

You can run this code without issue from yolov5/:

import torch

model = torch.load('yolov5s.pt', map_location='cpu')['model'].float().eval()
img = torch.rand(1,3,640,640)
prediction = model(img)[0]
prediction.shape  # torch.Size([1, 25200, 85])

@EvgeniiTitov
Copy link
Author

EvgeniiTitov commented Sep 19, 2020

Hi @glenn-jocher,

Apologies for the confusion, I must have worded my question badly. Just to reiterate I am unable to initialize the v5s model trained on the custom dataset outside the v5 project cloned and used for training.

Whenever I try to load my model either using the attempt_load() function provided by you guys or using the code snippet above I get the same error:
image

I would love to know what is the correct way to overcome this problem to be able to initialize and use the trained model in another project with a different folder structure.

Thank you a lot!
E

@EvgeniiTitov
Copy link
Author

Hi @glenn-jocher,

I would much appreciate it if you could reply to the above question. Sorry for being a pain in the ssa.

Thanks a lot!

@glenn-jocher
Copy link
Member

glenn-jocher commented Sep 21, 2020

@EvgeniiTitov hey buddy. Unfortunately model portability has not been high on our priority list, so I don't have any easy answers for you. We've been focusing on debugging and validating basic functionality (train, test, inference), which seems to be going well, as the new bug reports seem to have fallen recently, so soon I think we can focus more on useability and address some of these portability concerns. In general, if you would like to access YOLOv5 functionality outside this repo, your options are:

@glenn-jocher
Copy link
Member

glenn-jocher commented Sep 21, 2020

@EvgeniiTitov also note that PyTorch Hub and the yolov5 pip package (pip install yolov5) are planned to allow for extreme ease of use, i.e.:

from yolov5 import yolov5s

img = cv2.imread('image.jpg')
predictions = yolov5s(img)

Of these two, PyTorch hub should be there much sooner, hopefully in the next week or two. To enable this to work with a custom trained model, a simple call to update the state_dicts() should suffice.

@thejitenpatel
Copy link

Hi @glenn-jocher,

Apologies for the confusion, I must have worded my question badly. Just to reiterate I am unable to initialize the v5s model trained on the custom dataset outside the v5 project cloned and used for training.

Whenever I try to load my model either using the attempt_load() function provided by you guys or using the code snippet above I get the same error:
image

I would love to know what is the correct way to overcome this problem to be able to initialize and use the trained model in another project with a different folder structure.

Thank you a lot!
E

I was facing the same issue but I tried a different way which was mentioned ultralytics github repo.
you can load your model using the following line:
model_path = "your custom model path"
model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path)
model = model.autoshape()
this solution works for me well.

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
Development

No branches or pull requests

3 participants