With Udacity's car simulator, machine learning enthusiasts can easily use the embedded image feed from the virtual car to collect data to train a self driving car model. My implementation of this idea incorporates research publications from Nvidia to construct the neural network, Keras as the machine learning framework (with Tensorflow backend), and Python in order to drive a car around a map.
I trained a network with 280,000 parameters. Specifics on implementation can be found from inline comments or in the Nvidia link provided. My final model is stored in models/best_model.h5
.
Here are short gifs that demonstrate the model's ability to maneuver the car around a race track. While there are no automated benchmarks for performance, I ran the network overnight and the car had never crashed.
![]() |
![]() |
---|
- Since the kind and number of maps within the simulator are limited, the model is not sufficiently robust as training data may not encapsulate edge cases.
- The car's velocity was not considered in the model construction. Therefore, the I fixed the autonomous vehicles velocity to 10 mph. The disadvantages are straightforward.
- Install Udacity's self driving car simulator here.
- Install Tensorflow and Keras
- Data Generation
- Open the simulator
- Select "Training Mode"
- Hold "r" to record, save output data in /data
- Use the
data_process.py
script to auto-generate data
- Model Training
- Activate tensorflow/keras
- Use the
model.py
script to train the model
- Autonomous Driving
- Open the simulator
- Select "Autonomous mode"
- Activate tensorflow/keras
- Use the
drive.py
script to drive the car in the simulator
This script is used to convert our image data to numpy arrays so that the Keras model can use the data properly. It also handles all data processing and generation.
Since the virtual car uses three digital cameras (left, center, and right), we can incorporate the left and right cameras in our model training by augmenting the steering wheel value by 0.25 and -0.25 respectively.
Additionally, we may reflect the image about the y-axis and multiply the steering angle by -1 in order to train our model on reflections. Since the default track is a circle that almost entirely uses left turns, this is valuable in the construction of our model in order to increase its robustness.
Some command-line arguments that are helpful in prototyping data augmentation or data processing configurations:
-si
: saves processed images so that researchers may see what the final input to the model looks like-s
: shuffles the data in the dataset to create robustness in our training.-f
: reflects the data about the y axis
Some notes for data pre_processing:
- Conversion of RGB to YUV is mandatory for more efficient color coding so that the CNN can easily differentiate gradients in the image.
- Image cropping decreases the complexity of the model needed and training time. Items in the image such as the sky or sides of the road are not as important as the road itself for training a self-driving car in this simulated environment.
I ultimately used python3 data_process.py -s -f
to create my dataset.
This script trains the convolutional neural network model. It is designed by Nvidia and the actual research paper is found here.
1 : Lambda layer for preprocessing
5 : Convolutional 2D Layers
1 : Dropout Layer (0.5 dropout)
1 : Flatten Layer
4 : Dense Layers
The model uses mean squared error for its loss function, the Adam optimizer, and a 0.0001 learning rate.
Run python3 model.py
to create and train your model. The trained model is saved as a .h5
file.
This script is provided by Udacity in order to operate the virtual vehicle. To run the model with parameters that I trained, use python3 drive.py models/best_model.h5