| 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: |
| |
| ep_keys = sorted(f.keys()) |
| |
| for ep_key in tqdm(ep_keys): |
| ep = f[ep_key] |
| |
| |
| |
| |
| |
| orig_actions = ep['actions'][:] |
| new_actions = orig_actions[:, :3] |
| |
| |
| actions_tensor = torch.from_numpy(new_actions).float() |
| |
| |
| 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): |
| |
| 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.append({ |
| 'video_path': video_filename, |
| 'actions': actions_tensor, |
| 'length': len(new_actions), |
| 'cloth_dimx': 16, |
| 'cloth_dimy': 16 |
| }) |
| |
| |
| 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)) |
|
|