Spaces:
Sleeping
Sleeping
Add detailed training prompt and guidelines
Browse files- TRAIN_PROMPT.md +138 -0
TRAIN_PROMPT.md
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# YOLOv8 Traffic Sign Detection Training Script Prompt
|
| 2 |
+
|
| 3 |
+
## Context
|
| 4 |
+
Current model has 3M parameters and proper architecture, but **underfitted** - detects 300 objects with confidence < 0.0001. Need retraining with proper hyperparameters.
|
| 5 |
+
|
| 6 |
+
## Requirements
|
| 7 |
+
|
| 8 |
+
Write a **YOLOv8 training script** with the following specifications:
|
| 9 |
+
|
| 10 |
+
### 1. Dataset Setup
|
| 11 |
+
- **Source**: GTSRB (German Traffic Sign Recognition Benchmark) - 40,000+ images
|
| 12 |
+
- **Format**: YOLO format (images/ and labels/ directories)
|
| 13 |
+
- **Structure**:
|
| 14 |
+
```
|
| 15 |
+
dataset/
|
| 16 |
+
images/
|
| 17 |
+
train/ (70% of data)
|
| 18 |
+
val/ (30% of data)
|
| 19 |
+
labels/
|
| 20 |
+
train/
|
| 21 |
+
val/
|
| 22 |
+
```
|
| 23 |
+
- **Classes**: 43 traffic sign classes (see config.yaml for class names)
|
| 24 |
+
- **Class mapping file**: dataset.yaml with proper format
|
| 25 |
+
|
| 26 |
+
### 2. Training Hyperparameters
|
| 27 |
+
- **Model**: YOLOv8n (nano - fastest) or YOLOv8s (small - better accuracy)
|
| 28 |
+
- **Epochs**: 150-200 (more than 100 for proper convergence)
|
| 29 |
+
- **Batch size**: 16-32 (adjust based on GPU memory)
|
| 30 |
+
- **Image size**: 640x640 (match inference size)
|
| 31 |
+
- **Learning rate**:
|
| 32 |
+
- Initial (lr0): 0.01
|
| 33 |
+
- Final (lrf): 0.01
|
| 34 |
+
- Warmup epochs: 3
|
| 35 |
+
- **Optimizer**: SGD (not Adam) for YOLOv8
|
| 36 |
+
- **Weight decay**: 0.0005
|
| 37 |
+
- **Momentum**: 0.937
|
| 38 |
+
|
| 39 |
+
### 3. Augmentation Settings (Critical!)
|
| 40 |
+
```
|
| 41 |
+
- HSV augmentation: h=0.015, s=0.7, v=0.4
|
| 42 |
+
- Rotation: degrees=10
|
| 43 |
+
- Translation: translate=0.1
|
| 44 |
+
- Scale: scale=0.5
|
| 45 |
+
- Flip: flipud=0.5, fliplr=0.5
|
| 46 |
+
- Mosaic: mosaic=1.0
|
| 47 |
+
- Mixup: mixup=0.1
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
### 4. Training Features
|
| 51 |
+
- **Early stopping**: patience=20 (stop if val loss doesn't improve)
|
| 52 |
+
- **Validation monitoring**: track mAP50, precision, recall
|
| 53 |
+
- **Model checkpointing**: save best.pt when val metric improves
|
| 54 |
+
- **Logging**: TensorBoard or Weights&Biases integration (optional)
|
| 55 |
+
|
| 56 |
+
### 5. Output Structure
|
| 57 |
+
```
|
| 58 |
+
runs/detect/train/
|
| 59 |
+
βββ weights/
|
| 60 |
+
β βββ best.pt (use this for inference)
|
| 61 |
+
β βββ last.pt
|
| 62 |
+
βββ results.png (training curves)
|
| 63 |
+
βββ events.out.tfevents.*
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
### 6. Post-Training Validation
|
| 67 |
+
After training:
|
| 68 |
+
- Validate on test set
|
| 69 |
+
- Compute metrics (mAP50, mAP50-95, precision, recall)
|
| 70 |
+
- Test on sample images (visual inspection)
|
| 71 |
+
- Compare confidence scores (should be > 0.5 for good detections)
|
| 72 |
+
|
| 73 |
+
### 7. Python Libraries
|
| 74 |
+
```
|
| 75 |
+
Required:
|
| 76 |
+
- ultralytics>=8.0.0
|
| 77 |
+
- torch
|
| 78 |
+
- torchvision
|
| 79 |
+
- opencv-python
|
| 80 |
+
- numpy
|
| 81 |
+
- pyyaml
|
| 82 |
+
|
| 83 |
+
Optional:
|
| 84 |
+
- tensorboard (for visualization)
|
| 85 |
+
- wandb (for cloud logging)
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
### 8. Code Structure
|
| 89 |
+
1. **Setup phase**: Load config, prepare dataset.yaml
|
| 90 |
+
2. **Model initialization**: Load pretrained YOLOv8n/s
|
| 91 |
+
3. **Training phase**: Call model.train() with params
|
| 92 |
+
4. **Validation phase**: Evaluate on val set
|
| 93 |
+
5. **Testing phase**: Inference on test images
|
| 94 |
+
6. **Save phase**: Export best.pt to deployment location
|
| 95 |
+
|
| 96 |
+
### 9. Expected Outcomes
|
| 97 |
+
After proper training (150 epochs):
|
| 98 |
+
- **mAP50**: > 0.7 (good)
|
| 99 |
+
- **Precision**: > 0.75
|
| 100 |
+
- **Recall**: > 0.75
|
| 101 |
+
- **Confidence scores**: majority > 0.3 (not 0.0001!)
|
| 102 |
+
- Training time: 2-6 hours on GPU (or 24+ hours on CPU)
|
| 103 |
+
|
| 104 |
+
### 10. Deployment
|
| 105 |
+
```python
|
| 106 |
+
# After training, replace model path in config.yaml:
|
| 107 |
+
model:
|
| 108 |
+
path: 'runs/detect/train/weights/best.pt'
|
| 109 |
+
confidence_threshold: 0.25 (adjust based on precision/recall tradeoff)
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
## Tips
|
| 113 |
+
1. Monitor training curves (loss should decrease smoothly)
|
| 114 |
+
2. If overfitting: increase augmentation or reduce epochs
|
| 115 |
+
3. If underfitting: increase epochs or reduce augmentation
|
| 116 |
+
4. Use GPU if possible (50x faster than CPU)
|
| 117 |
+
5. Save weights regularly (every 10 epochs)
|
| 118 |
+
6. Validate on completely unseen test set
|
| 119 |
+
7. Test confidence distribution on real images
|
| 120 |
+
|
| 121 |
+
## Example Command Structure
|
| 122 |
+
```bash
|
| 123 |
+
python train.py \
|
| 124 |
+
--data dataset.yaml \
|
| 125 |
+
--model yolov8n.pt \
|
| 126 |
+
--epochs 150 \
|
| 127 |
+
--batch 32 \
|
| 128 |
+
--imgsz 640 \
|
| 129 |
+
--device 0 \
|
| 130 |
+
--patience 20 \
|
| 131 |
+
--augment
|
| 132 |
+
```
|
| 133 |
+
|
| 134 |
+
---
|
| 135 |
+
Use this prompt to:
|
| 136 |
+
- Ask an AI to write the complete training script
|
| 137 |
+
- Guide your own script writing
|
| 138 |
+
- Review if training script meets these requirements
|