跳转到内容

C401 第 4 课:训练数据格式

处理流程的最后一步,把第 1 课到第 3 课产生的东西写成数据加载器可以读取的文件。本课讲解当前的第一人称数据发布所用的格式,其中 LeRobotDataset v3.0 讲得最细,因为实验就导出为这种格式。完整的讲解见训练数据格式:LeRobot、RLDS 及同类格式。

另见术语表的数据集与格式部分。

术语 含义
Episode 对一项任务从头到尾的一次连续录制:一串时间对齐的观测和动作
分片(Shard) 一个打包了许多 episode 的大文件,这样存储系统处理的是数量更少、体积更大的文件
Feature(特征字段) 一个有名字的逐帧字段,带有 dtype 和 shape,例如 observation.state 或 observation.images.<camera>
observation.state 描述智能体的逐帧向量:对机器人是关节角;对人手是手腕位姿和手指关节
action 某一时间步的指令或运动目标,可以是绝对的(一个目标位姿),也可以是相对的(一个增量)
MCAP 一种开放的容器,用于带时间戳的发布/订阅消息,内嵌模式定义并带有索引

录制容器是为采集而设计的:它按到达顺序追加带时间戳的数据包。训练数据格式是为学习而设计的:它把许多 episode 组织起来,使加载器可以随机采样帧、切出短的时间窗口,并对状态和动作做归一化,而不必打开几百万个小文件(训练数据格式:LeRobot、RLDS 及同类格式)。

当前公开的第一人称数据发布常常两种都提供。Lightwheel 把它的数据集同时发布为 MCAP 和 LeRobot,两张数据集卡片都警告说:“LeRobot and MCAP are alternative representations of the same episodes and must not be counted as additional hours”(意为:LeRobot 和 MCAP 是同一批 episode 的两种可互换的封装形式,不得算作额外的小时数)(EgoStandard 数据集卡片)。

LeRobotDataset 是 Hugging Face 为 lerobot 库设计的数据集格式。状态、动作和时间戳放在 Parquet 表中,相机帧放在 MP4 中,元数据放在 JSON 和 Parquet 中。在 v2.1 中,每个 episode 有自己的文件;v3.0 把许多 episode 拼接进每个文件。v3.0 文档解释说:“To scale to millions of episodes, tabular rows and video frames from multiple episodes are concatenated into larger files.”(意为:为了扩展到数百万个 episode,来自多个 episode 的表格行和视频帧被拼接成更大的文件。)

Where one episode lives in LeRobot v2.1 and v3.0 Two panels. Left, LeRobotDataset v2.1: every episode has its own files, one Parquet file per episode under data/chunk-000 (episode_000000.parquet, episode_000001.parquet, episode_000002.parquet and so on) and one MP4 per camera per episode under videos/chunk-000/camera name, so the file count grows with episodes times cameras. Right, LeRobotDataset v3.0: rows from many episodes are concatenated into one Parquet shard, data/chunk-000/file-000.parquet, and frames from the same episodes into one MP4 shard per camera, videos/camera name/chunk-000/file-000.mp4. A row in meta/episodes holds each episode's length, task and offsets; arrows from the row for episode 1 point to its range of rows in the data shard and its time range in the video shard. A new shard starts at about 100 MB for data and 200 MB for video by default, with up to 1,000 files per chunk folder. Where one episode lives: LeRobot v2.1 and v3.0 v2.1: one file per episode v3.0: many episodes per file data/chunk-000/ episode_000000.parquet episode_000001.parquet episode_000002.parquet … videos/chunk-000/<camera>/ episode_000000.mp4 episode_000001.mp4 episode_000002.mp4 … Files = episodes × (1 + cameras): millions of small files strain file systems and object stores. meta/episodes/chunk-000/file-000.parquet episode 0: length, task, offsets episode 1: length, task, offsets episode 2: length, task, offsets data/chunk-000/file-000.parquet ep 0 ep 1 ep 2 ep 3 … videos/<camera>/chunk-000/file-000.mp4 ep 0 ep 1 ep 2 ep 3 … frame rows: episode 1 is a row range in the shard one MP4 per camera: episode 1 is a time range New shard at about 100 MB (data) or 200 MB (video); up to 1,000 files per chunk folder (lerobot defaults).
Why LeRobotDataset v3.0 changed the layout. In v2.1 every episode has its own Parquet file and one MP4 per camera, so the file count grows with episodes times cameras. In v3.0 many episodes are concatenated into each Parquet shard and each camera's MP4 shard, and a row in meta/episodes records each episode's length, task and offsets, which locate it as a row range in the data and a time range in the video. Shard sizes are lerobot's defaults. Sources: the LeRobotDataset v3.0 docs and lerobot's datasets/utils.py (v0.3.3 for v2.1, main for v3.0).

