File size: 2,813 Bytes
2c30fb5
 
72ebc5e
2c30fb5
72ebc5e
 
 
 
2c30fb5
 
 
 
 
 
 
 
 
72ebc5e
 
2c30fb5
 
 
 
72ebc5e
2c30fb5
72ebc5e
 
 
 
2c30fb5
 
 
72ebc5e
 
 
2c30fb5
 
72ebc5e
 
 
 
 
 
 
 
 
2c30fb5
72ebc5e
 
 
 
 
 
 
 
 
 
 
 
2c30fb5
 
 
 
72ebc5e
2c30fb5
72ebc5e
2c30fb5
72ebc5e
2c30fb5
72ebc5e
 
 
2c30fb5
 
 
 
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
# -*- coding: utf-8 -*-
"""
Fixed_Viewpoint_Tactile_Dataset 加载示例。

    python example_usage.py                          # 从 HuggingFace 下载
    python example_usage.py --root <本地数据集路径>    # 用本地副本,不下载

需要 Python >= 3.10 且已安装 lerobot。
"""
import argparse
import numpy as np

REPO = "Tachintech/Fixed_Viewpoint_Tactile_Dataset"


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--root", default=None, help="本地数据集路径,给了就不从 HF 下载")
    ap.add_argument("--save-img", default="sample_color.png")
    args = ap.parse_args()

    from lerobot.datasets.lerobot_dataset import LeRobotDataset
    ds = LeRobotDataset(REPO, root=args.root)
    print(f"frames={ds.num_frames}  episodes={ds.num_episodes}  fps={ds.fps}")

    # 字段列表
    print("\n[features]")
    for name, spec in ds.meta.info["features"].items():
        print(f"  {name:35s} {spec['dtype']:8s} {spec['shape']}")

    shapes = ds.meta.info.get("tactile_2d_shapes", {})

    # 取一帧
    s = ds[100]
    print("\n[frame 100]")
    for k in sorted(s.keys()):
        v = s[k]
        print(f"  {k:35s} {tuple(v.shape) if hasattr(v, 'shape') else v}")

    # 动捕:20 个标记点,各 3 位置 + 4 四元数
    pos20 = s["observation_motion_positions"].numpy().reshape(20, 3)
    quat20 = s["observation_motion_quaternions"].numpy().reshape(20, 4)
    print(f"\nmotion: pos {pos20.shape}, quat {quat20.shape}")

    # 触觉:展平向量按 tactile_2d_shapes 还原成 2D
    for i in (0, 19):
        flat = s[f"tactile_tactile_{i}"].numpy()
        grid = flat.reshape(shapes[f"tactile_{i}"])
        print(f"tactile_{i}: {flat.shape} -> {grid.shape}")

    # action 等于当前帧动捕的 位置 + 四元数 拼接
    act = s["action"].numpy()
    obs = np.concatenate([s["observation_motion_positions"].numpy(),
                          s["observation_motion_quaternions"].numpy()])
    print(f"action == motion(pos+quat): {np.allclose(act, obs)}")

    # 视频已从 mp4 解码为 [3,H,W] 张量,值域 [0,1]
    color = s["observation.images.color"]
    print(f"color {tuple(color.shape)}, depth {tuple(s['observation.images.depth'].shape)}")
    try:
        from PIL import Image
        img = (color.permute(1, 2, 0).numpy() * 255).astype(np.uint8)
        Image.fromarray(img).save(args.save_img)
        print(f"saved {args.save_img}")
    except Exception as e:
        print(f"skip save image: {e}")

    # 批训练
    from torch.utils.data import DataLoader
    batch = next(iter(DataLoader(ds, batch_size=8, shuffle=True, num_workers=0)))
    print(f"\nbatch: action {tuple(batch['action'].shape)}, "
          f"color {tuple(batch['observation.images.color'].shape)}")


if __name__ == "__main__":
    main()