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

How to load a custom model? #9802

Closed
1 task done
int3r5t377ar opened this issue Oct 13, 2022 · 10 comments
Closed
1 task done

How to load a custom model? #9802

int3r5t377ar opened this issue Oct 13, 2022 · 10 comments
Labels
question Further information is requested

Comments

@int3r5t377ar
Copy link

Search before asking

Question

I use the code
model = torch.hub.load('ultralytics/yolov5', 'custom', path='C:/Users/Documents/yolov5/runs/train/exp/weights/best.bt', force_reload=True)
to load the custom model I have trained.
The code is found in the internet and it works for the examples I referenced at.
But for me, it keeps generating errors:

PS C:\Users\Documents\yolov5> & C:/Users/anaconda3/python.exe "c:/Users/Documents/yolov5/myfile.py"
Downloading: "https://github.com/ultralytics/yolov5/zipball/master" to C:\Users/.cache\torch\hub\master.zip
YOLOv5  2022-10-13 Python-3.9.13 torch-1.12.1 CUDA:0 (NVIDIA GeForce RTX 3060, 12288MiB)

Traceback (most recent call last):
  File "C:\Users/.cache\torch\hub\ultralytics_yolov5_master\hubconf.py", line 47, in _create
    model = DetectMultiBackend(path, device=device, fuse=autoshape)  # detection model
  File "C:\Users/.cache\torch\hub\ultralytics_yolov5_master\models\common.py", line 331, in __init__
    pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, paddle, triton = self._model_type(w)
  File "C:\Users/.cache\torch\hub\ultralytics_yolov5_master\models\common.py", line 596, in _model_type
    check_suffix(p, sf)  # checks
  File "C:\Users/.cache\torch\hub\ultralytics_yolov5_master\utils\general.py", line 409, in check_suffix

Traceback (most recent call last):  File "c:\Users\Documents\yolov5\myfile.py", line 11, in <module>
    model = torch.hub.load('ultralytics/yolov5', 'custom', path='C:/Users/Documents/yolov5/runs/train/exp/weights/best.bt', force_reload=True)   
  File "C:\Users\anaconda3\lib\site-packages\torch\hub.py", line 540, in load
    model = _load_local(repo_or_dir, model, *args, **kwargs)
  File "C:\Users\anaconda3\lib\site-packages\torch\hub.py", line 569, in _load_local
    model = entry(*args, **kwargs)
  File "C:\Users/.cache\torch\hub\ultralytics_yolov5_master\hubconf.py", line 81, in custom
    return _create(path, autoshape=autoshape, verbose=_verbose, device=device)
  File "C:\Users/.cache\torch\hub\ultralytics_yolov5_master\hubconf.py", line 76, in _create
    raise Exception(s) from e
Exception: [Errno 2] No such file or directory: 'C:\\Users\\Documents\\yolov5\\runs\\train\\exp\\weights\\best.bt'. Cache may be out of date, try `force_reload=True` or see https://github.com/ultralytics/yolov5/issues/36 for help.

Also, I have no idea why the path becomes 'C:\\Users\\Documents\\yolov5\\runs\\train\\exp\\weights\\best.bt'.

Additional

No response

@int3r5t377ar int3r5t377ar added the question Further information is requested label Oct 13, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Oct 13, 2022

👋 Hello @int3r5t377ar, 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 support@ultralytics.com.

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):

Status

YOLOv5 CI

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.

@glenn-jocher
Copy link
Member

@int3r5t377ar the error is telling you that there is no file in the location you specified:

Exception: [Errno 2] No such file or directory: 'C:\\Users\\Documents\\yolov5\\runs\\train\\exp\\weights\\[best.bt](http://best.bt/)'

Windows loading of YOLOv5 PyTorch Hub models is run every 24 hours and operates correctly:
https://github.com/ultralytics/yolov5/actions/runs/3238824126/jobs/5307473149

for path in '$m', '$b':
model = torch.hub.load('.', 'custom', path=path, source='local')
print(model('data/images/bus.jpg'))

@int3r5t377ar
Copy link
Author

