Object Detection
Transformers
Safetensors
rt_detr
pill-detection
medical
rt-detr
computer-vision
counting
healthcare
Eval Results (legacy)
Instructions to use SARANGx/rtdetr-pill-detector with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use SARANGx/rtdetr-pill-detector with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("object-detection", model="SARANGx/rtdetr-pill-detector")# Load model directly from transformers import AutoImageProcessor, AutoModelForObjectDetection processor = AutoImageProcessor.from_pretrained("SARANGx/rtdetr-pill-detector") model = AutoModelForObjectDetection.from_pretrained("SARANGx/rtdetr-pill-detector", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update model card with full training details and usage
Browse files
README.md
CHANGED
|
@@ -3,76 +3,178 @@ library_name: transformers
|
|
| 3 |
license: apache-2.0
|
| 4 |
base_model: PekingU/rtdetr_r18vd_coco_o365
|
| 5 |
tags:
|
| 6 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
model-index:
|
| 8 |
-
- name: rtdetr-pill-detector
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
| 13 |
-
should probably proofread and complete it, then remove this comment. -->
|
| 14 |
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
-
It achieves the following results on the evaluation set:
|
| 19 |
-
- Loss: 3.5814
|
| 20 |
|
| 21 |
-
## Model
|
| 22 |
|
| 23 |
-
|
| 24 |
|
| 25 |
-
##
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
-
|
| 41 |
-
-
|
| 42 |
-
|
| 43 |
-
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
###
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
license: apache-2.0
|
| 4 |
base_model: PekingU/rtdetr_r18vd_coco_o365
|
| 5 |
tags:
|
| 6 |
+
- object-detection
|
| 7 |
+
- pill-detection
|
| 8 |
+
- medical
|
| 9 |
+
- rt-detr
|
| 10 |
+
- computer-vision
|
| 11 |
+
- counting
|
| 12 |
+
- healthcare
|
| 13 |
+
datasets:
|
| 14 |
+
- Francesco/pills-sxdht
|
| 15 |
+
pipeline_tag: object-detection
|
| 16 |
model-index:
|
| 17 |
+
- name: rtdetr-pill-detector
|
| 18 |
+
results:
|
| 19 |
+
- task:
|
| 20 |
+
type: object-detection
|
| 21 |
+
name: Object Detection
|
| 22 |
+
dataset:
|
| 23 |
+
name: pills-sxdht (Roboflow 100)
|
| 24 |
+
type: Francesco/pills-sxdht
|
| 25 |
+
metrics:
|
| 26 |
+
- type: loss
|
| 27 |
+
value: 3.528
|
| 28 |
+
name: Best Validation Loss
|
| 29 |
---
|
| 30 |
|
| 31 |
+
# 💊 RT-DETR Pill Detector & Counter
|
|
|
|
| 32 |
|
| 33 |
+
A real-time medicine pill detection model that can **detect and count pills, capsules, and specific medications** in images.
|
| 34 |
|
| 35 |
+
**🎮 Try it live:** [**Pill Detector Demo**](https://huggingface.co/spaces/SARANGx/pill-detector-demo)
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
## Model Description
|
| 38 |
|
| 39 |
+
This model is a fine-tuned [RT-DETR R18](https://huggingface.co/PekingU/rtdetr_r18vd_coco_o365) (Real-Time DEtection TRansformer with ResNet-18 backbone) for detecting medicine pills in images.
|
| 40 |
|
| 41 |
+
### Why RT-DETR?
|
| 42 |
+
- **NMS-free architecture** — Each pill is detected exactly once, making it ideal for **accurate counting**
|
| 43 |
+
- **End-to-end detection** — No post-processing like Non-Maximum Suppression needed
|
| 44 |
+
- **Real-time capable** — 20M parameters, fast inference even on CPU
|
| 45 |
+
- **Strong baseline** — Pre-trained on Objects365 (365 categories, 2M images)
|
| 46 |
+
|
| 47 |
+
### What it detects (9 classes)
|
| 48 |
+
|
| 49 |
+
| Class | Description |
|
| 50 |
+
|-------|-------------|
|
| 51 |
+
| `pills` | Generic pill detection |
|
| 52 |
+
| `Cipro 500` | Ciprofloxacin 500mg |
|
| 53 |
+
| `Ibuphil 600 mg` | Ibuprofen 600mg |
|
| 54 |
+
| `Ibuphil Cold 400-60` | Ibuprofen/Pseudoephedrine combination |
|
| 55 |
+
| `Xyzall 5mg` | Levocetirizine 5mg |
|
| 56 |
+
| `blue` | Blue-colored pills |
|
| 57 |
+
| `pink` | Pink-colored pills |
|
| 58 |
+
| `red` | Red-colored pills |
|
| 59 |
+
| `white` | White-colored pills |
|
| 60 |
+
|
| 61 |
+
## Usage
|
| 62 |
+
|
| 63 |
+
### Quick Start with Pipeline
|
| 64 |
+
|
| 65 |
+
```python
|
| 66 |
+
from transformers import pipeline
|
| 67 |
+
from PIL import Image
|
| 68 |
+
|
| 69 |
+
detector = pipeline("object-detection", model="SARANGx/rtdetr-pill-detector")
|
| 70 |
+
image = Image.open("pills.jpg")
|
| 71 |
+
|
| 72 |
+
results = detector(image, threshold=0.5)
|
| 73 |
+
for r in results:
|
| 74 |
+
print(f"{r['label']}: {r['score']:.2%} at {r['box']}")
|
| 75 |
+
|
| 76 |
+
print(f"Total pills: {len(results)}")
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
### Manual Inference (more control)
|
| 80 |
+
|
| 81 |
+
```python
|
| 82 |
+
import torch
|
| 83 |
+
from transformers import RTDetrForObjectDetection, RTDetrImageProcessor
|
| 84 |
+
from PIL import Image
|
| 85 |
+
from collections import Counter
|
| 86 |
+
|
| 87 |
+
model_id = "SARANGx/rtdetr-pill-detector"
|
| 88 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 89 |
+
|
| 90 |
+
image_processor = RTDetrImageProcessor.from_pretrained(model_id)
|
| 91 |
+
model = RTDetrForObjectDetection.from_pretrained(model_id).to(device).eval()
|
| 92 |
+
|
| 93 |
+
image = Image.open("pills.jpg").convert("RGB")
|
| 94 |
+
inputs = image_processor(images=image, return_tensors="pt").to(device)
|
| 95 |
+
|
| 96 |
+
with torch.no_grad():
|
| 97 |
+
outputs = model(**inputs)
|
| 98 |
+
|
| 99 |
+
# Post-process — boxes in original image coordinates
|
| 100 |
+
target_sizes = torch.tensor([(image.height, image.width)], device=device)
|
| 101 |
+
results = image_processor.post_process_object_detection(
|
| 102 |
+
outputs, target_sizes=target_sizes, threshold=0.5
|
| 103 |
+
)[0]
|
| 104 |
+
|
| 105 |
+
# Count pills by class
|
| 106 |
+
counts = Counter()
|
| 107 |
+
for score, label_id, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 108 |
+
label = model.config.id2label[label_id.item()]
|
| 109 |
+
counts[label] += 1
|
| 110 |
+
x1, y1, x2, y2 = box.tolist()
|
| 111 |
+
print(f" {label}: {score:.2%} at [{x1:.0f}, {y1:.0f}, {x2:.0f}, {y2:.0f}]")
|
| 112 |
+
|
| 113 |
+
print(f"\nTotal pills detected: {sum(counts.values())}")
|
| 114 |
+
for label, count in sorted(counts.items(), key=lambda x: -x[1]):
|
| 115 |
+
print(f" {label}: {count}")
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
## Training Details
|
| 119 |
+
|
| 120 |
+
### Dataset
|
| 121 |
+
- **[Francesco/pills-sxdht](https://huggingface.co/datasets/Francesco/pills-sxdht)** from Roboflow 100
|
| 122 |
+
- 316 training / 45 validation / 90 test images
|
| 123 |
+
- All images 640×640 with COCO-format bounding box annotations
|
| 124 |
+
- 9 object classes (pills + specific medications + color categories)
|
| 125 |
+
|
| 126 |
+
### Training Configuration
|
| 127 |
+
| Parameter | Value |
|
| 128 |
+
|-----------|-------|
|
| 129 |
+
| Base model | PekingU/rtdetr_r18vd_coco_o365 (Objects365 pretrained) |
|
| 130 |
+
| Image size | 480×480 |
|
| 131 |
+
| Epochs | 20 |
|
| 132 |
+
| Batch size | 8 |
|
| 133 |
+
| Learning rate | 5e-5 |
|
| 134 |
+
| LR scheduler | Cosine with 50 warmup steps |
|
| 135 |
+
| Optimizer | AdamW (fused) |
|
| 136 |
+
| Max grad norm | 0.1 |
|
| 137 |
+
| Augmentations | HorizontalFlip, ColorJitter, RandomBrightnessContrast, GaussNoise, Blur |
|
| 138 |
+
|
| 139 |
+
### Training Loss Curve
|
| 140 |
+
|
| 141 |
+
| Epoch | Train Loss | Eval Loss |
|
| 142 |
+
|-------|-----------|-----------|
|
| 143 |
+
| 1 | 36.61 | 25.70 |
|
| 144 |
+
| 5 | 8.57 | 5.21 |
|
| 145 |
+
| 10 | 6.32 | 3.79 |
|
| 146 |
+
| 15 | 5.55 | 3.53 |
|
| 147 |
+
| **19** | **5.31** | **3.53 (best)** |
|
| 148 |
+
| 20 | 5.78 | 3.58 |
|
| 149 |
+
|
| 150 |
+
Best validation loss: **3.528** at epoch 19 (loaded as final checkpoint).
|
| 151 |
+
|
| 152 |
+
### Technical Notes
|
| 153 |
+
- Classification head re-initialized from 80 COCO classes → 9 pill classes
|
| 154 |
+
- Auxiliary loss enabled for training stability
|
| 155 |
+
- `freeze_backbone_batch_norms=True` to preserve pretrained backbone statistics
|
| 156 |
+
- Focal loss (α=0.75, γ=2.0) for handling class imbalance
|
| 157 |
+
|
| 158 |
+
## Limitations
|
| 159 |
+
- Trained on a small dataset (316 images) — may not generalize well to all pill types
|
| 160 |
+
- Best on images similar to training data (top-down views, clean backgrounds)
|
| 161 |
+
- Color-based classes (blue, pink, red, white) may overlap with medication-specific classes
|
| 162 |
+
- Not intended for medical decision-making — for counting/inventory purposes only
|
| 163 |
+
|
| 164 |
+
## Framework Versions
|
| 165 |
+
- Transformers 5.6.1
|
| 166 |
+
- PyTorch 2.11.0
|
| 167 |
+
- Datasets 4.8.4
|
| 168 |
|
| 169 |
+
## Citation
|
| 170 |
|
| 171 |
+
If you use this model, please cite the underlying RT-DETR architecture:
|
| 172 |
|
| 173 |
+
```bibtex
|
| 174 |
+
@article{zhao2024detrs,
|
| 175 |
+
title={DETRs Beat YOLOs on Real-time Object Detection},
|
| 176 |
+
author={Zhao, Yian and Lv, Wenyu and Xu, Shangliang and Wei, Jinman and Wang, Guanzhong and Dang, Qingqing and Liu, Yi and Chen, Jie},
|
| 177 |
+
journal={CVPR},
|
| 178 |
+
year={2024}
|
| 179 |
+
}
|
| 180 |
+
```
|