File size: 14,928 Bytes
9de9fbf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
import tensorflow as tf
import tensorflow_datasets as tfds
from data.utils import clean_task_instruction, quaternion_to_euler
import tensorflow as tf
import h5py
import numpy as np
from tqdm import tqdm
import os
import imageio
import concurrent.futures
import fnmatch
import cv2
import random
path2json = {
"mnt/raid5/data/jaydv/robohive_base/episodes/franka-FrankaPlanarPushReal_v2d/set_17_plannar_push_eval": [
"Push the green object to the red line."
],
"mnt/raid5/data/jaydv/robohive_base/episodes/franka-FrankaBinReorientRealRP03_v2d_set16/set_16_bin_reorient_3": [
"Pick up the bottle and and stand it upright."
],
"mnt/raid5/data/jaydv/robohive_base/demonstrations/orange_block/set_7_orange_block": [
"Pick up the orange block."
],
"mnt/raid5/data/jaydv/robohive_base/demonstrations/wooden_block": [
"Pick up the wooden block."
],
"mnt/raid5/data/jaydv/robohive_base/episodes/franka-FrankaBinReorientReal_v2d-orig/set_15_bin_reorient_eval_2": [
"Pick up the bottle and and stand it upright."
],
"mnt/raid5/data/jaydv/robohive_base/episodes/franka-FrankaBinPushReal_v2d_set14/set_14_bin_push": [
"Push the object to the red line."
],
"mnt/raid5/data/jaydv/robohive_base/episodes/franka-FrankaBinPickRealRP05_v2d/set_11_bottle_pick": [
"Pick up the bottle."
],
"mnt/raid5/data/jaydv/robohive_base/episodes/franka-FrankaBinPickRealRP03_v2d/set_10_bin_pick_2004": [
"Pick up the wooden block."
],
"mnt/raid5/data/jaydv/bin_pick_data_30029/set_2_softtoys": [
"Pick up one toy in the basket."
],
"home/jaydv/Documents/RoboSet/pick_banana_from_toaster_place_on_table_data": [
"Pick banana from toaster and place on table."
],
"mnt/raid5/data/jaydv/robohive_base/episodes/franka-FrankaBinPickReal_v2d/set_12_bin_pick_eval": [
"Pick up the block."
],
"mnt/raid5/data/roboset/v0.3/flap_open_toaster_oven_data": [
"Flap open toaster."
],
"mnt/raid5/data/jaydv/robohive_base/episodes/franka-FrankaBinReorientReal_v2d_set13/set_13_bin_reorient": [
"Pick up the bottle and stand it upright."
],
"home/jaydv/Documents/RoboSet/drag_mug_from_right_to_left_data": [
"Drag mug right to left."
],
"home/jaydv/Documents/RoboSet/drag_strainer_backward_data": [
"Drag strainer backwards."
],
"mnt/raid5/data/roboset/v0.4/baking_prep/scene_4/baking_prep_slide_close_drawer_scene_4": [
"Slide and close the drawer."
],
"mnt/raid5/data/roboset/v0.4/heat_soup/scene_4/baking_slide_in_bowl_scene_4": [
"Place the bowl into the container."
],
"set_5_bottle_cube_14": [
"Pick up the bottle."
],
"home/jaydv/Documents/RoboSet/drag_mug_forward_data": [
"Drag mug forwards."
],
"mnt/raid5/data/roboset/v0.4/clean_kitchen/scene_3/clean_kitchen_pick_towel_scene_3": [
"Pick up the towel from the oven."
],
"mnt/raid5/data/roboset/v0.4/heat_soup/scene_2/baking_pick_bowl_scene_2": [
"Pick up the bowl."
],
"mnt/raid5/data/roboset/v0.4/heat_soup/scene_2/baking_slide_in_bowl_scene_2": [
"Place the bowl into the oven."
],
"mnt/raid5/data/roboset/v0.4/heat_soup/scene_2/baking_close_oven_scene_2": [
"Flap and close the oven."
],
"home/jaydv/Documents/RoboSet/pick_banana_place_in_strainer_data": [
"Pick banana and place it in strainer."
],
"mnt/raid5/data/roboset/v0.3/pick_banana_place_in_mug_data": [
"Pick banana and place it in mug."
],
"mnt/raid5/data/roboset/v0.4/make_tea/scene_2/make_tea_pick_tea_scene_2": [
"Pick up the tea from the container."
],
"home/jaydv/Documents/RoboSet/drag_strainer_forward_data": [
"Drag strainer forwards."
],
"mnt/raid5/data/roboset/v0.3/pick_ketchup_from_strainer_place_on_table_data": [
"Pick ketchup from strainer and place it on the table."
],
"mnt/raid5/data/roboset/v0.4/baking_prep/scene_4/baking_prep_place_butter_scene_4": [
"Place the butter on the cutting board."
],
"mnt/raid5/data/roboset/v0.4/baking_prep/scene_1/baking_prep_slide_open_drawer_scene_1": [
"Slide and open the drawer."
],
"mnt/raid5/data/roboset/v0.3/drag_strainer_right_to_left_data": [
"Drag strainer right to left."
],
"home/jaydv/Documents/RoboSet/pick_banana_from_plate_place_on_table_data": [
"Pick banana from plate and place on table."
],
"mnt/raid5/data/roboset/v0.3/pick_ketchup_from_plate_place_on_table_data": [
"Pick ketchup from plate and place it on table."
],
"mnt/raid5/data/roboset/v0.4/baking_prep/scene_1/baking_prep_slide_close_drawer_scene_1": [
"Slide and close the drawer."
],
"mnt/raid5/data/roboset/v0.3/drag_strainer_left_to_right_data": [
"Drag strainer left to right."
],
"pick_ketchup_place_on_toaster_data": [
"Pick ketchup from table and place on toaster."
],
"mnt/raid5/data/roboset/v0.4/make_tea/scene_2/make_tea_place_tea_scene_2": [
"Place the tea into the cup."
],
"mnt/raid5/data/roboset/v0.3/pick_ketchup_place_in_strainer_data": [
"Pick ketchup from the table and place it in strainer."
],
"home/jaydv/Documents/RoboSet/pick_ketchup_place_on_plate_data": [
"Pick ketchup from table and place on plate."
],
"home/jaydv/Documents/RoboSet/drag_mug_backward_data": [
"Drag mug backwards."
],
"set_1_blocks_897": [
"Pick up one block in the basket."
],
"mnt/raid5/data/roboset/v0.4/make_tea/scene_2/make_tea_place_lid_scene_2": [
"Place lid on the cutting board."
],
"mnt/raid5/data/roboset/v0.4/heat_soup/scene_4/baking_pick_bowl_scene_4": [
"Pick up the bowl."
],
"mnt/raid5/data/roboset/v0.4/baking_prep/scene_4/baking_prep_pick_butter_scene_4": [
"Pick up the butter from the drawer."
],
"mnt/raid5/data/roboset/v0.4/baking_prep/scene_1/baking_prep_pick_butter_scene_1": [
"Pick up the butter from the drawer."
],
"home/jaydv/Documents/RoboSet/flap_close_toaster_oven_data": [
"Flap close toaster."
],
"home/jaydv/Documents/RoboSet/drag_mug_from_left_to_right_data": [
"Drag mug left to right."
],
"set_6_planar_push_120": [
"Push the object from left to right."
],
"mnt/raid5/data/roboset/v0.4/clean_kitchen/scene_3/clean_kitchen_slide_close_drawer_scene_3": [
"Slide and close the drawer."
],
"set_4_med_block_7": [
"Pick up the wooden block."
],
"mnt/raid5/data/roboset/v0.3/pick_banana_place_on_toaster_data": [
"Pick banana from table and place on toaster."
],
"mnt/raid5/data/roboset/v0.3/pick_ketchup_from_toaster_place_on_table_data": [
"Pick ketchup from toaster and place it on table."
],
"mnt/raid5/data/roboset/v0.4/baking_prep/scene_1/baking_prep_place_butter_scene_1": [
"Place the butter on the cutting board."
],
"mnt/raid5/data/roboset/v0.4/baking_prep/scene_4/baking_prep_slide_open_drawer_scene_4": [
"Slide and open the drawer."
],
"home/jaydv/Documents/RoboSet/pick_banana_place_on_plate_data": [
"Pick banana from table and place on plate."
],
"set_8_pick_bottle_10": [
"Pick up the bottle."
],
"home/jaydv/Documents/RoboSet/pick_ketchup_place_in_toaster_data": [
"Pick ketchup from the table and place in toaster."
]
}
image_shape = (240, 424, 3)
Dmanus = ['']
def stash_image_into_observation(step):
step['observation'] = {'cam_high': [], 'cam_left_wrist': [], 'cam_right_wrist':[]}
step['observation']['cam_high'] = step['cam_high']
step['observation']['cam_left_wrist'] = step['cam_left_wrist']
step['observation']['cam_right_wrist'] = step['cam_right_wrist']
return step
def _parse_function(proto,instruction):
# Update the keys_to_features dictionary to match the new TFRecord format
keys_to_features = {
'action': tf.io.FixedLenFeature([], tf.string),
'action_gripper': tf.io.FixedLenFeature([], tf.string),
'qpos': tf.io.FixedLenFeature([], tf.string),
'qvel': tf.io.FixedLenFeature([], tf.string),
'qpos_gripper': tf.io.FixedLenFeature([], tf.string),
'qvel_gripper': tf.io.FixedLenFeature([], tf.string),
'rgb_left': tf.io.FixedLenFeature([], tf.string),
'rgb_right': tf.io.FixedLenFeature([], tf.string),
'rgb_top': tf.io.FixedLenFeature([], tf.string),
'terminate_episode': tf.io.FixedLenFeature([], tf.int64)
}
# Parse the incoming features according to the dictionary
parsed_features = tf.io.parse_single_example(proto, keys_to_features)
# Deserialize and reshape tensors as necessary
action = tf.io.parse_tensor(parsed_features['action'], out_type=tf.float16)
action_gripper = tf.io.parse_tensor(parsed_features['action_gripper'], out_type=tf.float16)
qpos = tf.io.parse_tensor(parsed_features['qpos'], out_type=tf.float16)
qvel = tf.io.parse_tensor(parsed_features['qvel'], out_type=tf.float16)
qpos_gripper = tf.io.parse_tensor(parsed_features['qpos_gripper'], out_type=tf.float16)
qvel_gripper = tf.io.parse_tensor(parsed_features['qvel_gripper'], out_type=tf.float16)
rgb_left = tf.io.parse_tensor(parsed_features['rgb_left'], out_type=tf.uint8)
rgb_right = tf.io.parse_tensor(parsed_features['rgb_right'], out_type=tf.uint8)
rgb_top = tf.io.parse_tensor(parsed_features['rgb_top'], out_type=tf.uint8)
terminate_episode = tf.cast(parsed_features['terminate_episode'], tf.int64)
# Reshape or modify other fields as needed to fit the model input
rgb_left = tf.reshape(rgb_left, image_shape)
rgb_right = tf.reshape(rgb_right, image_shape)
rgb_top = tf.reshape(rgb_top, image_shape)
return {
"action": action,
"action_gripper": action_gripper,
"qpos": qpos,
"qvel": qvel,
"qpos_gripper": qpos_gripper,
"qvel_gripper": qvel_gripper,
"observation": {
"rgb_left": rgb_left,
"rgb_right": rgb_right,
"rgb_top": rgb_top
},
"terminate_episode": terminate_episode,
"instruction": instruction
}
def dataset_generator_from_tfrecords(seed):
tfrecord_path = './data/datasets/roboset/tfrecords/'
failure = [f'set_{i}' for i in range(10, 18)]
filepaths = []
for root, dirs, files in os.walk(tfrecord_path):
# skip datasets with failure
fail = False
for f in failure:
if f in root:
fail = True
break
if fail:
continue
for filename in fnmatch.filter(files, '*.tfrecord'):
filepath = os.path.join(root, filename)
filepaths.append(filepath)
random.seed(seed)
random.shuffle(filepaths)
for filepath in filepaths:
for path in path2json:
if path in filepath:
instruction = path2json[path]
raw_dataset = tf.data.TFRecordDataset(filepath)
dataset = raw_dataset.map(lambda x: _parse_function(x,instruction))
yield {
'steps': dataset
}
def load_dataset(seed):
dataset = tf.data.Dataset.from_generator(
lambda: dataset_generator_from_tfrecords(seed),
output_signature={
'steps': tf.data.DatasetSpec(
element_spec={
'action': tf.TensorSpec(shape=(None), dtype=tf.float16),
'action_gripper': tf.TensorSpec(shape=(None), dtype=tf.float16),
'qpos': tf.TensorSpec(shape=(None), dtype=tf.float16),
'qvel': tf.TensorSpec(shape=(None), dtype=tf.float16),
'qpos_gripper': tf.TensorSpec(shape=(None), dtype=tf.float16),
'qvel_gripper': tf.TensorSpec(shape=(None), dtype=tf.float16),
'observation': {
'rgb_left': tf.TensorSpec(shape=image_shape, dtype=tf.uint8),
'rgb_right': tf.TensorSpec(shape=image_shape, dtype=tf.uint8),
'rgb_top': tf.TensorSpec(shape=image_shape, dtype=tf.uint8),
},
'terminate_episode': tf.TensorSpec(shape=(), dtype=tf.int64),
'instruction': tf.TensorSpec(shape=(None), dtype=tf.string)
}
)
}
)
return dataset
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.float16)),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.
"""
# Convert raw action to our action
step['action'] = {}
step['action']['terminate'] = step['terminate_episode']
# undetermined action
state = step['observation']
qpos = tf.cast(step['qpos'], tf.float32)
# qvel = tf.cast(step['qvel'], tf.float32)
gripper_pos = tf.expand_dims(tf.cast(step['qpos_gripper'], tf.float32), axis=0)
# delete due to all zeros
# gripper_vel = tf.expand_dims(tf.cast(step['qvel_gripper'], tf.float32), axis=0)
# state['arm_concat'] = tf.concat([qpos, qvel, gripper_pos, gripper_vel], axis=0)
state['arm_concat'] = tf.concat([qpos, gripper_pos], axis=0)
# state['format'] = tf.constant(
# "arm_joint_0_pos,arm_joint_1_pos,arm_joint_2_pos,arm_joint_3_pos,arm_joint_4_pos,arm_joint_5_pos,arm_joint_6_pos,arm_joint_0_vel,arm_joint_1_vel,arm_joint_2_vel,arm_joint_3_vel,arm_joint_4_vel,arm_joint_5_vel,arm_joint_6_vel,gripper_joint_0_pos,gripper_joint_0_vel"
# )
state['format'] = tf.constant(
"arm_joint_0_pos,arm_joint_1_pos,arm_joint_2_pos,arm_joint_3_pos,arm_joint_4_pos,arm_joint_5_pos,arm_joint_6_pos,gripper_joint_0_pos"
)
# Clean the task instruction
# Define the replacements (old, new) as a dictionary
replacements = {
'_': ' ',
'1f': ' ',
'4f': ' ',
'-': ' ',
'50': ' ',
'55': ' ',
'56': ' ',
}
instr = step['instruction'][0]
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 = load_dataset()
for step in dataset.take(100):
for data in step['steps']:
data = process_step(data)
print(data)
break
|