Scaling robotics datasets with video encoding
aliberts, cadene, mfarre
Over the past few years, text and image-based models have seen dramatic performance improvements, primarily due to scaling up model weights and dataset sizes. While the internet provides an extensive database of text and images for LLMs and image generation models, robotics lacks such a vast and diverse qualitative data source and efficient data formats. Despite efforts like [Open X](https://robotics-transformer-x.github.io/), we are still far from achieving the scale and diversity seen with Large Language Models. Additionally, we lack the necessary tools for this endeavor, such as dataset formats that are lightweight, fast to load from, easy to share and visualize online. This gap is what [🤗 LeRobot](https://github.com/huggingface/lerobot) aims to address.

## What's a dataset in robotics?

In their general form — at least the one we are interested in within an end-to-end learning framework — robotics datasets typically come in two modalities: the visual modality and the robot's proprioception / goal positions modality (state/action vectors). Here's what this can look like in practice:

<center>
    <iframe 
        width="560" 
        height="315" 
        src="https://www.youtube.com/embed/69rEK7eSBAk?si=fE4Z2Ax6pP3vazUH"
        title="Aloha static battery video player"
        frameborder="0"
        allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
        referrerpolicy="strict-origin-when-cross-origin" allowfullscreen>
    </iframe>
</center>

Until now, the best way to store visual modality was PNG for individual frames. This is very redundant as there's a lot of repetition among the frames. Practitioners did not use videos because the loading times could be orders of magnitude above. These datasets are usually released in various formats from academic papers (hdf5, zarr, pickle, tar, zip...). These days, modern video codecs can achieve impressive compression ratios — meaning the size of the encoded video compared to the original uncompressed frames — while still preserving excellent quality. This means that with a compression ratio of 1:20, or 5% for instance (which is easily achievable), you get from a 20GB dataset down to a single GB of data. Because of this, we decided to use video encoding to store the visual modalities of our datasets.

## Contribution

We propose a `LeRobotDataset` format that is simple, lightweight, easy to share (with native integration to the hub) and easy to visualize.
Our datasets are on average 14% the size their original version (reaching up to 0.2% in the best case) while preserving full training capabilities on them by maintaining a very good level of quality. Additionally, we observed decoding times of video frames to follow this pattern, depending on resolution:
- In the nominal case where we're decoding a single frame, our loading time is comparable to that of loading the frame from a compressed image (png).
- In the advantageous case where we're decoding multiple successive frames, our loading time is 25%-50% that of loading those frames from compressed images.

On top of this, we're building tools to easily understand and browse these datasets.
You can explore a few examples yourself in the following Spaces using our visualization tool (click the images):

<div style="display: flex; align-items: center; justify-content: space-around;">
    <div style="position: relative; text-align: center;">
        <p style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); 
                  background-color: rgba(0, 0, 0, 0.6); color: white; padding: 5px 10px; 
                  border-radius: 5px; font-weight: bold; font-size: 1.1em;">
            aliberts/koch_tutorial
        </p>
        <a href="https://cadene-visualize-dataset-train.hf.space" target="_blank">
            <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/visualize_lego.png" alt="visualize_lego" style="width: 95%;">
        </a>
    </div>
    <div style="position: relative; text-align: center;">
        <p style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); 
                  background-color: rgba(0, 0, 0, 0.6); color: white; padding: 5px 10px; 
                  border-radius: 5px; font-weight: bold; font-size: 1.1em;">
            cadene/koch_bimanual_folding
        </p>
        <a href="https://cadene-visualize-dataset-koch-bimanual-folding.hf.space" target="_blank">
            <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/visualize_fold.png" alt="visualize_fold" style="width: 95%;">
        </a>
    </div>
</div>

## But what is a codec? And what is video encoding & decoding actually doing?

<center>
    <iframe 
        width="560" 
        height="315" 
        src="https://www.youtube.com/embed/7YQ1mikDhIo?si=YRx_Rlq0c3pjJYAm"
        title="Video Codecs 101 video player"
        frameborder="0"
        allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
        referrerpolicy="strict-origin-when-cross-origin" allowfullscreen>
    </iframe>
</center>

At its core, video encoding reduces the size of videos by using mainly 2 ideas:

- **Spatial Compression:** This is the same principle used in a compressed image like JPEG or PNG. Spatial compression uses the self-similarities of an image to reduce its size. For instance, a single frame of a video showing a blue sky will have large areas of similar color. Spatial compression takes advantage of this to compress these areas without losing much in quality.

