Spaces:
Running
Running
Download src/scripts/02b_gen_outcome_based_tasks.py from ManiacWallnut/ManiTaskGen: direct link, hf CLI and curl.
- Browser
- Download file 7.59 kB
-
https://huggingface.co/spaces/ManiacWallnut/ManiTaskGen/resolve/main/src/scripts/02b_gen_outcome_based_tasks.py
- Command line
-
hf download hf://spaces/ManiacWallnut/ManiTaskGen/src/scripts/02b_gen_outcome_based_tasks.py
-
curl -L -o 02b_gen_outcome_based_tasks.py https://huggingface.co/spaces/ManiacWallnut/ManiTaskGen/resolve/main/src/scripts/02b_gen_outcome_based_tasks.py
7.59 kB
| # %% | |
| import os | |
| import sys | |
| import argparse | |
| script_dir = os.path.dirname(os.path.abspath(__file__)) | |
| # sys.path.append("d:/workplace/scene_graph/task_generation/") | |
| # NOTE: VLM-only imports (visualize_scene_sapien / VLMVoter / | |
| # TaskFeasibilityEvaluator / vlm_interactor) are deferred to the point of use | |
| # so that ``--skip_vlm_vote`` can run without VLMEvalKit or Vulkan installed. | |
| from src.preprocessing import RawSceneParserFactory | |
| from src.utils.config_manager import ConfigManager | |
| from src.core import gen_scene_graph | |
| from src.core.outcome_based_task_generation import ( | |
| OutcomeBasedTaskGenerator, | |
| OutcomeBasedTaskPattern, | |
| ) | |
| import pickle | |
| import random | |
| import time | |
| from enum import Enum | |
| import colorama | |
| from colorama import Fore, Style | |
| import glog | |
| import copy | |
| import json | |
| # %% | |
| def parse_arguments(): | |
| """Parse command line arguments""" | |
| parser = argparse.ArgumentParser(description="Task Generation System") | |
| # Basic configuration | |
| parser.add_argument( | |
| "--config", | |
| type=str, | |
| default=None, | |
| help="Path to the configuration file, None for default config.", | |
| ) | |
| parser.add_argument( | |
| "--output_dir", | |
| type=str, | |
| default=None, | |
| help="Output directory for results", | |
| ) | |
| parser.add_argument( | |
| "--scene_graph_pkl_load_path", | |
| type=str, | |
| default=None, | |
| help="Path to load pre-generated scene graph pickle file, None to generate from JSON.", | |
| ) | |
| parser.add_argument( | |
| "--rename_dict_path", | |
| type=str, | |
| default=None, | |
| help="Path to the renaming dictionary JSON file, None to skip renaming.", | |
| ) | |
| # The default value is set the same as in config_manager.py. | |
| parser.add_argument( | |
| "--vlm_list", | |
| type=str, | |
| nargs="+", | |
| default=[ | |
| "openai/gpt-4.1", | |
| "anthropic/claude-3.5-haiku", | |
| "google/gemini-2.5-flash-lite-preview-06-17", | |
| ], | |
| help="List of VLM models to use.", | |
| ) | |
| parser.add_argument( | |
| "--manitaskot_pattern_file", | |
| type=str, | |
| default="./src/utils/manitask-ot200/manitask_ot200.txt", | |
| help="Path to the file containing ManiTaskOT patterns, one per line.", | |
| ) | |
| parser.add_argument( | |
| "--outcome_based_task_txt_save_path", | |
| type=str, | |
| default="./data/output/outcome_based_task.txt", | |
| help="Directory to save outputs like images and logs.", | |
| ) | |
| parser.add_argument( | |
| "--skip_vlm_vote", | |
| action="store_true", | |
| default=False, | |
| help="Skip VLM feasibility voting entirely (no API keys, no SAPIEN " | |
| "render scene needed). All successfully-bound candidates are written " | |
| "out with voting_status='not_run'.", | |
| ) | |
| return parser.parse_args() | |
| def update_config_from_args(config_manager, args): | |
| """Update configuration using command line arguments, command line args have higher priority""" | |
| config_dict = {} | |
| args_dict = vars(args) | |
| for key, value in args_dict.items(): | |
| if value is not None: | |
| config_dict[key] = value | |
| config_manager._update_config_from_dict(config_dict) | |
| def main(args): | |
| os.environ["KMP_DUPLICATE_LIB_OK"] = "True" | |
| # 0. Initialize the configuration manager | |
| # We only need to load the config once. | |
| config_path = args.config | |
| run_dir = args.output_dir | |
| glog.info(args.output_dir) | |
| glog.info(args.config) | |
| config_manager = ConfigManager(config_file_path=config_path, run_dir=run_dir) | |
| update_config_from_args(config_manager, args) | |
| # export the final config in this run, named by the timestamp | |
| if not os.path.exists(config_manager.config_file_export_dir): | |
| os.makedirs(config_manager.config_file_export_dir) | |
| config_manager.save_to_yaml_staged( | |
| os.path.join( | |
| config_manager.config_file_export_dir, | |
| f"used_config_{int(time.time())}.yaml", | |
| ) | |
| ) | |
| glog.info(args.output_dir) | |
| glog.info(args.config) | |
| main_config = config_manager.config | |
| input_json_path = main_config.input_json_path | |
| output_json_path = main_config.output_json_path | |
| entity_json_path = main_config.entity_json_path | |
| scene_graph_pkl_load_path = main_config.scene_graph_pkl_load_path | |
| rename_dict_path = main_config.rename_dict_path | |
| skip_vlm_vote = bool(getattr(args, "skip_vlm_vote", False)) | |
| # The SAPIEN render scene is only needed for VLM voting images (and the | |
| # renaming engine). When voting is skipped, do not create it at all so the | |
| # entry can run on CPU-only machines without Vulkan. | |
| scene = None | |
| if not skip_vlm_vote: | |
| from src.preprocessing import visualize_scene_sapien | |
| sapien_scene_manager = visualize_scene_sapien.SapienSceneManager() | |
| scene = sapien_scene_manager.create_scene() | |
| # 2 Generate the scene graph | |
| scene_graph = None | |
| 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) | |
| else: | |
| ts = time.perf_counter() | |
| json_tree_path = gen_scene_graph.load_json_file(entity_json_path) | |
| scene_graph = gen_scene_graph.gen_multi_layer_graph_with_free_space( | |
| json_tree_path | |
| ) | |
| glog.info(f"scene graph tree generation time: {time.perf_counter() - ts}") | |
| rename_dict_path = main_config.rename_dict_path | |
| rename_dict = {} | |
| if rename_dict_path is not None and os.path.exists(rename_dict_path): | |
| with open(rename_dict_path, "r") as f: | |
| rename_dict = json.load(f) | |
| else: | |
| glog.warning("No renaming dict found, using empty dict.") | |
| rename_dict = {} | |
| scene_graph.rename_all_features(rename_dict) | |
| if scene is not None: | |
| scene_graph.corresponding_scene = scene | |
| outcome_based_task_generator = OutcomeBasedTaskGenerator(scene_graph=scene_graph) | |
| outcome_based_task_generator.load_task_patterns(main_config.manitaskot_pattern_file) | |
| outcome_based_task_generator.generate_task_with_all_patterns() | |
| output_dir = main_config.outcome_based_task_txt_save_path | |
| os.makedirs(os.path.dirname(os.path.abspath(output_dir)), exist_ok=True) | |
| if skip_vlm_vote: | |
| # Vote-free mode: keep every successfully-bound candidate and record | |
| # that voting has not been run. | |
| candidates = outcome_based_task_generator.task_list | |
| skipped = 0 | |
| with open(output_dir, "w") as f: | |
| for task in candidates: | |
| if task.task_description is None: | |
| skipped += 1 | |
| continue | |
| f.write(f"[voting_status: not_run] {task}\n") | |
| glog.info( | |
| f"Skipped VLM voting. Wrote {len(candidates) - skipped} candidates " | |
| f"({skipped} unbindable pattern instantiations dropped) to {output_dir}" | |
| ) | |
| return | |
| from src.core.outcome_based_task_generation import VLMVoter | |
| vlm_voter = VLMVoter() | |
| feasible_task_list = [] | |
| for task in outcome_based_task_generator.task_list: | |
| task_is_feasible = vlm_voter.is_task_feasible(task, scene_graph) | |
| if task_is_feasible: | |
| feasible_task_list.append(task) | |
| with open(output_dir, "w") as f: | |
| for task in feasible_task_list: | |
| f.write(str(task) + "\n") | |
| # %% | |
| if __name__ == "__main__": | |
| """ | |
| Args: | |
| """ | |
| args = parse_arguments() | |
| main(args) | |
| # %% | |