Reader small image

You're reading from  Applied Deep Learning and Computer Vision for Self-Driving Cars

Product typeBook
Published inAug 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781838646301
Edition1st Edition
Languages
Right arrow
Authors (2):
Sumit Ranjan
Sumit Ranjan
author image
Sumit Ranjan

Sumit Ranjan is a silver medalist in his Bachelor of Technology (Electronics and Telecommunication) degree. He is a passionate data scientist who has worked on solving business problems to build an unparalleled customer experience across domains such as, automobile, healthcare, semi-conductor, cloud-virtualization, and insurance. He is experienced in building applied machine learning, computer vision, and deep learning solutions, to meet real-world needs. He was awarded Autonomous Self-Driving Car Scholar by KPIT Technologies. He has also worked on multiple research projects at Mercedes Benz Research and Development. Apart from work, his hobbies are traveling and exploring new places, wildlife photography, and blogging.
Read more about Sumit Ranjan

Dr. S. Senthamilarasu
Dr. S. Senthamilarasu
author image
Dr. S. Senthamilarasu

Dr. S. Senthamilarasu was born and raised in the Coimbatore, Tamil Nadu. He is a technologist, designer, speaker, storyteller, journal reviewer educator, and researcher. He loves to learn new technologies and solves real world problems in the IT industry. He has published various journals and research papers and has presented at various international conferences. His research areas include data mining, image processing, and neural network. He loves reading Tamil novels and involves himself in social activities. He has also received silver medals in international exhibitions for his research products for children with an autism disorder. He currently lives in Bangalore and is working closely with lead clients.
Read more about Dr. S. Senthamilarasu

View More author details
Right arrow
Behavioral Cloning Using Deep Learning

Behavioral cloning is a means of identifying and reproducing human sub-cognitive abilities within a computer program. For example, a person performs tasks such as driving a vehicle, recording their actions along with the situation, and subsequently feeding the log of these records into the input of the learning algorithm. Then, the learning algorithm generates a series of rules replicating the behaviors of humans, which is called behavioral cloning. This approach can be used to construct automatic control systems for complex tasks where traditional control theory is inadequate. 

In this chapter, we are going to implement behavioral cloning. Here, we need to train a neural network to predict the steering angles of a vehicle based on input images from different cameras. In this chapter we are going to drive a car autonomously...

Neural network for regression

So far, we have only learned about neural networks for classification problems. But in this chapter, we will learn about one of the most important topics surrounding behavioral cloning: neural network for regression. The main aim of classification problems is to group the classes of objects into categories and predict the category of the class later. For example, cat and dog can be two different classes.

Now, we will look into a regression type example, where we will use linear regression to predict continuous values based on the relationship between the independent variable (X) and the dependent variable (y). In general, linear regression helps us predict values on a continuous spectrum, rather than on discrete classes.

The following is a linear regression plot:

Fig 10.1: Linear regression plot

Linear regression is the simplest form of regression. The goal is to find the line parameterized by the set of slope and y-intercept...

Behavior cloning using deep learning

This section will focus on a very useful technique called behavioral cloning. This chapter is relatively intense and will combine all the previous techniques we have dealt with in this book, such as deep learning, feature extraction from images, CNNs, and continuous regression. 

We are going to follow these steps:

  1. Download an open source SDC simulator by Udacity.
  2. Collect the training data by driving the car in manual mode in the simulator. The training data consists of images from the surrounding environment of the car and the steering angles.
  3. Clean the collected dataset using various OpenCV techniques.
  4. Train a convolution neural network model.
  5. Evaluate the model in Autonomous mode of the Udacity simulator.

This project isn't easy as it requires complex deep learning techniques and image preprocessing techniques. For this reason, I have structured this book so that you have all the necessary skills to complete...

Data collection

In this section, we will download the simulator that will enable us to begin our behavioral cloning process. We're going to start by driving the car through the simulator using our keyboard keys. That way, we're able to train a convolutional neural network to monitor the controlled operation and movement of the vehicle. Depending on how you're driving, it copies your behavior to then drive on its own in Autonomous mode. How well the neural network drives the car is determined by how well you're able to drive the car in the simulator.

We'll be using the following simulator, which is open source and available on GitHub. You can find it at https://github.com/udacity/self-driving-car-simThere are other simulators that you can make use of, such as AirSim (https://github.com/microsoft/AirSim), which is another open source SDC simulator that's based on Unreal Engine.

