-
Notifications
You must be signed in to change notification settings - Fork 5
/
collect_data.py
535 lines (450 loc) · 17.7 KB
/
collect_data.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import os
import random
import time
from collections import defaultdict, deque
import h5py
import numpy as np
import rlbench.backend.task as rlbench_task
from absl import app, flags
from rlbench import ObservationConfig
from rlbench.action_modes.action_mode import MoveArmThenGripper
from rlbench.action_modes.arm_action_modes import JointVelocity
from rlbench.action_modes.gripper_action_modes import Discrete
from rlbench.backend.const import *
from rlbench.backend.utils import task_file_to_task_class
from rlbench.environment import Environment
from tqdm.auto import tqdm, trange
from data.utils import convert_keypoints, keypoint_discovery
FLAGS = flags.FLAGS
flags.DEFINE_string("save_path", "./data/keypoint/", "Where to save the demos.")
flags.DEFINE_list(
"tasks",
[
"reach_target",
"phone_on_base",
"pick_and_lift",
"pick_up_cup",
"put_rubbish_in_bin",
"stack_wine",
"take_lid_off_saucepan",
"take_umbrella_out_of_umbrella_stand",
],
"The tasks to collect. If empty, all tasks are collected.",
)
flags.DEFINE_list("image_size", [256, 256], "The size of the images tp save.")
flags.DEFINE_integer(
"train_episodes_per_task", 10, "The number of episodes to collect per task."
)
flags.DEFINE_integer(
"val_episodes_per_task", 1, "The number of episodes to collect per task."
)
flags.DEFINE_integer(
"variations", -1, "Number of variations to collect per task. -1 for all."
)
flags.DEFINE_integer("num_frames", 4, "Number of frames to stack.")
flags.DEFINE_integer("vox_size", 16, "Voxel size to discretize translation.")
flags.DEFINE_integer(
"rotation_resolution", 5, "Rotation resolution to discretize rotation."
)
# fmt: off
# ['left_shoulder_depth', 'left_shoulder_mask', 'left_shoulder_point_cloud', 'right_shoulder_depth', 'right_shoulder_mask', 'right_shoulder_point_cloud', 'overhead_depth', 'overhead_rgb', 'overhead_mask', 'overhead_point_cloud', 'wrist_depth', 'wrist_mask', 'wrist_point_cloud', 'front_depth', 'front_mask', 'front_point_cloud', 'joint_forces', 'gripper_pose', 'gripper_matrix', 'gripper_joint_positions', 'gripper_touch_forces', 'task_low_dim_state', 'misc',
# 'front_rgb', 'left_shoulder_rgb', 'right_shoulder_rgb', 'wrist_rgb', 'joint_positions', 'gripper_open', 'joint_velocities']
# fmt: on
TO_BE_REMOVED = ["misc", "task_low_dim_state"]
TO_BE_ADDED = [
"cont_action",
"disc_action",
"time",
"gripper_pose_delta",
"task_id",
"variation_id",
# "ignore_collisions",
]
def get_shape_dtype(k, dummy_timestep):
if k == "gripper_open" or k == "time":
v_dtype = np.float32
v_shape = (1,)
elif k == "cont_action":
v_dtype = np.float32
v_shape = (8,)
elif k == "disc_action":
v_dtype = np.int32
v_shape = (7,)
elif k == "gripper_pose_delta":
v_dtype = np.float32
v_shape = (7,)
elif k == "task_id":
v_dtype = h5py.special_dtype(vlen=np.dtype("uint8"))
v_shape = ()
elif k == "variation_id":
v_dtype = np.uint8
v_shape = (1,)
elif k == "ignore_collisions":
v_dtype = np.uint8
v_shape = (1,)
else:
v_dtype = dummy_timestep.__dict__[k].dtype
v_shape = dummy_timestep.__dict__[k].shape
return v_shape, v_dtype
def create_hdf5(rlbench_env, task, hdf5_name, hdf5_shuffled_name, size=int(1e5)):
dummy_task = rlbench_env.get_task(task)
dummy_demo = np.array(dummy_task.get_demos(1, live_demos=True)[0])
dummy_timestep = dummy_demo[0]
keys = list(dummy_timestep.__dict__.keys())
keys.extend(TO_BE_ADDED)
for key in TO_BE_REMOVED:
keys.remove(key)
h5_file = h5py.File(hdf5_name, "x")
h5_file_shuffled = h5py.File(hdf5_shuffled_name, "x")
for k in keys:
v_shape, v_dtype = get_shape_dtype(k, dummy_timestep)
h5_file.create_dataset(
k,
(size, FLAGS.num_frames, *v_shape),
dtype=v_dtype,
chunks=(16, FLAGS.num_frames, *v_shape),
)
h5_file_shuffled.create_dataset(
k,
(size, FLAGS.num_frames, *v_shape),
dtype=v_dtype,
chunks=(16, FLAGS.num_frames, *v_shape),
)
return h5_file, h5_file_shuffled
def collect_data(
rlbench_env,
task,
task_name,
tasks_with_problems,
h5_file,
h5_file_shuffled,
num_episodes,
):
variation_count = 0
total_timestep = 0
task_env = rlbench_env.get_task(task)
dummy_task = rlbench_env.get_task(task)
dummy_demo = np.array(dummy_task.get_demos(1, live_demos=True)[0])
dummy_timestep = dummy_demo[0]
keys = list(dummy_timestep.__dict__.keys())
keys.extend(TO_BE_ADDED)
for key in TO_BE_REMOVED:
keys.remove(key)
total_data = defaultdict(list)
var_target = task_env.variation_count()
if FLAGS.variations >= 0:
var_target = np.minimum(FLAGS.variations, var_target)
print("Task:", task_env.get_name(), "// Variation Target:", var_target)
while True:
if variation_count >= var_target:
break
task_env.set_variation(variation_count)
_, _ = task_env.reset()
abort_variation = False
for ex_idx in range(num_episodes):
print(
"Task:",
task_env.get_name(),
"// Variation:",
variation_count,
"// Demo:",
ex_idx,
)
attempts = 10
while attempts > 0:
try:
(demo,) = task_env.get_demos(amount=1, live_demos=True)
print(f"success. demo length {len(demo)}.")
except Exception as e:
attempts -= 1
if attempts > 0:
continue
problem = (
"Failed collecting task %s (variation: %d, "
"example: %d). Skipping this task/variation.\n%s\n"
% (task_env.get_name(), variation_count, ex_idx, str(e))
)
print(problem)
tasks_with_problems += problem
abort_variation = True
break
def add_more(time, demo, inital_obs, episode_keypoints):
obs = inital_obs
more_cont_action = []
more_disc_action = []
more_obs = []
more_timestep = []
for k, keypoint in enumerate(episode_keypoints):
obs_tp1 = demo[keypoint]
cont_action, disc_action = convert_keypoints(
demo,
episode_keypoints,
FLAGS.vox_size,
FLAGS.rotation_resolution,
)
more_obs.append(obs)
t = (1.0 - time / float(len(demo) - 1)) * 2.0 - 1.0
more_timestep.append(t)
more_cont_action.append(cont_action)
more_disc_action.append(disc_action)
obs = obs_tp1
t = keypoint
return more_obs, more_cont_action, more_disc_action, more_timestep
episode_keypoints = keypoint_discovery(demo)
cont_action_list = []
disc_action_list = []
obs_list = []
time_list = []
for i in range(len(demo) - 1):
obs = demo[i]
# If our starting point is past one of the keypoints, then remove it
while len(episode_keypoints) > 0 and i >= episode_keypoints[0]:
episode_keypoints = episode_keypoints[1:]
if len(episode_keypoints) == 0:
break
cont_action, disc_action = convert_keypoints(
demo, episode_keypoints
)
obs_list.append(obs)
cont_action_list.append(cont_action)
disc_action_list.append(disc_action)
t = (1.0 - i / float(len(demo) - 1)) * 2.0 - 1.0
time_list.append(t)
(
more_obs,
more_cont_action,
more_disc_action,
more_timestep,
) = add_more(i, demo, obs, episode_keypoints)
obs_list.extend(more_obs)
cont_action_list.extend(more_cont_action)
disc_action_list.extend(more_disc_action)
time_list.extend(more_timestep)
stack = defaultdict(lambda: deque([], maxlen=FLAGS.num_frames))
for idx, timestep in enumerate(obs_list):
for k in keys:
if k == "gripper_open":
v = np.array([timestep.__dict__[k]])
elif k == "cont_action":
v = cont_action_list[idx]
elif k == "disc_action":
v = disc_action_list[idx]
elif k == "time":
v = np.array([time_list[idx]])
elif k == "gripper_pose_delta":
timestep_tp1 = obs_list[min(idx + 1, len(obs_list) - 1)]
v = (
timestep_tp1.__dict__["gripper_pose"]
- timestep.__dict__["gripper_pose"]
)
elif k == "task_id":
v = np.frombuffer(
str(task_name).encode("utf-8"), dtype=np.uint8
)
elif k == "variation_id":
v = np.array([variation_count], dtype=np.uint8)
elif k == "ignore_collisions":
timestep_tm1 = obs_list[max(0, idx - 1)]
v = np.array(
[int(timestep_tm1.ignore_collisions)], dtype=np.uint8
)
else:
v = timestep.__dict__[k]
if idx == 0:
stack[k].extend([v] * FLAGS.num_frames)
else:
stack[k].append(v)
for k in keys:
total_data[k].append(np.stack(stack[k]))
total_timestep += 1
break
if abort_variation:
break
variation_count += 1
start = time.process_time()
for k in keys:
v_shape, v_dtype = get_shape_dtype(k, dummy_timestep)
h5_file[k][:total_timestep] = np.array(total_data[k])
h5_file[k].resize((total_timestep, FLAGS.num_frames, *v_shape))
h5_file_shuffled[k].resize((total_timestep, FLAGS.num_frames, *v_shape))
print(time.process_time() - start)
indices = list(range(total_timestep))
random.shuffle(indices)
for i, j in enumerate(tqdm(indices, desc="shuffling", ncols=0)):
for k in keys:
if k == "task_id":
h5_file_shuffled[k][i] = h5_file[k][j][0]
else:
h5_file_shuffled[k][i] = h5_file[k][j]
h5_file.close()
h5_file_shuffled.close()
def create_env():
img_size = list(map(int, FLAGS.image_size))
obs_config = ObservationConfig()
obs_config.set_all(True)
obs_config.right_shoulder_camera.image_size = img_size
obs_config.left_shoulder_camera.image_size = img_size
obs_config.overhead_camera.image_size = img_size
obs_config.wrist_camera.image_size = img_size
obs_config.front_camera.image_size = img_size
# Store depth as 0 - 1
obs_config.right_shoulder_camera.depth_in_meters = False
obs_config.left_shoulder_camera.depth_in_meters = False
obs_config.overhead_camera.depth_in_meters = False
obs_config.wrist_camera.depth_in_meters = False
obs_config.front_camera.depth_in_meters = False
# We want to save the masks as rgb encodings.
obs_config.left_shoulder_camera.masks_as_one_channel = False
obs_config.right_shoulder_camera.masks_as_one_channel = False
obs_config.overhead_camera.masks_as_one_channel = False
obs_config.wrist_camera.masks_as_one_channel = False
obs_config.front_camera.masks_as_one_channel = False
action_mode = MoveArmThenGripper(
arm_action_mode=JointVelocity(), gripper_action_mode=Discrete()
)
rlbench_env = Environment(action_mode, obs_config=obs_config, headless=True)
rlbench_env.launch()
return rlbench_env
def main(argv):
task_files = [
t.replace(".py", "")
for t in os.listdir(rlbench_task.TASKS_PATH)
if t != "__init__.py" and t.endswith(".py")
]
if len(FLAGS.tasks) > 0:
for t in FLAGS.tasks:
if t not in task_files:
raise ValueError(f"Task {t} not recognised!.")
task_files = FLAGS.tasks
tasks = [task_file_to_task_class(t) for t in task_files]
num_tasks = len(tasks)
tasks_with_problems = ""
rlbench_env = create_env()
for task_index in range(num_tasks):
task_name = FLAGS.tasks[task_index]
# train
train_hdf5_name = os.path.join(FLAGS.save_path, f"{task_name}_train.hdf5")
train_hdf5_shuffled_name = os.path.join(
FLAGS.save_path, f"{task_name}_train_shuffled.hdf5"
)
try:
os.remove(train_hdf5_name)
os.remove(train_hdf5_shuffled_name)
except OSError:
pass
h5_file, h5_file_shuffled = create_hdf5(
rlbench_env,
tasks[task_index],
train_hdf5_name,
train_hdf5_shuffled_name,
)
collect_data(
rlbench_env,
tasks[task_index],
task_name,
tasks_with_problems,
h5_file,
h5_file_shuffled,
num_episodes=FLAGS.train_episodes_per_task,
)
# val
val_hdf5_name = os.path.join(FLAGS.save_path, f"{task_name}_val.hdf5")
val_hdf5_shuffled_name = os.path.join(
FLAGS.save_path, f"{task_name}_val_shuffled.hdf5"
)
try:
os.remove(val_hdf5_name)
os.remove(val_hdf5_shuffled_name)
except OSError:
pass
h5_file, h5_file_shuffled = create_hdf5(
rlbench_env,
tasks[task_index],
val_hdf5_name,
val_hdf5_shuffled_name,
)
collect_data(
rlbench_env,
tasks[task_index],
task_name,
tasks_with_problems,
h5_file,
h5_file_shuffled,
num_episodes=FLAGS.val_episodes_per_task,
)
print(tasks_with_problems)
combine_multi_task(rlbench_env, tasks, num_tasks)
rlbench_env.shutdown()
test(argv)
def combine_multi_task(rlbench_env, tasks, num_tasks):
dummy_task = rlbench_env.get_task(tasks[0])
dummy_demo = np.array(dummy_task.get_demos(1, live_demos=True)[0])
dummy_timestep = dummy_demo[0]
keys = list(dummy_timestep.__dict__.keys())
keys.extend(TO_BE_ADDED)
for key in TO_BE_REMOVED:
keys.remove(key)
combine_hdf5_files(
rlbench_env, tasks, num_tasks, dummy_timestep, keys, split="train"
)
combine_hdf5_files(rlbench_env, tasks, num_tasks, dummy_timestep, keys, split="val")
def combine_hdf5_files(
rlbench_env, tasks, num_tasks, dummy_timestep, keys, split="train"
):
print("combining hdf5 files for", split)
total_data_fn = os.path.join(FLAGS.save_path, f"multi_task_{split}.hdf5")
total_data_shuffled_fn = os.path.join(
FLAGS.save_path, f"multi_task_{split}_shuffled.hdf5"
)
try:
os.remove(total_data_fn)
os.remove(total_data_shuffled_fn)
except OSError:
pass
total_data, total_data_shuffled = create_hdf5(
rlbench_env,
tasks[0],
total_data_fn,
total_data_shuffled_fn,
size=int(1e5) * num_tasks,
)
total_timestep = 0
for task_index in trange(num_tasks, desc="combining data", ncols=0):
task_name = FLAGS.tasks[task_index]
h5_file_name = os.path.join(
FLAGS.save_path, f"{task_name}_{split}_shuffled.hdf5"
)
h5_file = h5py.File(h5_file_name, "r")
h5_size = h5_file[keys[0]].shape[0]
for k in keys:
total_data[k][total_timestep : total_timestep + h5_size] = h5_file[k][:]
total_timestep += h5_file[k].shape[0]
for k in keys:
v_shape, v_dtype = get_shape_dtype(k, dummy_timestep)
total_data[k].resize((total_timestep, FLAGS.num_frames, *v_shape))
total_data_shuffled[k].resize((total_timestep, FLAGS.num_frames, *v_shape))
indices = list(range(total_timestep))
random.shuffle(indices)
for i, j in enumerate(tqdm(indices, desc="shuffling", ncols=0)):
for k in keys:
if k == "task_id":
total_data_shuffled[k][i] = total_data[k][j][0]
else:
total_data_shuffled[k][i] = total_data[k][j]
def test(argv):
h5_file = h5py.File(
os.path.join(FLAGS.save_path, "multi_task_train_shuffled.hdf5"), "r"
)
for k in [
"joint_velocities",
"joint_positions",
"gripper_pose",
"gripper_joint_positions",
"gripper_pose_delta",
]:
print(k, h5_file[k].shape)
for k in ["front_rgb", "left_shoulder_rgb", "right_shoulder_rgb", "wrist_rgb"]:
print(k, h5_file[k].shape)
if __name__ == "__main__":
app.run(main) # run and test
# app.run(test) # test only