int3r5t377ar commented Oct 13, 2022

@glenn-jocher

@int3r5t377ar the error is telling you that there is no file in the location you specified:

Exception: [Errno 2] No such file or directory: 'C:\\Users\\Documents\\yolov5\\runs\\train\\exp\\weights\\[best.bt](http://best.bt/)'

What should I do to use the best.bt file? The file is in the location there.

@glenn-jocher
Copy link
Member

Looks like user error due a mispelling. You've got a *.bt suffix which you probably meant to write as *.pt

@int3r5t377ar
Copy link
Author

Looks like user error due a mispelling. You've got a *.bt suffix which you probably meant to write as *.pt

Oh thank you so much. I overlooked such a simple mistake over long time. And it works fine now. I am gonna close this question.

@bukobeer01
Copy link

@int3r5t377ar the error is telling you that there is no file in the location you specified:

Exception: [Errno 2] No such file or directory: 'C:\\Users\\Documents\\yolov5\\runs\\train\\exp\\weights\\[best.bt](http://best.bt/)'

Windows loading of YOLOv5 PyTorch Hub models is run every 24 hours and operates correctly: https://github.com/ultralytics/yolov5/actions/runs/3238824126/jobs/5307473149

for path in '$m', '$b':
model = torch.hub.load('.', 'custom', path=path, source='local')
print(model('data/images/bus.jpg'))


can you help me I'm using torch 1.11.0+cu113

import torch
path = 'yolov5s.pt'
model = torch.hub.load('.', 'custom', path=path, source='local')
print(model('images\1.jpg'))

I got

File ".\load3.py", line 3, in
model = torch.hub.load('.', 'custom', path=path, source='local')
File "C:\Users\bukob\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\hub.py", line 404, in load
model = _load_local(repo_or_dir, model, *args, **kwargs)
File "C:\Users\bukob\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\hub.py", line 430, in _load_local
hub_module = _import_module(MODULE_HUBCONF, hubconf_path)
File "C:\Users\bukob\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\hub.py", line 76, in _import_module
spec.loader.exec_module(module)
File "", line 779, in exec_module
File "", line 915, in get_code
File "", line 972, in get_data
FileNotFoundError: [Errno 2] No such file or directory: '.\hubconf.py'

@Sha312003
Copy link

In the first argument of torch.hub.load write the path of yolo directory.
For example, instead of model = torch.hub.load('.', 'custom', path=path, source='local') write
model = torch.hub.load('/content/yolov5', 'custom', path=path, source='local').
Hope this helps

@glenn-jocher
Copy link
Member

@Sha312003 your error message suggests that your current directory does not contain a file called hubconf.py. You can specify the path to the directory that contains yolov5s.pt file using torch.hub.load() function like this:

import torch

path = 'yolov5s.pt'
model = torch.hub.load('ultralytics/yolov5', 'custom', path=path, source='local') 
print(model(torch.randn(1,3,640,640))))

This code loads the yolov5s.pt file from the Ultralytics YOLOv5 hub repository and returns a detection model that can be used for inference on images. You can then call the model with an image tensor to get the predicted detections.

@dark292929
Copy link

tengo un error

model = torch.hub.load('ultralytics/yolov5','custom',
path = 'D:/python/pythonProject/model/carros.pt', force_reload=True)

para subir mi modelo personalizable no reconoce,no se si alguien le paso

@glenn-jocher
Copy link
Member

Hola! Parece que estás enfrentado un problema al cargar tu modelo personalizado. Asegúrate que la ruta al archivo .pt es correcta y accesible.

Tu código parece estar mayormente correcto, solo verifica la ruta. Aquí tienes un ejemplo ligeramente ajustado para asegurarnos que se usa correctamente:

import torch
model = torch.hub.load('ultralytics/yolov5', 'custom', path='D:/python/pythonProject/model/carros.pt', force_reload=True)

Si el modelo sigue sin reconocer, verifica que 'carros.pt' exista en la ruta especificada y que no haya errores en el nombre del archivo. ¡Espero que esto te ayude! 😊

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

5 participants