File size: 2,874 Bytes
fdfe463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import h5py
import torch
import os
import shutil
from tqdm import tqdm
import numpy as np

def transform_dataset(src_dir, dest_dir):
    os.makedirs(dest_dir, exist_ok=True)
    src_h5 = os.path.join(src_dir, 'dataset.h5')
    src_episodes = os.path.join(src_dir, 'episodes')
    
    if not os.path.exists(src_h5):
        print(f"Skipping {src_dir}, no dataset.h5 found.")
        return

    print(f"Transforming {src_dir} to {dest_dir}...")
    
    metadata = []
    with h5py.File(src_h5, 'r') as f:
        # Sort episode keys to maintain order
        ep_keys = sorted(f.keys())
        
        for ep_key in tqdm(ep_keys):
            ep = f[ep_key]
            
            # Extract actions
            # Original actions: (64, 8) -> [dx1, dy1, dz1, pick1, dx2, dy2, dz2, pick2]
            # New actions: (64, 3) -> [dx, dy, dz] (where both grippers have the same dx, dy, dz)
            # We'll take the first 3 dimensions of the original actions
            orig_actions = ep['actions'][:]
            new_actions = orig_actions[:, :3] # Take dx1, dy1, dz1
            
            # Convert to torch tensor
            actions_tensor = torch.from_numpy(new_actions).float()
            
            # Video path
            video_filename = f"{ep_key}.mp4"
            src_video = os.path.join(src_episodes, video_filename)
            dest_video = os.path.join(dest_dir, video_filename)
            
            if os.path.exists(src_video):
                # Use symlink instead of copy to save space
                if os.path.lexists(dest_video):
                    os.remove(dest_video)
                os.symlink(src_video, dest_video)
            else:
                print(f"Warning: Video {src_video} not found.")
            
            # Metadata entry
            metadata.append({
                'video_path': video_filename,
                'actions': actions_tensor,
                'length': len(new_actions),
                'cloth_dimx': 16, # Default values from clothmove if not in source
                'cloth_dimy': 16
            })
            
    # Save metadata.pt
    torch.save(metadata, os.path.join(dest_dir, 'metadata.pt'))
    print(f"Finished {dest_dir}. Total episodes: {len(metadata)}")

if __name__ == "__main__":
    base_src = "/storage/ice-shared/ae8803che/ychen3302/cloth_data/"
    base_dest = "/storage/ice-shared/ae8803che/hxue/data/world_model/datasets/deformable/clothmove_lessaction/"
    
    mapping = [
        ('cloth_push_sphere_synced', 'ind_train'),
        ('cloth_push_sphere_synced_id_test', 'ind_test'),
        ('cloth_push_sphere_synced_ood_test_larger', 'ood_test_larger'),
        ('cloth_push_sphere_synced_ood_test_smaller', 'ood_test_smaller'),
    ]
    
    for src_sub, dest_sub in mapping:
        transform_dataset(os.path.join(base_src, src_sub), os.path.join(base_dest, dest_sub))