I have trained RT-DETR model (taken from ultralytics) for the object detection task. I am using the following code for inferencing using the saved weights:
from ultralytics import YOLO
import cv2
# Load your trained model
model = YOLO(r"/content/drive/MyDrive/LN/best.pt")
# Load an image for inference
image_path = r"/content/drive/MyDrive/LN/114_jpg.rf.6b9c3febc395a3968078eec417f1c9ed.jpg"
image = cv2.imread(image_path)
# Perform object detection on the image
results = model(image)
But I am getting the following error
KeyError Traceback (most recent call last)
<ipython-input-3-d814bef29ddf> in <cell line: 12>()
10
11 # Perform object detection on the image
---> 12 results = model(image)
6 frames
/usr/local/lib/python3.10/dist-packages/ultralytics/engine/results.py in verbose(self)
661 for c in boxes.cls.unique():
662 n = (boxes.cls == c).sum() # detections per class
--> 663 log_string += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, "
664 return log_string
665
KeyError: 263
I have trained for one class only.
Maybe the key error is related to the class name, hence I tried the following code to know the number of classes and their names:
from ultralytics import YOLO
# Load the trained model
model = YOLO(r"D:\Research\Bhargav-Kaushik\best.pt")
# Print the class names
print("Class Names:", model.names)
Use Ultralytics RTDETR module instead of YOLO to operate with the RT-DETR model:
from ultralytics import RTDETR
# Load a model
model = RTDETR(r"/content/drive/MyDrive/LN/best.pt")
# Display model information (optional)
model.info()
image_path = r"/content/drive/MyDrive/LN/114_jpg.rf.6b9c3febc395a3968078eec417f1c9ed.jpg"
# Run inference
results = model(image_path)