Recording a Labelled Driving Dataset
Why self-driving teams use simulated data, what labels a perception model needs, recording your own dataset in the simulator with weather and steering variety, and loading it in Python. Every model you train in Part 2 is trained on data you record here.
Softmaxima Simulator StudioTry this topic's code in the Studio: it opens with the camera and trained networks project, ready to run.OpenIn plain words
Machine learning learns from examples, so the examples decide what it learns. Our simulator records each camera frame together with the right answers (where the lane is, what each pixel shows, where every car is), so you can build a dataset in minutes instead of months.
By the end of this topic you will be able to record a labelled driving dataset and check it is good before training on it.
Start with: the demo Try it: record your dataset.
Data is the hard part
A perception model is only as good as its labelled data. Labelling real driving images is slow and expensive: drawing an outline around every car, person and lane in a single image can take a person over an hour. Datasets with real labels exist, but many of the best known allow non-commercial use only.
A simulator gives labels for free and perfectly, because it already knows what every pixel is. Self-driving companies generate huge amounts of synthetic data, especially for rare events. The catch is the domain gap: a model trained only on simulated images can fail on real ones, because real images have different textures, lighting, noise and lens effects. Teams close the gap with more realistic rendering, by randomising the simulation (domain randomisation) so the model cannot rely on any one look, and by fine-tuning on a smaller set of real images. You will see the same effect between our day and night images.
What our simulator records
| Label | Shape | Used to train |
|---|---|---|
| images: the camera frame | N × H × W × 3 | everything |
| seg: the class of every pixel (own lane, other lane, lane marking, vehicle, pedestrian, traffic light, background) | N × H × W | the U-Net for lanes and drivable area |
| depth: distance to every pixel, in 0.25 m steps | N × H × W | the depth-from-one-camera network |
| boxes: [frame, class, x1, y1, x2, y2, state] for each car, pedestrian and traffic light; state is a traffic light's own colour | M × 7 | the object detector and the traffic-light classifier |
| light: state of the next traffic light (none, red, amber, green) | N | the traffic-light classifier |
| steer, speed: what the autopilot did | N | driving by imitation |
| cte, lane10: the true position of the lane | N | checking lane finding |
Two details matter for imitation learning. First, the autopilot sometimes adds short steering wobbles while recording, but the recorded steer is always its clean command, so the data teaches how to recover from mistakes, not only how to drive perfectly. Second, frames are saved five times a second rather than thirty, because neighbouring frames are almost identical and add little.
Try it: record your dataset
Choose a track, the number of frames, the image size and the weather, then press Record. The file downloads to your computer: keep it for the rest of Part 2. Record more than once, on different tracks and in different weather, to get more varied data. A 500-frame dataset at 120 × 240 is about 75 MB.
Interactive: this part runs in your browser.
Code: load and look at your data
Open a new notebook in Google Colab ↗ (free), paste this in and run it. It asks you to upload your file, then shows one frame with its boxes, segmentation and depth.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
# In Google Colab: upload the .npz file you recorded in the simulator
from google.colab import files
d = np.load(next(iter(files.upload())))
print({k: d[k].shape for k in d.files if k != "README.txt"})
i = 20 # any frame number
img, seg, depth = d["images"][i], d["seg"][i], d["depth"][i]
boxes = d["boxes"][d["boxes"][:, 0] == i]
PALETTE = np.array([[20, 20, 30], [60, 160, 255], [130, 90, 220], [255, 230, 60],
[255, 80, 80], [80, 230, 120], [255, 150, 30]], dtype=np.uint8)
NAMES = ["car", "pedestrian", "traffic light"]
fig, ax = plt.subplots(1, 3, figsize=(15, 3.5))
ax[0].imshow(img)
for _, cls, x1, y1, x2, y2, _ in boxes:
ax[0].add_patch(patches.Rectangle((x1, y1), x2 - x1, y2 - y1, fill=False, color="red"))
ax[0].text(x1, y1 - 2, NAMES[cls], color="red", fontsize=8)
ax[0].set_title(f"camera, steering {d['steer'][i]:+.3f} rad")
ax[1].imshow(PALETTE[seg]); ax[1].set_title("segmentation labels")
metres = np.where(depth == 255, np.nan, depth * 0.25)
ax[2].imshow(metres, cmap="magma_r"); ax[2].set_title("depth (m)")
for a in ax: a.axis("off")
plt.show()
print("light states:", np.bincount(d["light"], minlength=4), "(none, red, amber, green)")Check your data before you train
- Look at many frames, not one: scroll through 50 at random. Most data bugs are obvious once you look.
- Count the classes: how many red, amber and green lights, how many pedestrians? Rare classes need more recording or extra weight in the loss.
- Split by drive, not by frame: frames from the same few seconds are nearly identical. If some go to training and their neighbours to testing, your test score will be too good. Keep whole recordings (or whole tracks) for validation and testing, the same leakage rule you met in the Machine Learning course.
- Keep a test set the model never sees, ideally a track or weather it was not trained on, to measure how well it generalises.
Quiz
1. What is the "domain gap"?
2. Why does the recorder add steering wobbles but save the clean steering command?
3. You split frames randomly into training and test sets. What goes wrong?
📚 Further reading
Trusted, mostly free sources: official documentation, well-known textbooks and courses. When you want to confirm or go deeper on anything here, start with these.
- Tobin et al. (2017): Domain Randomization for Transferring Deep Neural Networks from Simulation to the Real World ↗ the paper behind randomising simulations
- CARLA: an open-source driving simulator ↗ the research simulator used by many self-driving courses and papers