| | import pickle, os |
| | import numpy as np |
| | import pdb |
| | from copy import deepcopy |
| | import zarr |
| | import shutil |
| | import argparse |
| | import yaml |
| | import cv2 |
| | import h5py |
| |
|
| |
|
| | def load_hdf5(dataset_path): |
| | if not os.path.isfile(dataset_path): |
| | print(f"Dataset does not exist at \n{dataset_path}\n") |
| | exit() |
| |
|
| | with h5py.File(dataset_path, "r") as root: |
| | left_gripper, left_arm = ( |
| | root["/joint_action/left_gripper"][()], |
| | root["/joint_action/left_arm"][()], |
| | ) |
| | right_gripper, right_arm = ( |
| | root["/joint_action/right_gripper"][()], |
| | root["/joint_action/right_arm"][()], |
| | ) |
| | vector = root["/joint_action/vector"][()] |
| | image_dict = dict() |
| | for cam_name in root[f"/observation/"].keys(): |
| | image_dict[cam_name] = root[f"/observation/{cam_name}/rgb"][()] |
| |
|
| | return left_gripper, left_arm, right_gripper, right_arm, vector, image_dict |
| |
|
| |
|
| | def main(): |
| | parser = argparse.ArgumentParser(description="Process some episodes.") |
| | parser.add_argument( |
| | "task_name", |
| | type=str, |
| | help="The name of the task (e.g., beat_block_hammer)", |
| | ) |
| | parser.add_argument("task_config", type=str) |
| | parser.add_argument( |
| | "expert_data_num", |
| | type=int, |
| | help="Number of episodes to process (e.g., 50)", |
| | ) |
| | args = parser.parse_args() |
| |
|
| | task_name = args.task_name |
| | num = args.expert_data_num |
| | task_config = args.task_config |
| |
|
| | load_dir = "../../data/" + str(task_name) + "/" + str(task_config) |
| |
|
| | total_count = 0 |
| |
|
| | save_dir = f"./data/{task_name}-{task_config}-{num}.zarr" |
| |
|
| | if os.path.exists(save_dir): |
| | shutil.rmtree(save_dir) |
| |
|
| | current_ep = 0 |
| |
|
| | zarr_root = zarr.group(save_dir) |
| | zarr_data = zarr_root.create_group("data") |
| | zarr_meta = zarr_root.create_group("meta") |
| |
|
| | head_camera_arrays, front_camera_arrays, left_camera_arrays, right_camera_arrays = ( |
| | [], |
| | [], |
| | [], |
| | [], |
| | ) |
| | episode_ends_arrays, action_arrays, state_arrays, joint_action_arrays = ( |
| | [], |
| | [], |
| | [], |
| | [], |
| | ) |
| |
|
| | while current_ep < num: |
| | print(f"processing episode: {current_ep + 1} / {num}", end="\r") |
| |
|
| | load_path = os.path.join(load_dir, f"data/episode{current_ep}.hdf5") |
| | ( |
| | left_gripper_all, |
| | left_arm_all, |
| | right_gripper_all, |
| | right_arm_all, |
| | vector_all, |
| | image_dict_all, |
| | ) = load_hdf5(load_path) |
| |
|
| | for j in range(0, left_gripper_all.shape[0]): |
| |
|
| | head_img_bit = image_dict_all["head_camera"][j] |
| | joint_state = vector_all[j] |
| |
|
| | if j != left_gripper_all.shape[0] - 1: |
| | head_img = cv2.imdecode(np.frombuffer(head_img_bit, np.uint8), cv2.IMREAD_COLOR) |
| | head_camera_arrays.append(head_img) |
| | state_arrays.append(joint_state) |
| | if j != 0: |
| | joint_action_arrays.append(joint_state) |
| |
|
| | current_ep += 1 |
| | total_count += left_gripper_all.shape[0] - 1 |
| | episode_ends_arrays.append(total_count) |
| |
|
| | print() |
| | episode_ends_arrays = np.array(episode_ends_arrays) |
| | |
| | state_arrays = np.array(state_arrays) |
| | head_camera_arrays = np.array(head_camera_arrays) |
| | joint_action_arrays = np.array(joint_action_arrays) |
| |
|
| | head_camera_arrays = np.moveaxis(head_camera_arrays, -1, 1) |
| |
|
| | compressor = zarr.Blosc(cname="zstd", clevel=3, shuffle=1) |
| | |
| | state_chunk_size = (100, state_arrays.shape[1]) |
| | joint_chunk_size = (100, joint_action_arrays.shape[1]) |
| | head_camera_chunk_size = (100, *head_camera_arrays.shape[1:]) |
| | zarr_data.create_dataset( |
| | "head_camera", |
| | data=head_camera_arrays, |
| | chunks=head_camera_chunk_size, |
| | overwrite=True, |
| | compressor=compressor, |
| | ) |
| | zarr_data.create_dataset( |
| | "state", |
| | data=state_arrays, |
| | chunks=state_chunk_size, |
| | dtype="float32", |
| | overwrite=True, |
| | compressor=compressor, |
| | ) |
| | zarr_data.create_dataset( |
| | "action", |
| | data=joint_action_arrays, |
| | chunks=joint_chunk_size, |
| | dtype="float32", |
| | overwrite=True, |
| | compressor=compressor, |
| | ) |
| | zarr_meta.create_dataset( |
| | "episode_ends", |
| | data=episode_ends_arrays, |
| | dtype="int64", |
| | overwrite=True, |
| | compressor=compressor, |
| | ) |
| |
|
| |
|
| | if __name__ == "__main__": |
| | main() |
| |
|