| import tensorflow as tf |
|
|
| from data.utils import clean_task_instruction, quaternion_to_euler, euler_to_quaternion |
|
|
|
|
| def terminate_act_to_bool(terminate_act: tf.Tensor) -> tf.Tensor: |
| """ |
| Convert terminate action to a boolean, where True means terminate. |
| """ |
| return tf.where(tf.equal(terminate_act, tf.constant(0.0, dtype=tf.float32)),tf.constant(False),tf.constant(True)) |
|
|
|
|
| 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'] |
| action['terminate'] = terminate_act_to_bool(action['terminate_episode']) |
|
|
| eef_delta_pos = action['world_vector'] |
| eef_ang = action['rotation_delta'] |
| eef_ang = euler_to_quaternion(eef_ang) |
|
|
| |
|
|
| |
|
|
| |
| arm_action = tf.concat([eef_delta_pos, eef_ang], axis=0) |
| action['arm_concat'] = arm_action |
|
|
| |
| 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") |
|
|
| |
| state = step['observation'] |
| eef_pos = state['robot_state'] |
| |
| |
| state['arm_concat'] = eef_pos |
|
|
| |
| state['format'] = tf.constant( |
| "eef_pos_x,eef_pos_y") |
|
|
| |
| |
| 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 = 'columbia_cairlab_pusht_real' |
| |
| 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) |
|
|