more_robotwin / docs /flow_format.md
yjh001's picture
Document the deduplicated RoboTwin snapshot and upload layout
f32079d verified
|
Raw History Blame Contribute Delete
14.6 kB

robotwin_flow/scripts 数据格式说明

本文档说明当前两个独立处理脚本的输入、输出目录和 HDF5 数据结构:

  • render_robot_only.py:Stage 1,SAPIEN robot-only 渲染
  • compute_flow.py:Stage 2/Stage 3,背景纹理增强、光流计算、mask、可视化编码

1. 总体处理链路

原始 RoboTwin 数据
<dataset_root>/<task>/<variant>/data/episode*.hdf5
        |
        | render_robot_only.py
        v
Stage 1 robot-only RGB
<robot_only_root>/<task>/<variant>/robot_only/data/episode*.hdf5
        |
        | compute_flow.py
        v
resize 320x240
        |
背景颜色检测与静态纹理增强
        |
RAFT 或 Farneback 光流
        |
机器人区域 mask
        |
光流 resize 到 640x480
        |
HSV/RGB 可视化编码与 JPEG 保存
        v
Stage 3 flow 输出
flow/data/episode*.hdf5
或
robot_flow/episode*.hdf5

当前脚本保存的是光流可视化图片,不是原始的 float32 (dx, dy) 光流场。

2. 目录结构

2.1 原始输入目录

<dataset_root>/
└── <task>/
    └── <variant>/
        └── data/
            ├── episode0.hdf5
            ├── episode1.hdf5
            └── ...

<task> 是任务名,<variant> 是机器人和数据变体名。两个脚本都扫描 data/episode*.hdf5。

2.2 Stage 1 输出目录

不指定 --save-dir 时:

<dataset_root>/<task>/<variant>/robot_only/data/episode0.hdf5

指定 --save-dir 时:

<save_dir>/<task>/<variant>/robot_only/data/episode0.hdf5

2.3 默认 flow 输出目录

不指定 --flow-stride 时,compute_flow.py 输出到:

<dataset_root>/<task>/<variant>/flow/data/episode0.hdf5

当前独立脚本的默认 rollout 输出路径使用源数据集目录;设置 --robot-only-root 只影响 Stage 1 输入文件位置,不会把默认 flow 输出移动到 robot-only 缓存目录。

2.4 robot_flow 输出目录

指定 --flow-stride N 时:

<robot_flow_root>/<task>/<variant>/robot_flow/episode0.hdf5

如果没有指定 --robot-flow-root,脚本依次尝试使用:

  1. --robot-only-root
  2. 源数据集对应目录

3. HDF5 图像编码约定

所有图片都以 HDF5 variable-length uint8 数据集保存。每个元素是一张 JPEG 图片的字节数组:

HDF5 dtype: vlen uint8
单帧 dtype: uint8
单帧 shape: (jpeg_bytes,)

因此,读取后不能直接把一维数组当作 (height, width, 3) 图片,必须先 JPEG 解码:

import cv2
import h5py

with h5py.File("episode0.hdf5", "r") as file:
    encoded = file["observation/head_camera/rgb"][0]
    bgr = cv2.imdecode(encoded, cv2.IMREAD_COLOR)
    rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)

4. Stage 1:robot_only 文件

4.1 HDF5 结构

episode0.hdf5
├── observation/
│   ├── head_camera/
│   │   └── rgb
│   ├── left_camera/
│   │   └── rgb
│   ├── right_camera/
│   │   └── rgb
│   └── third_view/
│       └── rgb
└── file attributes

实际保存哪些 camera 由 --cameras 决定。不存在的 camera group 不会自动创建。

4.2 observation/<camera>/rgb

路径: observation/<camera_name>/rgb
类型: HDF5 variable-length uint8
shape: (num_frames,)
每个元素: JPEG 字节数组

解码后的图像:

shape: (render_height, render_width, 3)
dtype: uint8
通道顺序: RGB

默认 render_robot_only.py 输出为:

width  = 640
height = 480
shape  = (480, 640, 3)

如果使用 --render-width 或 --render-height,以实际文件属性为准。

Stage 1 默认按照动作轨迹长度渲染:

robot_only 帧数 = 原始 episode 动作长度

4.3 Camera group 属性

observation/<camera_name> group 包含:

属性 类型 说明
width int 图像宽度
height int 图像高度
codec string 当前为 "jpeg"

4.4 文件属性

属性 类型 说明
source_data_path string 原始 episode HDF5 路径
render_width int Stage 1 渲染宽度
render_height int Stage 1 渲染高度
stage string "robot_only_render"
task string task 名
variant string variant 名

4.5 Stage 1 输入动作字段

脚本支持 legacy 格式:

joint_action/left_arm
joint_action/right_arm
joint_action/left_gripper
joint_action/right_gripper

也支持 native RoboTwin 格式:

action/left_arm_joint_states
action/right_arm_joint_states
action/left_ee_joint_states
action/right_ee_joint_states