- **Temporal Compression:** Rather than storing each frame *as is*, which takes up a lot of space, temporal compression calculates the differences between each frame and keeps only those differences (which are generally much smaller) in the encoded video stream. At decoding time, each frame is reconstructed by applying those differences back. Of course, this approach requires at least one frame of reference to start computing these differences with. In practice though, we use more than one placed at regular intervals. There are several reasons for this, which are detailed in [this article](https://aws.amazon.com/blogs/media/part-1-back-to-basics-gops-explained/). These "reference frames" are called keyframes or I-frames (for Intra-coded frames).

Thanks to these 2 ideas, video encoding is able to reduce the size of videos down to something manageable. Knowing this, the encoding process roughly looks like this:
1. Keyframes are determined based on user's specifications and scenes changes.
2. Those keyframes are compressed spatially.
3. The frames in-between are then compressed temporally as "differences" (also called P-frames or B-frames, more on these in the article linked above).
4. These differences themselves are then compressed spatially.
5. This compressed data from I-frames, P-frames and B-frames is encoded into a bitstream.
6. That video bitstream is then packaged into a container format (MP4, MKV, AVI...) along with potentially other bitstreams (audio, subtitles) and metadata.
7. At this point, additional processing may be applied to reduce any visual distortions caused by compression and to ensure the overall video quality meets desired standards.

Obviously, this is a high-level summary of what's happening and there are a lot of moving parts and configuration choices to make in this process. Logically, we wanted to evaluate the best way of doing it given our needs and constraints, so we built a [benchmark](https://github.com/huggingface/lerobot/tree/main/benchmarks/video) to assess this according to a number of criteria.

## Criteria

While size was the initial reason we decided to go with video encoding, we soon realized that there were other aspects to consider as well. Of course, decoding time is an important one for machine learning applications as we want to maximize the amount of time spent training rather than loading data. Quality needs to remains above a certain level as well so as to not degrade our policies' performances. Lastly, one less obvious but equally important aspect is the compatibility of our encoded videos in order to be easily decoded and played on the majority of media player, web browser, devices etc. Having the ability to easily and quickly visualize the content of any of our datasets was a must-have feature for us.

To summarize, these are the criteria we wanted to optimize:
- **Size:** Impacts storage disk space and download times.
- **Decoding time:** Impacts training time.
- **Quality:** Impacts training accuracy.
- **Compatibility:** Impacts the ability to easily decode the video and visualize it across devices and platforms.

Obviously, some of these criteria are in direct contradiction: you can hardly e.g. reduce the file size without degrading quality and vice versa. The goal was therefore to find the best compromise overall.

Note that because of our specific use case and our needs, some encoding settings traditionally used for media consumption don't really apply to us. A good example of that is with [GOP](https://en.wikipedia.org/wiki/Group_of_pictures) (Group of Pictures) size. More on that in a bit.

## Metrics

Given those criteria, we chose metrics accordingly.

- **Size compression ratio (lower is better)**: as mentioned, this is the size of the encoded video over the size of its set of original, unencoded frames.

- **Load times ratio (lower is better)**: this is the time it take to decode a given frame from a video over the time it takes to load that frame from an individual image.

For quality, we looked at 3 commonly used metrics:

- **[Average Mean Square Error](https://en.wikipedia.org/wiki/Mean_squared_error) (lower is better):** the average mean square error between each decoded frame and its corresponding original image over all requested timestamps, and also divided by the number of pixels in the image to be comparable across different image sizes.

- **[Average Peak Signal to Noise Ratio](https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio) (higher is better):** measures the ratio between the maximum possible power of a signal and the power of corrupting noise that affects the fidelity of its representation. Higher PSNR indicates better quality.

- **[Average Structural Similarity Index Measure](https://en.wikipedia.org/wiki/Structural_similarity_index_measure) (higher is better):** evaluates the perceived quality of images by comparing luminance, contrast, and structure. SSIM values range from -1 to 1, where 1 indicates perfect similarity.

Additionally, we tried various levels of encoding quality to get a sense of what these metrics translate to visually. However, video encoding is designed to appeal to the human eye by taking advantage of several principles of how the human visual perception works, tricking our brains to maintain a level of perceived quality. This might have a different impact on a neural net. Therefore, besides these metrics and a visual check, it was important for us to also validate that the encoding did not degrade our policies performance by A/B testing it.

For compatibility, we don't have a metric *per se*, but it basically boils down to the video codec and the pixel format. For the video codec, the three that we chose (h264, h265 and AV1) are common and don't pose an issue. However, the pixel format is important as well and we found afterwards that on most browsers for instance, `yuv444p` is not supported and the video can't be decoded.

## Variables

#### Image content & size
We don't expect the same optimal settings for a dataset of images from a simulation, or from the real world in an apartment, or in a factory, or outdoor, or with lots of moving objects in the scene, etc. Similarly, loading times might not vary linearly with the image size (resolution).
For these reasons, we ran this benchmark on four representative datasets:
- `lerobot/pusht_image`: (96 x 96 pixels) simulation with simple geometric shapes, fixed camera.
- `aliberts/aloha_mobile_shrimp_image`: (480 x 640 pixels) real-world indoor, moving camera.
- `aliberts/paris_street`: (720 x 1280 pixels) real-world outdoor, moving camera.
- `aliberts/kitchen`: (1080 x 1920 pixels) real-world indoor, fixed camera.

### Encoding parameters

We used [FFmpeg](https://www.ffmpeg.org/) for encoding our videos. Here are the main parameters we played with:

#### Video Codec (`vcodec`)
The [codec](https://en.wikipedia.org/wiki/Video_codec) (**co**der-**dec**oder) is the algorithmic engine that's driving the video encoding. The codec defines a format used for encoding and decoding. Note that for a given codec, several implementations may exist. For example for AV1: `libaom` (official implementation), `libsvtav1` (faster, encoder only), `libdav1d` (decoder only).

Note that the rest of the encoding parameters are interpreted differently depending on the video codec used. In other words, the same `crf` value used with one codec doesn't necessarily translate into the same compression level with another codec. In fact, the default value (`None`) isn't the same amongst the different video codecs. Importantly, it is also the case for many other ffmpeg arguments like `g` which specifies the frequency of the key frames.

#### Pixel Format (`pix_fmt`)
Pixel format specifies both the [color space](https://en.wikipedia.org/wiki/Color_space) (YUV, RGB, Grayscale) and, for YUV color space, the [chroma subsampling](https://en.wikipedia.org/wiki/Chroma_subsampling) which determines the way chrominance (color information) and luminance (brightness information) are actually stored in the resulting encoded bitstream. For instance, `yuv420p` indicates YUV color space with 4:2:0 chroma subsampling. This is the most common format for web video and standard playback. For RGB color space, this parameter specifies the number of bits per pixel (e.g. `rbg24` means RGB color space with 24 bits per pixel).

#### Group of Pictures size (`g`)
[GOP](https://en.wikipedia.org/wiki/Group_of_pictures) (Group of Pictures) size determines how frequently keyframes are placed throughout the encoded bitstream. The lower that value is, the more frequently keyframes are placed. One key thing to understand is that when requesting a frame at a given timestamp, unless that frame happens to be a keyframe itself, the decoder will look for the last previous keyframe before that timestamp and will need to decode each subsequent frame up to the requested timestamp. This means that increasing GOP size will increase the average decoding time of a frame as fewer keyframes are available to start from. For a typical online content such as a video on Youtube or a movie on Netflix, a keyframe placed every 2 to 4 seconds of the video — 2s corresponding to a GOP size of 48 for a 24 fps video — will generally translate to a smooth viewer experience as this makes loading time acceptable for that use case (depending on hardware). For training a policy however, we need access to any frame as fast as possible meaning that we'll probably need a much lower value of GOP.

#### Constant Rate Factor (`crf`)
The constant rate factor represent the amount of lossy compression applied. A value of 0 means that no information is lost while a high value (around 50-60 depending on the codec used) is very lossy.
Using this parameter rather than specifying a target bitrate is [preferable](https://www.dr-lex.be/info-stuff/videotips.html#bitrate) since it allows to aim for a constant visual quality level with a potentially variable bitrate rather than the opposite.

<center>
<table>
    <thead>
        <tr>
            <th>crf</th>
            <th>libx264</th>
            <th>libx265</th>
            <th>libsvtav1</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><code>10</code></td>
            <td><a href="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libx264_yuv420p_2_10.png" target="_blank">
                <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libx264_yuv420p_2_10.png" alt="libx264_yuv420p_2_10" style="width: 100%;">
            </a></td>
            <td><a href="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libx265_yuv420p_2_10.png" target="_blank">
                <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libx265_yuv420p_2_10.png" alt="libx265_yuv420p_2_10" style="width: 100%;">
            </a></td>
            <td><a href="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libsvtav1_yuv420p_2_10.png" target="_blank">
                <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libsvtav1_yuv420p_2_10.png" alt="libsvtav1_yuv420p_2_10" style="width: 100%;">
            </a></td>
        </tr>
        <tr>
            <td><code>30</code></td>
            <td><a href="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libx264_yuv420p_2_30.png" target="_blank">
                <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libx264_yuv420p_2_30.png" alt="libx264_yuv420p_2_30" style="width: 100%;">
            </a></td>
            <td><a href="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libx265_yuv420p_2_30.png" target="_blank">
                <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libx265_yuv420p_2_30.png" alt="libx265_yuv420p_2_30" style="width: 100%;">
            </a></td>
            <td><a href="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libsvtav1_yuv420p_2_30.png" target="_blank">
                <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libsvtav1_yuv420p_2_30.png" alt="libsvtav1_yuv420p_2_30" style="width: 100%;">
            </a></td>
        </tr>
        <tr>
            <td><code>50</code></td>
            <td><a href="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libx264_yuv420p_2_50.png" target="_blank">
                <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libx264_yuv420p_2_50.png" alt="libx264_yuv420p_2_50" style="width: 100%;">
            </a></td>
            <td><a href="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libx265_yuv420p_2_50.png" target="_blank">
                <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libx265_yuv420p_2_50.png" alt="libx265_yuv420p_2_50" style="width: 100%;">
            </a></td>
            <td><a href="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libsvtav1_yuv420p_2_50.png" target="_blank">
                <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/video-encoding/libsvtav1_yuv420p_2_50.png" alt="libsvtav1_yuv420p_2_50" style="width: 100%;">
            </a></td>
        </tr>
    </tbody>
</table>
</center>


This table summarizes the different values we tried for our study:
| parameter   | values                                                       |
|