Kaggle: Lyft Motion Prediction for Autonomous Vehicles
Tags: machinelearning, deeplearning, datascience
Published: 2020-11-30 02:16:26

In this Kaggle competition, I built motion prediction models for self-driving vehicles to predict how cars, cyclists, and pedestrians move in the autonomous vehicles (AV’s) environment, with the support of the largest [Prediction Dataset](https://self-driving.lyft.com/level5/prediction/) [1] ever released to train and test the models.

# Competition Description

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0d7nvihnhqdanph97hoi.jpg)

In summary, the goal of this competition is to predict other car / cyclist / pedestrian (called “agent”)’s motion in the next 5 seconds by using past frames based on the view of the AV’s views. A raster generates a bird’s eye view (BEV) top-down raster, which encodes all agents and the map. The network infers the future coordinates of the agent-based upon this raster.

---

# Lyft Level 5 Prediction Dataset

![Image from the paper: One Thousand and One Hours: Self-driving Motion Prediction Dataset
](https://dev-to-uploads.s3.amazonaws.com/i/w3p4wgjmmy5bzk3mr5kx.png)

The dataset was collected along a fixed route in Palo Alto, California. It consists of 170,000 scenes capturing the environment around the autonomous vehicle. Each scene encodes the state of the vehicle’s surroundings at a given point in time.

The dataset consists of frames and agent states. A frame is a snapshot in time, consisting of ego pose, time, and multiple agent states. Each agent state describes the position, orientation, bounds, and type.

![data format](https://dev-to-uploads.s3.amazonaws.com/i/4vryz1qloc9oqfxmx52d.png)

A detailed exploratory data analysis is available in this [Jupiter Notebook](https://hirotaka-ishihara.netlify.app/project/lyft/lyft-first-data-exploration.html).

---

# Evaluation & Score

> This is a brief summary of the evaluation, please refer to the [metrics page in the L5Kit repository](https://github.com/lyft/l5kit/blob/master/competition.md).

After the positions of a trajectory are predicted, a negative log-likelihood of the ground truth data given these multi-modal predictions is calculated. Assume the ground truth positions of a sample trajectory are

![ground truth](https://dev-to-uploads.s3.amazonaws.com/i/eknant15udr1hnc3reas.gif)

and the predicted K hypotheses, represented by means

![hypothesis](https://dev-to-uploads.s3.amazonaws.com/i/k65qda34niomc9cj7oe8.gif)

for each hypothesis, the model also generates a confidence value c (i.e. for a single model, the value c is 1). Assume the ground truth positions to be modeled by a mixture of multi-dimensional independent Normal distributions over time. The goal is to maximize the following likelihood

![likelihood](https://dev-to-uploads.s3.amazonaws.com/i/3bgvddy2tev0rr1y51o7.gif)

As for yielding the loss, we simply take the log and take the negative of the likelihood equation.

![loss](https://dev-to-uploads.s3.amazonaws.com/i/zbjcjxne2smuppg7jgk1.gif)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8t89vruy09xeskxodiaw.gif)

For numeral stability (preventing the condition of underflow caused by an extremely small value), the [log-sum-exp trick](https://en.wikipedia.org/wiki/LogSumExp#log-sum-exp_trick_for_log-domain_calculations) is applied to the equation.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p8wj2e9m4o1uwgnb7mwz.gif)

A huge thanks to the competition host for providing the [implementation](https://github.com/lyft/l5kit/blob/20ab033c01610d711c3d36e1963ecec86e8b85b6/l5kit/l5kit/evaluation/metrics.py) of this loss function.

---

# Image Raster & Pixel Size Selection

In general, it is less feasible to implement and train state-of-art models for motion prediction in just 2 months. Instead, playing with the input data and applying feature engineering to them wisely is the key to winning the competition.

In this competition, the key factors for gaining higher accuracy are the image raster size the pixel size.

- raster size: the image size in pixel
- pixel size: spatial resolution (meters/pixel)

![raster size](https://dev-to-uploads.s3.amazonaws.com/i/rhke93c9fxsidt8njcn5.png)

For a fixed pixel size, a larger raster size means more surrounding information. In the meantime, it also means a longer training period (more computation).

![pixel size](https://dev-to-uploads.s3.amazonaws.com/i/ixys1650o4d7iwn7qspu.png)

For a fixed raster size, a smaller pixel size means higher resolution.

---

# Models

I’ve tried several baseline models. Each model has been trained for roughly 3 days on a Tesla V100 GPU. The following is a summary of the performance.

![models](https://dev-to-uploads.s3.amazonaws.com/i/fchvkz36l7zrmsiysv1f.png)

It turns out that more layers are not necessarily mean better scores. The trade-off between model/input size and the training speed further restricts my model choices. I decided to explore deeper with the models ResNet18 and ResNet34.

---

# Ensemble

In the end, I finished the competition with a score of 19.02 in the private leaderboard and 18.938 in the public leaderboard, ranked at 94/937.

> The private leaderboard is calculated with approximately 50% of the test data.

The ensembling improved my score (the best single model score) from 19.823 to 18.938 with the following models, the ensemble weights are based on their scores:

> [(sum score — individual score) / (sum score * 3)]

- ResNet34 / Raster Size 512 / Pixel Size 0.2
- ResNet34 / Raster Size 350 / Pixel Size 0.4
- ResNet18 / Raster Size 512 / Pixel Size 0.2
- ResNet18 / Raster Size 448 / Pixel Size 0.3

---

# Summary of Some Great Insights from the Top Rankers

- Ensemble with Gaussian Mixture Model (GMM).

> At the final ensemble stage, GMM with 3 components was used to fit the multiple trajectory positions generated by the trained models.

- Rasterizing based on the agent’s speed.

> Slow agent speed means the prediction only needs a small raster size (since the vehicle probably won’t travel too far in the next 5 seconds). In the meantime, the “slow model” increases the number of history frames to increase accuracy. Similarly, the “fast model” increases the raster size and reduces the frames.

- Extract meta-data from the Agent-Dataset

> There is some useful information in the agent dataset, such as centroid, rotation, velocity, etc. A second head with two fully connected layers could be used to encode this information, and then concatenate the output vector to the output of the ResNet pooling layer.

---

# My Experiments that Didn’t Work

- Lane encoder

> I separately trained a Conv2d-Autoencoder on the semantic lane channels and then fine-tuned with the ResNet model.

- GRU over time-stacked models

- Adding more layers on top of the ResNet head

- Graph Convolutional Network on the lane nodes

> I treated each 4x4 grid in the lane map as a single node and set up the adjacency matrix based on their pixel values. (e.g., a 4x4 grid has a maximum of 8 neighbors, exclude the neighbor if the value is below a threshold, which means no lane).

---

# Conclusion

This is my first Kaggle competition experience. It’s really encouraging to see my effort in the past three months earned a bronze medal. Also, I saw many novel usages of traditional machine learning techniques that showed their outstanding performance. My journey in Kaggle has just started.

---

# Reference

[1] Houston, J. and Zuidhof, G. and Bergamini, L. and Ye, Y. and Jain, A. and Omari, S. and Iglovikov, V. and Ondruska, P., One Thousand and One Hours: Self-driving Motion Prediction Dataset, [arXiv:2006.14480v2](https://arxiv.org/abs/2006.14480v2), 2020