-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacking_google_objects.py
More file actions
570 lines (522 loc) · 20.3 KB
/
Copy pathpacking_google_objects.py
File metadata and controls
570 lines (522 loc) · 20.3 KB
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
"""Packing Google Objects tasks."""
import os
import numpy as np
from cliport.tasks.task import Task
from cliport.utils import utils
import pybullet as p
class PackingSeenGoogleObjectsSeq(Task):
"""Packing Seen Google Objects Group base class and task."""
def __init__(self):
super().__init__()
self.max_steps = 6
self.lang_template = "pack the {obj} in the brown box"
self.task_completed_desc = "done packing objects."
self.object_names = self.get_object_names()
def get_object_names(self):
return {
'train': [
'alarm clock',
'android toy',
'ball puzzle',
'black and blue sneakers',
'black boot with leopard print',
'black fedora',
'black razer mouse',
'black sandal',
'black shoe with green stripes',
'black shoe with orange stripes',
'brown fedora',
'bull figure',
'butterfinger chocolate',
'c clamp',
'can opener',
'crayon box',
'dinosaur figure',
'dog statue',
'frypan',
'green and white striped towel',
'grey soccer shoe with cleats',
'hammer',
'hard drive',
'honey dipper',
'light brown boot with golden laces',
'lion figure',
'magnifying glass',
'mario figure',
'nintendo 3ds',
'nintendo cartridge',
'office depot box',
'orca plush toy',
'pepsi gold caffeine free box',
'pepsi max box',
'pepsi next box',
'pepsi wild cherry box',
'porcelain cup',
'porcelain salad plate',
'porcelain spoon',
'purple tape',
'red and white flashlight',
'red and white striped towel',
'red cup',
'rhino figure',
'rocket racoon figure',
'scissors',
'screwdriver',
'silver tape',
'spatula with purple head',
'spiderman figure',
'tablet',
'toy school bus',
'toy train',
'unicorn toy',
'white razer mouse',
'yoshi figure',
],
'val': [
'alarm clock',
'android toy',
'ball puzzle',
'black and blue sneakers',
'black boot with leopard print',
'black fedora',
'black razer mouse',
'black sandal',
'black shoe with green stripes',
'black shoe with orange stripes',
'brown fedora',
'bull figure',
'butterfinger chocolate',
'c clamp',
'can opener',
'crayon box',
'dinosaur figure',
'dog statue',
'frypan',
'green and white striped towel',
'grey soccer shoe with cleats',
'hammer',
'hard drive',
'honey dipper',
'light brown boot with golden laces',
'lion figure',
'magnifying glass',
'mario figure',
'nintendo 3ds',
'nintendo cartridge',
'office depot box',
'orca plush toy',
'pepsi gold caffeine free box',
'pepsi max box',
'pepsi next box',
'pepsi wild cherry box',
'porcelain cup',
'porcelain salad plate',
'porcelain spoon',
'purple tape',
'red and white flashlight',
'red and white striped towel',
'red cup',
'rhino figure',
'rocket racoon figure',
'scissors',
'screwdriver',
'silver tape',
'spatula with purple head',
'spiderman figure',
'tablet',
'toy school bus',
'toy train',
'unicorn toy',
'white razer mouse',
'yoshi figure',
],
'test': [
'alarm clock',
'android toy',
'ball puzzle',
'black and blue sneakers',
'black boot with leopard print',
'black fedora',
'black razer mouse',
'black sandal',
'black shoe with green stripes',
'black shoe with orange stripes',
'brown fedora',
'bull figure',
'butterfinger chocolate',
'c clamp',
'can opener',
'crayon box',
'dinosaur figure',
'dog statue',
'frypan',
'green and white striped towel',
'grey soccer shoe with cleats',
'hammer',
'hard drive',
'honey dipper',
'light brown boot with golden laces',
'lion figure',
'magnifying glass',
'mario figure',
'nintendo 3ds',
'nintendo cartridge',
'office depot box',
'orca plush toy',
'pepsi gold caffeine free box',
'pepsi max box',
'pepsi next box',
'pepsi wild cherry box',
'porcelain cup',
'porcelain salad plate',
'porcelain spoon',
'purple tape',
'red and white flashlight',
'red and white striped towel',
'red cup',
'rhino figure',
'rocket racoon figure',
'scissors',
'screwdriver',
'silver tape',
'spatula with purple head',
'spiderman figure',
'tablet',
'toy school bus',
'toy train',
'unicorn toy',
'white razer mouse',
'yoshi figure',
],
}
def reset(self, env):
super().reset(env)
# object names
object_names = self.object_names[self.mode]
# Add container box.
zone_size = self.get_random_size(0.2, 0.35, 0.2, 0.35, 0.05, 0.05)
zone_pose = self.get_random_pose(env, zone_size)
container_template = 'container/container-template.urdf'
half = np.float32(zone_size) / 2
replace = {'DIM': zone_size, 'HALF': half}
container_urdf = self.fill_template(container_template, replace)
env.add_object(container_urdf, zone_pose, 'fixed')
if os.path.exists(container_urdf): os.remove(container_urdf)
margin = 0.01
min_object_dim = 0.08
bboxes = []
# Construct K-D Tree to roughly estimate how many objects can fit inside the box.
# TODO(Mohit): avoid building K-D Trees
class TreeNode:
def __init__(self, parent, children, bbox):
self.parent = parent
self.children = children
self.bbox = bbox # min x, min y, min z, max x, max y, max z
def KDTree(node):
size = node.bbox[3:] - node.bbox[:3]
# Choose which axis to split.
split = size > 2 * min_object_dim
if np.sum(split) == 0:
bboxes.append(node.bbox)
return
split = np.float32(split) / np.sum(split)
split_axis = np.random.choice(range(len(split)), 1, p=split)[0]
# Split along chosen axis and create 2 children
cut_ind = np.random.rand() * \
(size[split_axis] - 2 * min_object_dim) + \
node.bbox[split_axis] + min_object_dim
child1_bbox = node.bbox.copy()
child1_bbox[3 + split_axis] = cut_ind - margin / 2.
child2_bbox = node.bbox.copy()
child2_bbox[split_axis] = cut_ind + margin / 2.
node.children = [
TreeNode(node, [], bbox=child1_bbox),
TreeNode(node, [], bbox=child2_bbox)
]
KDTree(node.children[0])
KDTree(node.children[1])
# Split container space with KD trees.
stack_size = np.array(zone_size)
stack_size[0] -= 0.01
stack_size[1] -= 0.01
root_size = (0.01, 0.01, 0) + tuple(stack_size)
root = TreeNode(None, [], bbox=np.array(root_size))
KDTree(root)
# Add Google Scanned Objects to scene.
object_points = {}
object_ids = []
bboxes = np.array(bboxes)
scale_factor = 5
object_template = 'google/object-template.urdf'
chosen_objs, repeat_category = self.choose_objects(object_names, len(bboxes))
object_descs = []
for i, bbox in enumerate(bboxes):
size = bbox[3:] - bbox[:3]
max_size = size.max()
position = size / 2. + bbox[:3]
position[0] += -zone_size[0] / 2
position[1] += -zone_size[1] / 2
shape_size = max_size * scale_factor
pose = self.get_random_pose(env, size)
# Add object only if valid pose found.
if pose[0] is not None:
# Initialize with a slightly tilted pose so that the objects aren't always erect.
slight_tilt = utils.q_mult(pose[1], (-0.1736482, 0, 0, 0.9848078))
ps = ((pose[0][0], pose[0][1], pose[0][2]+0.05), slight_tilt)
object_name = chosen_objs[i]
object_name_with_underscore = object_name.replace(" ", "_")
mesh_file = os.path.join(self.assets_root,
'google',
'meshes_fixed',
f'{object_name_with_underscore}.obj')
texture_file = os.path.join(self.assets_root,
'google',
'textures',
f'{object_name_with_underscore}.png')
try:
replace = {'FNAME': (mesh_file,),
'SCALE': [shape_size, shape_size, shape_size],
'COLOR': (0.2, 0.2, 0.2)}
urdf = self.fill_template(object_template, replace)
box_id = env.add_object(urdf, ps)
if os.path.exists(urdf):
os.remove(urdf)
object_ids.append((box_id, (0, None)))
texture_id = p.loadTexture(texture_file)
p.changeVisualShape(box_id, -1, textureUniqueId=texture_id)
p.changeVisualShape(box_id, -1, rgbaColor=[1, 1, 1, 1])
object_points[box_id] = self.get_mesh_object_points(box_id)
object_descs.append(object_name)
except Exception as e:
print("Failed to load Google Scanned Object in PyBullet")
print(object_name_with_underscore, mesh_file, texture_file)
print(f"Exception: {e}")
self.set_goals(object_descs, object_ids, object_points, repeat_category, zone_pose, zone_size)
for i in range(480):
p.stepSimulation()
def choose_objects(self, object_names, k):
repeat_category = None
return np.random.choice(object_names, k, replace=False), repeat_category
def set_goals(self, object_descs, object_ids, object_points, repeat_category, zone_pose, zone_size):
# Random picking sequence.
num_pack_objs = np.random.randint(1, len(object_ids))
object_ids = object_ids[:num_pack_objs]
true_poses = []
for obj_idx, (object_id, _) in enumerate(object_ids):
true_poses.append(zone_pose)
chosen_obj_pts = dict()
chosen_obj_pts[object_id] = object_points[object_id]
self.goals.append(([(object_id, (0, None))], np.int32([[1]]), [zone_pose],
False, True, 'zone',
(chosen_obj_pts, [(zone_pose, zone_size)]),
1 / len(object_ids)))
self.lang_goals.append(self.lang_template.format(obj=object_descs[obj_idx]))
# Only mistake allowed.
self.max_steps = len(object_ids)+1
class PackingUnseenGoogleObjectsSeq(PackingSeenGoogleObjectsSeq):
"""Packing Unseen Google Objects Sequence task."""
def __init__(self):
super().__init__()
def get_object_names(self):
return {
'train': [
'alarm clock',
'android toy',
'black boot with leopard print',
'black fedora',
'black razer mouse',
'black sandal',
'black shoe with orange stripes',
'bull figure',
'butterfinger chocolate',
'c clamp',
'can opener',
'crayon box',
'dog statue',
'frypan',
'green and white striped towel',
'grey soccer shoe with cleats',
'hard drive',
'honey dipper',
'magnifying glass',
'mario figure',
'nintendo 3ds',
'nintendo cartridge',
'office depot box',
'orca plush toy',
'pepsi gold caffeine free box',
'pepsi wild cherry box',
'porcelain cup',
'purple tape',
'red and white flashlight',
'rhino figure',
'rocket racoon figure',
'scissors',
'silver tape',
'spatula with purple head',
'spiderman figure',
'tablet',
'toy school bus',
],
'val': [
'ball puzzle',
'black and blue sneakers',
'black shoe with green stripes',
'brown fedora',
'dinosaur figure',
'hammer',
'light brown boot with golden laces',
'lion figure',
'pepsi max box',
'pepsi next box',
'porcelain salad plate',
'porcelain spoon',
'red and white striped towel',
'red cup',
'screwdriver',
'toy train',
'unicorn toy',
'white razer mouse',
'yoshi figure'
],
'test': [
'ball puzzle',
'black and blue sneakers',
'black shoe with green stripes',
'brown fedora',
'dinosaur figure',
'hammer',
'light brown boot with golden laces',
'lion figure',
'pepsi max box',
'pepsi next box',
'porcelain salad plate',
'porcelain spoon',
'red and white striped towel',
'red cup',
'screwdriver',
'toy train',
'unicorn toy',
'white razer mouse',
'yoshi figure'
],
}
class PackingSeenGoogleObjectsGroup(PackingSeenGoogleObjectsSeq):
"""Packing Seen Google Objects Group task."""
def __init__(self):
super().__init__()
self.lang_template = "pack all the {obj} objects in the brown box"
self.max_steps = 3
def choose_objects(self, object_names, k):
# Randomly choose a category to repeat.
chosen_objects = np.random.choice(object_names, k, replace=True)
repeat_category, distractor_category = np.random.choice(chosen_objects, 2, replace=False)
num_repeats = np.random.randint(2, 3)
chosen_objects[:num_repeats] = repeat_category
chosen_objects[num_repeats:2*num_repeats] = distractor_category
return chosen_objects, repeat_category
def set_goals(self, object_descs, object_ids, object_points, repeat_category, zone_pose, zone_size):
# Pack all objects of the chosen (repeat) category.
num_pack_objs = object_descs.count(repeat_category)
true_poses = []
chosen_obj_pts = dict()
chosen_obj_ids = []
for obj_idx, (object_id, info) in enumerate(object_ids):
if object_descs[obj_idx] == repeat_category:
true_poses.append(zone_pose)
chosen_obj_pts[object_id] = object_points[object_id]
chosen_obj_ids.append((object_id, info))
self.goals.append((
chosen_obj_ids, np.eye(len(chosen_obj_ids)), true_poses, False, True, 'zone',
(chosen_obj_pts, [(zone_pose, zone_size)]), 1))
self.lang_goals.append(self.lang_template.format(obj=repeat_category))
# Only one mistake allowed.
self.max_steps = num_pack_objs+1
class PackingUnseenGoogleObjectsGroup(PackingSeenGoogleObjectsGroup):
"""Packing Unseen Google Objects Group task."""
def __init__(self):
super().__init__()
def get_object_names(self):
return {
'train': [
'alarm clock',
'android toy',
'black boot with leopard print',
'black fedora',
'black razer mouse',
'black sandal',
'black shoe with orange stripes',
'bull figure',
'butterfinger chocolate',
'c clamp',
'can opener',
'crayon box',
'dog statue',
'frypan',
'green and white striped towel',
'grey soccer shoe with cleats',
'hard drive',
'honey dipper',
'magnifying glass',
'mario figure',
'nintendo 3ds',
'nintendo cartridge',
'office depot box',
'orca plush toy',
'pepsi gold caffeine free box',
'pepsi wild cherry box',
'porcelain cup',
'purple tape',
'red and white flashlight',
'rhino figure',
'rocket racoon figure',
'scissors',
'silver tape',
'spatula with purple head',
'spiderman figure',
'tablet',
'toy school bus',
],
'val': [
'ball puzzle',
'black and blue sneakers',
'black shoe with green stripes',
'brown fedora',
'dinosaur figure',
'hammer',
'light brown boot with golden laces',
'lion figure',
'pepsi max box',
'pepsi next box',
'porcelain salad plate',
'porcelain spoon',
'red and white striped towel',
'red cup',
'screwdriver',
'toy train',
'unicorn toy',
'white razer mouse',
'yoshi figure'
],
'test': [
'ball puzzle',
'black and blue sneakers',
'black shoe with green stripes',
'brown fedora',
'dinosaur figure',
'hammer',
'light brown boot with golden laces',
'lion figure',
'pepsi max box',
'pepsi next box',
'porcelain salad plate',
'porcelain spoon',
'red and white striped towel',
'red cup',
'screwdriver',
'toy train',
'unicorn toy',
'white razer mouse',
'yoshi figure'
],
}