Classifies 10 distracted driving behaviors from dashboard camera images using a custom ResNet50 implementation built from scratch in Keras β including manual
convolutional_blockandidentity_blockdefinitions,glorot_uniforminitialization, and LOGO cross-validation strategy.
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.
- About the Project
- How It Works
- Dataset
- Class Definitions
- Model Architecture
- Training Analysis & Challenges
- Project Structure
- Getting Started
- Tech Stack
- References
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_blockandconvolutional_blockimplementations in Keras resnets_utilshelper module for block definitions- Diagnosing and addressing underfitting (high bias) and overfitting (high variance)
- LOGO cross-validation to prevent driver-level data leakage
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
| 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 |
| 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 |
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) |
The notebook provides honest, detailed bias-variance analysis across training runs β a key learning documented in the project:
| Set | Accuracy |
|---|---|
| Train | ~26% |
| Dev | ~13% |
High bias (underfitting) β model hasn't converged. High variance β large gap between train/dev.
| 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%."
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
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) fromdriver_imgs_list.csv.
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
git clone https://github.com/shsarv/Machine-Learning-Projects.git
cd "Machine-Learning-Projects/Distracted Driver Detection"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
python -m venv venv
source venv/bin/activate # Linux / macOS
venv\Scripts\activate # Windows
pip install -r requirements.txtjupyter notebook distracted_driver_detection.ipynb| 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 |
- State Farm Distracted Driver Detection β Kaggle
- He, K., Zhang, X., Ren, S., & Sun, J. (2015). Deep Residual Learning for Image Recognition. arXiv:1512.03385
- deeplearning.ai β ResNet50 from scratch (Coursera)
- Keras Functional API Documentation
Part of the Machine Learning Projects collection by Sarvesh Kumar Sharma
β Star the main repo if this helped you!