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

Excuse me, I trained a 5-class detection model with RT-DETR, but reported exceeding the category during inference. How can this error be resolved #12563

Open
2 tasks done
Shybert-AI opened this issue May 11, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@Shybert-AI
Copy link

Search before asking

  • I have searched the YOLOv8 issues and found no similar bug report.

YOLOv8 Component

No response

Bug

D:\mywork\pythonProject\yolov8>yolo task=detect mode=predict model="./runs/detect/train12/weights/last.pt" conf=0.25 source="00001.jpg"
Ultralytics YOLOv8.0.131 Python-3.11.5 torch-2.2.2+cu121 CUDA:0 (NVIDIA GeForce RTX 4080 SUPER, 16376MiB)
rtdetr-l summary: 498 layers, 31994015 parameters, 0 gradients

Traceback (most recent call last):
File "", line 198, in run_module_as_main
File "", line 88, in run_code
File "D:\software\anaconda\Scripts\yolo.exe_main
.py", line 7, in
File "D:\software\anaconda\Lib\site-packages\ultralytics\yolo\cfg_init
.py", line 411, in entrypoint
getattr(model, mode)(**overrides) # default args from model
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\torch\utils_contextlib.py", line 115, in decorate_context
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\ultralytics\yolo\engine\model.py", line 255, in predict
return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\ultralytics\yolo\engine\predictor.py", line 195, in predict_cli
for _ in gen: # running CLI inference without accumulating any outputs (do not modify)
File "D:\software\anaconda\Lib\site-packages\torch\utils_contextlib.py", line 35, in generator_context
response = gen.send(None)
^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\ultralytics\yolo\engine\predictor.py", line 267, in stream_inference
s += self.write_results(i, self.results, (p, im, im0))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\ultralytics\yolo\engine\predictor.py", line 161, in write_results
log_string += result.verbose()
^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\ultralytics\yolo\engine\results.py", line 265, in verbose
log_string += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, "
~~~~~~~~~~^^^^^^^^
KeyError: 196

Environment

No response

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!
@Shybert-AI Shybert-AI added the bug Something isn't working label May 11, 2024
Copy link

👋 Hello @Shybert-AI, thank you for your interest in Ultralytics YOLOv8 🚀! We recommend a visit to the Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.

If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Join the vibrant Ultralytics Discord 🎧 community for real-time conversations and collaborations. This platform offers a perfect space to inquire, showcase your work, and connect with fellow Ultralytics users.

Install

Pip install the ultralytics package including all requirements in a Python>=3.8 environment with PyTorch>=1.8.

pip install ultralytics

Environments

YOLOv8 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

Ultralytics CI

If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLOv8 Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

Hello! It seems like the model is encountering a "KeyError" which typically suggests that there's a mismatch in the number of classes between the trained model and the class names provided during inference.

Ensure that your class names file (often labeled *.names or defined in your dataset .yaml file) correctly lists all 5 classes you trained the model on, and that it's properly linked in your inference script or command line parameters. The error suggests the model is expecting a class index that doesn't exist in the provided class names.

Here’s a basic checklist:

  1. Check the .names file or similar to ensure all your classes are listed there.
  2. Verify the path to your class names file in the .yaml or as a model argument is correct.
  3. Re-run the inference ensuring all files are in the correct locations.

If everything appears correct and the error persists, consider re-exporting or re-saving your model configuration.

Let me know how it goes, or if there's anything else you need help with! 🚀

@Shybert-AI
Copy link
Author

Shybert-AI commented May 12, 2024

Thank you very much, it has been resolved. When using the yolo command line, an error will be reported. Replace the command line with the following Python code to perform inference。

yolo detect predict model=./runs/detect/train/weights/best.pt source="00001.jpg"

replace

from ultralytics import YOLO,RTDETR
model = RTDETR("./ultralytics/cfg/models/rt-detr/rtdetr-l.yaml").load('runs/detect/train/weights/last.pt')
results = model(['00001.jpg']) # return a list of Results objects
for result in results:
boxes = result.boxes # Boxes object for bounding box outputs
masks = result.masks # Masks object for segmentation masks outputs
keypoints = result.keypoints # Keypoints object for pose outputs
probs = result.probs # Probs object for classification outputs
obb = result.obb # Oriented boxes object for OBB outputs
result.show() # display to screen
result.save(filename='result.jpg') # save to disk

@glenn-jocher
Copy link
Member

Hello! I'm glad to hear that the issue has been resolved 🌟! The method you used to perform inference through the Python script is indeed a great alternative if you encounter issues with the command line. It also provides more flexibility for post-processing and handling the results. Thanks for sharing your solution; it could be helpful to others facing similar problems. If there's anything more we can assist you with, don't hesitate to ask. Happy detecting! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants