yolov5 icon indicating copy to clipboard operation
yolov5 copied to clipboard

Exception: Can't get attribute 'DetectionModel' on <module 'models.yolo' from./yolo.py>. Cache maybe out of date, try force_reload=TRUE

Open TDVL opened this issue 3 years ago • 12 comments

Search before asking

  • [X] I have searched the YOLOv5 issues and discussions and found no similar questions.

Question

Hello, I have installed a repo with yolov5, Ubuntu 18.04 and ROS Melodic. Please, see the link: https://github.com/qq44642754a/Yolov5_ros It works great with original yolov5s.pt weight, but when I put my best.pt weights, an attribute error occurs. Screenshot from 2022-09-06 20-33-18 I have tried the suggestions from this link, but the error is still on https://github.com/ultralytics/yolov5/issues/36 Also, I have changed the name in the launch file to best.pt. The screenshots are attached here: Screenshot from 2022-09-06 20-31-37 Screenshot from 2022-09-06 20-34-36 Screenshot from 2022-09-06 20-40-19 Screenshot from 2022-09-06 20-55-35 Also, I have installed an older version of Pytorch, but the error remains. What could be the problem? Why does weights file change cause a problem/eroor? The yolov5 in this ROS Melodic works with original weights, but it fails to start with my weights best.pt trained in Colab and later tested successfully in Python in Ubuntu.

Thank you in advance. Best regards,

Additional

No response

TDVL avatar Sep 06 '22 19:09 TDVL

👋 Hello! Thanks for asking about handling inference results. YOLOv5 🚀 PyTorch Hub models allow for simple model loading and inference in a pure python environment without using detect.py.

Simple Inference Example

This example loads a pretrained YOLOv5s model from PyTorch Hub as model and passes an image for inference. 'yolov5s' is the YOLOv5 'small' model. For details on all available models please see the README. Custom models can also be loaded, including custom trained PyTorch models and their exported variants, i.e. ONNX, TensorRT, TensorFlow, OpenVINO YOLOv5 models.

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')  # yolov5n - yolov5x6 official model
#                                            'custom', 'path/to/best.pt')  # custom model

# Images
im = 'https://ultralytics.com/images/zidane.jpg'  # or file, Path, URL, PIL, OpenCV, numpy, list

# Inference
results = model(im)

# Results
results.print()  # or .show(), .save(), .crop(), .pandas(), etc.
results.xyxy[0]  # im predictions (tensor)

results.pandas().xyxy[0]  # im predictions (pandas)
#      xmin    ymin    xmax   ymax  confidence  class    name
# 0  749.50   43.50  1148.0  704.5    0.874023      0  person
# 2  114.75  195.75  1095.0  708.0    0.624512      0  person
# 3  986.00  304.00  1028.0  420.0    0.286865     27     tie

results.pandas().xyxy[0].value_counts('name')  # class counts (pandas)
# person    2
# tie       1

See YOLOv5 PyTorch Hub Tutorial for details.

Good luck 🍀 and let us know if you have any other questions!

glenn-jocher avatar Sep 06 '22 19:09 glenn-jocher

Hello Glenn, Thank you for the prompt answer and for the attention. I have tried to correct the problem, but I have a new error now. What am i doing wrong? I have changed the line in yolo_v5.py according to the guide (I think). Now, there are new errors. Screenshot from 2022-09-06 22-09-02 What could be the problem? Thank you in advance.

TDVL avatar Sep 06 '22 20:09 TDVL

@TDVL I don't have availability to debug custom code. I suggest you follow my example above.

glenn-jocher avatar Sep 06 '22 20:09 glenn-jocher

Thank you Glenn. I have followed it, it works great with yolov5s.pt weights, here is the screenshot. Screenshot from 2022-09-07 08-04-47 The path to the model and to the weights have been added. When I change the weights, the error is again on. Screenshot from 2022-09-07 08-32-44 The force_reload=True does not help. Here is the screenshot. Screenshot from 2022-09-07 08-38-00 Hence I have followed all the guides and suggestions, but something is wrong with new weights. The basic guide is here, the new model should work with new weights and with change in launch file https://github.com/qq44642754a/Yolov5_ros This ROS repo uses your yolov5 model and it works with your *.pt files? Is it possible that only the weights change can cause these problems?

TDVL avatar Sep 07 '22 06:09 TDVL

@TDVL pytorch hub loading and inference for both official models and custom trained models on every operating system and python version is part of YOLOv5 CI which is run every 24 hours and on every commit. These tests are currently all passing. https://github.com/ultralytics/yolov5/blob/5a134e06530a8c24fdb9774c2c4ab0b513b08260/.github/workflows/ci-testing.yml#L124

