ACWM-Phys / deformable /transform_cloth_lessaction.py
t1an's picture
Add files using upload-large-folder tool
fdfe463 verified
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))