如果 action 不存在,则使用同名的 state 字段。四组动作轨迹长度必须一致。

5. 默认 rollout flow 文件

该格式对应不指定 --flow-stride 的情况。

5.1 HDF5 结构

episode0.hdf5
├── observation/
│   └── <camera_name>/
│       ├── flow
│       ├── frame_indices
│       └── max_magnitudes
└── file attributes

5.2 observation/<camera>/flow

类型: HDF5 variable-length uint8
shape: (num_flow_frames,)
每个元素: JPEG 光流可视化图
解码 shape: (output_height, output_width, 3)
解码 dtype: uint8
通道顺序: RGB

默认输出尺寸为:

width  = 640
height = 480
shape  = (480, 640, 3)

5.3 observation/<camera>/frame_indices

类型: int64
shape: (num_selected_frames,)

该数组保存从 Stage 1 robot_only 视频中实际选择的原始帧索引。

光流图片的时间语义为:

flow[0] = 白色 zero-flow 初始占位图
flow[1] = frame_indices[0] -> frame_indices[1]
flow[2] = frame_indices[1] -> frame_indices[2]
...
flow[i] = frame_indices[i-1] -> frame_indices[i]

因此:

len(flow) == len(frame_indices)

第一张图片不是由视频帧计算得到的光流,而是为了时间对齐而添加的全白图片。

5.4 observation/<camera>/max_magnitudes

类型: float32
shape: (num_flow_frames,)

每个元素记录对应图片编码时使用的最大光流幅值。第一项固定为:

max_magnitudes[0] == 0.0

默认 --flow-max-magnitude=20.0,因此正常情况下后续元素使用固定幅值上限 20.0。

5.5 Camera group 属性

属性 类型 说明
width int flow 图片宽度
height int flow 图片高度
codec string 当前为 "jpeg"
num_rollouts int rollout 采样数量

5.6 文件属性

属性 类型 说明
source_data_path string 原始 episode HDF5 路径
output_width int flow 输出宽度
output_height int flow 输出高度
stage string "encoded_flow"
task string task 名
variant string variant 名

6. robot_flow 均匀采样文件

该格式对应指定 --flow-stride N 的情况。

6.1 HDF5 结构

episode0.hdf5
├── observation/
│   └── <camera_name>/
│       └── <N>
└── metadata/
    └── <camera_name>/
        └── <N>/
            ├── frame_indices
            └── max_magnitudes

例如 --flow-stride 4:

observation/head_camera/4
metadata/head_camera/4/frame_indices
metadata/head_camera/4/max_magnitudes

其中 4 是 HDF5 group 名,实际类型是字符串。

6.2 observation/<camera>/<N>

类型: HDF5 variable-length uint8
shape: (num_flow_frames,)
每个元素: JPEG 光流可视化图

该数据集的图片语义和默认 flow 模式相同:

  • 第一张是白色 zero-flow
  • 后续图片对应相邻采样帧之间的光流
  • 默认解码尺寸为 (480, 640, 3)

6.3 metadata/<camera>/<N>/frame_indices

类型: int64
shape: (num_selected_frames,)

当前默认采样函数为:

def _uniform_resample_indices(src_frames, tgt_frames):
    if src_frames <= tgt_frames:
        return list(range(src_frames))
    return [
        round(i * (src_frames - 1) / (tgt_frames - 1))
        for i in range(tgt_frames)
    ]

因此,--flow-stride N 的实际含义是:

目标采样帧数 = N

它不是“每隔 N 帧取一帧”。

示例:

源帧数: 9
--flow-stride 4
采样索引: [0, 3, 5, 8]

如果源视频帧数少于目标采样数,则保留全部源帧:

源帧数: 3
--flow-stride 4
采样索引: [0, 1, 2]

6.4 metadata/<camera>/<N>/max_magnitudes

类型: float32
shape: (num_flow_frames,)

与 flow 图片一一对应,第一项为 0.0。

6.5 属性

observation/<camera> group:

属性 类型 说明
width int flow 图片宽度
height int flow 图片高度
codec string 当前为 "jpeg"

metadata/<camera>/<N> group:

属性 类型 说明
num_rollouts int 均匀采样模式固定为 1

文件属性与默认 flow 文件相同,但:

stage = "robot_flow"

7. 默认 rollout 采样

不指定 --flow-stride 时,默认配置为:

num_frames   = 121
max_stride   = 3
max_rollouts = 2

rollout 数量计算为:

chunk_interval = (num_frames - 1) * max_stride
num_rollouts = min(
    max_rollouts,
    max(1, ceil((source_frames - 1) / chunk_interval)),
)

每个 rollout 内部进行均匀采样,相邻 rollout 复用边界帧。最终 flow 序列长度由 frame_indices 长度决定,而不是直接等于原始动作长度。

8. 光流可视化编码

内部计算的光流为:

flow[..., 0] = dx
flow[..., 1] = dy

保存前转换为 HSV/RGB 图片:

