rf-detr icon indicating copy to clipboard operation
rf-detr copied to clipboard

predict with onnx

Open valavanisleonidas opened this issue 7 months ago • 9 comments
trafficstars

Description

This PR adds support for ONNX-based inference in RF-DETR, enabling faster model execution compared to PyTorch CPU inference. This change introduces an alternative inference path using the onnxruntime backend.

Ticket : https://github.com/roboflow/rf-detr/issues/64

Type of change

  • [x] New feature (non-breaking change which adds functionality)
  • [ ] This change requires a documentation update

How has this change been tested, please provide a testcase or example of how you tested the change?

Python test file

import time
import cv2
import numpy as np
from PIL import Image
import supervision as sv
from rfdetr.detr import RFDETRLarge
from rfdetr.util.coco_classes import COCO_CLASSES


def predict(model, image_path, im_save):

    image = Image.open(image_path)

    start = time.time()
    detections = model.predict(image_path, threshold=0.5)
    end = time.time()
    print("only predict : ", end - start)

    labels = [
        f"{COCO_CLASSES[class_id]} {confidence:.2f}"
        for class_id, confidence in zip(detections.class_id, detections.confidence)
    ]

    image = image.convert("RGB")
    annotated_image = sv.BoxAnnotator().annotate(image.copy(), detections)
    annotated_image = sv.LabelAnnotator().annotate(annotated_image, detections, labels)

    sv.plot_image(annotated_image)
    cv2.imwrite(im_save, np.array(annotated_image))


image_url = "path/to/image"

start = time.time()
model = RFDETRLarge()
predict(model, image_url, "predict_old.jpg")
end = time.time()
print("old: ", end - start)

model = RFDETRLarge(onnx_path="output/inference_model.onnx")
predict(model, image_url, "predict_onnx.jpg")
end = time.time()
print("onnx: ", end - start)

The bboxes and confidence scores of the image i tested are the same.

The time using GPU is 0,14 sec for cuda with pth model and 0,11 sec for onnx model

Any specific deployment considerations

Docs

  • [ ] Docs updated? What were the changes:

valavanisleonidas avatar Apr 09 '25 09:04 valavanisleonidas

Let's wait this PR until we'll merge https://github.com/roboflow/rf-detr/pull/129.

SkalskiP avatar Apr 09 '25 12:04 SkalskiP

@valavanisleonidas #129 got merged. We can work on this PR now.

SkalskiP avatar Apr 10 '25 21:04 SkalskiP

@valavanisleonidas #129 got merged. We can work on this PR now.

Yes I saw that thank you. should we also have dynamic batch size for onnx models since the predict function support batch inference ?

valavanisleonidas avatar Apr 10 '25 21:04 valavanisleonidas

@SkalskiP I think the PR is mostly ready. Two things to mention.

  1. in the providers for onnx we can add a device id for the GPU to use (and add an argument as well).
  2. I tried to export dynamic onnx model but something crashes. Even though the model input is ['batch', 3, 560, 560] it crashes with error in layer transformer/Reshape_13, so probably model does not support dynamic batching in onnx. We have to handle this somehow in predict or maybe fix the model to support dynamic batching ?? :D

valavanisleonidas avatar Apr 10 '25 22:04 valavanisleonidas

Hey @SkalskiP, any news on this PR ?

valavanisleonidas avatar Apr 24 '25 11:04 valavanisleonidas

Hi, dear developer, how is going on ? I also need to use the ONNX model to predict.

NOTGOOOOD avatar Jun 12 '25 09:06 NOTGOOOOD

Hi, dear developer, how is going on ? I also need to use the ONNX model to predict.

this PR is not up to date with the latest changes. I can do it if @SkalskiP and the team is still interested in merging it.

valavanisleonidas avatar Jun 12 '25 14:06 valavanisleonidas

Hello @valavanisleonidas any update on this PR? It's a super interesting PR which alot of people are waiting for I guess.

MustafaKiwaPI avatar Nov 03 '25 14:11 MustafaKiwaPI

Hello @MustafaKiwaPI ,

it’s not up to me to merge this. In order to use it because I wanted it as well I forked the code with the change and use the forked version.

If the maintainers of the project are still interested we can update the branch in order to merge this.

Hello @valavanisleonidas any update on this PR? It's a super interesting PR which alot of people are waiting for I guess.

valavanisleonidas avatar Nov 03 '25 17:11 valavanisleonidas