Spaces:
Running
Running
File size: 52,638 Bytes
9ee6f68 | 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 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 | """
outcome_based_task_generation/outcome_based_task_generation.py
This module provides classes and functions for generating outcome-based tasks given ManiTaskOT-200.
Not finished, mainly for the tasks involving 1 type of object/platform/category.
"""
import re
import glog
import random
import numpy as np
from src.utils.string_convertor import StringConvertor
from src.core.gen_scene_graph import Tree
from src.vlm_interaction.interact_prompt_helper import FeasibilityPromptFormatter
from src.utils.config_manager import (
get_config_manager,
get_config,
get_outcome_based_task_generation_config,
)
# NOTE: VLM-related imports (VLMInteractor / TaskFeasibilityEvaluator) are done
# lazily inside VLMVoter.__init__ so that the no-VLM entry points (e.g. the
# Gradio demo) can import and use OutcomeBasedTaskGenerator without the
# VLMEvalKit dependency installed.
import os
import pickle
class OBTSurfaceObject:
def __init__(self, object_index: int, surface_object_name: str = None):
"""
Initializes the OutcomeBasedTaskSurfaceObject with a specific object index.
Args:
object_index (int): The index of the object.
"""
self.object_index = object_index
self.surface_object_name = surface_object_name
self.major_category = StringConvertor.get_category(surface_object_name)
self.minor_category = StringConvertor.get_name_wo_id(surface_object_name)
self.name = surface_object_name
class OBTPlatform:
def __init__(
self,
platform,
platform_index: int,
platform_name: str = None,
children_list: list = [],
):
"""
Initializes the OutcomeBasedTaskPlatform with a specific platform index.
Args:
platform_index (int): The index of the platform.
"""
self.platform_index = platform_index
self.platform_name = platform_name
major_category_list = []
minor_category_list = []
single_object_list = []
for child in children_list:
if child.major_category not in major_category_list:
major_category_list.append(child.major_category)
if child.minor_category not in minor_category_list:
minor_category_list.append(child.minor_category)
if child.surface_object_name not in single_object_list:
single_object_list.append(child.surface_object_name)
# unify the major category and minor category
self.major_category_list = major_category_list
self.minor_category_list = minor_category_list
self.single_object_list = single_object_list
def is_available(self, major_num=0, minor_num=0, single_num=0):
"""
Checks if the platform is available based on the number of categories and objects.
Args:
major_num (int): The number of major categories.
minor_num (int): The number of minor categories.
single_num (int): The number of single objects.
Returns:
bool: True if the platform is available, False otherwise.
"""
return (
len(self.major_category_list) >= major_num
and len(self.minor_category_list) >= minor_num
and len(self.single_object_list) >= single_num
)
def random_pick(self, major_num=0, minor_num=0, single_num=0):
"""
Randomly picks categories and objects from the platform.
Args:
major_num (int): The number of major categories to pick.
minor_num (int): The number of minor categories to pick.
single_num (int): The number of single objects to pick.
Returns:
tuple: A tuple containing the picked major categories, minor categories, and single objects.
"""
major_category = random.sample(self.major_category_list, major_num)
minor_category = random.sample(self.minor_category_list, minor_num)
single_object = random.sample(self.single_object_list, single_num)
return major_category, minor_category, single_object
def add_major_category(self, category):
self.major_category_list.append(category)
def add_minor_category(self, category):
self.minor_category_list.append(category)
def add_single_object(self, obj):
self.single_object_list.append(obj)
class OBTMultilayerObject:
def __init__(
self,
object_index: int,
multilayer_object_name: str = None,
children_list: list = [],
layer_num: int = 0,
):
"""
Initializes the OutcomeBasedTaskMultilayerObject with a specific object index.
Args:
object_index (int): The index of the object.
"""
self.object_index = object_index
self.major_category_list = []
self.minor_category_list = []
self.layer_num = layer_num
self.multilayer_object_name = multilayer_object_name
for child in children_list:
if child.major_category not in self.major_category_list:
self.major_category_list.append(child.major_category)
if child.minor_category not in self.minor_category_list:
self.minor_category_list.append(child.minor_category)
def is_available(self, major_num=0, minor_num=0):
"""
Checks if the multilayer object is available based on the number of categories and objects.
Args:
major_num (int): The number of major categories.
minor_num (int): The number of minor categories.
Returns:
bool: True if the multilayer object is available, False otherwise.
"""
return (
len(self.major_category_list) >= major_num
and len(self.minor_category_list) >= minor_num
)
def random_pick(self, requirement: dict):
top_layer_name = None
specific_layer_name = None
category_name_list = []
small_category_name_list = []
if requirement is None:
return {
"top_layer_name": None,
"specific_layer_name": None,
"category_name_list": None,
"small_category_name_list": None,
}
if "requires_top_layer" in requirement and requirement["requires_top_layer"]:
# Randomly pick from the top layer
top_layer_name = self.multilayer_object_name + "_top_layer"
pass
if (
"requires_specific_layer" in requirement
and requirement["requires_specific_layer"]
):
# Randomly pick from the specific layer
specific_layer_name = (
self.multilayer_object_name
+ f"_{random.randint(0,self.layer_num - 1)}th_layer"
)
pass
if (
"category_objects_count" in requirement
and requirement["category_objects_count"] > 0
):
category_name_list = random.sample(
self.major_category_list, requirement["category_objects_count"]
)
if (
"small_category_objects_count" in requirement
and requirement["small_category_objects_count"] > 0
):
small_category_name_list = random.sample(
self.minor_category_list, requirement["small_category_objects_count"]
)
return {
"top_layer_name": top_layer_name,
"specific_layer_name": specific_layer_name,
"category_name_list": category_name_list,
"small_category_name_list": small_category_name_list,
}
class VLMVoter:
def __init__(self):
# You have to set up your API keys and the environment variables first.
# Lazy imports: pulling these in at module level would force every
# consumer of this module (including the VLM-free demo) to install
# VLMEvalKit.
from src.vlm_interaction.vlm_interactor import VLMInteractor
from src.core.task_feasibility_evaluator import TaskFeasibilityEvaluator
self.global_config = get_config()
self.config = get_outcome_based_task_generation_config()
self.vlm_list = self.config.vlm_list
self.evaluator = TaskFeasibilityEvaluator()
self.vlm_interactor = VLMInteractor(mode="online")
def is_task_feasible(self, task, scene_graph):
feasible_cnt = 0
for vlm in self.vlm_list:
self.vlm_interactor.change_model_name(vlm)
self.vlm_interactor.model_name = vlm # only change the model name
vote_str = self.evaluator.evaluate_task_feasibility(
self.vlm_interactor,
task,
scene_graph,
width=512,
height=512,
save_path=self.global_config.image4vote_path,
)
feasible_cnt += 1 if vote_str == "Feasible" else 0
return feasible_cnt >= (len(self.vlm_list) + 1) // 2
class OutcomeBasedTaskPattern:
def __init__(self, task_pattern: str):
"""
Initializes the OutcomeBasedTaskPattern with a specific task pattern.
Args:
task_pattern (str): The pattern for the task.
"""
self.task_pattern = task_pattern
self.task_base_type = None
self.require_platform_list = (
OutcomeBasedTaskPattern.parse_platform_requirements(self.task_pattern)
)
self.require_multilayer_object_info = (
OutcomeBasedTaskPattern.parse_multilayer_object_requirements(
self.task_pattern
)
)
self.require_room_objects = (
OutcomeBasedTaskPattern.parse_room_object_requirements(self.task_pattern)
)
self.pattern_info_dict = {}
if self.require_multilayer_object_info is not None:
self.task_base_type = "multi-layer-object"
elif (
self.require_platform_list
): # If not multi-layer but has platform requirements
self.task_base_type = "platforms"
elif (
self.require_room_objects["large_category_objects_count"] > 0
or self.require_room_objects["small_category_objects_count"] > 0
):
self.task_base_type = "room-objects" # Or some other appropriate type
def __repr__(self):
return f"OutcomeBasedTaskPattern(task_pattern={self.task_pattern}, require_platform_list = {self.require_platform_list},require_multilayer_object_info = {self.require_multilayer_object_info},task_base_type={self.task_base_type})"
def generate_task_description(
self, platform_list, multilayer_object_list, room_object_list
):
"""
Generates a task using the specified platforms.
Args:
platforms (list): The list of platforms to use for the task.
"""
# Placeholder for actual task generation logic
pass
platform_name_list = []
platform_object_list = []
multilayer_feature = None
multilayer_object_name_list = [
multilayer_object.multilayer_object_name
for multilayer_object in multilayer_object_list
]
relevant_platform_list = []
relevant_multilayer_list = []
random_room_object_list = room_object_list.copy()
random_multilayer_object_list = multilayer_object_list.copy()
random_platform_list = platform_list.copy()
random.shuffle(random_multilayer_object_list)
random.shuffle(random_platform_list)
random.shuffle(random_room_object_list)
if self.require_multilayer_object_info is not None:
for multilayer_object in random_multilayer_object_list:
if not multilayer_object.is_available(
major_num=self.require_multilayer_object_info.get(
"category_objects_count", 0
),
minor_num=self.require_multilayer_object_info.get(
"small_category_objects_count", 0
),
):
continue
multilayer_feature = multilayer_object.random_pick(
self.require_multilayer_object_info
)
relevant_multilayer_list.append(multilayer_object)
break
if multilayer_feature is None:
glog.warning(
f"{self.task_pattern} cannot be fulfilled by the given scene."
)
return None, None, None
id = 0
for i, platform in enumerate(random_platform_list):
if id >= len(self.require_platform_list):
break
requirement = self.require_platform_list[id]
if (
platform.is_available(requirement[0], requirement[1], requirement[2])
is False
):
continue
major, minor, single = platform.random_pick(
requirement[0], requirement[1], requirement[2]
)
platform_name_list.append(platform.platform_name)
platform_object_list.append([major, minor, single])
relevant_platform_list.append(platform)
id += 1
if id < len(self.require_platform_list) or (
multilayer_feature is None
and self.require_multilayer_object_info is not None
):
glog.warning(f"{self.task_pattern} cannot be fulfilled by the given scene.")
return None, None, None
room_major_category_list = [
room_major_category.major_category
for room_major_category in room_object_list
]
room_minor_category_list = [
room_minor_category.minor_category
for room_minor_category in room_object_list
]
random.shuffle(room_major_category_list)
random.shuffle(room_minor_category_list)
if self.require_room_objects is not None:
room_major_category_list = room_major_category_list[
: self.require_room_objects.get("large_category_objects_count", 0)
]
room_minor_category_list = room_minor_category_list[
: self.require_room_objects.get("small_category_objects_count", 0)
]
pass
# import ipdb; ipdb.set_trace()
def replace_placeholder(match):
placeholder = match.group(0)
if placeholder.startswith("[PLATFORM"):
platform_match = re.match(r"\[PLATFORM(\d+)\]", placeholder)
if platform_match:
index = int(platform_match.group(1))
if index < len(platform_name_list):
return platform_name_list[index]
else:
return f"PLATFORM_NAME_{index}_NOT_FOUND"
elif placeholder.startswith("[MULTILAYER-OBJECT"):
multilayer_match = re.match(r"\[MULTILAYER-OBJECT(\d+)\]", placeholder)
if multilayer_match:
index = int(multilayer_match.group(1))
if index < len(multilayer_object_name_list):
return multilayer_object_name_list[index]
else:
return f"MULTILAYER_OBJECT_{index}_NOT_FOUND"
elif placeholder.startswith("[SUB-OBJECT-CATEGORY"):
category_match = re.match(
r"\[SUB-OBJECTS?-CATEGORY(\d+)\]", placeholder
)
if category_match:
index = int(category_match.group(1))
if index < len(room_major_category_list):
return f"{room_major_category_list[index]}(s)"
else:
return f"ROOM_MAJOR_CATEGORY_{index}_NOT_FOUND"
elif placeholder.startswith("[SUB-OBJECT"):
object_match = re.match(r"\[SUB-OBJECTS?(\d+)\]", placeholder)
if object_match:
index = int(object_match.group(1))
if index < len(room_minor_category_list):
return room_minor_category_list[index]
else:
return f"ROOM_MINOR_CATEGORY_{index}_NOT_FOUND"
elif placeholder.startswith("[SUB-PLATFORM-CATEGORY-OBJECT"):
category_match = re.match(
r"\[SUB-PLATFORM-CATEGORY-OBJECTS?(\d+)(\d+)\]", placeholder
)
if category_match:
platform_index = int(category_match.group(1))
object_index = int(category_match.group(2))
if platform_index < len(
platform_object_list
) and object_index < len(platform_object_list[platform_index][0]):
return f"{platform_object_list[platform_index][0][object_index]}(s)"
else:
return f"PLATFORM_{platform_index}_CATEGORY_{object_index}_NOT_FOUND"
elif placeholder.startswith("[SUB-PLATFORM-OBJECT"):
object_match = re.match(
r"\[SUB-PLATFORM-OBJECTS?(\d+)(\d+)\]", placeholder
)
if object_match:
platform_index = int(object_match.group(1))
object_index = int(object_match.group(2))
if platform_index < len(
platform_object_list
) and object_index < len(platform_object_list[platform_index][1]):
return f"{platform_object_list[platform_index][1][object_index]}(s)"
else:
return (
f"PLATFORM_{platform_index}_OBJECT_{object_index}_NOT_FOUND"
)
elif placeholder.startswith("[SUB-PLATFORM-SINGLE-OBJECT"):
single_match = re.match(
r"\[SUB-PLATFORM-SINGLE-OBJECTS?(\d+)(\d+)\]", placeholder
)
if single_match:
platform_index = int(single_match.group(1))
object_index = int(single_match.group(2))
if platform_index < len(
platform_object_list
) and object_index < len(platform_object_list[platform_index][2]):
return platform_object_list[platform_index][2][object_index]
else:
return f"PLATFORM_{platform_index}_SINGLE_OBJECT_{object_index}_NOT_FOUND"
elif placeholder.startswith("[TOP-LAYER]"):
if multilayer_feature["top_layer_name"]:
return re.sub(
r"_platform_\d+$",
"'s top layer",
multilayer_feature["top_layer_name"],
)
return multilayer_feature["top_layer_name"]
elif placeholder.startswith("[SPECIFIC-LAYER]"):
return multilayer_feature["specific_layer_name"]
elif placeholder.startswith("[SUB-LAYER-CATEGORY-OBJECT"):
layer_category_match = re.match(
r"\[SUB-LAYER-CATEGORY-OBJECTS?(\d+)\]", placeholder
)
if layer_category_match:
object_index = int(layer_category_match.group(1))
if multilayer_feature["category_name_list"] and object_index < len(
multilayer_feature["category_name_list"]
):
return f"{multilayer_feature['category_name_list'][object_index]}(s)"
else:
return f"LAYER_CATEGORY_{object_index}_NOT_FOUND"
elif placeholder.startswith("[SUB-LAYER-OBJECT"):
layer_object_match = re.match(
r"\[SUB-LAYER-OBJECTS?(\d+)\]", placeholder
)
if layer_object_match:
object_index = int(layer_object_match.group(1))
if multilayer_feature[
"small_category_name_list"
] and object_index < len(
multilayer_feature["small_category_name_list"]
):
return f"{multilayer_feature['small_category_name_list'][object_index]}(s)"
else:
return f"LAYER_OBJECT_{object_index}_NOT_FOUND"
else:
return placeholder
task_description = re.sub(r"\[.*?\]", replace_placeholder, self.task_pattern)
print(f"Generated Task Description: {task_description}")
return task_description, relevant_platform_list, relevant_multilayer_list
def generate_task_images(
self,
scene_graph,
platform_name_list,
multilayer_object_list,
room_object_list,
width=1366,
height=768,
current_path=None,
):
for id, platform in enumerate(platform_name_list):
platform = None
for (
platform_in_tree_name,
platform_in_tree,
) in scene_graph.platforms.items():
if (
platform_in_tree.get_name_for_interaction()
== self.platform_name_list[id]
):
platform = platform_in_tree
break
if platform is not None:
platform_img, platform_img_list = (
self.scene_graph.auto_take_platform_picture(
platform_name=platform.name,
view="human_full",
mark_object=True,
mark_freespace=len(platform.children) == 0,
standing_direction=self.standing_direction,
width=width,
height=height,
focus_ratio=0.6,
save_path=f"{current_path}/image4interact/{self.model}/Task{id}_Idle_{self.vlm_interactor.interaction_count}.png",
)
)
for id, multilayer_object in enumerate(multilayer_object_list):
multilayer_object = None
for (
multilayer_object_in_tree_name,
multilayer_object_in_tree,
) in scene_graph.multilayer_objects.items():
if (
multilayer_object_in_tree.get_name_for_interaction()
== self.multilayer_object_name_list[id]
):
multilayer_object = multilayer_object_in_tree
break
if multilayer_object is not None:
img = multilayer_object.auto_take_non_ground_object_picture(
scene=scene_graph.corresponding_scene,
view="human_focus",
mark_object=False,
only_mark_itself=False,
mark_freespace=False,
diagonal_mode="old",
need_afford_rect=None,
standing_direction=self.standing_direction,
width=self.picture_width,
height=self.picture_height,
focus_ratio=0.5,
fovy_range=[np.deg2rad(40), np.deg2rad(60)],
)
pass
@staticmethod
def parse_platform_requirements(pattern_string: str) -> list:
""" """
platform_details = {}
max_seen_platform_index = -1
explicitly_declared_platforms = set()
def ensure_platform_details(p_idx):
nonlocal max_seen_platform_index
max_seen_platform_index = max(max_seen_platform_index, p_idx)
if p_idx not in platform_details:
platform_details[p_idx] = {
"counts": [0, 0, 0],
"seen_category_indices": set(),
"seen_small_category_indices": set(),
"seen_single_object_indices": set(),
}
platform_direct_mentions = re.findall(r"\[PLATFORM(\d+)\]", pattern_string)
for p_idx_str in platform_direct_mentions:
p_idx = int(p_idx_str)
explicitly_declared_platforms.add(p_idx)
ensure_platform_details(p_idx)
# Regex based on pattern.txt (e.g., SUB-PLATFORM-CATEGORY-OBJECT00 - no 'S')
category_matches = re.findall(
r"\[SUB-PLATFORM-CATEGORY-OBJECTS?(\d+)(\d+)\]", pattern_string
)
for p_idx_str, obj_idx_str in category_matches:
p_idx = int(p_idx_str)
obj_idx = int(obj_idx_str)
ensure_platform_details(p_idx)
if p_idx not in explicitly_declared_platforms:
glog.warning(
f"Platform {p_idx} inferred from [SUB-PLATFORM-CATEGORY-OBJECTS{p_idx_str}{obj_idx_str}] "
f'but not explicitly declared with [PLATFORM{p_idx}]. Pattern: "{pattern_string}"'
)
if obj_idx not in platform_details[p_idx]["seen_category_indices"]:
platform_details[p_idx]["seen_category_indices"].add(obj_idx)
platform_details[p_idx]["counts"][0] = max(
platform_details[p_idx]["counts"][0], obj_idx + 1
)
small_category_matches = re.findall(
r"\[SUB-PLATFORM-OBJECTS?(\d+)(\d+)\]", pattern_string
)
for p_idx_str, obj_idx_str in small_category_matches:
p_idx = int(p_idx_str)
obj_idx = int(obj_idx_str)
ensure_platform_details(p_idx)
if p_idx not in explicitly_declared_platforms:
glog.warning(
f"Platform {p_idx} inferred from [SUB-PLATFORM-OBJECTS{p_idx_str}{obj_idx_str}] "
f'but not explicitly declared with [PLATFORM{p_idx}]. Pattern: "{pattern_string}"'
)
if obj_idx not in platform_details[p_idx]["seen_small_category_indices"]:
platform_details[p_idx]["seen_small_category_indices"].add(obj_idx)
platform_details[p_idx]["counts"][1] = max(
platform_details[p_idx]["counts"][1], obj_idx + 1
)
single_object_matches = re.findall(
r"\[SUB-PLATFORM-SINGLE-OBJECTS?(\d+)(\d+)\]", pattern_string
)
for p_idx_str, obj_idx_str in single_object_matches:
p_idx = int(p_idx_str)
obj_idx = int(obj_idx_str)
ensure_platform_details(p_idx)
if p_idx not in explicitly_declared_platforms:
glog.warning(
f"Platform {p_idx} inferred from [SUB-PLATFORM-SINGLE-OBJECTS{p_idx_str}{obj_idx_str}] "
f'but not explicitly declared with [PLATFORM{p_idx}]. Pattern: "{pattern_string}"'
)
if obj_idx not in platform_details[p_idx]["seen_single_object_indices"]:
platform_details[p_idx]["seen_single_object_indices"].add(obj_idx)
platform_details[p_idx]["counts"][2] = max(
platform_details[p_idx]["counts"][2], obj_idx + 1
)
# import ipdb
# ipdb.set_trace()
if max_seen_platform_index == -1:
if not (
platform_direct_mentions
or category_matches
or small_category_matches
or single_object_matches
):
return []
return []
num_platforms = max_seen_platform_index + 1
result_list = [(0, 0, 0)] * num_platforms
for p_idx, details in platform_details.items():
if 0 <= p_idx < num_platforms:
result_list[p_idx] = tuple(details["counts"])
else:
glog.error(
f"Platform index {p_idx} from platform_details is out of bounds "
f'for result_list of size {num_platforms}. Pattern: "{pattern_string}"'
)
return result_list
@staticmethod
def parse_multilayer_object_requirements(pattern_string: str) -> dict:
"""
Parse the string to extract requirements for multilayer objects.
"""
if "[MULTILAYER-OBJECT0]" not in pattern_string:
return None
requires_top_layer = bool(
re.search(r"\[TOP-LAYER\]|\[TOP-SHELF\]", pattern_string)
)
requires_specific_layer = bool(
re.search(r"\[SPECIFIC-LAYER\]|\[SPECIFIC-SHELF\]", pattern_string)
)
# Regex based on pattern.txt (e.g. SUB-LAYER-CATEGORY-OBJECT0 - no 'S')
category_object_indices = set(
re.findall(r"\[SUB-LAYER-CATEGORY-OBJECTS?(\d+)\]", pattern_string)
)
category_objects_count = len(category_object_indices)
small_object_indices = set(
re.findall(r"\[SUB-LAYER-OBJECTS?(\d+)\]", pattern_string)
)
small_category_objects_count = len(small_object_indices)
return {
"requires_top_layer": requires_top_layer,
"requires_specific_layer": requires_specific_layer,
"category_objects_count": category_objects_count,
"small_category_objects_count": small_category_objects_count,
}
@staticmethod
def parse_room_object_requirements(pattern_string: str) -> dict:
"""
Parse the string to extract requirements for room objects.
"""
# Regex based on pattern.txt (e.g. SUB-OBJECT-CATEGORY0 - no 'S')
large_category_indices = set(
re.findall(r"\[SUB-OBJECT-CATEGORY(\d+)\]", pattern_string)
)
small_category_indices = set(
re.findall(r"\[SUB-OBJECTS?(\d+)\]", pattern_string)
)
return {
"large_category_objects_count": len(large_category_indices),
"small_category_objects_count": len(small_category_indices),
}
class OutcomeBasedTask:
def __init__(
self,
task_description: str = None,
task_pattern: OutcomeBasedTaskPattern = None,
multi_layer_object_list: list = None,
platform_list: list = None,
room_object_list: list = None,
):
self.task_description = task_description
self.task_pattern = task_pattern
self.multi_layer_object_list = (
multi_layer_object_list if multi_layer_object_list is not None else []
)
self.platform_list = platform_list if platform_list is not None else []
self.room_object_list = room_object_list if room_object_list is not None else []
self.config = get_outcome_based_task_generation_config()
self.task_num_per_pattern = self.config.task_num_per_pattern
def __str__(self):
return f"Task: {self.task_description}, Pattern: {self.task_pattern}, Multi-layer Objects: {self.multi_layer_object_list}, Platforms: {self.platform_list}, Room Objects: {self.room_object_list}"
class OutcomeBasedTaskGenerator:
def __init__(self, task_pattern_file: str = None, scene_graph=None):
"""
Initializes the OutcomeBasedTaskGenerator with a file containing task patterns.
Args:
task_pattern_file (str): The file containing task patterns.
"""
self.global_config = get_config()
self.config = get_outcome_based_task_generation_config()
self.task_pattern_file = (
self.global_config.manitaskot_pattern_file
if task_pattern_file is None
else task_pattern_file
)
self.task_pattern_list = []
self.task_num_per_pattern = self.config.task_num_per_pattern
if task_pattern_file and os.path.exists(task_pattern_file):
with open(task_pattern_file, "r", encoding="utf-8") as file:
for line in file:
task_pattern = line.strip()
if task_pattern: # Ignore empty lines
self.task_pattern_list.append(
OutcomeBasedTaskPattern(task_pattern)
)
# self.task_pattern_list = [
# "Locate all [SUB-OBJECTS0] in the room, group them by size.",
# "On the top layer of [PLATFORM0], gather all loose [SUB-PLATFORM-OBJECTS00].",
# "Optimize shelving layout of [ALL_LAYERS] on [MULTILAYER-OBJECT0].",
# "Group objects on the [SPECIFIC-LAYER] of [MULTILAYER-OBJECT0] by color.",
# "Reorganize the contents of [MULTILAYER-OBJECT0] such that all items related to [SUB-LAYER-OBJECTS00] are grouped together on the top shelf, while [SUB-LAYER-OBJECTS01] and [SUB-LAYER-OBJECTS02] are arranged on the lower shelves.",
# "Reconfigure the setup of [PLATFORM0], [PLATFORM1], and [PLATFORM2] to optimize for a multi-step workflow.",
# "Reconfigure the items on [PLATFORM0] to establish a central focal point for [SUB-PLATFORM-SINGLE-OBJECT00].",
# ]
self.task_pattern_list = [
OutcomeBasedTaskPattern(pattern) for pattern in self.task_pattern_list
]
self.scene_graph = scene_graph
self.platform_list = []
self.multilayer_object_list = []
self.platform_list, self.multilayer_object_list, self.room_object_list = (
self.parse_scene_graph()
)
self.task_list = []
# NOTE: patterns are already loaded above when task_pattern_file was
# given; calling load_task_patterns again would duplicate every pattern.
def load_task_patterns(self, task_pattern_file: str):
"""
Loads task patterns from a specified file.
Args:
task_pattern_file (str): The file containing task patterns.
"""
try:
with open(task_pattern_file, "r", encoding="utf-8") as file:
for line in file:
# Assuming each line contains a task pattern
task_pattern = line.strip()
if task_pattern: # Ignore empty lines
self.task_pattern_list.append(
OutcomeBasedTaskPattern(task_pattern)
)
except FileNotFoundError:
print(f"Error: The file '{task_pattern_file}' was not found.")
except Exception as e:
print(f"An error occurred while loading task patterns: {e}")
def generate_prompt_for_task(self, task: OutcomeBasedTaskPattern):
"""
Generates a prompt for a given task.
Args:
task (OutcomeBasedTaskPattern): The task for which to generate a prompt.
"""
# Placeholder for actual prompt generation logic
conversation_list = []
conversation_list.append(
{
"role": "system",
"content": "Please evaluate the feasibility of the following platform-based task: Task: [TASK_DESCRIPTION]\n",
}
)
return f"Prompt for task: {task.task_pattern}"
def generate_all_possible_tasks(self):
"""
Generates all possible tasks based on the loaded task patterns.
"""
all_tasks = []
if not self.platform_list or not self.multilayer_object_list:
glog.error(
"Platform list or multilayer object list is empty. Cannot generate tasks."
)
return all_tasks
def generate_sample_task(self):
""" """
# Placeholder for actual task generation logic
from itertools import combinations
multilayer_object_list = self.multilayer_object_list
platform_list = self.platform_list
well_occupied_platform_list = []
for id, platform in enumerate(self.platform_list):
if len(platform.single_object_list) > 1:
well_occupied_platform_list.append(platform)
if not well_occupied_platform_list:
glog.error("No well-occupied platforms found. Cannot generate tasks.")
return None
all_task_list = []
task_pattern = self.task_pattern_list
for task_pattern in self.task_pattern_list:
required_platform_list = task_pattern.require_platform_list
required_multilayer_object_list = (
task_pattern.require_multilayer_object_info
)
if len(required_platform_list) != 1:
continue
junior_platform_combinations = combinations(
platform_list, len(required_platform_list) - 1
)
for senior_platform in well_occupied_platform_list:
# import ipdb
# ipdb.set_trace()
if senior_platform.is_available(
task_pattern.require_platform_list[0][0],
task_pattern.require_platform_list[0][1],
task_pattern.require_platform_list[0][2],
):
flag = True
if flag:
if (
required_multilayer_object_list is not None
and len(required_multilayer_object_list) > 0
):
for multilayer_object in multilayer_object_list:
if (
not task_pattern.require_multilayer_object_info
or multilayer_object.is_available(
task_pattern.require_multilayer_object_info[
"category_objects_count"
],
task_pattern.require_multilayer_object_info[
"small_category_objects_count"
],
)
):
# Generate a task using the selected platforms
# Generate a task using the selected platforms
task_description = (
task_pattern.generate_task_description(
platform_list=[senior_platform],
multilayer_object_list=[multilayer_object],
room_object_list=[],
)
)
task = {
"PLATFORM0": senior_platform,
"MULTILAYER-OBJECT0": multilayer_object,
"task_description": task_description,
"task_pattern": task_pattern,
}
all_task_list.append(task)
# Print or log the generated task
else:
# Generate a task using the selected platforms
task_description = task_pattern.generate_task_description(
platform_list=[senior_platform],
multilayer_object_list=[],
room_object_list=[],
)
task = {
"PLATFORM0": senior_platform,
"task_description": task_description,
"task_pattern": task_pattern,
}
all_task_list.append(task)
# Print or log the generated task
pass
pass
self.all_task_list = all_task_list
glog.info(f"Generated {len(all_task_list)} tasks.")
return self.all_task_list
def generate_task_with_all_patterns(self, task_num=None, desired_pattern_list=None):
"""
Generate outcome-based task candidates by instantiating each (selected)
pattern against the current scene.
Args:
task_num: number of candidates to generate per pattern. Defaults to
``self.task_num_per_pattern``. Patterns that cannot be bound to
the scene simply contribute fewer (or zero) candidates.
desired_pattern_list: indices of the patterns to use. Defaults to
all loaded patterns.
Returns:
The accumulated list of candidates (also stored in ``self.task_list``).
Candidates whose pattern could not be instantiated on this scene are
skipped (previously they were appended with ``task_description=None``).
"""
task_num = self.task_num_per_pattern if task_num is None else task_num
desired_pattern_list = (
desired_pattern_list
if desired_pattern_list is not None
else [i for i in range(len(self.task_pattern_list))]
)
# Accumulate candidates across patterns. (Previously this list was
# reset inside the loop, so only the last pattern's task survived.)
all_task_list = []
for pattern in desired_pattern_list:
if not (0 <= pattern < len(self.task_pattern_list)):
glog.warning(f"Pattern index {pattern} is out of range, skipped.")
continue
task_pattern = self.task_pattern_list[pattern]
pattern_task_list = []
pattern_description_set = set()
# random.sample inside generate_task_description draws different
# entities per call, so a few retries are usually enough to fill
# task_num candidates; patterns that cannot be fulfilled at all
# bail out quickly via the attempt cap.
max_attempts = max(task_num, 1) * 10
attempts = 0
while len(pattern_task_list) < task_num and attempts < max_attempts:
attempts += 1
(
task_description,
relevant_platform_list,
relevant_multlayer_list,
) = task_pattern.generate_task_description(
platform_list=self.platform_list,
multilayer_object_list=self.multilayer_object_list,
room_object_list=self.room_object_list,
)
if task_description is None:
# The pattern cannot be fulfilled by the given scene.
continue
if task_description in pattern_description_set:
continue
pattern_description_set.add(task_description)
new_ob_task = OutcomeBasedTask(
task_description=task_description,
task_pattern=task_pattern,
multi_layer_object_list=relevant_multlayer_list,
platform_list=relevant_platform_list,
room_object_list=self.room_object_list,
)
pattern_task_list.append(new_ob_task)
all_task_list.extend(pattern_task_list)
self.task_list = all_task_list
return self.task_list
def parse_scene_graph(self):
platform_list = []
multilayer_object_list = []
room_object_list = []
for platform_name, platform in self.scene_graph.platforms.items():
child_name_list = platform.get_child_name_list()
child_list = []
for child_name in child_name_list:
child_list.append(
OBTSurfaceObject(
object_index=len(child_list), surface_object_name=child_name
)
)
room_object_list.extend(child_list)
platform_list.append(
OBTPlatform(
platform=platform,
platform_index=len(platform_list),
platform_name=platform.get_name_for_interaction(),
children_list=child_list,
)
)
for node_name, node in self.scene_graph.nodes.items():
if node.is_multilayer_object():
major_category_list, minor_category_list = (
node.get_categories_and_objects_for_mlo()
)
child_name_list = node.get_child_name_list()
child_list = []
for child_name in child_name_list:
child_list.append(
OBTSurfaceObject(
object_index=len(child_list), surface_object_name=child_name
)
)
multilayer_object_list.append(
OBTMultilayerObject(
multilayer_object_name=node.name,
children_list=child_list,
object_index=len(multilayer_object_list),
layer_num=len(node.own_platform),
)
)
self.platform_list = platform_list
self.multilayer_object_list = multilayer_object_list
self.room_object_list = room_object_list
return (platform_list, multilayer_object_list, room_object_list)
pass
pass
import json
def main():
print("--- Outcome-based Task Pattern Parsing Tests ---")
scene_graph = None
scene_graph_pkl_load_path = "./scene_graph.pkl"
if scene_graph_pkl_load_path is not None and os.path.exists(
scene_graph_pkl_load_path
):
glog.info(f"Loading scene graph from {scene_graph_pkl_load_path}")
with open(scene_graph_pkl_load_path, "rb") as f:
scene_graph = pickle.load(f)
rename_dict = json.load(open("rename_dict_replica.json", "r"))
scene_graph.rename_all_features(rename_dict)
outcome_based_task_generator = OutcomeBasedTaskGenerator(scene_graph=scene_graph)
patterns_to_test = [
"Arrange [SUB-PLATFORM-CATEGORY-OBJECTS00] on [PLATFORM0] by height and type.",
"Reorganize the left side of the top of [PLATFORM0].",
"Align all [SUB-PLATFORM-OBJECTS00] on [PLATFORM0] symmetrically.",
"On [PLATFORM0], create a symmetrical arrangement of [SUB-PLATFORM-OBJECTS00] and [SUB-PLATFORM-OBJECTS01].",
"Organize all the items on [PLATFORM0] by separating them into categories: place all [SUB-PLATFORM-OBJECT00] in a single stack, align any [SUB-PLATFORM-OBJECT01] in a row, and move [SUB-PLATFORM-OBJECT02] to the left side.",
"Use items from [PLATFORM0] and [PLATFORM1] to create an artistic installation on [PLATFORM2].",
"Move frequently used items from [PLATFORM0] and [PLATFORM1] to the top of [PLATFORM2].",
"Arrange [PLATFORM0], [PLATFORM1], and [PLATFORM2] for collaborative work.",
"Take all [SUB-PLATFORM-OBJECTS00] from [PLATFORM0] and arrange them on [PLATFORM1].",
"Transform [PLATFORM0] into a presentation space by placing colorful [SUB-PLATFORM-CATEGORY-OBJECT00].",
"On [PLATFORM0], organize the [SUB-PLATFORM-CATEGORY-OBJECTS00] and [SUB-PLATFORM-CATEGORY-OBJECTS01] by size.",
"Find all [SUB-LAYER-OBJECTS0] and [SUB-LAYER-OBJECTS1] scattered across [MULTILAYER-OBJECT0].",
"Locate all [SUB-OBJECTS0] in the room, group them by size.",
"Clear [PLATFORM0] by folding [SUB-PLATFORM-OBJECTS00] and placing them on [PLATFORM1].",
"Leave room in the corner of the [PLATFORM0] for the [SUB-PLATFORM-OBJECTS10] from [PLATFORM1].",
"Transform [PLATFORM0] into a visually appealing display by organizing [SUB-PLATFORM-CATEGORY-OBJECTS02].",
"Gather all the [SUB-PLATFORM-OBJECTS00] from [PLATFORM0], then organize them on the top shelf of [MULTILAYER-OBJECT0].",
"Transform the [TOP-LAYER] of [MULTILAYER-OBJECT0] by aligning all [SUB-LAYER-CATEGORY-OBJECTS00].",
"Rearrange and optimize the [SPECIFIC-LAYER] of [MULTILAYER-OBJECT0].",
"Gather all [SUB-PLATFORM-OBJECTS00], [SUB-PLATFORM-OBJECTS10], [SUB-PLATFORM-OBJECTS20] scattered across [PLATFORM0], [PLATFORM1], and [PLATFORM2], and arrange them on [PLATFORM3].",
"Sort items from [PLATFORM0] and [PLATFORM1] into categories and place them on [PLATFORM2] and [PLATFORM3].",
"Gather all [SUB-OBJECT-CATEGORY0] from the [ROOM], clean them, and place them on [PLATFORM0].",
"Clear the top of [PLATFORM0], then reposition the [SUB-PLATFORM-SINGLE-OBJECTS00], [SUB-PLATFORM-SINGLE-OBJECTS01], and [SUB-PLATFORM-SINGLE-OBJECTS02].",
"Conduct a thorough inspection of [PLATFORM0], placing vivid [SUB-PLATFORM-SINGLE-OBJECTS00] at eye level.",
"Optimize shelving layout of [ALL_LAYERS] on [MULTILAYER-OBJECT0].",
"Group objects on the [SPECIFIC-LAYER] of [MULTILAYER-OBJECT0] by color.",
"Reorganize the contents of [MULTILAYER-OBJECT0] such that all items related to [SUB-LAYER-OBJECTS00] are grouped together on the top shelf, while [SUB-LAYER-OBJECTS01] and [SUB-LAYER-OBJECTS02] are arranged on the lower shelves.",
"Reconfigure the setup of [PLATFORM0], [PLATFORM1], and [PLATFORM2] to optimize for a multi-step workflow.",
"Reconfigure the items on [PLATFORM0] to establish a central focal point for [SUB-PLATFORM-SINGLE-OBJECT00].",
]
for i, pattern in enumerate(patterns_to_test):
print(f"\n--- Test Pattern {i+1} ---")
print(f'Pattern: "{pattern}"')
task_instance = OutcomeBasedTaskPattern(pattern)
print(f" Platform Requirements: {task_instance.require_platform_list}")
print(
f" Multilayer Object Info: {task_instance.require_multilayer_object_info}"
)
print(f" Room Object Requirements: {task_instance.require_room_objects}")
print(f" Determined Task Base Type: {task_instance.task_base_type}")
outcome_based_task_generator.generate_task_with_all_patterns()
vlm_voter = VLMVoter()
for task in outcome_based_task_generator.task_list:
vlm_voter.is_task_feasible(task, scene_graph)
# Example from pattern.txt for SUB-OBJECT-CATEGORY0
pattern_locatel = "Locate all [SUB-OBJECT0] in the room, group them by size, and place them in descending order along the edge of [PLATFORM0]."
print(f"\n--- Test Pattern (from file) ---")
print(f'Pattern: "{pattern_locatel}"')
task_locatel = OutcomeBasedTaskPattern(pattern_locatel)
print(f" Platform Requirements: {task_locatel.require_platform_list}")
print(f" Multilayer Object Info: {task_locatel.require_multilayer_object_info}")
print(f" Room Object Requirements: {task_locatel.require_room_objects}")
print(f" Determined Task Base Type: {task_locatel.task_base_type}")
if __name__ == "__main__":
main()
|