D-FINE: Redefine Regression Task of DETRs as Fine-grained Distribution Refinement ๐ฅ๐ฅ๐ฅ
English | ็ฎไฝไธญๆ | ๆฅๆฌ่ช | English Blog | ไธญๆๅๅฎข
๐ This is the official implementation of the paper:
D-FINE: Redefine Regression Task of DETRs as Fine-grained Distribution Refinement
Yansong Peng, Hebei Li, Peixi Wu, Yueyi Zhang, Xiaoyan Sun, and Feng Wu
University of Science and Technology of China
If you like D-FINE, please give us a โญ! Your support motivates us to keep improving!
D-FINE is a powerful real-time object detector that redefines the bounding box regression task in DETRs as Fine-grained Distribution Refinement (FDR) and introduces Global Optimal Localization Self-Distillation (GO-LSD), achieving outstanding performance without introducing additional inference and training costs.
We conduct object detection using D-FINE and YOLO11 on a complex street scene video from YouTube. Despite challenging conditions such as backlighting, motion blur, and dense crowds, D-FINE-X successfully detects nearly all targets, including subtle small objects like backpacks, bicycles, and traffic lights. Its confidence scores and the localization precision for blurred edges are significantly higher than those of YOLO11.
https://github.com/user-attachments/assets/e5933d8e-3c8a-400e-870b-4e452f5321d9
Model | Dataset | APval | #Params | Latency | GFLOPs | config | checkpoint | logs |
---|---|---|---|---|---|---|---|---|
DโFINEโN | COCO | 42.8 | 4M | 2.12ms | 7 | yml | 42.8 | url |
DโFINEโS | COCO | 48.5 | 10M | 3.49ms | 25 | yml | 48.5 | url |
DโFINEโM | COCO | 52.3 | 19M | 5.62ms | 57 | yml | 52.3 | url |
DโFINEโL | COCO | 54.0 | 31M | 8.07ms | 91 | yml | 54.0 | url |
DโFINEโX | COCO | 55.8 | 62M | 12.89ms | 202 | yml | 55.8 | url |
Model | Dataset | APval | #Params | Latency | GFLOPs | config | checkpoint | logs |
---|---|---|---|---|---|---|---|---|
DโFINEโS | Objects365+COCO | 50.7 | 10M | 3.49ms | 25 | yml | 50.7 | url |
DโFINEโM | Objects365+COCO | 55.1 | 19M | 5.62ms | 57 | yml | 55.1 | url |
DโFINEโL | Objects365+COCO | 57.3 | 31M | 8.07ms | 91 | yml | 57.3 | url |
DโFINEโX | Objects365+COCO | 59.3 | 62M | 12.89ms | 202 | yml | 59.3 | url |
We highly recommend that you use the Objects365 pre-trained model for fine-tuning:
โ ๏ธ Important: Please note that this is generally beneficial for complex scene understanding. If your categories are very simple, it might lead to overfitting and suboptimal performance.
Model | Dataset | APval | AP5000 | #Params | Latency | GFLOPs | config | checkpoint | logs |
---|---|---|---|---|---|---|---|---|---|
DโFINEโS | Objects365 | 31.0 | 30.5 | 10M | 3.49ms | 25 | yml | 30.5 | url |
DโFINEโM | Objects365 | 38.6 | 37.4 | 19M | 5.62ms | 57 | yml | 37.4 | url |
DโFINEโL | Objects365 | - | 40.6 | 31M | 8.07ms | 91 | yml | 40.6 | url |
DโFINEโL (E25) | Objects365 | 44.7 | 42.6 | 31M | 8.07ms | 91 | yml | 42.6 | url |
DโFINEโX | Objects365 | 49.5 | 46.5 | 62M | 12.89ms | 202 | yml | 46.5 | url |
Notes:
conda create -n dfine python=3.11.9
conda activate dfine
pip install -r requirements.txt
Download COCO2017 from OpenDataLab or COCO.
Modify paths in coco_detection.yml
train_dataloader:
img_folder: /data/COCO2017/train2017/
ann_file: /data/COCO2017/annotations/instances_train2017.json
val_dataloader:
img_folder: /data/COCO2017/val2017/
ann_file: /data/COCO2017/annotations/instances_val2017.json
Download Objects365 from OpenDataLab.
Set the Base Directory:
export BASE_DIR=/data/Objects365/data
${BASE_DIR}/train
โโโ images
โ โโโ v1
โ โ โโโ patch0
โ โ โ โโโ 000000000.jpg
โ โ โ โโโ 000000001.jpg
โ โ โ โโโ ... (more images)
โ โโโ v2
โ โ โโโ patchx
โ โ โ โโโ 000000000.jpg
โ โ โ โโโ 000000001.jpg
โ โ โ โโโ ... (more images)
โโโ zhiyuan_objv2_train.json
${BASE_DIR}/val
โโโ images
โ โโโ v1
โ โ โโโ patch0
โ โ โ โโโ 000000000.jpg
โ โ โ โโโ ... (more images)
โ โโโ v2
โ โ โโโ patchx
โ โ โ โโโ 000000000.jpg
โ โ โ โโโ ... (more images)
โโโ zhiyuan_objv2_val.json
mkdir -p ${BASE_DIR}/train/images_from_val
cp -r ${BASE_DIR}/val/images/v1 ${BASE_DIR}/train/images_from_val/
cp -r ${BASE_DIR}/val/images/v2 ${BASE_DIR}/train/images_from_val/
python tools/remap_obj365.py --base_dir ${BASE_DIR}
python tools/resize_obj365.py --base_dir ${BASE_DIR}
Modify paths in obj365_detection.yml
train_dataloader:
img_folder: /data/Objects365/data/train
ann_file: /data/Objects365/data/train/new_zhiyuan_objv2_train_resized.json
val_dataloader:
img_folder: /data/Objects365/data/val/
ann_file: /data/Objects365/data/val/new_zhiyuan_objv2_val_resized.json
Download COCO format dataset here: url
To train on your custom dataset, you need to organize it in the COCO format. Follow the steps below to prepare your dataset:
Set remap_mscoco_category
to False
:
This prevents the automatic remapping of category IDs to match the MSCOCO categories.
remap_mscoco_category: False
Organize Images:
Structure your dataset directories as follows:
dataset/
โโโ images/
โ โโโ train/
โ โ โโโ image1.jpg
โ โ โโโ image2.jpg
โ โ โโโ ...
โ โโโ val/
โ โ โโโ image1.jpg
โ โ โโโ image2.jpg
โ โ โโโ ...
โโโ annotations/
โโโ instances_train.json
โโโ instances_val.json
โโโ ...
images/train/
: Contains all training images.images/val/
: Contains all validation images.annotations/
: Contains COCO-formatted annotation files.Convert Annotations to COCO Format:
If your annotations are not already in COCO format, youโll need to convert them. You can use the following Python script as a reference or utilize existing tools:
import json
def convert_to_coco(input_annotations, output_annotations):
# Implement conversion logic here
pass
if __name__ == "__main__":
convert_to_coco('path/to/your_annotations.json', 'dataset/annotations/instances_train.json')
Update Configuration Files:
Modify your custom_detection.yml.
task: detection
evaluator:
type: CocoEvaluator
iou_types: ['bbox', ]
num_classes: 777 # your dataset classes
remap_mscoco_category: False
train_dataloader:
type: DataLoader
dataset:
type: CocoDetection
img_folder: /data/yourdataset/train
ann_file: /data/yourdataset/train/train.json
return_masks: False
transforms:
type: Compose
ops: ~
shuffle: True
num_workers: 4
drop_last: True
collate_fn:
type: BatchImageCollateFunction
val_dataloader:
type: DataLoader
dataset:
type: CocoDetection
img_folder: /data/yourdataset/val
ann_file: /data/yourdataset/val/ann.json
return_masks: False
transforms:
type: Compose
ops: ~
shuffle: False
num_workers: 4
drop_last: False
collate_fn:
type: BatchImageCollateFunction
export model=l # n s m l x
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --master_port=7777 --nproc_per_node=4 train.py -c configs/dfine/dfine_hgnetv2_${model}_coco.yml --use-amp --seed=0
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --master_port=7777 --nproc_per_node=4 train.py -c configs/dfine/dfine_hgnetv2_${model}_coco.yml --test-only -r model.pth
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --master_port=7777 --nproc_per_node=4 train.py -c configs/dfine/dfine_hgnetv2_${model}_coco.yml --use-amp --seed=0 -t model.pth
export model=l # n s m l x
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --master_port=7777 --nproc_per_node=4 train.py -c configs/dfine/objects365/dfine_hgnetv2_${model}_obj365.yml --use-amp --seed=0
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --master_port=7777 --nproc_per_node=4 train.py -c configs/dfine/objects365/dfine_hgnetv2_${model}_obj2coco.yml --use-amp --seed=0 -t model.pth
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --master_port=7777 --nproc_per_node=4 train.py -c configs/dfine/dfine_hgnetv2_${model}_coco.yml --test-only -r model.pth
export model=l # n s m l x
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --master_port=7777 --nproc_per_node=4 train.py -c configs/dfine/custom/dfine_hgnetv2_${model}_custom.yml --use-amp --seed=0
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --master_port=7777 --nproc_per_node=4 train.py -c configs/dfine/custom/dfine_hgnetv2_${model}_custom.yml --test-only -r model.pth
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --master_port=7777 --nproc_per_node=4 train.py -c configs/dfine/custom/objects365/dfine_hgnetv2_${model}_obj2custom.yml --use-amp --seed=0 -t model.pth
When using the Objects365 pre-trained weights to train on your custom dataset, the example assumes that your dataset only contains the classes 'Person'
and 'Car'
. For faster convergence, you can modify self.obj365_ids
in src/solver/_solver.py
as follows:
self.obj365_ids = [0, 5] # Person, Cars
You can replace these with any corresponding classes from your dataset. The list of Objects365 classes with their corresponding IDs:
https://github.com/Peterande/D-FINE/blob/352a94ece291e26e1957df81277bef00fe88a8e3/src/solver/_solver.py#L330
New training command:
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --master_port=7777 --nproc_per_node=4 train.py -c configs/dfine/custom/dfine_hgnetv2_${model}_custom.yml --use-amp --seed=0 -t model.pth
However, if you donโt wish to modify the class mappings, the pre-trained Objects365 weights will still work without any changes. Modifying the class mappings is optional and can potentially accelerate convergence for specific tasks.
For example, if you want to double the total batch size when training D-FINE-L on COCO2017, here are the steps you should follow:
Modify your dataloader.yml to increase the total_batch_size
:
train_dataloader:
total_batch_size: 64 # Previously it was 32, now doubled
Modify your dfine_hgnetv2_l_coco.yml. Hereโs how the key parameters should be adjusted:
optimizer:
type: AdamW
params:
-
params: '^(?=.*backbone)(?!.*norm|bn).*$'
lr: 0.000025 # doubled, linear scaling law
-
params: '^(?=.*(?:encoder|decoder))(?=.*(?:norm|bn)).*$'
weight_decay: 0.
lr: 0.0005 # doubled, linear scaling law
betas: [0.9, 0.999]
weight_decay: 0.0001 # need a grid search
ema: # added EMA settings
decay: 0.9998 # adjusted by 1 - (1 - decay) * 2
warmups: 500 # halved
lr_warmup_scheduler:
warmup_duration: 250 # halved
If youโd like to train D-FINE-L on COCO2017 with an input size of 320x320, follow these steps:
Modify your dataloader.yml:
train_dataloader:
dataset:
transforms:
ops:
- {type: Resize, size: [320, 320], }
collate_fn:
base_size: 320
dataset:
transforms:
ops:
- {type: Resize, size: [320, 320], }
Modify your dfine_hgnetv2.yml:
eval_spatial_size: [320, 320]
pip install onnx onnxsim
export model=l # n s m l x
python tools/deployment/export_onnx.py --check -c configs/dfine/dfine_hgnetv2_${model}_coco.yml -r model.pth
trtexec --onnx="model.onnx" --saveEngine="model.engine" --fp16
pip install -r tools/inference/requirements.txt
export model=l # n s m l x
Inference on images and videos is now supported.
python tools/inference/onnx_inf.py --onnx model.onnx --input image.jpg # video.mp4
python tools/inference/trt_inf.py --trt model.engine --input image.jpg
python tools/inference/torch_inf.py -c configs/dfine/dfine_hgnetv2_${model}_coco.yml -r model.pth --input image.jpg --device cuda:0
pip install -r tools/benchmark/requirements.txt
export model=l # n s m l x
python tools/benchmark/get_info.py -c configs/dfine/dfine_hgnetv2_${model}_coco.yml
python tools/benchmark/trt_benchmark.py --COCO_dir path/to/COCO2017 --engine_dir model.engine
pip install fiftyone
export model=l # n s m l x
python tools/visualization/fiftyone_vis.py -c configs/dfine/dfine_hgnetv2_${model}_coco.yml -r model.pth
bash reference/safe_training.sh
python reference/convert_weight.py model.pth
Visualizations of FDR across detection scenarios with initial and refined bounding boxes, along with unweighted and weighted distributions.
The following visualization demonstrates D-FINEโs predictions in various complex detection scenarios. These include cases with occlusion, low-light conditions, motion blur, depth of field effects, and densely populated scenes. Despite these challenges, D-FINE consistently produces accurate localization results.
If you use D-FINE
or its methods in your work, please cite the following BibTeX entries:
@misc{peng2024dfine,
title={D-FINE: Redefine Regression Task in DETRs as Fine-grained Distribution Refinement},
author={Yansong Peng and Hebei Li and Peixi Wu and Yueyi Zhang and Xiaoyan Sun and Feng Wu},
year={2024},
eprint={2410.13842},
archivePrefix={arXiv},
primaryClass={cs.CV}
}
Our work is built upon RT-DETR.
Thanks to the inspirations from RT-DETR, GFocal, LD, and YOLOv9.
โจ Feel free to contribute and reach out if you have any questions! โจ