Self-Driving RC Car

This summer, upon completing the Machine Learning MOOC offered by Stanford University on Coursera, I wanted to enjoy the fruits of my labour through a side-project. I achieved this by recreating Hamuchiwa’s AutoRCCcar project and programming my own neural network using the python library Keras. Learning Keras was greatly simplified by reading the book “Deep Learning With Python”.

SDC Configuration(1).png

Overview

The Raspberry Pi is responsible for obtaining distance data from the sensors, and images from the PiCamera. The Raspberry Pi then sends this data to the Laptop using TCP/IP. At the laptop, image processing is done and the trained deep learning model makes a prediction on what button to press in the form of a one-hot-encoded array (e.g. [0 0 1] to move right). The laptop then sends instructions to the Arduino which is interfaced with the RC controller to send the signal to move.

controller.jpg

side_view.jpg

front.jpg

 

back_view.jpg

Deep Learning

Data Collection and Preprocessing

With the limitations of my RC car, training was a made a lot more difficult. The turning radius of the car prevented me from training on a circular track. Thus, I had to train on a linear track, resulting in having to manually pick up the car to bring it back to the staring line. By the end of the training, I had  gathered about 1000 samples, yet it still wasn’t enough. So, I used a trick I learned from the Machine Learning MOOC to generate more data by flipping all the images and labels in the x direction.

Building The Model

The next step was to actually build the Keras model. We first split the data into training and testing sets. Our model consists of 3 Dense layers, but we add some Dropout between layers to prevent the model from picking up random patterns. The final activation is a softmax function which is necessary for our multi-label classification problem.

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)

# define model
model = Sequential()
training_start = time.time()
print("Training data...")
model.add(Dense(100, input_dim=38400, init='uniform'))
model.add(Dropout(0.2))
model.add(Activation('relu'))
model.add(Dense(100, init='uniform'))
model.add(Dropout(0.2))
model.add(Activation('relu'))
model.add(Dense(3, init='uniform'))
model.add(Activation('softmax'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])

# Fit the model
history = model.fit(X_train, y_train,
          nb_epoch=20,
          batch_size=1000,
          validation_data=(X_test, y_test))


 

The final result is cross validation accuracy of 77% with loss under 0.3.

accuracy-epoch.png

loss.png

 

 

 

Chaotic Mapping: Arnold’s Cat Map

Chaos

Chaos is a branch of mathematics that deals with underlying patterns within dynamical systems that appear to behave randomly. Such behaviour is displayed in real world systems such as shuffling cards, cardiac arrhythmia, planetary orbits, and fractals in nature. [1] This post is based on a type of chaotic mapping of an image.

Arnold’s Cat Map

My Implementation In Python

Arnold’s cat map is a transformation which seems to distort an image into random patterns. The transformation, \Gamma : \mathbb{T}^2 \to \mathbb{T}^2 of an n x n image in a unit square is:

\Gamma \, : \, (x,y) \to (2x+y,x+y) \bmod 1.

And in matrix notation:

\Gamma \left( \begin{bmatrix} x \\ y \end{bmatrix} \right) = \begin{bmatrix} 2 & 1 \\ 1 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} \bmod 1 = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} \bmod 1.

This applies a shear in the x-direction by a factor of 1, and then a shear in the y-direction by a factor of 1, and shift of all the parts outside the unit square back into it.

*The mod 1 returns a value between 0 and 1.

https://image.slidesharecdn.com/c15e216c-49e3-4664-98c7-bfcbdb3b5a06-150601160933-lva1-app6891/95/digital-watermarking-using-different-chaotic-equations-14-638.jpg?cb=1433175230

The Period of The Pixel Map

The underlying pattern of this mapping is that after a number of transformations, the image cycles back into it’s original state. There is no single function that shows the correlation between the side length p (in pixels), and the period Π (the number of iterations it takes to return back to the original image). However, the following observations can be made [1]:

Π(p) = 3p if and only if p = 2 · 5k for k = 1, 2,….
Π(p) = 2p if and only if p = 5k for k = 1, 2,… or
Π(p) = 6 · 5k for k = 0, 1, 2,….
Π(p) ≤ 12p/7 for all other choices of p

dexter.gif

Works Cited

[1] H. Anton and C. Rorres, Elementary linear algebra, 11th ed. Wiley, 2013, pp. 678-689.