YOLO v5 with Pytorch 'Detect' object has no attribute 'grid' error

139 Views Asked by At

I trained a YOLO v5su model using Ultralytics HUB and downloaded the best.pt file for later use. When I try to run the model using this file with pytorch on my own device, I get the following error: '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.

My Full Code:

from PIL import Image
import torch
import cv2
model_path = "/run/media/emre/Fedora_extra/best.pt"

model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path, force_reload=True)
img_path = "/run/media/emre/Fedora_extra/2d geometry figures.v3i.yolov5pytorch/valid/images/C-Shapes-project-1_jpg.rf.393826e8598965acfee9b405020e8902.jpg"

model(img_path)

And This İs My Full Error Code:

Traceback (most recent call last): File "/home/emre/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 78, in _create return model.to(device) ^^^^^^^^^^^^^^^^ File "/run/media/emre/Fedora_extra/anaconda/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1160, in to return self._apply(convert) ^^^^^^^^^^^^^^^^^^^^ File "/home/emre/.cache/torch/hub/ultralytics_yolov5_master/models/common.py", line 689, in _apply m.grid = list(map(fn, m.grid)) ^^^^^^ File "/run/media/emre/Fedora_extra/anaconda/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1695, in getattr 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:

Traceback (most recent call last): File "/run/media/emre/Fedora_extra/model_v5m.py", line 11, in model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path, force_reload=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/run/media/emre/Fedora_extra/anaconda/lib/python3.11/site-packages/torch/hub.py", line 566, in load model = _load_local(repo_or_dir, model, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/run/media/emre/Fedora_extra/anaconda/lib/python3.11/site-packages/torch/hub.py", line 595, in _load_local model = entry(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^ File "/home/emre/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 88, in custom return _create(path, autoshape=autoshape, verbose=_verbose, device=device) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/emre/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 83, in _create raise Exception(s) from e 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.

I wrote it according to the Ultralytics Pytorch guide. I tried the try force_reload=True solution in the error code and it didn't work.

0

There are 0 best solutions below