Roboflow RF-DETR
Supported on:RVC4
Overview
- export the model to
ONNX, - package it as an NN Archive,
- convert it to an RVC4 archive with HubAI, and
- run it with DepthAI Nodes.
1.7.1 and RF-DETR Nano, but the same general approach can be used for other RF-DETR variants as long as the relevant modules are present.If you only need a ready-to-run model, RF-DETR Nano is already available in the Model Zoo: RF-DETR Nano model card.Usage
For the full end-to-end notebook, see the RF-DETR RVC4 conversion tutorial.
Main steps
- Install the required packages and clone the official
rf-detrrepository at a known-good version. - Apply a small runtime class injection before export so the generated ONNX graph is compatible with the RVC4 conversion flow, without modifying the upstream RF-DETR source tree.
- Export the model to
ONNXwith a fixed384x384input andopset 17. - Create an NN Archive that describes the model inputs, outputs, preprocessing, and the
RFDETRParserhead used at runtime. - Convert the archive to RVC4 with the HubAI SDK.
- Run the resulting
.rvc4.tar.xzarchive with a DepthAI inference pipeline.
Export compatibility changes
- the windowed DINOv2 backbone is adjusted where image windows are merged back into a full spatial feature map,
- the
MSDeformAttnmodule is adjusted where multi-scale deformable attention computes sampling locations and attention weights.
Export to ONNX
Python
1from rfdetr import RFDETRNano
2from rvc4_class_injection import apply_rvc4_class_injection
3
4model = RFDETRNano()
5apply_rvc4_class_injection(model, require=True, verbose=True)
6
7model.export(
8 output_dir="onnx_model",
9 shape=(384, 384),
10 batch_size=1,
11 dynamic_batch=False,
12 opset_version=17,
13 format="onnx",
14)Create the NN Archive
input tensor, the dets and labels outputs, and an RFDETRParser head so runtime parsing can be configured automatically:Python
1from luxonis_ml.nn_archive.archive_generator import ArchiveGenerator
2
3generator = ArchiveGenerator(
4 archive_name="rfdetr-nano-onnx",
5 save_path=".",
6 cfg_dict=cfg_dict,
7 executables_paths=["onnx_model/rfdetr-nano.onnx"],
8)
9
10archive_path = generator.make_archive()Convert the NN Archive to RVC4
Python
1import os
2from hubai_sdk import HubAIClient
3
4client = HubAIClient(api_key=os.environ["HUBAI_API_KEY"])
5
6response = client.convert.RVC4(
7 path="rfdetr-nano-onnx.tar.xz",
8 name="rfdetr-nano-rvc4",
9 quantization_mode="FP16_STANDARD",
10 tool_version="2.41.0",
11 output_dir=".",
12)
13
14print(response.downloaded_path)rfdetr-nano-onnx.rvc4.tar.xz.Small inference example
RFDETRParser, ParsingNeuralNetwork can wire the parser automatically:Python
1from pathlib import Path
2
3import depthai as dai
4from depthai_nodes.node.parsing_neural_network import ParsingNeuralNetwork
5
6MODEL_PATH = Path("rfdetr-nano-onnx.rvc4.tar.xz")
7nn_archive = dai.NNArchive(str(MODEL_PATH))
8visualizer = dai.RemoteConnection(httpPort=8082)
9
10with dai.Pipeline() as pipeline:
11 cam = pipeline.create(dai.node.Camera).build(sensorFps=10)
12 cam_out = cam.requestOutput(
13 size=(384, 384),
14 fps=10,
15 type=dai.ImgFrame.Type.BGR888i,
16 )
17
18 nn_with_parser = pipeline.create(ParsingNeuralNetwork).build(
19 input=cam_out,
20 nn_source=nn_archive,
21 )
22
23 visualizer.addTopic(topicName="rgb", output=nn_with_parser.passthrough)
24 visualizer.addTopic(topicName="detections", output=nn_with_parser.out)
25
26 pipeline.start()
27 visualizer.registerPipeline(pipeline)
28
29 while pipeline.isRunning():
30 pipeline.processTasks()