ducnhan0804's picture
Upload README.md with huggingface_hub
0733fe6 verified
|
Raw
History Blame Contribute Delete
5.26 kB
---
language:
- vi
license: apache-2.0
library_name: d-fine
pipeline_tag: object-detection
tags:
- d-fine
- object-detection
- document-layout
- fine-tuned
- hgnetv2
---
<div align="center">
# πŸ” D-FINE X β€” Object Detection for Document Layout
**D-FINE HGNetv2-X fine-tuned for document layout analysis**
[![license](https://img.shields.io/badge/license-Apache%202.0-green.svg)](https://github.com/Peterande/D-FINE/blob/master/LICENSE)
[![mAP50:95](https://img.shields.io/badge/mAP@50%3A95-83.75%25-blue)]()
[![mAP50](https://img.shields.io/badge/mAP@50-91.82%25-blue)]()
[![Python](https://img.shields.io/badge/python-3.8%2B-blue)]()
</div>
---
## Overview
MΓ΄ hΓ¬nh **D-FINE HGNetv2-X** được fine-tune cho bΓ i toΓ‘n nhαΊ­n diện bα»‘ cα»₯c tΓ i liệu (Document Layout Analysis). MΓ΄ hΓ¬nh sα»­ dα»₯ng kiαΊΏn trΓΊc D-FINE tiΓͺn tiαΊΏn vα»›i cΖ‘ chαΊΏ Fine-grained Distribution Refinement (FDR) để tinh chỉnh bounding box, kαΊΏt hợp backbone HGNetv2-B5 mαΊ‘nh mαΊ½.
| Property | Value |
|---|---|
| Base model | D-FINE (HGNetv2-X) |
| Task | Object Detection |
| Domain | Document Layout |
| Input resolution | 640 Γ— 640 |
| Training | 80 epochs (72 Stage 1 + 8 Stage 2) |
---
## Classes
| ID | Label | Description |
|:---:|---|---|
| 0 | `Object-detection` | Generic detectable region |
| 1 | `Figure` | Diagrams, illustrations, charts |
| 2 | `Icon` | Small symbolic graphics |
| 3 | `Table` | Tabular data structures |
---
## Benchmark
### βœ… Best validation metrics (Stage 2)
| Metric | Value |
|---|---:|
| mAP @ \[0.50 : 0.95\] | **0.8375** |
| mAP @ 0.50 | **0.9182** |
| mAP @ 0.75 | **0.8966** |
| AP (Medium) | **0.8703** |
| AP (Large) | **0.8384** |
| AR @ 100 | **0.9348** |
---
## Usage
### Option 1 β€” HuggingFace Transformers (recommended)
```bash
pip install transformers torch torchvision
# Clone D-FINE source code (bαΊ―t buα»™c)
git clone https://github.com/Peterande/D-FINE.git
cd D-FINE
pip install -r requirements.txt
```
```python
from transformers import AutoModel, AutoImageProcessor
from PIL import Image
processor = AutoImageProcessor.from_pretrained(
"ducnhan0804/dfine-x-obj-detection",
trust_remote_code=True
)
model = AutoModel.from_pretrained(
"ducnhan0804/dfine-x-obj-detection",
trust_remote_code=True
)
image = Image.open("path/to/your/document.png")
inputs = processor.preprocess(image)
outputs = model(inputs["images"], threshold=0.4)
results = processor.post_process_object_detection(outputs, threshold=0.4)
# results[0]["boxes"] β†’ xyxy coordinates (tensor)
# results[0]["scores"] β†’ confidence scores (tensor)
# results[0]["labels"] β†’ class ids (tensor)
print(results)
```
> **Note:** `trust_remote_code=True` is required because this model uses custom `modeling_dfine.py`. The checkpoint (~1GB) is downloaded automatically to `~/.cache/huggingface/` on first run.
> **Important:** You must run this code from the D-FINE root directory so that the source code (`src/`) can be imported.
### Option 2 β€” D-FINE directly
```bash
git clone https://github.com/Peterande/D-FINE.git
cd D-FINE
pip install -r requirements.txt
```
```python
# Download checkpoint from HuggingFace
from huggingface_hub import hf_hub_download
weights = hf_hub_download(
repo_id="ducnhan0804/dfine-x-obj-detection",
filename="best_stg2.pth"
)
# Run inference using D-FINE's built-in script
# python tools/inference/torch_inf.py -c configs/dfine/custom/dfine_hgnetv2_x_custom.yml -r <weights_path> -i <image_path> -d cuda:0
```
---
## Training Configuration
```yaml
model:
architecture: D-FINE (HGNetv2-X)
backbone: HGNetv2-B5
training:
total_epochs: 35
stage1_epochs: 30 (heavy augmentation)
stage2_epochs: 5 (fine-tuning, no heavy augmentation)
resolution: 640
device: cuda
optimizer:
type: AdamW
lr: 0.00025
backbone_lr: 0.0000025
```
### Dataset split (COCO format)
```
datasets/
β”œβ”€β”€ train/
β”‚ β”œβ”€β”€ _annotations.coco.json
β”‚ └── *.jpg
└── valid/
β”œβ”€β”€ _annotations.coco.json
└── *.jpg
```
---
## Repository Structure
```
ducnhan0804/dfine-x-obj-detection
β”œβ”€β”€ README.md # Model card
β”œβ”€β”€ config.json # HuggingFace model config
β”œβ”€β”€ preprocessor_config.json # Image processor config
β”œβ”€β”€ modeling_dfine.py # Custom modeling code (trust_remote_code)
β”œβ”€β”€ best_stg2.pth # Best checkpoint (Stage 2)
β”œβ”€β”€ dfine_hgnetv2_x_custom.yml # D-FINE training config
└── custom_detection.yml # Dataset config
```
---
## Limitations
- Trained on a specific document layout dataset; generalization to other document types is not guaranteed.
- Requires D-FINE source code to be cloned and available locally for inference.
- The checkpoint file is ~1GB in size.
---
## License
This model is released under the **Apache License 2.0**, inherited from the D-FINE base model.
---
## Citation
**Base model (D-FINE):**
```bibtex
@article{peng2024d,
title={D-FINE: Redefine Regression Task in DETRs as Fine-grained Distribution Refinement},
author={Peng, Yansong and Songtao, Liu and others},
journal={arXiv preprint arXiv:2407.06537},
year={2024}
}
```