We have two versions of the simulator, but we will go with...

Data preparation

In this section, we are going to perform data preparation. We have to prepare the data before running a deep learning model. 

We will start by exploring the data. Later, we will apply the computer vision techniques that we studied in Chapter 4, Computer Vision for Self-Driving Cars. Let's get started:

  1. First, add the necessary libraries, including numpy, pandas, and keras, as shown in the following code block: 
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.models import Sequential
from keras.optimizers import Adam
from keras.layers import Convolution2D, MaxPooling2D, Dropout, Flatten, Dense
import cv2
import pandas as pd
import random
import os
import ntpath
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
import matplotlib.image as mpimg
from imgaug import augmenters as iaa
  1. The next step is to initialize the folder name where the training images and driving logs will be stored, as well...

Model development

In this section, we will design our model architecture. We are going to use the model architecture provided by NVIDIA. 

  1. The code for developing the NVIDIA architecture for behavioral cloning can be seen in the following code block. Here we are going to use ADAM as optimizer, loss as MSE as output is steering angle and it is a regression problem. Also Exponential Linear Unit (ELU) is used as activation function. ELU is better than ReLU as it reduces cost function faster to zero. ELU is more accurate and good at solving vanishing gradient problem.You can read more about ELU at, https://arxiv.org/abs/1511.07289Let's get started:
 def nvidia_model():
model = tf.keras.Sequential()
model.add(tf.keras.layers.Convolution2D(24, 5, 5, subsample=(2, 2), input_shape=(66, 200, 3), activation='elu'))
model.add(tf.keras.layers.Convolution2D(36, 5, 5, subsample=(2, 2), activation='elu'))
model.add(tf.keras.layers.Convolution2D...

Evaluating the simulator

The final step is to check how our model is performing. To validate the simulator, we have to run the model in Autonomous mode. To run the model in Autonomous mode, we have to write a script that will set up bidirectional client-server communication and connect the model to the simulator. An example of this can be seen in the following diagram:

Fig 10.24: Bidirectional client-server communication

We have to run the following code to establish a connection to the simulator. This section is not related to deep learning; it is just about connecting to the simulator:

  1. First, we will import the required libraries, including socketio, eventlet, numpy, and OpenCV:
import socketio
import eventlet
import numpy as np
from flask import Flask
from keras.models import load_model
import base64
from io import BytesIO
from PIL import Image
import cv2
  1. Next, we will connect to the socket: 
sio = socketio.Server()

app = Flask(__name__) #'__main__&apos...

Summary

In this chapter, we learned about behavioral cloning. We downloaded the Udacity open source simulator, ran the car in manual mode, and collected the data. Later, we cloned the model using the deep learning architecture provided by NVIDIA. We also applied various computer vision techniques as part of our data preparation process. In the end, our autonomous car drove smoothly. We hope you enjoyed building an autonomous car!

In the next chapter, we will learn about object detection. We are going to use a popular pre-trained model called YOLO and implement a deep learning project for vehicle detection.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Applied Deep Learning and Computer Vision for Self-Driving Cars
Published in: Aug 2020Publisher: PacktISBN-13: 9781838646301
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (2)

author image
Sumit Ranjan

Sumit Ranjan is a silver medalist in his Bachelor of Technology (Electronics and Telecommunication) degree. He is a passionate data scientist who has worked on solving business problems to build an unparalleled customer experience across domains such as, automobile, healthcare, semi-conductor, cloud-virtualization, and insurance. He is experienced in building applied machine learning, computer vision, and deep learning solutions, to meet real-world needs. He was awarded Autonomous Self-Driving Car Scholar by KPIT Technologies. He has also worked on multiple research projects at Mercedes Benz Research and Development. Apart from work, his hobbies are traveling and exploring new places, wildlife photography, and blogging.
Read more about Sumit Ranjan

author image
Dr. S. Senthamilarasu

Dr. S. Senthamilarasu was born and raised in the Coimbatore, Tamil Nadu. He is a technologist, designer, speaker, storyteller, journal reviewer educator, and researcher. He loves to learn new technologies and solves real world problems in the IT industry. He has published various journals and research papers and has presented at various international conferences. His research areas include data mining, image processing, and neural network. He loves reading Tamil novels and involves himself in social activities. He has also received silver medals in international exhibitions for his research products for children with an autism disorder. He currently lives in Bangalore and is working closely with lead clients.
Read more about Dr. S. Senthamilarasu