图片信息 来源
Hue 光流方向 atan2(dy, dx)
Saturation 光流幅值归一化
Value 固定为 255

因此:

  • 颜色主要表示运动方向
  • 饱和度表示运动幅值
  • 白色 zero-flow 表示初始占位帧

JPEG 保存会引入压缩误差,当前输出不能无损恢复原始 float32 (dx, dy)。 如果下游需要数值光流场,需要额外保存形状为 (height, width, 2) 的 float32 数据集;当前脚本没有保存该数据。

9. 读取示例

9.1 读取 Stage 1 RGB

import cv2
import h5py

path = "/cache/task/variant/robot_only/data/episode0.hdf5"

with h5py.File(path, "r") as file:
    encoded = file["observation/head_camera/rgb"][0]
    bgr = cv2.imdecode(encoded, cv2.IMREAD_COLOR)
    rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)

9.2 读取默认 flow

import cv2
import h5py

path = "/data/task/variant/flow/data/episode0.hdf5"

with h5py.File(path, "r") as file:
    group = file["observation/head_camera"]
    flow_dataset = group["flow"]
    frame_indices = group["frame_indices"][:]
    max_magnitudes = group["max_magnitudes"][:]

    encoded = flow_dataset[1]
    bgr = cv2.imdecode(encoded, cv2.IMREAD_COLOR)
    flow_rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)

9.3 读取 robot_flow

import cv2
import h5py

path = "/cache/task/variant/robot_flow/episode0.hdf5"

with h5py.File(path, "r") as file:
    flow_dataset = file["observation/head_camera/4"]
    frame_indices = file["metadata/head_camera/4/frame_indices"][:]
    max_magnitudes = file["metadata/head_camera/4/max_magnitudes"][:]

    encoded = flow_dataset[1]
    bgr = cv2.imdecode(encoded, cv2.IMREAD_COLOR)
    flow_rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)

9.4 显示

import matplotlib.pyplot as plt

plt.imshow(flow_rgb)
plt.axis("off")
plt.show()

10. 索引对应关系

假设:

frame_indices = [0, 3, 5, 8]

则:

flow index 语义
0 白色 zero-flow,占位
1 原始帧 0 -> 3
2 原始帧 3 -> 5
3 原始帧 5 -> 8

frame_indices 是原始 robot_only 视频索引,不是 flow 数据集的索引。

11. 多 camera

使用:

--cameras head_camera,left_camera,right_camera,third_view

结果按 camera 独立保存:

observation/head_camera/...
observation/left_camera/...
observation/right_camera/...
observation/third_view/...

脚本会分别读取、计算和写入各 camera 的结果。不同 camera 应具有一致的帧数。

12. 批处理 summary JSON

两个脚本都打印 JSON summary,也支持 --summary-json path.json。

12.1 Stage 1

{
  "items": 1,
  "skipped": 0,
  "results": [
    {
      "episode": "episode0",
      "task": "blocks_ranking_size",
      "variant": "aloha-agilex_clean_83",
      "skipped": false,
      "num_frames": 740,
      "cameras": ["head_camera"],
      "rendered_cameras": ["head_camera"],
      "robot_only_hdf5": "/cache/.../robot_only/data/episode0.hdf5"
    }
  ]
}

12.2 Stage 3

{
  "items": 1,
  "skipped": 0,
  "failed": 0,
  "results": [
    {
      "episode": "episode0",
      "task": "blocks_ranking_size",
      "variant": "aloha-agilex_clean_83",
      "skipped": false,
      "robot_only_hdf5": "/cache/.../robot_only/data/episode0.hdf5",
      "flow_hdf5": "/cache/.../robot_flow/episode0.hdf5",
      "robot_only_frames": {"head_camera": 740},
      "num_flow_frames": {"head_camera": 4},
      "flow_stride": 4
    }
  ]
}

失败的 episode 会记录:

{
  "episode": "episode0",
  "failed": true,
  "error": "ValueError: ...",
  "robot_only_hdf5": "...",
  "flow_hdf5": "..."
}

13. 断点续算

Stage 1 检查:

observation/<camera>/rgb

默认 flow 检查:

observation/<camera>/flow

均匀采样 flow 检查:

observation/<camera>/<N>

当所有请求 camera 的目标数据集都存在时,episode 会被跳过。使用 --overwrite 可强制重算请求的 camera。

14. 使用注意事项

  1. robot_only 和 flow 图片都是 JPEG 字节,不是直接存储的三维数组。
  2. flow 第 0 帧是白色 zero-flow,占位帧。
  3. flow 图片数量通常等于 len(frame_indices),因为包含 zero-flow。
  4. 不读取 frame_indices 时,无法可靠知道 flow 图片对应原始视频的哪些帧。
  5. --flow-stride N 是目标采样帧数,不是每隔 N 帧取一帧。
  6. 当前输出不能恢复原始浮点 (dx, dy) 光流场。
  7. 如果下游训练需要原始 flow tensor,应扩展保存格式,而不是从 JPEG 可视化图反解。