Implementation:Roboflow Rf detr RFDETR Init Finetuned
| Knowledge Sources | |
|---|---|
| Domains | Object_Detection, Transfer_Learning |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for loading fine-tuned RF-DETR checkpoints with custom class names for domain-specific inference.
Description
Uses the same RFDETR.__init__ pathway as standard initialization but with pretrain_weights pointing to a fine-tuned checkpoint. The Model.__init__ method detects the checkpoint's class count from the class_embed.bias shape, reinitializes the detection head if needed, and loads custom class names from checkpoint['args'].class_names. The model's class_names property then returns the fine-tuned class names instead of COCO classes.
Usage
Instantiate any RFDETR size variant with the pretrain_weights parameter set to the path of a fine-tuned checkpoint file.
Code Reference
Source Location
- Repository: rf-detr
- File: rfdetr/detr.py
- Lines: L63-76 (RFDETR.__init__), L252-264 (class_names property)
- File: rfdetr/main.py
- Lines: L94-166 (Model.__init__ - weight loading with class mismatch handling)
Signature
# Same API as standard initialization
model = RFDETRBase(pretrain_weights="output/checkpoint_best_total.pth")
# Access custom class names
model.class_names # -> {1: "cat", 2: "dog", ...}
Import
from rfdetr import RFDETRBase # or matching size variant used during training
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| pretrain_weights | str | Yes | Path to fine-tuned checkpoint (e.g. "output/checkpoint_best_total.pth") |
Outputs
| Name | Type | Description |
|---|---|---|
| model | RFDETR | Model loaded with fine-tuned weights and custom class names |
| model.class_names | Dict[int, str] | Custom class name mapping from checkpoint |
Usage Examples
Load and Run Fine-tuned Model
from rfdetr import RFDETRBase
# Load fine-tuned model
model = RFDETRBase(pretrain_weights="output/checkpoint_best_total.pth")
# Verify custom classes loaded
print(model.class_names) # {1: "hardhat", 2: "vest", 3: "person"}
# Run inference
detections = model.predict("construction_site.jpg", threshold=0.5)
# Use custom class names for labels
labels = [
f"{model.class_names[cid]} {conf:.2f}"
for cid, conf in zip(detections.class_id, detections.confidence)
]