-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
openx.py
441 lines (391 loc) · 15.2 KB
/
openx.py
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import os
import subprocess
import argparse
import time
import numpy as np
from fog_x.loader import RLDSLoader, VLALoader, HDF5Loader
import tensorflow as tf
import pandas as pd
import fog_x
import csv
import stat
from fog_x.loader.lerobot import LeRobotLoader
from fog_x.loader.vla import get_vla_dataloader
from fog_x.loader.hdf5 import get_hdf5_dataloader
# Constants
DEFAULT_EXP_DIR = "/mnt/data/fog_x/"
DEFAULT_NUMBER_OF_TRAJECTORIES = -1 # Load all trajectories
DEFAULT_DATASET_NAMES = [
"nyu_door_opening_surprising_effectiveness",
"berkeley_cable_routing",
"berkeley_autolab_ur5",
"bridge",
]
# DEFAULT_DATASET_NAMES = ["bridge"]
# CACHE_DIR = "/tmp/fog_x/cache/"
CACHE_DIR = "/mnt/data/fog_x/cache/"
DEFAULT_LOG_FREQUENCY = 20
# suppress tensorflow warnings
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import logging
logger = logging.getLogger(__name__)
class DatasetHandler:
def __init__(
self,
exp_dir,
dataset_name,
num_batches,
dataset_type,
batch_size,
log_frequency=DEFAULT_LOG_FREQUENCY,
):
self.exp_dir = exp_dir
self.dataset_name = dataset_name
self.num_batches = num_batches
self.dataset_type = dataset_type
self.dataset_dir = os.path.join(exp_dir, dataset_type, dataset_name)
self.batch_size = batch_size
# Resolve the symbolic link if the dataset_dir is a soft link
self.dataset_dir = os.path.realpath(self.dataset_dir)
self.log_frequency = log_frequency
self.results = []
self.log_level = "debug"
def measure_average_trajectory_size(self):
"""Calculates the average size of trajectory files in the dataset directory."""
total_size = 0
for dirpath, dirnames, filenames in os.walk(self.dataset_dir):
for f in filenames:
file_path = os.path.join(dirpath, f)
total_size += os.path.getsize(file_path)
logger.debug(f"total_size: {total_size} of directory {self.dataset_dir}")
# trajectory number
traj_num = 0
if self.dataset_name == "nyu_door_opening_surprising_effectiveness":
traj_num = 435
if self.dataset_name == "berkeley_cable_routing":
traj_num = 1482
if self.dataset_name == "bridge":
traj_num = 25460
if self.dataset_name == "berkeley_autolab_ur5":
traj_num = 896
return (total_size / traj_num) / (1024 * 1024) # Convert to MB
def clear_cache(self):
"""Clears the cache directory."""
if os.path.exists(CACHE_DIR):
logger.info(f"Clearing cache directory: {CACHE_DIR}")
subprocess.run(["rm", "-rf", CACHE_DIR], check=True)
def clear_os_cache(self):
"""Clears the OS cache."""
subprocess.run(["sync"], check=True)
subprocess.run(["sudo", "sh", "-c", "echo 3 > /proc/sys/vm/drop_caches"], check=True)
logger.info(f"Cleared OS cache")
def _recursively_load_data(self, data):
logger.debug(f"Data summary for loader {self.dataset_type.upper()}")
if None in data:
logger.warning(f"None value found in data")
def summarize_trajectory(trajectory):
def summarize_value(value):
if isinstance(value, np.ndarray):
return value.shape
elif isinstance(value, (list, tuple)):
if len(value) > 0 and isinstance(value[0], np.ndarray):
return [v.shape for v in value]
return len(value)
elif isinstance(value, dict):
return {k: summarize_value(v) for k, v in value.items()}
elif isinstance(value, str):
return value
else:
logger.warning(f"Unknown type: {type(value)}")
return type(value).__name__
return {key: summarize_value(value) for key, value in trajectory.items()}
trajectory_summaries = [summarize_trajectory(trajectory) for trajectory in data]
log_func = logger.debug if self.log_level == 'debug' else logger.info
for i, summary in enumerate(trajectory_summaries):
log_func(f"Trajectory {i + 1}:")
for feature, dimension in summary.items():
if isinstance(dimension, dict):
log_func(f" {feature}:")
for sub_feature, sub_dimension in dimension.items():
log_func(f" {sub_feature}: {sub_dimension}")
else:
log_func(f" {feature}: {dimension}")
log_func(f"Total number of trajectories: {len(trajectory_summaries)}")
def write_result(self, format_name, elapsed_time, index):
result = {
"Dataset": self.dataset_name,
"Format": format_name,
"AverageTrajectorySize(MB)": self.measure_average_trajectory_size(),
"LoadingTime(s)": elapsed_time,
"AverageLoadingTime(s)": elapsed_time / (index + 1),
"Index": index,
"BatchSize": self.batch_size,
}
csv_file = f"{self.dataset_name}_results.csv"
file_exists = os.path.isfile(csv_file)
with open(csv_file, "a", newline="") as f:
writer = csv.DictWriter(f, fieldnames=result.keys())
if not file_exists:
writer.writeheader()
writer.writerow(result)
def measure_random_loading_time(self):
start_time = time.time()
loader = self.get_loader()
last_batch_time = time.time()
for batch_num, data in enumerate(loader):
if batch_num >= self.num_batches:
break
self._recursively_load_data(data)
current_batch_time = time.time()
elapsed_time = current_batch_time - last_batch_time
last_batch_time = current_batch_time
self.write_result(
f"{self.dataset_type.upper()}", elapsed_time, batch_num
)
if batch_num % self.log_frequency == 0:
logger.info(
f"{self.dataset_type.upper()} - Loaded {batch_num} random {self.batch_size} batches from {self.dataset_name}, Time: {elapsed_time:.2f} s, Total Average Time: {(current_batch_time - start_time) / (batch_num + 1):.2f} s, Batch Average Time: {elapsed_time / self.batch_size:.2f} s"
)
return time.time() - start_time
def get_loader(self):
raise NotImplementedError("Subclasses must implement get_loader method")
class RLDSHandler(DatasetHandler):
def __init__(
self,
exp_dir,
dataset_name,
num_batches,
batch_size,
log_frequency=DEFAULT_LOG_FREQUENCY,
):
super().__init__(
exp_dir,
dataset_name,
num_batches,
dataset_type="rlds",
batch_size=batch_size,
log_frequency=log_frequency,
)
self.file_extension = ".tfrecord"
def get_loader(self):
return RLDSLoader(self.dataset_dir, split="train", batch_size=self.batch_size)
def _recursively_load_data(self, data):
log_level = self.log_level
# rlds returns a list of dictionaries
log_func = logger.debug if log_level == 'debug' else logger.info
log_func(f"Data summary for loader {self.dataset_type.upper()}")
for i, trajectory in enumerate(data):
log_func(f"Trajectory {i + 1}:")
# each trajectory is a list of dictionaries
for j, step in enumerate(trajectory):
log_func(f" Step {j + 1}:")
for key, value in step.items():
if isinstance(value, np.ndarray):
log_func(f" {key}: {value.shape}")
elif isinstance(value, dict):
log_func(f" {key}:")
for sub_key, sub_value in value.items():
log_func(f" {sub_key}: {sub_value.shape}")
else:
log_func(f" {key}: {type(value).__name__}")
log_func(f"Total number of trajectories: {len(data)}")
class VLAHandler(DatasetHandler):
def __init__(
self,
exp_dir,
dataset_name,
num_batches,
batch_size,
log_frequency=DEFAULT_LOG_FREQUENCY,
):
super().__init__(
exp_dir,
dataset_name,
num_batches,
dataset_type="vla",
batch_size=batch_size,
log_frequency=log_frequency,
)
self.file_extension = ".vla"
def get_loader(self):
return get_vla_dataloader(
self.dataset_dir, batch_size=self.batch_size, cache_dir=CACHE_DIR
)
class HDF5Handler(DatasetHandler):
def __init__(
self,
exp_dir,
dataset_name,
num_batches,
batch_size,
log_frequency=DEFAULT_LOG_FREQUENCY,
):
super().__init__(
exp_dir,
dataset_name,
num_batches,
dataset_type="hdf5",
batch_size=batch_size,
log_frequency=log_frequency,
)
self.file_extension = ".h5"
def get_loader(self):
return get_hdf5_dataloader(
path=os.path.join(self.dataset_dir, "*.h5"),
batch_size=self.batch_size,
num_workers=0, # You can adjust this if needed
)
class LeRobotHandler(DatasetHandler):
def __init__(
self,
exp_dir,
dataset_name,
num_batches,
batch_size,
log_frequency=DEFAULT_LOG_FREQUENCY,
):
super().__init__(
exp_dir,
dataset_name,
num_batches,
dataset_type="hf",
batch_size=batch_size,
log_frequency=log_frequency,
)
self.file_extension = (
"" # LeRobot datasets don't have a specific file extension
)
def get_loader(self):
path = os.path.join(self.exp_dir, "hf")
return LeRobotLoader(path, self.dataset_name, batch_size=self.batch_size)
def _recursively_load_data(self, data):
import torch
log_level = self.log_level
# LeRobot returns a list of lists
log_func = logger.debug if log_level == 'debug' else logger.info
log_func(f"Data summary for loader {self.dataset_type.upper()}")
for i, trajectory in enumerate(data):
log_func(f"Trajectory {i + 1}:")
# each trajectory is a list of dictionaries
for j, step in enumerate(trajectory):
log_func(f" Step {j + 1}:")
for key, value in step.items():
if isinstance(value, np.ndarray):
log_func(f" {key}: {value.shape}")
elif isinstance(value, dict):
log_func(f" {key}:")
for sub_key, sub_value in value.items():
log_func(f" {sub_key}: {sub_value.shape}")
elif isinstance(value, torch.Tensor):
log_func(f" {key}: {value.shape}")
else:
log_func(f" {key}: {type(value).__name__}")
log_func(f"Total number of trajectories: {len(data)}")
class FFV1Handler(DatasetHandler):
def __init__(self, exp_dir, dataset_name, num_batches, batch_size, log_frequency=DEFAULT_LOG_FREQUENCY):
super().__init__(exp_dir, dataset_name, num_batches, dataset_type="ffv1", batch_size=batch_size, log_frequency=log_frequency)
self.file_extension = ".vla"
def get_loader(self):
return VLALoader(self.dataset_dir, batch_size=self.batch_size)
def evaluation(args):
csv_file = "format_comparison_results.csv"
if os.path.exists(csv_file):
existing_results = pd.read_csv(csv_file).to_dict("records")
else:
existing_results = []
new_results = []
for dataset_name in args.dataset_names:
logger.debug(f"Evaluating dataset: {dataset_name}")
handlers = [
# VLAHandler(
# args.exp_dir,
# dataset_name,
# args.num_batches,
# args.batch_size,
# args.log_frequency,
# ),
HDF5Handler(
args.exp_dir,
dataset_name,
args.num_batches,
args.batch_size,
args.log_frequency,
),
# LeRobotHandler(
# args.exp_dir,
# dataset_name,
# args.num_batches,
# args.batch_size,
# args.log_frequency,
# ),
# RLDSHandler(
# args.exp_dir,
# dataset_name,
# args.num_batches,
# args.batch_size,
# args.log_frequency,
# ),
# FFV1Handler(
# args.exp_dir,
# dataset_name,
# args.num_batches,
# args.batch_size,
# args.log_frequency,
# ),
]
for handler in handlers:
handler.clear_cache()
handler.clear_os_cache()
avg_traj_size = handler.measure_average_trajectory_size()
random_load_time = handler.measure_random_loading_time()
new_results.append(
{
"Dataset": dataset_name,
"Format": f"{handler.dataset_type.upper()}",
"AverageTrajectorySize(MB)": avg_traj_size,
"LoadingTime(s)": random_load_time,
"AverageLoadingTime(s)": random_load_time / (args.num_batches + 1),
"Index": args.num_batches,
"BatchSize": args.batch_size,
}
)
logger.debug(
f"{handler.dataset_type.upper()} - Average Trajectory Size: {avg_traj_size:.2f} MB, Loading Time: {random_load_time:.2f} s"
)
# Combine existing and new results
all_results = existing_results + new_results
# Write all results to CSV
results_df = pd.DataFrame(all_results)
results_df.to_csv(csv_file, index=False)
logger.debug(f"Results appended to {csv_file}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Prepare and evaluate loading times and folder sizes for RLDS, VLA, and HDF5 formats."
)
parser.add_argument(
"--exp_dir", type=str, default=DEFAULT_EXP_DIR, help="Experiment directory."
)
parser.add_argument(
"--dataset_names",
nargs="+",
default=DEFAULT_DATASET_NAMES,
help="List of dataset names to evaluate.",
)
parser.add_argument(
"--log_frequency",
type=int,
default=DEFAULT_LOG_FREQUENCY,
help="Frequency of logging results.",
)
parser.add_argument(
"--num_batches",
type=int,
default=1000,
help="Number of batches to load for each loader.",
)
parser.add_argument(
"--batch_size", type=int, default=16, help="Batch size for loaders."
)
args = parser.parse_args()
evaluation(args)