Skip to main content
Donkeycar supports training deep learning models using both TensorFlow/Keras and PyTorch frameworks. These models learn to predict steering and throttle from camera images by analyzing recorded driving data.

Model Architectures

Donkeycar provides several pre-built neural network architectures optimized for different use cases:

Linear Model

The simplest model using linear activation for continuous steering and throttle outputs.
Architecture:
  • 5 convolutional layers with dropout (24, 32, 64, 64, 64 filters)
  • Flatten layer
  • 2 fully connected layers (100, 50 neurons)
  • 2 output neurons with linear activation (steering, throttle)
Loss function: Mean Squared Error (MSE) Use case: Best for smooth, continuous control with unbounded outputs.

Categorical Model

Converts steering and throttle into discrete bins using categorical cross-entropy.
Architecture:
  • Same CNN base as Linear model
  • 2 output layers with softmax activation:
    • Steering: 15 bins (covering -1.0 to 1.0)
    • Throttle: 20 bins (covering throttle_range)
Loss function: Categorical cross-entropy with equal weights (0.5, 0.5) Use case: Better for discrete decision-making and provides confidence distributions. Training parameters:

LSTM Model

Recurrent model that uses sequences of images for temporal reasoning.
Architecture:
  • Time-distributed CNN layers on image sequences
  • 2 LSTM layers (128 units each)
  • Dense layers (128, 64, 10 neurons)
  • 2 output neurons
Use case: Learns temporal patterns and motion, smoother driving behavior. Note: Requires sequential data during inference - maintains a deque of recent images.

3D CNN Model

Uses 3D convolutions over video sequences for spatiotemporal feature extraction.
Architecture:
  • 4 Conv3D layers (16, 32, 64, 128 filters) with MaxPooling3D
  • Flatten and batch normalization
  • 2 dense layers (256 neurons) with dropout (0.5)
  • 2 output neurons
Use case: Best for learning spatiotemporal patterns, requires longer sequences.

Memory Model

Linear model augmented with recent steering/throttle history for smoother outputs.
Architecture:
  • CNN base (same as Linear)
  • Memory input: last mem_length steering/throttle pairs
  • Dense layers to process memory
  • Concatenation of CNN and memory features
  • Output layers with tanh/sigmoid activation
Use case: Produces smoother control by considering recent actions.

IMU Model

Combines camera images with IMU sensor data (accelerometer/gyroscope).
Architecture:
  • Image branch: CNN layers
  • IMU branch: 3 dense layers (14 neurons each)
  • Concatenation of both branches
  • 2 dense layers (50 neurons) with dropout
  • 2 output neurons
IMU inputs: ['imu/acl_x', 'imu/acl_y', 'imu/acl_z', 'imu/gyr_x', 'imu/gyr_y', 'imu/gyr_z'] Use case: Improves stability on rough terrain or aggressive driving.

Behavioral Model

Multi-task learning with different behaviors (e.g., left lane, right lane, obstacles).
Architecture:
  • Image branch: CNN layers
  • Behavior branch: Dense layers for one-hot behavior state
  • Concatenation of branches
  • Categorical outputs (15 angle bins, 20 throttle bins)
Use case: Single model that can switch between different driving behaviors.

Localizer Model

Predicts steering, throttle, and track location simultaneously.
Architecture:
  • Shared CNN base
  • 3 outputs: steering (linear), throttle (linear), location (softmax)
Use case: Multi-task learning for position-aware driving.

Training Pipeline

Training Command

Train a model using the command line:
Common arguments:
  • --tub: Comma-separated list of tub paths
  • --model: Output model path (.h5 for TensorFlow, .ckpt for PyTorch)
  • --type: Model type (linear, categorical, lstm, 3d_cnn, memory, imu, behavioral, localizer)
  • --transfer: Path to model for transfer learning
  • --comment: Training description for database

Training Configuration

Key configuration parameters in myconfig.py:

Training Process

The training pipeline performs these steps:
  1. Data Loading: Load tub data and split into train/validation sets
  2. Model Creation: Initialize the selected model architecture
  3. Compilation: Set optimizer, loss function, and metrics
  4. Augmentation: Apply image augmentations (optional)
  5. Training: Fit model with early stopping and checkpointing
  6. Export: Save best model and optionally convert to TFLite/TensorRT
Training loop (internal):
Model compilation:

Optimizer Configuration

Customize the optimizer in your training script:

Image Augmentation

Augmentation helps prevent overfitting and improves generalization.

Configuration

Custom Augmentation

Create custom augmentation pipeline:

Transfer Learning

Use a pre-trained model as starting point:
Benefits:
  • Faster training convergence
  • Better performance with limited data
  • Reuse features learned from other tracks

PyTorch Training

Donkeycar also supports PyTorch with ResNet18 transfer learning.

PyTorch Model

Features:
  • Pre-trained ResNet18 on ImageNet
  • Feature extraction layers frozen
  • Fine-tuned classifier layer
  • PyTorch Lightning for training

Training PyTorch Model

Configuration:

PyTorch Data Pipeline

Model Selection Guide

Performance Tips

  1. Start with Linear model: Simple and fast, good baseline
  2. Try Categorical for better accuracy: Especially on complex tracks
  3. Use augmentation: Prevents overfitting on small datasets
  4. Monitor validation loss: Use early stopping to prevent overfitting
  5. Collect diverse data: Various lighting, positions, speeds
  6. Try transfer learning: Start from pre-trained model
  7. Tune batch size: Larger batches are faster but use more memory
  8. Use GPU: Significantly faster training (CUDA)

Model Inference

Use trained model in your car:

Training Output

Training produces:
  • Model file: mypilot.h5 (TensorFlow) or mypilot.ckpt (PyTorch)
  • TFLite model: mypilot.tflite (for embedded devices)
  • Training plot: mypilot.png (loss curves)
  • Database entry: Training metadata and history
Database location: data/pilot_db.json

Next Steps