-
Notifications
You must be signed in to change notification settings - Fork 25
/
tasks.py
619 lines (519 loc) · 21.5 KB
/
tasks.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
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
from invoke import task
ASSET_DATABASE_PATH = "procthor/databases/asset-database.json"
MATERIAL_DATABASE_PATH = "procthor/databases/material-database.json"
ASSET_IMAGE_SIZE = 450
#%% Utils
@task
def set_version(c, version):
"""Writes the version upon a release."""
for filename in ["setup.py", "procthor/__init__.py"]:
with open(filename, "r") as f:
file = f.read()
file = file.replace("<REPLACE_WITH_VERSION>", version)
with open(filename, "w") as f:
f.write(file)
'''
from ai2thor.controller import Controller
from PIL import Image
import numpy as np
from houses.constants import PROCTHOR_INITIALIZATION
from houses.databases import ASSET_DATABASE, MATERIAL_DATABASE, PLACEMENT_ANNOTATIONS
#%% Material Database
def reset_material_database():
controller = Controller(**PROCTHOR_INITIALIZATION)
update_material_database(controller)
update_material_visualizations()
def save_material_database(MATERIAL_DATABASE):
for materials in MATERIAL_DATABASE.values():
materials.sort()
with open(MATERIAL_DATABASE_PATH, "w") as f:
f.write(json.dumps(MATERIAL_DATABASE, indent=4, sort_keys=True))
def update_material_database(controller: Controller) -> None:
controller.reset(scene="Procedural")
event = controller.step(action="GetMaterials")
save_material_database(event.metadata["actionReturn"])
def update_material_visualizations():
controller = Controller(branch="nanna-cmb", scene="Procedural")
for material_group, materials in MATERIAL_DATABASE.items():
print(material_group)
for material in materials:
print("|", end="")
controller.reset()
controller.step(
action="CreateMaterialBall",
materialGroup=material_group,
material=material,
)
controller.step(
action="AddThirdPartyCamera",
position=dict(x=0, y=0, z=-0.85),
rotation=dict(x=0, y=0, z=0),
skyboxColor="white",
)
os.makedirs(f"materials/{material_group}", exist_ok=True)
image = Image.fromarray(controller.last_event.third_party_camera_frames[0])
image.save(f"materials/{material_group}/{material}.png")
shutil.make_archive("materials", "zip", "materials")
#%% Asset Database
def reset_asset_database():
controller = Controller(**PROCTHOR_INITIALIZATION)
update_asset_database_by_type(controller)
set_scenes_per_asset(controller)
set_object_states(controller)
set_materials_per_asset_id()
create_splits()
update_asset_images()
def save_asset_database(asset_database_by_type: dict):
for assets in asset_database_by_type.values():
assets.sort(key=lambda asset: asset["assetId"])
with open(ASSET_DATABASE_PATH, "w") as f:
f.write(json.dumps(asset_database_by_type, indent=4, sort_keys=True))
def create_splits() -> None:
overrides = 0
for asset_type, assets in ASSET_DATABASE.items():
assets.sort(key=lambda asset: asset["assetId"])
random.seed(42)
if len(assets) < 6:
for asset in assets:
asset["split"] = None
else:
random.shuffle(assets)
train, val, test = np.split(
assets, [int(len(assets) * 2 / 3), int(len(assets) * 5 / 6)]
)
for asset in train:
asset["split"] = "train"
for asset in val:
asset["split"] = "val"
for asset in test:
asset["split"] = "test"
# manual overrides
for asset in assets:
if asset_type == "Fridge":
if asset["assetId"] == "Fridge_11":
assert asset["split"] == "train"
asset["split"] = "test"
overrides += 1
elif asset["assetId"] == "Fridge_1":
assert asset["split"] == "test"
asset["split"] = "train"
overrides += 1
elif asset_type == "Doorway":
if asset["assetId"] == "Doorway_8":
assert asset["split"] == "test"
asset["split"] = "train"
overrides += 1
elif asset["assetId"] == "Doorway_Double_3":
assert asset["split"] == "train"
asset["split"] = "test"
overrides += 1
assert overrides == 4
save_asset_database(ASSET_DATABASE)
def update_asset_database_by_type(controller: Controller) -> None:
"""Set the initial metadata in the asset database and fix each asset's bounding boxes."""
controller.reset(scene="Procedural")
# _asset_database contains un-filtered objects
_asset_database = controller.step(action="GetAssetDatabase").metadata[
"actionReturn"
]
asset_database_by_type = defaultdict(list)
for asset_id, asset_meta in _asset_database.items():
if asset_id.endswith("_Master"):
# these are duplicate prefabs
continue
asset_meta["assetId"] = asset_id
asset_database_by_type[asset_meta["objectType"]].append(asset_meta)
# remove unnecessary objects
del asset_database_by_type["EggCracked"]
del asset_database_by_type["StoveKnob"]
# fix bounding boxes
for asset_type in sorted(asset_database_by_type.keys()):
print(asset_type, end=": ")
for i, asset in enumerate(asset_database_by_type[asset_type]):
print("|", end="")
asset_id = asset["assetId"]
controller.reset()
controller.step(
action="SpawnAsset", assetId=asset_id, generatedId="asset_0"
)
asset = next(
obj
for obj in controller.last_event.metadata["objects"]
if obj["objectId"] == "asset_0"
)
bb = asset["axisAlignedBoundingBox"]["size"]
asset_database_by_type[asset_type][i]["boundingBox"] = bb
print()
save_asset_database(asset_database_by_type)
def update_bounding_box():
controller = Controller(**PROCTHOR_INITIALIZATION)
for obj_type in ["Bed", "TennisRacket"]:
for i in range(len(ASSET_DATABASE[obj_type])):
controller.step(action="DestroyHouse")
asset = ASSET_DATABASE[obj_type][i]
controller.step(
action="SpawnAsset",
assetId=asset["assetId"],
generatedId=asset["assetId"],
)
asset = next(
obj
for obj in controller.last_event.metadata["objects"]
if obj["objectId"] == asset["assetId"]
)
bb = asset["axisAlignedBoundingBox"]["size"]
ASSET_DATABASE[obj_type][i]["boundingBox"] = bb
save_asset_database(ASSET_DATABASE)
def set_materials_per_asset_id():
"""Update each asset with the set of materials it has from the material database."""
controller = Controller(branch="nanna-cmb", scene="Procedural")
material_id_to_group = {
material: material_group
for material_group, materials in MATERIAL_DATABASE.items()
for material in materials
}
for asset_group, assets in ASSET_DATABASE.items():
print(asset_group, end=": ")
for asset in assets:
print("|", end="")
controller.step(
action="SpawnAsset", assetId=asset["assetId"], generatedId="asset_0"
)
event = controller.step(action="GetMaterialsOnObject", objectId="asset_0")
materials = [
[material_id_to_group[material], material]
for material in event.metadata["actionReturn"]
if material in material_id_to_group
]
asset["materials"] = materials
controller.step(action="DisableObject", objectId="asset_0")
print()
save_asset_database(ASSET_DATABASE)
def set_scenes_per_asset(controller: Controller):
"""Update the asset database with each scene that each assetId appears in."""
asset_id_to_scenes = defaultdict(list)
scene_names = [
scene[: -len("_physics")] if scene.endswith("_physics") else scene
for scene in controller.scene_names()
]
for i, scene in enumerate(scene_names):
print(f"{i}/{len(scene_names)}")
event = controller.reset(scene, procedural=False)
asset_ids = set(
[obj["assetId"] for obj in event.metadata["objects"] if obj["assetId"]]
)
for asset_id in asset_ids:
asset_id_to_scenes[asset_id].append(scene)
for assets in ASSET_DATABASE.values():
for asset in assets:
asset["scenes"] = asset_id_to_scenes[asset["assetId"]]
save_asset_database(ASSET_DATABASE)
def update_asset_images(
asset_types: Optional[Set[str]] = None,
asset_ids: Optional[Set[str]] = None,
sides: Optional[Set[str]] = None,
) -> None:
controller = Controller(
cameraNearPlane=1e-10,
cameraFarPlane=1e5,
makeAgentsVisible=False,
renderInstanceSegmentation=True,
width=ASSET_IMAGE_SIZE,
height=ASSET_IMAGE_SIZE,
renderDepthImage=True,
**PROCTHOR_INITIALIZATION,
)
i_obj = 0
total_objects = sum([len(objs) for objs in ASSET_DATABASE.values()])
LARGE_DISTANCE = 20
base_dir = "images/orthographic"
for asset_type in sorted(ASSET_DATABASE.keys()):
if asset_types is not None and asset_type not in asset_types:
continue
print(asset_type, end=": ")
folder = f"{base_dir}/{asset_type}"
os.makedirs(folder, exist_ok=True)
for asset in ASSET_DATABASE[asset_type]:
asset_id = asset["assetId"]
if asset_ids is not None and asset_id not in asset_ids:
continue
i_obj += 1
print("|", end="")
controller.reset()
controller.step(
action="RandomizeLighting",
randomizeColor=False,
brightness=(1.25, 1.25),
)
controller.step(
action="SpawnAsset", assetId=asset_id, generatedId="asset_0"
)
asset = next(
obj
for obj in controller.last_event.metadata["objects"]
if obj["objectId"] == "asset_0"
)
bb = asset["axisAlignedBoundingBox"]["size"]
controller.step(
action="AddThirdPartyCamera",
position=dict(x=0, y=0, z=0),
rotation=dict(x=0, y=0, z=0),
orthographic=True,
skyboxColor="white",
orthographicSize=max(
bb["x"],
bb["z"],
bb["y"],
)
* 0.75,
)
camera_sides = {
"bottom": dict(x=270, y=180, z=0),
"top": dict(x=90, y=180, z=0),
"front": dict(x=0, y=180, z=0),
"back": dict(x=0, y=0, z=0),
"right": dict(x=0, y=270, z=0),
"left": dict(x=0, y=90, z=0),
}
for side, rotation in camera_sides.items():
if sides is not None and side not in sides:
continue
controller.step(
action="TeleportObject",
objectId="asset_0",
position=asset["position"],
rotation=rotation,
forceAction=True,
)
asset = next(
obj
for obj in controller.last_event.metadata["objects"]
if obj["objectId"] == "asset_0"
)
bb = asset["axisAlignedBoundingBox"]
controller.step(
action="UpdateThirdPartyCamera",
position=dict(
x=bb["center"]["x"], y=bb["center"]["y"], z=-LARGE_DISTANCE
),
)
# save the transparent frame
event = controller.last_event
rgb_frame = event.third_party_camera_frames[0]
r, g, b = np.rollaxis(rgb_frame, axis=-1)
seg_frame = event.third_party_instance_segmentation_frames[0]
a = np.zeros_like(r)
for key in event.object_id_to_color:
if key.startswith("asset_0"):
a += (
np.all(
seg_frame == event.object_id_to_color[key], axis=-1
).astype(np.uint8)
* 255
)
depth_frame = event.third_party_depth_frames[0]
a += (depth_frame == 0).astype(np.uint8) * 255
Image.fromarray(np.stack([r, g, b, a], axis=2), "RGBA").save(
f"{folder}/{asset_id}-{side}.png"
)
print(f" {i_obj}/{total_objects}")
shutil.make_archive(base_dir, "zip", base_dir)
def add_max_image_pixel_length():
for asset_type, assets in ASSET_DATABASE.items():
print(asset_type)
sides = ["back", "bottom", "front", "left", "right", "top"]
max_asset_type_lengths = dict()
for asset in assets:
asset_id = asset["assetId"]
max_length = 0
for side in sides:
im_path = f"orthographic/{asset_type}/{asset_id}-{side}.png"
im = Image.open(im_path)
bbox = im.getbbox()
if bbox is None:
# ignores fully transparent images
continue
left, upper, right, lower = bbox
width = right - left
height = lower - upper
if width > max_length:
max_length = width
if height > max_length:
max_length = height
if max_length == 0:
print(f"Zero length with assetId {asset_id}!")
asset["maxImagePixelLength"] = max_length / ASSET_IMAGE_SIZE
max_asset_type_lengths[asset_id] = max_length / ASSET_IMAGE_SIZE
save_asset_database(ASSET_DATABASE)
def set_object_states(controller):
"""Set properties in the metadata for different object states."""
def encompass_points(bbox, points) -> None:
for point in points:
px, py, pz = point
for k, p in zip(["x", "y", "z"], [px, py, pz]):
if p < bbox["min"][k]:
bbox["min"][k] = p
if p > bbox["max"][k]:
bbox["max"][k] = p
controller.reset(scene="Procedural")
for asset_group, assets in ASSET_DATABASE.items():
for i in range(len((assets))):
ASSET_DATABASE[asset_group][i]["states"] = dict()
for object_type in ASSET_DATABASE:
print(object_type)
for i, asset in enumerate(ASSET_DATABASE[object_type]):
if "CanOpen" not in asset["secondaryProperties"]:
continue
print("|", end="")
controller.reset()
assert controller.step(
action="SpawnAsset",
assetId=asset["assetId"],
generatedId="asset_0",
raise_for_failure=True,
)
for obj in controller.last_event.metadata["objects"]:
if obj["openable"]:
assert controller.step(
action="OpenObject",
objectId=obj["objectId"],
forceAction=True,
)
bbox = {
"min": dict(x=float("inf"), y=float("inf"), z=float("inf")),
"max": dict(x=float("-inf"), y=float("-inf"), z=float("-inf")),
}
for obj in controller.last_event.metadata["objects"]:
encompass_points(bbox, obj["axisAlignedBoundingBox"]["cornerPoints"])
bbox_size = {k: bbox["max"][k] - bbox["min"][k] for k in ["x", "y", "z"]}
assert any(bbox_size[k] > 1e-3 for k in ["x", "y", "z"])
ASSET_DATABASE[object_type][i]["states"]["open"] = dict(
boundingBox=bbox_size
)
print()
save_asset_database(ASSET_DATABASE)
if __name__ == "__main__":
controller = Controller(branch="nanna")
set_object_states(controller)
#%%
def save_ai2thor_object_metadata():
"""Save the starting object metadata in each AI2-THOR scene."""
controller = Controller(**PROCTHOR_INITIALIZATION)
scenes = {
"kitchens": [f"FloorPlan{i}" for i in range(1, 31)],
"living_rooms": [f"FloorPlan{200 + i}" for i in range(1, 31)],
"bedrooms": [f"FloorPlan{300 + i}" for i in range(1, 31)],
"bathrooms": [f"FloorPlan{400 + i}" for i in range(1, 31)],
"robothor": (
[f"FloorPlan_Train{i}_{j}" for i in range(1, 13) for j in range(1, 6)]
+ [f"FloorPlan_Val{i}_{j}" for i in range(1, 4) for j in range(1, 6)]
),
}
out = dict()
for scene_group, scenes in scenes.items():
object_meta = []
for scene in scenes:
event = controller.reset(scene=scene, procedural=False)
object_meta.append(event.metadata["objects"])
out[scene_group] = object_meta
with open("procthor/databases/ai2thor-object-metadata.json", "w") as f:
f.write(json.dumps(out, indent=4, sort_keys=True))
#%%
def assign_object_groups():
placeable_object_types = PLACEMENT_ANNOTATIONS[
(
(PLACEMENT_ANNOTATIONS["isPickupable"] == False)
& (PLACEMENT_ANNOTATIONS["onWall"] == False)
)
| (
(
(PLACEMENT_ANNOTATIONS["isPickupable"] == True)
| (PLACEMENT_ANNOTATIONS["onWall"] == True)
)
& (PLACEMENT_ANNOTATIONS["onFloor"] == True)
)
]
object_groups = {
obj_type: [asset["assetId"] for asset in ASSET_DATABASE[obj_type]]
for obj_type in placeable_object_types.index
}
with open("procthor/databases/object-groups.json", "w") as f:
f.write(json.dumps(object_groups, indent=4, sort_keys=True))
import json
from collections import Counter, defaultdict
#%%
from houses.databases import AI2THOR_OBJECT_METADATA, PLACEMENT_ANNOTATIONS
def save_receptacles():
used_asset_types = set(PLACEMENT_ANNOTATIONS.index)
receptacle_types = set()
for room_type, scenes in AI2THOR_OBJECT_METADATA.items():
for objects in scenes:
for obj in objects:
if obj["parentReceptacles"]:
parents = obj["parentReceptacles"]
for parent in parents:
parent_type = parent[: parent.find("|")]
if (
parent_type in used_asset_types
and obj["objectType"] in used_asset_types
):
receptacle_types.add(parent_type)
receptacle_counts = defaultdict(Counter)
for room_type, scenes in AI2THOR_OBJECT_METADATA.items():
for objects in scenes:
for obj in objects:
if obj["objectType"] in receptacle_types:
receptacle_counts[obj["objectType"]]["_count"] += 1
if obj["parentReceptacles"]:
parents = obj["parentReceptacles"]
for parent in parents:
parent_type = parent[: parent.find("|")]
if (
parent_type in receptacle_types
and obj["objectType"] in used_asset_types
and not obj["objectType"]
in {
"Sofa",
"Television",
"CounterTop",
"Chair",
"DiningTable",
"ShelvingUnit",
"SideTable",
"Toilet",
"Desk",
"Sink",
"Fridge",
}
):
receptacle_counts[parent_type][obj["objectType"]] += 1
out = defaultdict(dict)
for receptacle, child_objects in receptacle_counts.items():
parent_count = child_objects["_count"]
for key, child_count in child_objects.items():
if key == "_count":
continue
out[receptacle][key] = {
"p": child_count / parent_count,
"count": child_count,
}
out = dict(out)
with open("procthor/databases/receptacles.json", "w") as f:
f.write(json.dumps(out, indent=2, sort_keys=True))
#%% wall holes
def set_wall_holes():
import json
from ai2thor.controller import Controller
from houses.constants import PROCTHOR_INITIALIZATION
from houses.databases import ASSET_DATABASE
controller = Controller(**PROCTHOR_INITIALIZATION)
window_ids = [obj["assetId"] for obj in ASSET_DATABASE["Window"]]
door_ids = [obj["assetId"] for obj in ASSET_DATABASE["Doorway"]]
hole_ids = window_ids + door_ids
out = dict()
for hole_id in hole_ids:
event = controller.step(action="GetAssetHoleMetadata", assetId=hole_id)
out[hole_id] = event.metadata["actionReturn"]
with open("procthor/databases/wall-holes.json", "w") as f:
f.write(json.dumps(out, indent=4, sort_keys=True))
'''