| | import tensorflow as tf |
| |
|
| | from data.utils import clean_task_instruction, euler_to_quaternion, \ |
| | euler_to_rotation_matrix, rotation_matrix_to_ortho6d |
| |
|
| |
|
| | def terminate_act_to_bool(terminate_act: tf.Tensor) -> tf.Tensor: |
| | """ |
| | Convert terminate action to a boolean, where True means terminate. |
| | """ |
| | return tf.reduce_all(tf.equal(terminate_act, tf.constant([1, 0, 0], dtype=tf.float32))) |
| |
|
| |
|
| | def process_step(step: dict) -> dict: |
| | """ |
| | Unify the action format and clean the task instruction. |
| | |
| | DO NOT use python list, use tf.TensorArray instead. |
| | """ |
| | |
| | |
| | action = step['action'] |
| | |
| | eef_delta_pos = action['future/xyz_residual'][:3] |
| | |
| | eef_ang = action['future/axis_angle_residual'][:3] |
| | eef_ang = euler_to_quaternion(eef_ang) |
| | |
| | grip_open = tf.cast(tf.expand_dims(1 - action['future/target_close'][0], axis=0), dtype=tf.float32) |
| |
|
| | |
| | step['action'] = {} |
| | action = step['action'] |
| | |
| | action['terminate'] = step['is_terminal'] |
| | action['arm_concat'] = tf.concat([eef_delta_pos, eef_ang, grip_open], axis=0) |
| | |
| | |
| | action['format'] = tf.constant( |
| | "eef_delta_pos_x,eef_delta_pos_y,eef_delta_pos_z,eef_delta_angle_x,eef_delta_angle_y,eef_delta_angle_z,eef_delta_angle_w,gripper_open") |
| |
|
| | |
| | state = step['observation'] |
| |
|
| | gripper_ang = state['present/axis_angle'] |
| | gripper_ang = euler_to_rotation_matrix(gripper_ang) |
| | gripper_ang = rotation_matrix_to_ortho6d(gripper_ang) |
| | gripper_pos = state['present/xyz'] |
| | |
| | gripper_open = 1- state['present/sensed_close'] |
| |
|
| |
|
| | |
| | state = step['observation'] |
| | state['arm_concat'] = tf.concat([gripper_pos, gripper_ang, gripper_open], axis=0) |
| |
|
| | |
| | state['format'] = tf.constant( |
| | "eef_pos_x,eef_pos_y,eef_pos_z,eef_angle_0,eef_angle_1,eef_angle_2,eef_angle_3,eef_angle_4,eef_angle_5,gripper_open") |
| |
|
| | |
| | |
| | replacements = { |
| | '_': ' ', |
| | '1f': ' ', |
| | '4f': ' ', |
| | '-': ' ', |
| | '50': ' ', |
| | '55': ' ', |
| | '56': ' ', |
| | |
| | } |
| | instr = step['observation']['natural_language_instruction'] |
| | instr = clean_task_instruction(instr, replacements) |
| | step['observation']['natural_language_instruction'] = instr |
| |
|
| | return step |
| |
|
| |
|
| | if __name__ == "__main__": |
| | import tensorflow_datasets as tfds |
| | from data.utils import dataset_to_path |
| |
|
| | DATASET_DIR = 'data/datasets/openx_embod' |
| | DATASET_NAME = 'fractal20220817_data' |
| | |
| | dataset = tfds.builder_from_directory( |
| | builder_dir=dataset_to_path( |
| | DATASET_NAME, DATASET_DIR)) |
| | dataset = dataset.as_dataset(split='all') |
| |
|
| | |
| | for episode in dataset: |
| | for step in episode['steps']: |
| | print(step) |
| |
|