File size: 12,020 Bytes
f6d03a4 | 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 | import os
import gc
import ctypes
import numpy as np
import torch
from torch.utils.data import IterableDataset, DataLoader
import tensorflow as tf
import tensorflow_datasets as tfds
tf.config.set_visible_devices([], 'GPU')
# Force glibc to return freed memory to the OS (Linux only)
try:
_LIBC = ctypes.CDLL("libc.so.6")
def _malloc_trim():
_LIBC.malloc_trim(0)
except Exception:
def _malloc_trim():
pass
class DroidAct(IterableDataset):
def __init__(
self,
droid_path,
dataset_name='droid',
length=None,
history_len=15,
future_len=15,
full_sequence=False,
input_modality="video",
view_mode="single",
load_future_image=False,
future_image_mode="horizon",
buffer_size=30000,
):
super().__init__()
self.droid_path = droid_path
self.dataset_name = dataset_name
self.length = length
self.history_len = history_len
self.future_len = future_len
self.full_sequence = full_sequence
self.input_modality = input_modality
self.view_mode = view_mode
self.load_future_image = load_future_image
self.future_image_mode = future_image_mode
self.buffer_size = buffer_size
def __iter__(self):
builder = tfds.builder_from_directory(builder_dir=self.droid_path)
read_config = tfds.ReadConfig(shuffle_seed=42, shuffle_reshuffle_each_iteration=False)
droid_ds = builder.as_dataset(split='train', shuffle_files=False, read_config=read_config)
if self.length is not None:
droid_ds = droid_ds.take(self.length)
shuffle_buffer = []
BUFFER_SIZE = self.buffer_size
cam_key = 'exterior_image_1_left'
wrist_key = 'wrist_image_left'
if torch.distributed.is_available() and torch.distributed.is_initialized():
rank = torch.distributed.get_rank()
world_size = torch.distributed.get_world_size()
else:
rank = 0
world_size = 1
worker_info = torch.utils.data.get_worker_info()
if worker_info is None:
worker_id = 0
num_workers = 1
else:
worker_id = worker_info.id
num_workers = worker_info.num_workers
total_shards = world_size * num_workers
shard_index = rank * num_workers + worker_id
ds_iterator = droid_ds.shard(num_shards=total_shards, index=shard_index)
ds_iter = iter(ds_iterator)
traj_id = -1
while True:
try:
try:
traj_data = next(ds_iter)
except StopIteration:
break
except tf.errors.DataLossError as e:
traj_id += 1
print(f"[Warn] Skipping trajectory {traj_id}: TF DataLossError during iteration: {e}")
continue
traj_id += 1
traj_batch = next(iter(traj_data['steps'].batch(5000)))
if traj_batch['reward'][-1].numpy() != 1:
del traj_batch
continue
traj_len = traj_batch['action'].shape[0]
images_np = traj_batch['observation'][cam_key].numpy()
if images_np.dtype != np.uint8:
images_np = (images_np * 255).astype(np.uint8)
wrist_np = None
if self.view_mode == "multi":
obs = traj_batch['observation']
if wrist_key in obs:
wrist_np = obs[wrist_key].numpy()
if wrist_np.dtype != np.uint8:
wrist_np = (wrist_np * 255).astype(np.uint8)
else:
wrist_np = images_np.copy()
del obs
# Process Proprioception: Cartesian + Gripper
cart_pos = traj_batch['observation']['cartesian_position']
# [Proprio Gripper]
# Raw: 1D position in [0, 1]. 0 = Open, 1 = Closed.
# Processed: Direct copy.
# Result: Range [0, 1]. 0 = Open, 1 = Closed.
gripper_pos = traj_batch['observation']['gripper_position']
proprio_np = tf.concat([cart_pos, gripper_pos], axis=-1).numpy().astype(np.float32)
# Process Actions: Cartesian Velocity + Gripper Command (Normalized)
# DROID Actions: 6 (vel) + 1 (gripper)
cart_vel = traj_batch['action_dict']['cartesian_velocity']
cart_vel = tf.cast(cart_vel, tf.float32)
cart_vel = tf.clip_by_value(cart_vel, -1.0, 1.0)
# [Action Gripper]
# Raw: 1D position in [0, 1]. 0 = Open, 1 = Closed.
# Processed: Binarized to {-1, 1} based on 0.5 threshold.
# Result: Range [-1, 1]. -1 = Open, 1 = Closed.
grip_pos = traj_batch['action_dict']['gripper_position']
grip_cmd = tf.where(grip_pos > 0.5, 1.0, -1.0)
grip_cmd = tf.cast(grip_cmd, tf.float32)
actions_np = tf.concat([cart_vel, grip_cmd], axis=-1).numpy().astype(np.float32)
instruction = traj_batch['language_instruction'][0].numpy().decode('utf-8')
del traj_batch, cart_pos, gripper_pos, cart_vel, grip_pos, grip_cmd
if self.full_sequence:
sample_indices = np.arange(traj_len)
else:
num_samples = max(1, int(traj_len / (15 * 5)))
sample_indices = np.random.choice(traj_len, size=num_samples, replace=False)
for t in sample_indices:
start_hist_obs = t - self.history_len + 1
hist_indices_obs = np.arange(start_hist_obs, t + 1)
hist_indices_obs = np.clip(hist_indices_obs, 0, traj_len - 1)
start_hist_act = t - self.history_len
hist_indices_act = np.arange(start_hist_act, t)
end_fut = t + self.future_len
fut_indices = np.arange(t, end_fut)
hist_imgs = images_np[hist_indices_obs]
hist_imgs_wrist = wrist_np[hist_indices_obs] if wrist_np is not None else None
hist_proprio = torch.from_numpy(proprio_np[hist_indices_obs])
hist_actions = np.zeros((self.history_len, actions_np.shape[1]), dtype=np.float32)
valid_mask = hist_indices_act >= 0
if np.any(valid_mask):
valid_indices = hist_indices_act[valid_mask]
valid_indices = np.clip(valid_indices, 0, traj_len - 1)
hist_actions[valid_mask] = actions_np[valid_indices]
hist_actions = torch.from_numpy(hist_actions)
fut_acts_np = np.zeros((self.future_len, actions_np.shape[1]), dtype=np.float32)
valid_mask_fut = fut_indices < traj_len
if np.any(valid_mask_fut):
valid_indices_fut = fut_indices[valid_mask_fut]
fut_acts_np[valid_mask_fut] = actions_np[valid_indices_fut]
fut_acts = torch.from_numpy(fut_acts_np)
sample = {
'proprioception': hist_proprio,
'history_actions': hist_actions,
'future_actions': fut_acts,
'instruction': instruction,
}
if self.load_future_image:
if self.future_image_mode == "last":
target_idx = traj_len - 1
else:
target_idx = min(t + self.future_len, traj_len - 1)
sample['future_image'] = images_np[target_idx].copy()
if self.input_modality == "video":
sample['video'] = hist_imgs
if self.view_mode == "multi":
sample['video_wrist'] = hist_imgs_wrist
elif self.input_modality == "image":
sample['image'] = images_np[t].copy()
if self.view_mode == "multi":
sample['image_wrist'] = wrist_np[t].copy() if wrist_np is not None else images_np[t].copy()
else:
raise ValueError(f"Unknown input_modality: {self.input_modality}")
shuffle_buffer.append(sample)
if len(shuffle_buffer) >= BUFFER_SIZE:
idx = np.random.randint(len(shuffle_buffer))
shuffle_buffer[idx], shuffle_buffer[-1] = shuffle_buffer[-1], shuffle_buffer[idx]
yield shuffle_buffer.pop()
del images_np, actions_np, proprio_np
if wrist_np is not None:
del wrist_np
except Exception as e:
print(f"[Warn] Skipping trajectory {traj_id} due to error: {e}")
continue
finally:
if traj_id % 50 == 0:
gc.collect()
_malloc_trim()
np.random.shuffle(shuffle_buffer)
for sample in shuffle_buffer:
yield sample
def collate_fn(batch):
return batch
if __name__ == "__main__":
"""
Fast stats: count how many training samples DroidAct would yield.
Now includes a progress bar (tqdm).
"""
import argparse
from tqdm import tqdm
parser = argparse.ArgumentParser()
parser.add_argument("--droid_path", type=str, default="/mnt/NTU_slab/draven/data/open_x_embodiment/droid/1.0.1")
parser.add_argument("--split", type=str, default="train")
parser.add_argument("--limit_traj", type=int, default=None)
parser.add_argument("--full_sequence", action="store_true")
args = parser.parse_args()
builder = tfds.builder_from_directory(builder_dir=args.droid_path)
read_config = tfds.ReadConfig(shuffle_seed=42, shuffle_reshuffle_each_iteration=False)
ds = builder.as_dataset(split=args.split, shuffle_files=False, read_config=read_config)
total_files = builder.info.splits[args.split].num_examples
if args.limit_traj is not None:
ds = ds.take(int(args.limit_traj))
total_files = min(total_files, int(args.limit_traj))
total_trajs = 0
success_trajs = 0
total_samples = 0
SUBSAMPLE_DENOM = 15 * 5
print(f"Scanning {total_files} trajectories from {args.droid_path}...")
pbar = tqdm(enumerate(ds), total=total_files, unit="traj", desc="Scanning")
for traj_id, traj_data in pbar:
total_trajs += 1
try:
# Load only necessary data
traj_batch = next(iter(traj_data["steps"].batch(5000)))
if traj_batch["reward"][-1].numpy() != 1:
continue
success_trajs += 1
traj_len = int(traj_batch["action"].shape[0])
if args.full_sequence:
total_samples += traj_len
else:
total_samples += max(1, int(traj_len / SUBSAMPLE_DENOM))
pbar.set_postfix({"Succ": success_trajs, "Samples": total_samples})
except Exception as e:
pbar.write(f"[Warn] Skipping traj {traj_id}: {e}")
continue
print("\n" + "="*40)
print(f"DONE. Split: {args.split} | FullSeq: {args.full_sequence}")
print(f"Total Trajectories: {total_trajs}")
print(f"Successful Trajs: {success_trajs}")
print(f"Total Samples: {total_samples}")
print("="*40) |