动手写之前要知道的事(LeRobotDataset):

  • 模式定义在 meta/info.json 中,包括 feature 的名称、dtype 和 shape,帧率和路径模板。归一化统计量在 meta/stats.json 中。
  • 相机是 feature,命名为 observation.images.<camera>,dtype 为 video。
  • 没有存放相机内参或外参的字段。第一人称数据发布在 meta/ 下加入自己的文件;MundoAI 的样例把 “camera intrinsics + head-to-camera offset, keyed by device”(意为:相机内参加头部到相机的偏移,以设备为键)放在那里的一个标定文件中(MundoAI-Robotics-Sample)。
  • 调用 finalize()。v3.0 以增量方式写 Parquet:“When creating or recording datasets, you must call dataset.finalize() to properly close parquet writers.”(意为:创建或录制数据集时,必须调用 dataset.finalize() 才能正确关闭 parquet 写入器。)不调用它,文件就不完整,数据集将无法加载(v3.0 文档)。
  • 时间戳由帧率得出。在 lerobot 0.7.0 中,add_frame() 把每一帧的时间戳算成它的索引除以数据集的 fps,调用方不得自行提供时间戳(dataset_writer.py)。因此你加入的帧必须按这个帧率等间隔排列。

其他格式,以及它们各适合什么情况

Section titled “其他格式,以及它们各适合什么情况”
下游用途 导出为 原因
PyTorch 模仿学习、VLA 训练、发布到 Hub LeRobotDataset v3.0 文件少而大;内置时间窗口、流式读取和 GPU 增强
检查、数据整理、ROS 2 / Foxglove / FiftyOne MCAP 保留传感器的原始速率、内嵌的模式定义和标定附件
稠密的逐帧骨架、深度、SLAM MP4 + HDF5,或 Zarr 视频仍可硬件解码;数组保存在一个带索引的文件或存储中
10,000 小时以上的纯视频预训练 WebDataset 对 .tar 分片做顺序流式读取,元数据开销很小

这张表就是实用指南的导出为哪种格式。MCAP 让每路数据流保持自己的速率,而 LeRobot 把一个 episode 压平成与视频帧对齐的固定速率的行(MCAP 作为分发和数据整理格式)。有几个第一人称数据发布把视频保留为 MP4,把运动学数据放在 HDF5 中:EgoDex 为每个 MP4 配一个 HDF5 文件,其中存有相机内参和外参以及上半身的关节位姿(HDF5 布局)。

