Wednesday, January 24, 2018

From Colors to Histograms to Neural Networks - Part 2

In my previous blog post, I explained the method I chose for representing big, uneven histograms that appear when the particle cloud weights at a timestep are divided into 10 bins.  I use the ratio of the tallest histogram bin divided by the 2nd tallest histogram bin.


I calculated this "Histogram Bin Ratio" (HBR) for each timestep and set up my datasets for an artificial neural network in Matlab. 

Experiment 1
My first trial had Inputs of the current timestep's HBR, and outputs of the difference in meters between the robot's ground-truth pose in the Gazebo simulator and the AMCL SLAM algorithm's estimate, for X, Y, and Heading. 

Experiment 1 Dataset:
Inputs:
Ratio (current)

Outputs:
|AMCL_X - Gazebo_X|
|AMCL_Y - Gazebo_Y|
|AMCL_Heading - Gazebo_Heading|

The neural network's outputs were hopelessly off, with MSE's on the scale of 300.


Side Study
I wrote a test for my neural network setup to see if it could be trained to learn a sine function.  I found a working example that used a different Matlab NN library, and ran the same datasets through my code.  My neural network function was able to match the expected results, so I'm confident that my algorithm should be able to learn the kidnapping pattern.


Experiment 2
I scrapped the idea of using the difference between AMCL and Gazebo as the NN outputs.  Instead, I used a 0/1 coding scheme of "not post-kidnapping event" or "post-kidnapping event".  Including all the data after the kidnapping event resulted in MSEs in the 200s.

Experiment 2 Dataset:
Inputs:
Ratio (current)

Outputs:
0 or 1 (kidnapped or not)


Experiment 3
My next change was to cut off the datasets immediately following the kidnaping event's visible effects on the HBR (histogram ratio).  After the kidnapping, the HBR jumps to 5 or 18 or 40, and hovers there for a few more timesteps. Once the HBR gets back to normal, I truncate the dataset.  This worked marginally better - MSEs of the NN performance in the 100s.

Experiment 3 Dataset:
Inputs:
Ratio(current)

Outputs:
0 or 1 (kidnapped or not) but killed the dataset after the kidnapping event.


Experiment 4
Next, I added a history of 2 timestamps to the data inputs, so that the HBRs of the current timestep, the previous and the 2nd previous timesteps were represented.  This drove my NN's MSEs down to 25!


Experiment 3 Dataset:
Inputs:
Ratio(current)
Prev_Ratio
Prev_Prev_Ratio

Outputs:
0 or 1 (kidnapped or not) but killed the dataset after the kidnapping event.




HOWEVER.  Now my NN output matches  the HBR ratios (current timestep's ratio), when I expected it - and theoretically, trained it - to give me a 0 or a 1! 


The goofy part is that I could threshold the NN output around 4, and say anything over that indicates a Kidnapping.  At this rate, I don't need a neural network!

From Colors to Histograms to Neural Networks - Part 1

My previous blog post was about noticing the unique teal coloring of the particle cloud weights following a Kidnap event.  In the time since, I have:

-Used histograms for representing the color change numerically
-Experimented with arranging the datasets to train the neural network
-Tested that my neural network *should* work by training it on a standard sine function
-Found a combination for the neural network that "works" (kinda).

Histograms:

The algorithm behind coloring the particles by weight is this:
1. Come up with a range of color representations.  There should be as many different colors as there are unique weights.
2. Sort the colors from dark to light, and sort the list of unique weights least to greatest.
3. Match each unique weight to a unique color.
4. Apply this mapping to assign a color to each weights in the dataset




I needed a way to numerically recreate the message that the teal color conveys.  I applied a rule and printed out the resulting histograms:

*At each timestep, divide up the data into 10 bins.  Always 10 bins.*

The histograms at each timestep during a "normal" trial looked like this, with some variation:


The histograms following a Kidnapping event and corresponding to the teal particle cloud looked like this:


The next step was preparing the data for the neural network.  I represented the dramatic histogram of the Kidnap event by creating a ratio of the tallest histogram bin at the timestep to the 2nd tallest histogram bin.  Ratios that indicate a Kidnap event are higher (5, 18, 40) while normal ratios are 1 or 2 or less.

Here's a plot of the ratios for the whole duration of the driving trials.  It's bad resolution, but take my word for it: the "sprinkle" outliers at ratios of 20 or above are from the kidnapping trials.  There are even 3 "normal" trials that appear in the outliers, that helped me identify unintentional kidnapping occurrences during these trials.






Note: after explaining the histogram method and showing that it works...I'm still not sure why it works, and why the method appears to work exclusively for kidnapping instances.