Skip to content

Latest commit

Β 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš— Distracted Driver Detection β€” ResNet50 from Scratch

Python Keras TensorFlow Dataset Classes License

Classifies 10 distracted driving behaviors from dashboard camera images using a custom ResNet50 implementation built from scratch in Keras β€” including manual convolutional_block and identity_block definitions, glorot_uniform initialization, and LOGO cross-validation strategy.

πŸ”™ Back to Main Repository


⚠️ Safety Context

Distracted driving causes thousands of road fatalities annually. Automated in-vehicle behavior classification from dashboard cameras is an active area of road safety AI research.


πŸ“Œ Table of Contents


πŸ”¬ About the Project

This project tackles the State Farm Distracted Driver Detection Kaggle challenge β€” classifying driver images into 10 behavior classes. What makes it distinctive is that ResNet50 is implemented completely from scratch using the Keras functional API, manually defining every bottleneck block and skip connection rather than using tf.keras.applications.

The notebook also demonstrates handling real-world ML challenges: high bias, high variance, and the LOGO (Leave-One-Group-Out) cross-validation strategy needed because multiple images belong to the same driver β€” random splits would leak the same driver into both train and validation sets.

What this project covers:

  • Manual identity_block and convolutional_block implementations in Keras
  • resnets_utils helper module for block definitions
  • Diagnosing and addressing underfitting (high bias) and overfitting (high variance)
  • LOGO cross-validation to prevent driver-level data leakage

βš™οΈ How It Works

Dashboard Camera Image
         β”‚
         β–Ό
  Load + Preprocess
  (Normalize pixel values / 255)
         β”‚
         β–Ό
  ResNet50 Forward Pass
  (Custom Keras implementation)
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ ZeroPadding2D (3,3)             β”‚
  β”‚ Conv2D(64,7Γ—7,s=2) β†’ BN β†’ ReLU β”‚
  β”‚ MaxPool(3Γ—3, s=2)               β”‚
  β”‚ Stage 2: ConvBlock + IdBlockΓ—2  β”‚
  β”‚ Stage 3: ConvBlock + IdBlockΓ—3  β”‚
  β”‚ Stage 4: ConvBlock + IdBlockΓ—5  β”‚
  β”‚ Stage 5: ConvBlock + IdBlockΓ—2  β”‚
  β”‚ AveragePooling2D(2Γ—2)           β”‚
  β”‚ Flatten β†’ Dense(10, softmax)    β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
  10-Class Softmax Output β†’ c0–c9

πŸ“Š Dataset

Property Details
Name State Farm Distracted Driver Detection
Source Kaggle Competition
Training Images 22,424
Classes 10 driving behaviors
Input Shape Resized to 64 Γ— 64 Γ— 3 for training
Metadata driver_imgs_list.csv β€” subject ID, classname, filename
Key Challenge Multiple images per driver β†’ LOGO cross-validation required

🚦 Class Definitions

Code Behavior
c0 βœ… Safe Driving
c1 πŸ“± Texting β€” Right Hand
c2 πŸ“ž Phone Call β€” Right Hand
c3 πŸ“± Texting β€” Left Hand
c4 πŸ“ž Phone Call β€” Left Hand
c5 🎡 Operating Radio
c6 πŸ₯€ Drinking
c7 πŸ”™ Reaching Behind
c8 πŸ’„ Hair / Makeup
c9 πŸ’¬ Talking to Passenger

πŸ—οΈ Model Architecture

The notebook defines ResNet50 from scratch β€” no pretrained weights, no tf.keras.applications:

from keras.layers import (Input, Add, Dense, Activation, ZeroPadding2D,
    BatchNormalization, Flatten, Conv2D, AveragePooling2D, MaxPooling2D)
from keras.models import Model
from keras.initializers import glorot_uniform
from resnets_utils import *

def ResNet50(input_shape=(64, 64, 3), classes=10, init=glorot_uniform(seed=0)):
    """
    CONV2D -> BATCHNORM -> RELU -> MAXPOOL
    -> CONVBLOCK -> IDBLOCK*2
    -> CONVBLOCK -> IDBLOCK*3
    -> CONVBLOCK -> IDBLOCK*5
    -> CONVBLOCK -> IDBLOCK*2
    -> AVGPOOL -> TOPLAYER
    """