glenn-jocher avatar Sep 07 '22 10:09 glenn-jocher

Thank you Glenn, I will check it again. Also, I have renamed the best.pt to yolov5s.pt and it is working, but I will check again. I think there is a simple typing/naming problem when I want to use my best.pt weights. If I find the mistake, I will report you Thanks for the attention.

TDVL avatar Sep 07 '22 10:09 TDVL

Hello Glenn, I have corrected the ROS Melodic yolov5 repo and now it is working (the force_reload=True option helped finally). I have changed the weights in the launch file too. My weights should detect the connector on the car, but now the yolov5 in this ROS repo detects the car and other objects from the official yolov5 network (it uses the original weights). Why is this happening? My weights are in the weights folder, there are no *.pt files in other folders. What could be the problem? The screenshot is attached. I should note, that my weights work great in Python, I have trained them with your Google Colab repo. Is there some other settings for custom weights? Also, if I rename the best.pt to yolov5s.pt, the problem remains. I don't understand why. Please, will you check what could be the problem. Here are the files from the ROS yolov5 repo. https://github.com/qq44642754a/Yolov5_ros/tree/master/yolov5_ros/yolov5_ros Screenshot from 2022-09-15 21-01-28

TDVL avatar Sep 15 '22 19:09 TDVL

@TDVL 👋 Hello! Thanks for asking about handling inference results. YOLOv5 🚀 PyTorch Hub models allow for simple model loading and inference in a pure python environment without using detect.py.

Simple Inference Example

This example loads a pretrained YOLOv5s model from PyTorch Hub as model and passes an image for inference. 'yolov5s' is the YOLOv5 'small' model. For details on all available models please see the README. Custom models can also be loaded, including custom trained PyTorch models and their exported variants, i.e. ONNX, TensorRT, TensorFlow, OpenVINO YOLOv5 models.

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')  # yolov5n - yolov5x6 official model
#                                            'custom', 'path/to/best.pt')  # custom model

# Images
im = 'https://ultralytics.com/images/zidane.jpg'  # or file, Path, URL, PIL, OpenCV, numpy, list

# Inference
results = model(im)

# Results
results.print()  # or .show(), .save(), .crop(), .pandas(), etc.
results.xyxy[0]  # im predictions (tensor)

results.pandas().xyxy[0]  # im predictions (pandas)
#      xmin    ymin    xmax   ymax  confidence  class    name
# 0  749.50   43.50  1148.0  704.5    0.874023      0  person
# 2  114.75  195.75  1095.0  708.0    0.624512      0  person
# 3  986.00  304.00  1028.0  420.0    0.286865     27     tie

results.pandas().xyxy[0].value_counts('name')  # class counts (pandas)
# person    2
# tie       1

See YOLOv5 PyTorch Hub Tutorial for details.

Good luck 🍀 and let us know if you have any other questions!

glenn-jocher avatar Sep 15 '22 20:09 glenn-jocher

Hello Glenn, I have navigated to the yolov5 folder, now I have a new error related to hubconfig.py. What could be now the problem? The screenshot is attached. Thank you. Screenshot from 2022-09-19 11-56-28

TDVL avatar Sep 19 '22 10:09 TDVL

@TDVL for PyTorch Hub usage see above example I showed earlier.

glenn-jocher avatar Sep 19 '22 10:09 glenn-jocher

OK, I have tried it. but still not works Here is the screenshot If I remember, the *.pt files can be used without issues. I am using my best.pt in Python on VBox VM without issues. Screenshot from 2022-09-19 13-27-59

TDVL avatar Sep 19 '22 11:09 TDVL

@TDVL I don't assist in debugging custom code. If you have a reproducible issue in unmodified code please let us know.

glenn-jocher avatar Sep 19 '22 11:09 glenn-jocher

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

  • Wiki – https://github.com/ultralytics/yolov5/wiki
  • Tutorials – https://docs.ultralytics.com/yolov5
  • Docs – https://docs.ultralytics.com

Access additional Ultralytics ⚡ resources:

  • Ultralytics HUB – https://ultralytics.com/hub
  • Vision API – https://ultralytics.com/yolov5
  • About Us – https://ultralytics.com/about
  • Join Our Team – https://ultralytics.com/work
  • Contact Us – https://ultralytics.com/contact

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

github-actions[bot] avatar Oct 20 '22 00:10 github-actions[bot]