Distracted Drivers Detection Computer-Vision Project
Data Science

Distracted Drivers Detection Computer-Vision Project

Distracted Drivers Detection Computer-Vision Project

Project Classes
Residual Networks (ResNet)
A regular block (left) and a residual block (right)
The Technology we Used
Pipeline
# Basic Imports
import numpy as np
import pandas as pd
from math import exp
import numpy.random as nr
import os, random, time,copy, re, glob
# Pytorch Imports
import torch, torchvision
from torch import nn, optim
from torch.functional import F
from torch.autograd import Variable
from torch.utils.data import DataLoader, Dataset
from torchvision import models, transforms, datasets
# Tensorflow Imports
import tensorflow as tf
from tensorflow.keras.preprocessing import image
# Images and Plt Imports
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from IPython.display import display

Training and Evaluation Functions:

Training Function

def epoch_time(start_time, end_time):
elapsed_time = end_time - start_time
elapsed_mins = int(elapsed_time / 60)
elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
return elapsed_mins, elapsed_secs
def calculate_accuracy(y_pred, y):
top_pred = y_pred.argmax(1, keepdim = True)
correct = top_pred.eq(y.view_as(top_pred)).sum()
acc = correct.float() / y.shape[0]
return acc
def train(model, iterator, optimizer, criterion):

epoch_loss = 0
epoch_acc = 0

model.train()

for (x, y) in iterator:

#variables can already sort of do that by setting the requires_grad=False
x = Variable(torch.FloatTensor(np.array(x))).to(device)
y = Variable(torch.LongTensor(y)).to(device)

optimizer.zero_grad() #to clear the gradients from the previous training step

y_pred = model(x)

loss = criterion(y_pred, y)

acc = calculate_accuracy(y_pred, y)

loss.backward()

#makes the optimizer iterate over all parameters (tensors)
#it is supposed to update and use their internally stored grad to update their values
optimizer.step()

epoch_loss += loss.item()
epoch_acc += acc.item()

return epoch_loss / len(iterator), epoch_acc / len(iterator)
def evaluate(model, iterator, criterion):
epoch_loss = 0
epoch_acc = 0

model.eval()

with torch.no_grad():

for (x, y) in iterator:
#variables can already sort of do that by setting the requires_grad=False
x = Variable(torch.FloatTensor(np.array(x))).to(device)
y = Variable(torch.LongTensor(y)).to(device)

y_pred = model(x)
loss = criterion(y_pred, y)acc = calculate_accuracy(y_pred, y)epoch_loss += loss.item()
epoch_acc += acc.item()

return epoch_loss / len(iterator), epoch_acc / len(iterator)
def fit_model(model, model_name, train_iterator, valid_iterator, optimizer, loss_criterion, epochs):
""" Fits a dataset to model"""
best_valid_loss = float('inf')

train_losses = []
valid_losses = []
train_accs = []
valid_accs = []

for epoch in range(epochs):

start_time = time.time()

train_loss, train_acc = train(model, train_iterator, optimizer, loss_criterion, device)
valid_loss, valid_acc = evaluate(model, valid_iterator, loss_criterion, device)

train_losses.append(train_loss)
valid_losses.append(valid_loss)
train_accs.append(train_acc*100)
valid_accs.append(valid_acc*100)

if valid_loss < best_valid_loss:
best_valid_loss = valid_loss
torch.save(model.state_dict(), f'{model_name}.pt')

end_time = time.time()
epoch_mins, epoch_secs = epoch_time(start_time, end_time)

print(f'Epoch: {epoch+1:02} | Epoch Time: {epoch_mins}m {epoch_secs}s')
print(f'\tTrain Loss: {train_loss:.3f} | Train Acc: {train_acc*100:.2f}%')
print(f'\t Val. Loss: {valid_loss:.3f} | Val. Acc: {valid_acc*100:.2f}%')

return pd.DataFrame({f'{model_name}_Training_Loss':train_losses,
f'{model_name}_Training_Acc':train_accs,
f'{model_name}_Validation_Loss':valid_losses,
f'{model_name}_Validation_Acc':valid_accs})
def plot_training_statistics(train_stats, model_name):

fig, axes = plt.subplots(2, figsize=(15,15))
axes[0].plot(train_stats[f'{model_name}_Training_Loss'], label=f'{model_name}_Training_Loss')
axes[0].plot(train_stats[f'{model_name}_Validation_Loss'], label=f'{model_name}_Validation_Loss')
axes[1].plot(train_stats[f'{model_name}_Training_Acc'], label=f'{model_name}_Training_Acc')
axes[1].plot(train_stats[f'{model_name}_Validation_Acc'], label=f'{model_name}_Validation_Acc')

axes[0].set_xlabel("Number of Epochs"), axes[0].set_ylabel("Loss")
axes[1].set_xlabel("Number of Epochs"), axes[1].set_ylabel("Accuracy in %")

axes[0].legend(), axes[1].legend()
Loss and Accuracy Vs Number of Epochs

To use our Pip Package:

1. Install Our Package
pip install Distracted-Driver-Detection
2. Download the Finetunned Model Weights and Test Imagesimport gdown
PytorchURL = 'https://drive.google.com/uc?id=1P9r7pCc-5eTmW4krT4GZ1F6w_miTtxJA'
TfLiteURL = 'https://drive.google.com/uc?id=1WbZD6PMETHIH6oMj0bzyG3BoDUlyO2Ll'
TestImagesURL = 'https://drive.google.com/uc?id=1sodvME9eXHuZ-4qjTxmxsLsfFsg99KpK'
PytorchModel = 'model_ft.pth'
TfLiteModel = 'model.tflite'
TestImages = 'test_imgsN.zip'
gdown.download(PytorchURL, PytorchModel, quiet=False)
gdown.download(TfLiteURL, TfLiteModel, quiet=False)
gdown.download(TestImagesURL, TestImages, quiet=False)
3. Import the DistractedDriverDetection_Utils from distracted_driver_detection :from distracted_driver_detection import DistractedDriverDetection_Utils
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
4. Detect The Distraction Class for the Driver Using Pytorch Weights:# Run the Below Function by Input your image Path to get the outPut class and probability for the driver distraction class then show it
class_,pro = DistractedDriverDetection_Utils.PredictClass(imgPath)
print(class_,pro)
plt.imshow(mpimg.imread(imgPath));

# Plot Batch of Test Images from directory with Detection
DistractedDriverDetection_Utils.predMulti_images(test_img_dir,nImages=4)
5. Detect The Distraction Class for the Driver Using Tensorflow Lite Weights:# Run the Below Function by Input your image Path to get the outPut class and probability for the driver distraction class then show it
class_,pro = DistractedDriverDetection_Utils.tfliteModel_Prediction(imgPath)
print(class_,pro)
plt.imshow(mpimg.imread(imgPath));

# Plot Batch of Test Images from directory with Detection
DistractedDriverDetection_Utils.tfliteModel_Plot(test_img_dir,nImages=4)

Model Output:

Model Prediction

Links:

  • Mohamed Sebaie
  • Mar, 25 2022

Add New Comments

Please login in order to make a comment.

Recent Comments

Be the first to start engaging with the bis blog.