SARANGx commited on
Commit
993fb4c
·
verified ·
1 Parent(s): 471f271

Update model card with full training details and usage

Browse files
Files changed (1) hide show
  1. README.md +164 -62
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
- - generated_from_trainer
 
 
 
 
 
 
 
 
 
7
  model-index:
8
- - name: rtdetr-pill-detector
9
- results: []
 
 
 
 
 
 
 
 
 
 
10
  ---
11
 
12
- <!-- This model card has been generated automatically according to the information the Trainer had access to. You
13
- should probably proofread and complete it, then remove this comment. -->
14
 
15
- # rtdetr-pill-detector
16
 
17
- This model is a fine-tuned version of [PekingU/rtdetr_r18vd_coco_o365](https://huggingface.co/PekingU/rtdetr_r18vd_coco_o365) on an unknown dataset.
18
- It achieves the following results on the evaluation set:
19
- - Loss: 3.5814
20
 
21
- ## Model description
22
 
23
- More information needed
24
 
25
- ## Intended uses & limitations
26
-
27
- More information needed
28
-
29
- ## Training and evaluation data
30
-
31
- More information needed
32
-
33
- ## Training procedure
34
-
35
- ### Training hyperparameters
36
-
37
- The following hyperparameters were used during training:
38
- - learning_rate: 5e-05
39
- - train_batch_size: 8
40
- - eval_batch_size: 8
41
- - seed: 42
42
- - optimizer: Use OptimizerNames.ADAMW_TORCH_FUSED with betas=(0.9,0.999) and epsilon=1e-08 and optimizer_args=No additional optimizer arguments
43
- - lr_scheduler_type: cosine
44
- - lr_scheduler_warmup_steps: 50
45
- - num_epochs: 20
46
-
47
- ### Training results
48
-
49
- | Training Loss | Epoch | Step | Validation Loss |
50
- |:-------------:|:-----:|:----:|:---------------:|
51
- | 36.6133 | 1.0 | 40 | 25.6964 |
52
- | 16.9363 | 2.0 | 80 | 10.3644 |
53
- | 13.2576 | 3.0 | 120 | 7.4604 |
54
- | 11.2193 | 4.0 | 160 | 6.0646 |
55
- | 8.5718 | 5.0 | 200 | 5.2125 |
56
- | 8.3696 | 6.0 | 240 | 4.8302 |
57
- | 7.5242 | 7.0 | 280 | 4.4670 |
58
- | 7.0528 | 8.0 | 320 | 4.0657 |
59
- | 6.3887 | 9.0 | 360 | 3.8813 |
60
- | 6.3234 | 10.0 | 400 | 3.7910 |
61
- | 5.9830 | 11.0 | 440 | 3.6750 |
62
- | 5.8632 | 12.0 | 480 | 3.5891 |
63
- | 6.7105 | 13.0 | 520 | 3.7327 |
64
- | 5.4882 | 14.0 | 560 | 3.5292 |
65
- | 5.5451 | 15.0 | 600 | 3.5306 |
66
- | 5.3121 | 16.0 | 640 | 3.5427 |
67
- | 5.2448 | 17.0 | 680 | 3.5641 |
68
- | 5.3653 | 18.0 | 720 | 3.6307 |
69
- | 5.3143 | 19.0 | 760 | 3.5280 |
70
- | 5.7803 | 20.0 | 800 | 3.5814 |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
 
72
 
73
- ### Framework versions
74
 
75
- - Transformers 5.6.1
76
- - Pytorch 2.11.0+cu130
77
- - Datasets 4.8.4
78
- - Tokenizers 0.22.2
 
 
 
 
 
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
+ ```