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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# `robotwin_flow/scripts` 数据格式说明

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

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

## 1. 总体处理链路

```text
原始 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 原始输入目录

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

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

### 2.2 Stage 1 输出目录

不指定 `--save-dir` 时:

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

指定 `--save-dir` 时:

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

### 2.3 默认 flow 输出目录

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

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

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

### 2.4 `robot_flow` 输出目录

指定 `--flow-stride N` 时:

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

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

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

## 3. HDF5 图像编码约定

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

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

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

```python
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 结构

```text
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`

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

解码后的图像:

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

默认 `render_robot_only.py` 输出为:

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

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

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

```text
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 格式:

```text
joint_action/left_arm
joint_action/right_arm
joint_action/left_gripper
joint_action/right_gripper
```

也支持 native RoboTwin 格式:

```text
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 结构

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

### 5.2 `observation/<camera>/flow`

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

默认输出尺寸为:

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

### 5.3 `observation/<camera>/frame_indices`

```text
类型: int64
shape: (num_selected_frames,)
```

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

光流图片的时间语义为:

```text
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]
```

因此:

```text
len(flow) == len(frame_indices)
```

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

### 5.4 `observation/<camera>/max_magnitudes`

```text
类型: float32
shape: (num_flow_frames,)
```

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

```text
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 结构

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

例如 `--flow-stride 4`:

```text
observation/head_camera/4
metadata/head_camera/4/frame_indices
metadata/head_camera/4/max_magnitudes
```

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

### 6.2 `observation/<camera>/<N>`

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

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

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

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

```text
类型: int64
shape: (num_selected_frames,)
```

当前默认采样函数为:

```python
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` 的实际含义是:

```text
目标采样帧数 = N
```

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

示例:

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

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

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

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

```text
类型: 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 文件相同,但:

```text
stage = "robot_flow"
```

## 7. 默认 rollout 采样

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

```text
num_frames   = 121
max_stride   = 3
max_rollouts = 2
```

rollout 数量计算为:

```python
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. 光流可视化编码

内部计算的光流为:

```text
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

```python
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

```python
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`

```python
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 显示

```python
import matplotlib.pyplot as plt

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

## 10. 索引对应关系

假设:

```text
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

使用:

```bash
--cameras head_camera,left_camera,right_camera,third_view
```

结果按 camera 独立保存:

```text
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

```json
{
  "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

```json
{
  "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 会记录:

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

## 13. 断点续算

Stage 1 检查:

```text
observation/<camera>/rgb
```

默认 flow 检查:

```text
observation/<camera>/flow
```

均匀采样 flow 检查:

```text
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 可视化图反解。