Block types:

Block Shape Change Used When
Identity Block Input = Output shape Deepening without dimension change
Convolutional Block Input β‰  Output shape When stride changes or filter count increases

Stage filter configurations:

Stage Filters Blocks
Stage 2 [64, 64, 256] ConvBlock + IdBlock Γ— 2
Stage 3 [128, 128, 512] ConvBlock + IdBlock Γ— 3
Stage 4 [256, 256, 1024] ConvBlock + IdBlock Γ— 5
Stage 5 [512, 512, 2048] ConvBlock + IdBlock Γ— 2

Training config:

Parameter Value
Initializer glorot_uniform(seed=0)
Optimizer Adam
Loss Categorical Cross-Entropy
Input Shape (64, 64, 3)
Output Dense(10, softmax)

πŸ“‰ Training Analysis & Challenges

The notebook provides honest, detailed bias-variance analysis across training runs β€” a key learning documented in the project:

Epoch 2 Results

Set Accuracy
Train ~26%
Dev ~13%

High bias (underfitting) β€” model hasn't converged. High variance β€” large gap between train/dev.

Epoch 5 Results

Set Accuracy
Train 37.83%
Dev 25.79%

Train accuracy improved but underfitting persists (~62% away from 100%). Variance increased dramatically (+80% gap between epochs 2β†’5). The notebook diagnoses this explicitly:

"We still have an underfitting problem (high bias, about 62.17% from 100%),
however, our variance has increased dramatically between 2 and 5 epochs by about 80%."

Prescribed fixes documented in the notebook:

To address High Bias (underfitting):

  • Increase epoch count
  • Use a bigger/deeper network
  • Try different optimizers or learning rate schedules

To address High Variance (overfitting):

  • Apply L2 regularization
  • Add dropout layers
  • Use data augmentation
  • Increase training data volume

LOGO Cross-Validation Note

Standard random train/val splits cause data leakage β€” the same driver's images appear in both sets, inflating dev accuracy. The notebook flags this and recommends Leave-One-Group-Out (LOGO) cross-validation, splitting by subject (driver ID) from driver_imgs_list.csv.


πŸ“ Project Structure

Distracted Driver Detection/
β”‚
β”œβ”€β”€ πŸ“‚ dataset/
β”‚   β”œβ”€β”€ train/                         # Training images, organized by class
β”‚   β”‚   β”œβ”€β”€ c0/  c1/  c2/  ...  c9/
β”‚   └── test/                          # Unlabeled test images
β”‚
β”œβ”€β”€ driver_imgs_list.csv               # subject, classname, img columns
β”œβ”€β”€ resnets_utils.py                   # identity_block + convolutional_block helpers
β”œβ”€β”€ distracted_driver_detection.ipynb  # Main notebook
β”œβ”€β”€ requirements.txt                   # Python dependencies
└── README.md                          # You are here

πŸš€ Getting Started

1. Clone the repository

git clone https://github.com/shsarv/Machine-Learning-Projects.git
cd "Machine-Learning-Projects/Distracted Driver Detection"

2. Download the dataset from Kaggle

pip install kaggle
kaggle competitions download -c state-farm-distracted-driver-detection
unzip state-farm-distracted-driver-detection.zip -d dataset/

Or download manually from: kaggle.com/c/state-farm-distracted-driver-detection/data

3. Set up environment

python -m venv venv
source venv/bin/activate        # Linux / macOS
venv\Scripts\activate           # Windows

pip install -r requirements.txt

4. Run the notebook

jupyter notebook distracted_driver_detection.ipynb

πŸ› οΈ Tech Stack

Layer Technology
Language Python 3.7+
Deep Learning TensorFlow / Keras
Model ResNet50 (from scratch via Keras functional API)
Utilities resnets_utils.py (custom block helpers)
Data Pandas, NumPy
Visualization Matplotlib
Notebook Jupyter / Google Colab

πŸ“š References


Part of the Machine Learning Projects collection by Sarvesh Kumar Sharma

⭐ Star the main repo if this helped you!

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages