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

Inference on my pretrained model #12419

Closed
1 task done
unrue opened this issue Nov 23, 2023 · 15 comments
Closed
1 task done

Inference on my pretrained model #12419

unrue opened this issue Nov 23, 2023 · 15 comments
Labels
question Further information is requested Stale Stale and schedule for closing soon

Comments

@unrue
Copy link

unrue commented Nov 23, 2023

Search before asking

Question

I successfully trained yolov5x6 model on custom dataset using Yolov5 and Pytorch 1.13. Now, I want to make inference on new images:

model = torch.load('weights/best.pt')
model.eval()

Having the following error:

 model.eval()
AttributeError: 'dict' object has no attribute 'eval'

I don't understand, which is the right way to load a pretrained model and make inference? Thanks.

Additional

No response

@unrue unrue added the question Further information is requested label Nov 23, 2023
@glenn-jocher
Copy link
Member

@unrue this issue is due to using the torch.load method, that returns a dictionary rather than a model instance. Instead, load the model using model = torch.hub.load('ultralytics/yolov5', 'custom', path_or_model='weights/best.pt') for inference. This loads the best.pt and sets the model to eval mode. Further details can be found at https://docs.ultralytics.com/yolov5/.

@unrue
Copy link
Author

unrue commented Nov 24, 2023

Thansk Glenn. I tried as suggested, but I get:

urllib.error.URLError: <urlopen error [Errno 101] Network is unreachable>

I'm on a HPC machine, and compute nodes are not connected to internet. Why Yolo try to download something? The saved model is local. What I have to download? Thanks.

@enislalmi
Copy link

@unrue If you are not specifying the data that you want to train, it probably is using as default coco datasets and it is trying to download it. Try specifying you data by using --data keyword.

@unrue
Copy link
Author

unrue commented Nov 24, 2023

Ok, I solved:

model =torch.hub.load(os.getcwd(), 'custom',  source='local', path ='mymodel/best.pt', force_reload = True)
model.eval()

Not sure if force_reload is necessary, but it works.

Thanks.

@glenn-jocher
Copy link
Member

@unrue you're welcome! Your solution looks good. The force_reload = True is usually not necessary but doesn't hurt. Feel free to remove it if your model loads successfully without it. If you have any more questions, feel free to ask.

@watertianyi
Copy link

@glenn-jocher @unrue
FileNotFoundError: [Errno 2] No such file or directory: 'ultralytics/web/hubconf.py'
I reported the above error according to your configuration.

@glenn-jocher
Copy link
Member

@goldwater668 Apologies for the confusion. The error you're encountering seems to be related to a file not being found in the specified location. Are you using the latest version of YOLOv5? It's possible that the file hubconf.py may not be present in the directory you specified. Can you confirm the version you are using?

@watertianyi
Copy link

@glenn-jocher
If I don’t want to import so many files py and just want to use torch.load for model inference, what should I do?

@enislalmi
Copy link

@goldwater668 You will need to clone the github repository, if you want to use yolov5 at its full features. Check the documentation below https://docs.ultralytics.com/yolov5/tutorials/train_custom_data/

@watertianyi
Copy link

@glenn-jocher
I found the hubconf.py file and updated the code to the latest version. The following error was reported:
repo
File "/home/anaconda3/envs/yolov5/lib/python3.8/site-packages/torch/hub.py", line 566, in load
model = _load_local(repo_or_dir, model, *args, **kwargs)
File "/home/anaconda3/envs/yolov5/lib/python3.8/site-packages/torch/hub.py", line 595, in _load_local
model = entry(*args, **kwargs)
File "/media/EC3C5BDA3C5B9E80/win10/hrg/yolov5/runs/train/exp10/hubconf.py", line 83, in custom
return _create(path, autoshape=autoshape, verbose=_verbose, device=device)
File "/media/EC3C5BDA3C5B9E80/win10/hrg/yolov5/runs/train/exp10/hubconf.py", line 33, in _create
from models.common import AutoShape, DetectMultiBackend
ModuleNotFoundError: No module named 'models'

@watertianyi
Copy link

@glenn-jocher
Python>=3.7.0环境中安装requirements.txt,包括PyTorch>=1.7模型数据集会从最新的 YOLOv5版本自动下载。

pip install -r https://raw.githubusercontent.com/ultralytics/yolov5/master/requirements.txt
💡专业提示:不需要克隆https://github.com/ultralytics/yolov5 😃

If yolov5 is not cloned, an error will be reported. If it is cloned, an error will be reported.

@glenn-jocher
Copy link
Member

@goldwater668! 根据你的反馈,我会为你提供一些建议。

首先,请确保安装的是最新版的 YOLOv5。如果你使用了 torch.hub.load,代替了 torch.load,那么确保你在调用时已经按照正确的方式设置了模型的路径。在最新版本中,你也许需要在调用模型之前对其进行解析。此外,可能需要对模型进行自动调整以适应预测输入。你可以查看我们的官方文档,里面提供了详细的使用指南和示例代码。

希望这些信息对你有所帮助。如果你需要更多帮助,欢迎回复我,我会尽力协助你解决问题。

@watertianyi
Copy link

watertianyi commented Dec 2, 2023

@glenn-jocher
Yes, the latest yolov5 version, the following is the version information:

Name: ultralytics
Version: 8.0.199
Summary: Ultralytics YOLOv8 for SOTA object detection, multi-object tracking, instance segmentation, pose estimation and image classification.
Home-page: https://github.com/ultralytics/ultralytics
Author: Ultralytics
Author-email: [email protected]
License: AGPL-3.0
Location: /home/hjq/anaconda3/envs/yolov5/lib/python3.8/site-packages
Requires: matplotlib, numpy, opencv-python, pandas, pillow, psutil, py-cpuinfo, pyyaml, requests, scipy, seaborn, thop, torch, torchvision, tqdm
Required-by:

代码如下:
`import torch

model = torch.hub.load('./runs/train/exp10', 'custom', path='./runs/train/exp10/weights/best.pt', source='local') `

If yolov5 is not cloned, an error will be reported. If it is cloned, an error will be reported.

@glenn-jocher
Copy link
Member

@goldwater668 Thanks for providing the version information and the code snippet. It seems your code is attempting to load the custom model from the local directory. If the model is not cloned, you may encounter errors related to module imports. If it is cloned, you may still face issues with the relative paths and module imports.

I suggest adhering to the recommended installation process and using the absolute path to load the model.

import torch

model = torch.hub.load('ultralytics/yolov5', 'custom', path='absolute/path/to/your/model/best.pt', source='local')

Make sure to replace 'absolute/path/to/your/model/best.pt' with the actual absolute path to your model file.

If you continue to experience issues, please let me know and provide the specific error messages for further assistance.

Copy link
Contributor

github-actions bot commented Jan 2, 2024

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

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 YOLO 🚀 and Vision AI ⭐

@github-actions github-actions bot added the Stale Stale and schedule for closing soon label Jan 2, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale Stale and schedule for closing soon
Projects
None yet
Development

No branches or pull requests

4 participants