Open kanagarajan0709 opened 2 months ago
@kanagarajan0709 hello,
Thank you for reaching out and providing detailed information about the issue you're encountering. It looks like you're facing an AttributeError
related to the 'Detect' object while trying to load a YOLOv5 model trained from Ultralytics HUB.
Here are a few steps you can take to resolve this issue:
Verify Package Versions: Ensure that you are using the latest versions of the YOLOv5 repository and PyTorch. Sometimes, compatibility issues arise from using outdated versions. You can update YOLOv5 by pulling the latest changes from the repository:
git pull
And update PyTorch using:
pip install torch --upgrade
Force Reload: As suggested in the error message, try setting force_reload=True
to ensure that the cache is not causing the issue:
yolo_model = torch.hub.load(yolov5path, 'custom', force_reload=True, path=model_path, source='local')
Check Model Path: Ensure that the model_path
is correctly pointing to the trained weights file. Double-check the file path and name to avoid any discrepancies.
Update YOLOv5 Code: If you haven't already, make sure the YOLOv5 codebase in your yolov5path
directory is up-to-date. This can be done by navigating to the directory and pulling the latest changes:
cd C://temp//yolov5-latest
git pull
Environment Compatibility: Ensure that your Python environment and dependencies are compatible. Sometimes, creating a fresh virtual environment and reinstalling the dependencies can help:
conda create -n yolov5_env python=3.12
conda activate yolov5_env
pip install torch torchvision torchaudio
pip install -r requirements.txt
If the issue persists after trying the above steps, please let us know. Additionally, you can refer to the Ultralytics HUB documentation for more detailed guidance on model training and deployment.
Thank you for your patience, and we appreciate your efforts in using Ultralytics HUB. If you have any further questions, feel free to ask!
Search before asking
HUB Component
Models, Training, Export, Inference
Bug
import sys import os import torch import gradio as gr import cv2 import numpy as np from PIL import Image
Path to YOLOv5 directory
yolov5path = "C://temp//yolov5-latest" sys.path.append(yolov5path)
Change the working directory to YOLOv5
os.chdir(yolov5path)
Define the path to your YOLOv5 model weights
model_path = 'pre-trained-weights/ultra-yolov5su.pt'
Load the YOLOv5 model with custom weights
yolo_model = torch.hub.load(yolov5path, 'custom', force_reload=True,path=model_path, source='local')
Error output 👍
YOLOv5 v7.0-338-gff063284 Python-3.12.4 torch-2.3.1+cpu CPU
YOLOv5s summary (fused): 193 layers, 9,142,496 parameters, 0 gradients, 24.0 GFLOPs Adding AutoShape... AttributeError Traceback (most recent call last) File C://temp//yolov5-latest\hubconf.py:98, in _create(name, pretrained, channels, classes, autoshape, verbose, device) 97 LOGGER.setLevel(logging.INFO) # reset to default ---> 98 return model.to(device) 100 except Exception as e:
File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1173, in Module.to(self, *args, **kwargs) 1171 raise -> 1173 return self._apply(convert)
File C:\/Users//kanag//FinalProject//yolov5\models\common.py:814, in AutoShape._apply(self, fn) 813 m.stride = fn(m.stride) --> 814 m.grid = list(map(fn, m.grid)) 815 if isinstance(m.anchor_grid, list):
File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1709, in Module.getattr(self, name) 1708 return modules[name] -> 1709 raise AttributeError(f"'{type(self).name}' object has no attribute '{name}'")
AttributeError: 'Detect' object has no attribute 'grid'
The above exception was the direct cause of the following exception:
Exception Traceback (most recent call last) Cell In[74], line 20 17 model_path = 'pre-trained-weights/ultra-yolov5su.pt' 19 # Load the YOLOv5 model with custom weights ---> 20 yolo_model = torch.hub.load(yolov5path, 'custom', force_reload=True,path=model_path, source='local')
File ~\anaconda3\Lib\site-packages\torch\hub.py:568, in load(repo_or_dir, model, source, trust_repo, force_reload, verbose, skip_validation, *args, *kwargs) 564 if source == 'github': 565 repo_or_dir = _get_cache_or_reload(repo_or_dir, force_reload, trust_repo, "load", 566 verbose=verbose, skip_validation=skip_validation) --> 568 model = _load_local(repo_or_dir, model, args, **kwargs) 569 return model
File ~\anaconda3\Lib\site-packages\torch\hub.py:597, in _load_local(hubconf_dir, model, *args, *kwargs) 594 hub_module = _import_module(MODULE_HUBCONF, hubconf_path) 596 entry = _load_entry_from_hubconf(hub_module, model) --> 597 model = entry(args, **kwargs) 599 return model
File C://temp//yolov5-latest\hubconf.py:135, in custom(path, autoshape, _verbose, device) 106 def custom(path="path/to/model.pt", autoshape=True, _verbose=True, device=None): 107 """ 108 Loads a custom or local YOLOv5 model from a given path with optional autoshaping and device specification. 109 (...) 133 ``` 134 """ --> 135 return _create(path, autoshape=autoshape, verbose=_verbose, device=device)
File C://temp//yolov5-latest\hubconf.py:103, in _create(name, pretrained, channels, classes, autoshape, verbose, device) 101 help_url = "https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading" 102 s = f"{e}. Cache may be out of date, try
force_reload=True
or see {help_url} for help." --> 103 raise Exception(s) from eException: 'Detect' object has no attribute 'grid'. Cache may be out of date, try
force_reload=True
or see https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading for help.Environment
Minimal Reproducible Example
No response
Additional
Uploaded dataset for detection task
Selected Ultralytics Hub model yolov5su variant for training
Selected own agent for training the model
Downloaded the trained model
Loading the trained model for inference test
Throws an exception as below while loading the model :
YOLOv5s summary (fused): 193 layers, 9,142,496 parameters, 0 gradients, 24.0 GFLOPs Adding AutoShape...
AttributeError Traceback (most recent call last)
Exception: 'Detect' object has no attribute 'grid'. Cache may be out of date, try
force_reload=True
or see https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading for help.