-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_icl_demonstrations.py
395 lines (335 loc) · 14.7 KB
/
form_icl_demonstrations.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
import copy
import glob
import itertools
import os
import pickle
import random
import string
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import List
import numpy as np
from PIL import Image
from pyrep.objects import VisionSensor
from scipy.spatial.transform import Rotation
from tqdm import tqdm
from utils import _image_to_float_array, normalize_quaternion, point_to_voxel_index, quaternion_to_discrete_euler, CAMERAS
ROOT = "" # TODO: change this
SYSTEM_PROMPT = "You are a Franka Panda robot with a parallel gripper. We provide you with some demos in the format of observation>[action_1, action_2, ...]. Then you will receive a new observation and you need to output a sequence of actions that match the trends in the demos. Do not output anything else."
# discretize translation, rotation, gripper open
def _get_action(
obs_tp1,
obs_tm1):
quat = normalize_quaternion(obs_tp1.gripper_pose[3:])
if quat[-1] < 0:
quat = -quat
disc_rot = quaternion_to_discrete_euler(quat)
trans_indicies = []
ignore_collisions = int(obs_tm1.ignore_collisions)
index = point_to_voxel_index(
obs_tp1.gripper_pose[:3])
trans_indicies.extend(index.tolist())
rot_and_grip_indicies = disc_rot.tolist()
rot_and_grip_indicies.extend([int(obs_tp1.gripper_open)])
return trans_indicies + rot_and_grip_indicies
def _get_point_cloud_dict(epis_path, idx):
# This function gets the point cloud using the same operations as PerAct Colab Tutorial
DEPTH_SCALE = 2**24 - 1
point_cloud_dict = {}
for camera_type in CAMERAS:
with open(os.path.join(epis_path, 'low_dim_obs.pkl'), 'rb') as f:
demo = pickle.load(f)
cam_extrinsics = demo[idx].misc[f"{camera_type}_camera_extrinsics"]
cam_intrinsics = demo[idx].misc[f"{camera_type}_camera_intrinsics"]
cam_depth = _image_to_float_array(Image.open(os.path.join(epis_path, f"{camera_type}_depth", f"{idx}.png")), DEPTH_SCALE)
near = demo[idx].misc[f"{camera_type}_camera_near"]
far = demo[idx].misc[f"{camera_type}_camera_far"]
cam_depth = (far - near) * cam_depth + near
point_cloud_dict[camera_type] = VisionSensor.pointcloud_from_depth_and_camera_params(cam_depth, cam_extrinsics, cam_intrinsics) # reconstructed 3D point cloud in world coordinate frame
return point_cloud_dict
def _get_mask_dict(epis_path, idx):
mask_dict = {}
for camera in CAMERAS:
img = Image.open(os.path.join(epis_path, f"{camera}_mask", f"{idx}.png"))
rgb_mask = np.array(img, dtype=int)
mask_dict[camera] = rgb_mask[:, :, 0] + rgb_mask[:, :, 1]*256 + rgb_mask[:, :, 2]*256*256
return mask_dict
def _get_mask_id_to_name_dict(epis_path, idx):
with open(os.path.join(epis_path, "low_dim_obs.pkl"), "rb") as f:
low_dim_obs = pickle.load(f)
mask_id_to_name_dict = {}
for camera in CAMERAS:
mask_id_to_name_dict[camera] = low_dim_obs[idx].misc[f"{camera}_mask_id_to_name"]
return mask_id_to_name_dict
# add individual data points to replay
def _add_keypoints_to_replay(
buffer,
i,
demo,
episode_keypoints,
epis_path_depth,
epis_path_char,
sim_name_to_real_name
):
prev_action = None
cur_index = i
mask_dict = _get_mask_dict(epis_path_char, cur_index)
mask_id_to_sim_name_dict = _get_mask_id_to_name_dict(epis_path_char, cur_index)
point_cloud_dict = _get_point_cloud_dict(epis_path_depth, cur_index)
mask_id_to_sim_name = {}
for camera in CAMERAS:
mask_id_to_sim_name.update(mask_id_to_sim_name_dict[camera])
mask_id_to_real_name = {mask_id: sim_name_to_real_name[name] for mask_id, name in mask_id_to_sim_name.items()
if name in sim_name_to_real_name}
avg_coord = form_obs(mask_dict, mask_id_to_real_name, point_cloud_dict)
buffer.append(avg_coord)
actions = []
for k, keypoint in enumerate(episode_keypoints):
obs_tp1 = demo[keypoint]
action = _get_action(
obs_tp1, obs_tp1)
actions.append(action)
buffer.append(actions)
def _is_stopped(demo, i, obs, stopped_buffer, delta=0.1):
next_is_not_final = i == (len(demo) - 2)
gripper_state_no_change = (
i < (len(demo) - 2) and
(obs.gripper_open == demo[i + 1].gripper_open and
obs.gripper_open == demo[i - 1].gripper_open and
demo[i - 2].gripper_open == demo[i - 1].gripper_open))
small_delta = np.allclose(obs.joint_velocities, 0, atol=delta)
stopped = (stopped_buffer <= 0 and small_delta and
(not next_is_not_final) and gripper_state_no_change)
return stopped
def _keypoint_discovery(demo, delta=0.1) -> List[int]:
episode_keypoints = []
prev_gripper_open = demo[0].gripper_open
stopped_buffer = 0
for i, obs in enumerate(demo):
stopped = _is_stopped(demo, i, obs, stopped_buffer, delta)
stopped_buffer = 4 if stopped else stopped_buffer - 1
# if change in gripper, or end of episode.
last = i == (len(demo) - 1)
if i != 0 and (obs.gripper_open != prev_gripper_open or
last or stopped):
episode_keypoints.append(i)
prev_gripper_open = obs.gripper_open
if len(episode_keypoints) > 1 and (episode_keypoints[-1] - 1) == \
episode_keypoints[-2]:
episode_keypoints.pop(-2)
#print('Found %d keypoints.' % len(episode_keypoints), episode_keypoints)
return episode_keypoints
def get_stored_demos(dataset_root, task_name, amount, sim_name_to_real_name):
total_num_keypoints = 0
buffer = []
task_root = os.path.join(dataset_root, task_name, 'all_variations', 'episodes')
for epi_id in tqdm(range(amount)):
epis_path_depth = os.path.join(task_root, f'episode{epi_id}')
epis_path_char = os.path.join(task_root, f'episode{epi_id}')
with open(os.path.join(epis_path_depth, 'low_dim_obs.pkl'), 'rb') as f:
demo = pickle.load(f)
with open(os.path.join(epis_path_depth, 'variation_number.pkl'), 'rb') as f:
demo.variation_number = pickle.load(f)
# language description
with open(os.path.join(epis_path_depth, 'variation_descriptions.pkl'), 'rb') as f:
demo.language_descriptions = pickle.load(f)
episode_keypoints = _keypoint_discovery(demo)
tmp = []
_add_keypoints_to_replay(
tmp, 0, demo, episode_keypoints, epis_path_depth, epis_path_char, sim_name_to_real_name)
buffer.append(tmp)
print("Average number of steps: ", sum([len(each[1]) for each in buffer])/len(buffer))
return buffer
def form_obs(
mask_dict,
mask_id_to_real_name,
point_cloud_dict):
# convert object id to char and average and discretize point cloud per object
uniques = np.unique(np.concatenate(list(mask_dict.values()), axis=0))
real_name_to_avg_coord = {}
for _, mask_id in enumerate(uniques):
if mask_id not in mask_id_to_real_name:
continue
avg_point_list = []
for camera in CAMERAS:
mask = mask_dict[camera]
point_cloud = point_cloud_dict[camera]
if not np.any(mask == mask_id):
continue
avg_point_list.append(np.mean(point_cloud[mask == mask_id].reshape(-1, 3), axis = 0))
avg_point = sum(avg_point_list) / len(avg_point_list)
real_name = mask_id_to_real_name[mask_id]
real_name_to_avg_coord[real_name] = list(point_to_voxel_index(avg_point))
return str(real_name_to_avg_coord)
class base_task_handler:
def __init__(self, sim_name_to_real_name):
self.sim_name_to_real_name = sim_name_to_real_name
self.save_root = os.path.join(ROOT, type(self).__name__)
self.num_demos = 10
print(f"Task handler {type(self).__name__} using demonstrations from {self.save_root}")
random.seed(42)
def get_user_prompt(self, mask_dict, mask_id_to_sim_name, point_cloud_dict):
assert os.path.exists(self.save_root), f"Cannot find save root {self.save_root}"
mask_id_to_real_name = {mask_id: self.sim_name_to_real_name[name] for mask_id, name in mask_id_to_sim_name.items()
if name in self.sim_name_to_real_name}
obs = form_obs(mask_dict, mask_id_to_real_name, point_cloud_dict)
# during evaluation, randomly choose one batch of 10 demonstrations from the saved demonstrations
print(self.save_root)
path = random.choice(glob.glob(os.path.join(self.save_root, "demonstrations", "*.txt")))
demonstration = open(path, "r").read()
return demonstration + obs + ">"
def save_in_context_demonstrations(self):
train_demos = get_stored_demos(ROOT, type(self).__name__, 100, self.sim_name_to_real_name)
# iterate over 100 demonstrations, each time take 10 demonstrations
for i, start_idx in enumerate(range(0, len(train_demos), self.num_demos)):
if start_idx + self.num_demos <= len(train_demos):
output = ""
for epi in train_demos[start_idx:start_idx+self.num_demos]:
output += f"{epi[0]}>{epi[1]}, "
d = os.path.join(ROOT, type(self).__name__, f"demonstrations")
os.makedirs(d, exist_ok=True)
with open(os.path.join(d, f'{i}.txt'), "w") as f:
f.write(output)
class close_jar(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"jar_lid0": "lid",
"jar0": "jar",
}
super().__init__(sim_name_to_real_name)
class open_drawer(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"drawer_bottom": "drawer",
}
super().__init__(sim_name_to_real_name)
class slide_block_to_color_target(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"target1": "target",
"block": "block"
}
super().__init__(sim_name_to_real_name)
class sweep_to_dustpan_of_size(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"dustpan_tall": "dustpan",
"broom_holder": "broom holder"
}
super().__init__(sim_name_to_real_name)
class meat_off_grill(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"chicken_visual": "chicken",
"grill_visual": "grill"
}
super().__init__(sim_name_to_real_name)
class turn_tap(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"tap_left_visual": "left tap",
"tap_right_visual": "right tap"
}
super().__init__(sim_name_to_real_name)
class put_item_in_drawer(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"item": "item",
"drawer_frame": "drawer"
}
super().__init__(sim_name_to_real_name)
class stack_blocks(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"stack_blocks_target0": "first block",
"stack_blocks_target1": "second block",
"stack_blocks_target2": "third block",
"stack_blocks_target3": "fourth block",
"stack_blocks_target_plane": "plane",
}
super().__init__(sim_name_to_real_name)
class light_bulb_in(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"bulb0": "blub",
"lamp_screw": "lamp screw",
}
super().__init__(sim_name_to_real_name)
class put_money_in_safe(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"dollar_stack": "money",
"safe_body": "shelf",
}
super().__init__(sim_name_to_real_name)
class place_wine_at_rack_location(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"wine_bottle_visual": "wine",
"rack_top_visual": "rack",
}
super().__init__(system_prompt, sim_name_to_real_name, num_demos, num_keypoints)
class put_groceries_in_cupboard(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"cupboard": "cupboard",
"crackers_visual": "cracker",
}
super().__init__(sim_name_to_real_name)
class place_shape_in_shape_sorter(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"cube": "cube",
"shape_sorter": "shape sorter",
}
super().__init__(sim_name_to_real_name)
class push_buttons(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"target_button_wrap0": "button",
}
super().__init__(sim_name_to_real_name)
class insert_onto_square_peg(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"square_ring": "ring",
"pillar1": "spok",
}
super().__init__(sim_name_to_real_name)
class stack_cups(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"cup1_visual": "first cup",
"cup2_visual": "second cup",
"cup3_visual": "third cup",
}
super().__init__(sim_name_to_real_name)
class place_cups(base_task_handler):
def __init__(self):
sim_name_to_real_name = {
"mug_visual1": "cup",
"place_cups_holder_spoke0": "holder"
}
super().__init__(sim_name_to_real_name)
task_name_to_handler = {"close_jar": close_jar,
"open_drawer": open_drawer,
"slide_block_to_color_target": slide_block_to_color_target,
"sweep_to_dustpan_of_size": sweep_to_dustpan_of_size,
"meat_off_grill": meat_off_grill,
"turn_tap": turn_tap,
"put_item_in_drawer": put_item_in_drawer,
"stack_blocks": stack_blocks,
"light_bulb_in": light_bulb_in,
"put_money_in_safe": put_money_in_safe,
"place_wine_at_rack_location": place_wine_at_rack_location,
"put_groceries_in_cupboard": put_groceries_in_cupboard,
"place_shape_in_shape_sorter": place_shape_in_shape_sorter,
"push_buttons": push_buttons,
"stack_cups": stack_cups,
"place_cups": place_cups
}
def create_task_handler(task_name):
return task_name_to_handler[task_name]()
if __name__ == "__main__":
for class_name in task_name_to_handler.values():
handler = class_name()
handler.save_in_context_demonstrations()