Using Deep learning to save lives by ensuring driver’s attention

Source: Deep Learning on Medium

Convolutional Neural Networks in real life

Go to the profile of Karan Bhanot
Photo by melissa mjoen on Unsplash

In 2017 alone, 3,166 people were killed in motor vehicle crashes involving distracted drivers. — NHTSA

Driving a motor vehicle is in itself a complex task. However, when we add distraction to the mix, it makes it even more difficult as there is lack of attention of drivers on the road. So, what if we could identify whenever a driver is distracted? Doing so would help us detect it and alert the driver to prevent accidents!

In this article, I’ll address the problem of detecting a distracted driver using Convolutional Neural Networks and even classify the kind of activity they are doing apart from safe driving. Here’s a link to the Kaggle Kernel.

Import libraries

I used Keras with Tensorflow as its backend to create the Convolutional Neural Network and hence, I imported the necessary libraries.

os.environ[‘KERAS_BACKEND’] = ‘tensorflow’ sets the keras backend as tensorflow and os.environ[‘TF_CPP_MIN_LOG_LEVEL’] = ‘3’ hides all tensorflow logs.

Import dataset

The csv file driver_imgs_list.csv contains the list of all training images, along with the reference to the person and the classname. The classname is a reference to the kind of activity the person in the image is doing.

Top 5 rows of the dataframe

Images overview

I decided to take a look at the dataset of images by displaying an image of each class. As the label classname is not descriptive, I decided to use a map to add title to each image.

The train folder has 10 folders, each corresponding to images of one class. I iterate through all folders and plot the first image from each folder. Using plt.subplot(5, 2, image_count), I define that there will be 10 images composed as 5 rows and 2 columns. The image_count defines the count of image being plot from a range of 1 to 10.

Drivers doing different things

Building the model

I’ll create a Convolutional Neural Network with 3 Conv2D layers (each followed by MaxPooling2D layer), 1 Flatten layer and 3 Dense layers. I finalised the neurons after conducting a few trials and taking cues from other kernels. Since this is a multi class problem, I have the last Dense layer with 10 neurons and the loss is identified using categorical_crossentropy.

Classifier model

Creating training data

Using ImageDataGenerator, I’ll increase the number of images I can train the model on. Also, I’ll use flow_from_directory method to read images corresponding to each class from its respective folder and create a training and validation split of 80% and 20%. Notice that I rescale each image by dividing all values by 255.

Now, I’ll train the model on this data and get the validation accuracy and loss.

Train the model

Using fit_generator, I’ll train the model.

The model achieved a Validation Accuracy of 97%.


Photo by Maxwell Ridgeway on Unsplash

Using Convolutional Neural Network, I was able to identify whenever a driver was distracted from driving with an accuracy of 97%. As a next step, we can improve the model further by increasing the complexity and layers of the CNN and realise its effect on the final output.