Training a Neural Network to Park a Car
- Last updated
- Reid Barber
Whenever we back our car into a parking space, we somehow know exactly how far to pull forward, how much to turn the steering wheel, and when to start reversing in order to come in at the perfect angle that lands us straight between the two lines.
This is difficult at first, but after some practice, we can do it from almost any starting position and angle.
I was curious if it was possible to train a neural network to do just this. Turns out you can! And you can do it with a model small enough to easily run in a web browser. Try it out in the demo above!
Below, I'll walk through how I went about training it.
Setup
The simulated lot is relatively simple in that there is one empty space, occupied spaces on both sides, and an aisle with another row of parked cars across from it. The car has the dimensions and turning limits of a standard midsize sedan.
Each attempt starts with the car anywhere within the aisle, pointed in any direction. To count as parked, the whole car has to end up inside the lines and centered, without touching anything along the way.
The network's view of the world is the four corners of the space's painted lines, measured from the car's point of view, plus its own steering angle and speed.
Approach 1: Reinforcement Learning
I thought a good first approach would be to use reinforcement learning to let the car try a bunch of actions and reward progress toward the parked position, while penalizing collisions.
I trained it with PPO in a simulator that ran 512 cars in parallel at about 65,000 simulated steps per second, with an extra bonus for actually parking.
It used a reverse curriculum where early episodes started with the car almost parked, and later ones gradually started farther away and at harder angles.
It quickly learned to reverse when it was already lined up, but it couldn't do much beyond that. Across the full range of starts, it succeeded only about 3–5% of the time.
After analyzing its moves, I noticed it wasn't driving forward at all.
Parking from an awkward angle usually means pulling forward and straightening out first, which takes several coordinated actions before it pays off. Random exploration almost never performed that sequence, and the reward for getting closer to the space likely discouraged driving away from it.
After tweaking the reward, I still wasn't able to get much improvement. Even a much better reward, based on the exact driving distance left to park (computed in the next section), only improved things slowly. So I decided to try a different approach.
Approach 2: Imitation Learning
The idea behind my second approach was to build a driver that parks perfectly using information the car wouldn't have, then train a small network to copy it.
That driver (the teacher) has full knowledge of the parking lot. I divided position and direction into a grid of roughly 1.4 million poses, then worked backward from the parked positions. At each pose, the car could move a short distance forward or backward at several steering angles.
The resulting table estimated how much driving remained before the car was parked. Gear changes were assigned an extra cost, so switching back and forth between forward and reverse was less desirable than a clean maneuver.
To drive, the teacher tried each of 36 steering and speed combinations, simulated half a second of each, and chose the one with the lowest remaining cost, skipping any that hit an obstacle. It made a new choice every tenth of a second, and it parked without contact in all 1,024 random starts tested.
The teacher parked every time, but it depended on a precomputed table for this exact lot and full knowledge of where everything was. What I wanted was a small neural network (student) that learns to imitate the teacher while seeing only the four corners of the target space, plus its own steering angle and speed. It chooses from those 36 commands 10 times a second.
Output commands
9 steering angles × 4 speeds = 36 (@ 10 Hz)
I used DAgger (dataset aggregation), where the student drove, with the teacher taking over some of the time early on, and the teacher labeled what the student should have done at each position it reached. Those examples went into the training data for the next round.
In the first round, the teacher did all of the driving. After that, its share halved each round until the student was driving almost entirely on its own, while the teacher still labeled every position the student reached. Over 12 rounds, this collected about 5 million labeled examples. To keep the student from relying on perfect measurements, the simulator added about 3 cm of random error to the corner positions during training.
The first student received 10 numbers: eight coordinates for the four corners, plus steering angle and speed. It reached a success rate of about 95%.
Those coordinates already contained the car's position and direction relative to the space. But extracting that relationship requires some geometry, and at that point, I was asking the network to learn that calculation along with the driving.
So I decided to add four computed values from the same corners: two describing position relative to the space and two describing orientation.
Input values
Example run (9.5 s in, reversing toward the space)
| Corner | Forward | Left |
|---|---|---|
| 1. Entrance, left | −5.2 m | 0.1 m |
| 2. Entrance, right | −3.7 m | −2.2 m |
| 3. Back, left | −9.8 m | −3.0 m |
| 4. Back, right | −8.3 m | −5.3 m |
All 14 values feed a small, fully connected network:
Neural network architecture
78,884 parameters, about 316 KB of weights (small enough to download with this page)
With those inputs, the student surpassed the earlier version after three rounds of training. On the same evaluation starts, the failure rate fell from 5.1% to 1.6%.
Training the second student
Share of the same 2,048 test starts parked after each DAgger round
The final evaluation used a larger set of 10,240 fresh starts in the same lot, with exact corner positions (no added error). The network parked without contact 98.1% of the time, taking 11.1 seconds and 1.1 gear changes on average.
Example results
Paths the final network drove from four test starts
A real system would also need to use vision to detect the painted lines and infer their position if they left the camera's view. Since this network only knows the one lot it trained in, it would also need to handle other layouts, plus moving obstacles like people and other cars.
This was a constrained environment, but nonetheless it was interesting to see how well a small neural network can perform at specific tasks with a relatively simple training run.