CARLA autonomous driving system with YOLO object detection, UFLD lane detection, traffic light recognition, and adaptive speed control.
| Demo | Description |
|---|---|
| carla_final_v2.webm | Final system with YOLO + UFLD + waypoint navigation |
This project implements a multi-modal perception and control pipeline for autonomous driving in the CARLA 0.9.15 simulator.
| Component | Model | Purpose |
|---|---|---|
| Object Detection | YOLO11n (custom-trained) | Vehicles, pedestrians, traffic lights, speed signs |
| Lane Detection | UFLD ResNet-18 (fine-tuned) | Lane boundary identification |
| Traffic Light Recognition | HSV Color Analysis | Red/Yellow/Green state detection |
| Vehicle Control | Hybrid Lane + Waypoint | Steering and speed management |
- Real-time object detection (4 classes: vehicle, pedestrian, traffic_light, speed_limit)
- Lane-following with UFLD neural network (100×56 grid)
- HSV-based traffic light color recognition
- Waypoint-based fallback navigation for intersections/turns > 5°
- Adaptive speed control based on obstacles
- 20 km/h speed cap with turn-aware reduction (70% in turns)
┌─────────────────────────────────────────────────────────────────────┐
│ CARLA Simulator (0.9.15) │
│ Map: Town01 | Vehicle: Tesla Model 3 | Traffic: NPCs │
└───────────────────────────┬─────────────────────────────────────────┘
│ RGB Camera (1640×590, FOV=150°)
▼
┌─────────────────────────────────────────────────────────────────────┐
│ Perception Pipeline │
│ ┌─────────────────────────┐ ┌─────────────────────────────────┐ │
│ │ YOLO11n Detection │ │ UFLD Lane Detection │ │
│ │ ├─ Vehicles (cls 0) │ │ ├─ Left lane boundary │ │
│ │ ├─ Pedestrians (cls 1) │ │ ├─ Right lane boundary │ │
│ │ ├─ Traffic lights (2) │ │ ├─ Center line computation │ │
│ │ └─ Speed signs (cls 3) │ │ └─ 800×288 → 1640×590 scale │ │
│ └───────────┬─────────────┘ └──────────────┬──────────────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────────────────────┐ ┌─────────────────────────────────┐ │
│ │ HSV Traffic Light │ │ Lane Center Steering │ │
│ │ Color Detection │ │ Calculation │ │
│ │ Red/Yellow → STOP │ │ ├─ Lateral offset │ │
│ │ Green → GO │ │ └─ Lookahead angle │ │
│ └───────────┬─────────────┘ └──────────────┬──────────────────┘ │
└──────────────┼──────────────────────────────────┼────────────────────┘
│ │
▼ ▼
┌─────────────────────────────────────────────────────────────────────┐
│ Control Pipeline │
│ ┌─────────────────────────────────────────────────────────────────┐│
│ │ Decision Logic: IF lane_angle ≤ 5° → Lane steering ││
│ │ ELSE → Waypoint steering (fallback) ││
│ └─────────────────────────────────────────────────────────────────┘│
│ ┌────────────────────────┐ ┌────────────────────────────────┐ │
│ │ Throttle/Brake │ │ Steering Control │ │
│ │ Base: 20 km/h cap │ │ Max: ±70° | Threshold: 5° │ │
│ │ Turn: 70% speed │ │ Normalized to [-1, 1] │ │
│ └────────────────────────┘ └────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────┘
├── src/ # Source code
│ ├── carla_autonomous_driving.py # Main autonomous driving system
│ ├── carla_connection_test.py # CARLA server connection test
│ └── ultrafastLaneDetector/ # UFLD model implementation
│ ├── ultrafastLaneDetector.py
│ ├── ultrafastLaneDetectorV2.py
│ └── ...
├── models/ # Pre-trained model weights
│ ├── yolo_carla/ # YOLO models trained on CARLA data
│ ├── ufld_carla/ # Fine-tuned UFLD models
│ └── tusimple_pretrained.pth # Pre-trained TuSimple weights
├── notebooks/ # Jupyter notebooks for training
│ ├── ufld_training.ipynb
│ └── yolo_training.ipynb
├── datasets/ # Dataset folder (not included in repo)
├── docs/ # Documentation and presentations
├── utils/ # Dataset utilities
│ ├── find_invalid_classes.py
│ ├── plot_class_distribution.py
│ ├── verify_dataset_labels.py
│ ├── validate_dataset_merge.py
│ └── validate_dataset_remap.py
├── configs/ # Configuration files
├── environment.yml # Conda environment file
├── requirements.txt # Pip requirements file
└── README.md # This file
- CARLA Simulator 0.9.15: Download from CARLA Releases
- Python 3.8: Required for CARLA 0.9.15 compatibility
- NVIDIA GPU: Recommended for real-time inference (tested on RTX 4070)
- CUDA: Installed and configured for PyTorch GPU support
-
Clone the repository:
git clone https://github.com/PreritSM/Carla_Autonomous_Driving.git cd carla-autonomous-driving -
Create the conda environment:
conda env create -f environment.yml conda activate carla_env
Or using pip:
pip install -r requirements.txt
-
Configure CARLA Path:
Open
src/carla_autonomous_driving.pyand update theCARLA_PATHvariable:CARLA_PATH = '/path/to/your/CARLA_0.9.15/PythonAPI/carla/dist/carla-0.9.15-py3.8-linux-x86_64.egg'
Note: The CARLA Python API is distributed as an
.eggfile. The path format varies by OS:- Linux:
carla-0.9.15-py3.8-linux-x86_64.egg - Windows:
carla-0.9.15-py3.8-win-amd64.egg
- Linux:
-
Download model weights (if using Git LFS):
git lfs pull
-
Start CARLA Server:
# Linux cd /path/to/CARLA_0.9.15 ./CarlaUE4.sh -quality-level=Low # Windows CarlaUE4.exe -quality-level=Low
-
Run the Autonomous Driving System:
cd src python carla_autonomous_driving.py -
Test CARLA Connection (optional):
python carla_connection_test.py
Custom-trained on CARLA simulator data using Ultralytics framework.
Classes Detected:
- 🚗 Class 0: Vehicles (cars, trucks)
- 🚶 Class 1: Pedestrians
- 🚦 Class 2: Traffic lights
- 🚸 Class 3: Speed limit signs
| Parameter | Value |
|---|---|
| Base Model | yolo11n.pt (Ultralytics) |
| Epochs | 250 |
| Batch Size | 128 (Google Colab) |
| Image Size | 640×640 |
| Optimizer | AdamW |
| Learning Rate | 0.005 |
| Early Stopping | 60 epochs patience |
| Augmentation | Enabled |
| Pretrained | Yes (COCO weights) |
| Freeze Layers | 0 (full fine-tune) |
Fine-tuned on CARLA synthetic lane data using TuSimple format.
| Parameter | Value |
|---|---|
| Backbone | ResNet-18 |
| Pre-trained | TuSimple dataset |
| Input Size | 800×288 (model) → 1640×590 (camera) |
| Grid Cells | 100 horizontal bins |
| Row Anchors | 56 (bottom 60% of image) |
| Lanes | 4 (only 2 used: left, right) |
| Parameter | Value |
|---|---|
| Epochs | 50 |
| Batch Size | 32 |
| Optimizer | Adam |
| Learning Rate | 1e-4 |
| Weight Decay | 1e-4 |
| Gradient Clip | 1.0 |
| LR Scheduler | CosineAnnealingLR |
| Sim Loss Weight | 1.0 |
| Shape Loss Weight | 0.0 |
| Loss | Purpose |
|---|---|
| SoftmaxFocalLoss | Row-anchor classification (γ=2, focuses on hard examples) |
| ParsingRelationLoss | Structural consistency between adjacent rows |
| ParsingRelationDis | Shape consistency (curvature smoothness) |
Modify in src/carla_autonomous_driving.py:
YOLO_MODEL_PATH = '../models/yolo_carla/carla_yolo11n_one_map2/weights/best.pt'
UFLD_MODEL_PATH = '../models/ufld_carla/checkpoint_best.pth'The system uses a hybrid steering approach:
- Lane-Based Steering (primary): When UFLD detects lanes with confidence > 0.3 and steering angle ≤ 5°
- Waypoint-Based Steering (fallback): For intersections, turns > 5°, or when lanes not detected
STEERING_THRESHOLD = 5.0 # degrees - threshold for lane vs waypoint steering
MAX_STEER_DEGREES = 70 # physical steering limit
base_target_speed = 20 # km/h - maximum speed cap# Safe distances (triggers speed reduction/stop)
safe_distance_vehicle = 15.0 # meters - slow down proportionally
safe_distance_pedestrian = 10.0 # meters - full stop
safe_distance_traffic_light = 20.0 # meters - check HSV color
# Turn-aware speed
if steering_angle > 10°:
target_speed = base_speed * 0.7 # 14 km/h in turns# Must match UFLD training data exactly
image_width = 1640
image_height = 590
fov = 150 # degrees (wide angle for lane visibility)
camera_position = (x=1.5, z=2.4) # meters from vehicle center| Component | Parameter | Value |
|---|---|---|
| YOLO | Confidence Threshold | 0.3 |
| YOLO | Classes | 4 (vehicle, pedestrian, traffic_light, speed_limit) |
| UFLD | Grid Resolution | 100×56 |
| UFLD | Coordinate Scaling | 2.05× (800→1640 width) |
| Control | Simulation Delta | 0.3 seconds |
| Control | Max Speed | 20 km/h |
| Control | Turn Speed | 14 km/h (70%) |
| Control | Lane Confidence | > 0.3 to use |
See the Jupyter notebooks for training details:
notebooks/yolo_training.ipynb- YOLO training pipelinenotebooks/ufld_training.ipynb- UFLD fine-tuning pipeline
The utils/ folder contains scripts for dataset validation:
cd utils
python plot_class_distribution.py # Visualize class distribution
python verify_dataset_labels.py # Validate label filesThis project is licensed under the MIT License - see the LICENSE file for details.
- CARLA Simulator - Open-source autonomous driving simulator
- Ultralytics YOLO - Object detection framework
- Ultra Fast Lane Detection - Lane detection model
- TuSimple Dataset - Lane detection benchmark