在遥操作的机器人数据集中,状态和动作来自机器人。人类采集没有机器人。正如 RoboX 的数据集卡片所说:“No robot retargeting is applied. target_embodiment is null and no robot telemetry is present”(意为:没有做机器人重定向。target_embodiment 为 null,也没有机器人遥测数据)(RoboX lerobot-dataset)。处理流程用手部姿态、世界空间中的手部追踪和 SLAM 头部位姿构造代理状态和代理动作。在已发表的模式定义中,有三个细节反复出现(把人类第一人称数据表示为状态和动作):

  1. 从手部动作中去掉头部运动。来自头戴相机的手部位姿处在一个运动的坐标系中。EgoMimic 和 EgoVerse 用 SLAM 位姿把每个未来的手部位姿表示在当前时刻的相机坐标系中。
  2. 不要对四元数做减法。RoboX 的动作使用 “a 3-D relative rotation vector rotvec(q[t+1] . inverse(q[t])) rather than a quaternion subtraction”(意为:一个 3 维相对旋转向量 rotvec(q[t+1] . inverse(q[t])),而不是四元数相减),并且它把四元数的各个索引排除在均值/标准差归一化之外,因为归一化会破坏它们的单位范数。
  3. 匹配人和机器人的速度。人的动作比遥操作的机器人快,所以固定长度的动作块在人类数据中覆盖的任务部分会比在机器人数据中多。EgoVerse 把 1 秒的人类窗口和 1.5 秒的机器人窗口各重采样为 100 步,并对每种本体分别归一化(匹配人和机器人的速度)。
Removing head motion from hand actions Three top-down panels. Left, the scene: between time t and a later time t+i the wearer turns the head, so the head camera points in a new direction, while the hand stays still in the world. Middle, hand positions left in each frame's own camera coordinates: at t the hand is straight ahead, at t+i it appears off to one side, so an action computed this way shows the hand moving when only the head moved. Right, the same positions re-expressed in the camera frame at t using the SLAM camera poses: both land on the same point and the action is zero, as it should be. The formula used by EgoMimic and EgoVerse is shown below: a at t+i equals the inverse of T_world_cam at t, times T_world_cam at t+i, times p_hand at t+i, for i from 1 to k. Schematic. The head turns, the hand stays still: what the action should say 1. Scene, from above 2. Each frame's own camera 3. Camera frame at time t hand (still) t t+i head turns between t and t+i p(t) p(t+i) looks like the hand moved: head motion leaks into the action same point SLAM poses undo the turn: action is zero, as it should be a(t+i) = inverse(T_world_cam(t)) · T_world_cam(t+i) · p_hand(t+i) for i = 1 … k; T_world_cam from SLAM on the head camera (EgoMimic, EgoVerse)
Why hand actions from a head camera need the camera's SLAM poses. If the wearer turns their head while the hand stays still, hand positions left in each frame's own camera coordinates appear to move, so head motion leaks into the action. Re-expressing every future hand pose in the camera frame at the current time t, using the SLAM poses as EgoMimic and EgoVerse do, puts a still hand at the same point and gives a zero action. Schematic, top-down.

把这些变成机器人策略是课程 C402 的内容。对本课程,规则更简单:无论导出什么,都要写下每个位姿处在哪个坐标系、它的单位,以及它的旋转约定。

  1. 你录制了一个 ROS 2 bag,图像为 30 Hz,IMU 为 200 Hz。为什么你可能会在 LeRobot 导出之外同时发布 MCAP?
  2. 你的转换脚本运行没有报错,但数据集无法加载。在 v3.0 导出中首先要检查什么?
  3. 在 LeRobotDataset 中,相机内参放在哪里?
  4. 你的追踪器在一个 episode 中间有 40 帧没有给出位姿。为什么调用 add_frame() 时不能直接跳过它们?
  5. 为什么 RoboX 把它的四元数分量排除在均值/标准差归一化之外?
  1. LeRobot 把一个 episode 压平成与视频帧对齐的固定速率的行;MCAP 让每路数据流保持自己的速率,并内嵌模式定义,用于检查和数据整理。如果两种都发布,要在数据集卡片上说明它们是同一批 episode。
  2. 是否在最后一次 save_episode() 之后调用了 finalize();不调用它,Parquet 文件就不完整。
  3. 没有标准字段。像第一人称数据发布那样在 meta/ 下加一个文件,并在数据集卡片上说明。
  4. lerobot 根据帧索引和 fps 计算时间戳,所以跳过帧会使之后每一帧的时间戳都发生偏移。在缺口处结束 episode,或用有文档记录的方法填补它。
  5. 均值/标准差缩放会破坏四元数的单位范数。