File size: 4,502 Bytes
5b86813 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | # Road Anomaly Detection - Model Training
Complete training pipeline for road anomaly detection using YOLOv8, optimized for **NVIDIA RTX 2050 (4GB VRAM)** and **Raspberry Pi 4** deployment.
## π Quick Start
```fish
# 1. Setup environment
chmod +x setup_training.fish
./setup_training.fish
# 2. Activate environment
source .venv/bin/activate.fish
# 3. Verify your dataset
python verify_dataset.py
# 4. Train model
python train_road_anomaly_model.py
# 5. Package for Raspberry Pi
python package_for_rpi.py
```
## π Project Structure
```
model/
βββ setup_training.fish # Environment setup (run first)
βββ train_road_anomaly_model.py # Main training script
βββ verify_dataset.py # Dataset validation
βββ inference.py # Run inference on images/video
βββ package_for_rpi.py # Create RPi deployment package
βββ plan.txt # Detailed training plan
βββ README.md # This file
../dataset/dataset/ # Your dataset (YOLO format)
βββ data.yaml
βββ train/images/ train/labels/
βββ valid/images/ valid/labels/
βββ test/images/ test/labels/
```
## π― RTX 2050 Optimization
The training is pre-configured for 4GB VRAM:
| Parameter | Value | Reason |
|-----------|-------|--------|
| Batch Size | 8 | 4GB VRAM safe |
| Image Size | 416 | Edge-optimized |
| AMP | Enabled | 30-50% faster |
| Workers | 4 | 8-core CPU / 2 |
| Cache | Disabled | Save RAM for GPU |
### If you get OOM (Out of Memory):
Edit `train_road_anomaly_model.py`:
```python
BATCH_SIZE = 4 # Reduce from 8
IMAGE_SIZE = 320 # Reduce from 416
```
## π Dataset Format
YOLO format with normalized coordinates:
```
<class_id> <x_center> <y_center> <width> <height>
```
Example label file:
```
0 0.5 0.5 0.2 0.15
1 0.3 0.7 0.1 0.08
```
Classes (default):
- 0: pothole
- 1: crack
- 2: bump
- 3: obstacle
- 4: road_damage
## π§ Scripts
### 1. Setup Environment
```fish
./setup_training.fish
```
Installs PyTorch with CUDA, Ultralytics, and all dependencies.
### 2. Verify Dataset
```bash
python verify_dataset.py [path/to/dataset]
```
Checks dataset structure, validates annotations, shows class distribution.
### 3. Train Model
```bash
python train_road_anomaly_model.py
```
Trains YOLOv8n model with RTX 2050 optimizations, exports to TFLite.
### 4. Run Inference
```bash
# Image
python inference.py --model road-anomaly-detection/train_*/weights/best.pt --source image.jpg
# Video
python inference.py --model best.pt --source video.mp4
# Camera
python inference.py --model best.pt --source camera
# TFLite model
python inference.py --model best.tflite --source image.jpg
```
### 5. Package for Raspberry Pi
```bash
python package_for_rpi.py
```
Creates deployment package with TFLite model and detection script.
## π¦ Output
After training:
```
road-anomaly-detection/
βββ train_YYYYMMDD_HHMMSS/
βββ weights/
β βββ best.pt # PyTorch model
β βββ best.tflite # TFLite for RPi β
βββ training_report.txt # Performance metrics
βββ results.png # Training curves
βββ confusion_matrix.png # Per-class accuracy
βββ sample_predictions/ # Test predictions
```
## π Raspberry Pi Deployment
```bash
# Transfer to RPi
scp -r rpi_deployment_*/ pi@raspberrypi.local:/home/pi/
# On RPi
cd rpi_deployment_*/
pip install -r requirements.txt
python detect.py --source 0 # Webcam
```
Expected performance:
- RPi 4: 10-12 FPS at 416x416
- RPi 5: 15-20 FPS
## π Expected Results
| Metric | Target |
|--------|--------|
| mAP@50 | β₯ 0.80 |
| Precision | β₯ 0.75 |
| Recall | β₯ 0.75 |
| Model Size | β€ 10 MB |
| RPi FPS | 10-12 |
## π Troubleshooting
### CUDA Out of Memory
```python
# In train_road_anomaly_model.py
BATCH_SIZE = 4
IMAGE_SIZE = 320
```
### Training Slow
```fish
# Check GPU utilization
nvidia-smi
# Should see 85-95% GPU usage
# If low, increase batch size
```
### No GPU Detected
```fish
# Check NVIDIA driver
nvidia-smi
# Check PyTorch CUDA
python -c "import torch; print(torch.cuda.is_available())"
# Reinstall PyTorch
uv pip install torch --index-url https://download.pytorch.org/whl/cu121 --force-reinstall
```
## π References
- [Ultralytics YOLOv8](https://docs.ultralytics.com/)
- [TensorFlow Lite](https://www.tensorflow.org/lite)
- [PyTorch](https://pytorch.org/)
## π License
MIT License
|