Spaces:
Running
Running
Download src/scripts/02a_gen_process_based_tasks.py from ManiacWallnut/ManiTaskGen: direct link, hf CLI and curl.
- Browser
- Download file 6.72 kB
-
https://huggingface.co/spaces/ManiacWallnut/ManiTaskGen/resolve/main/src/scripts/02a_gen_process_based_tasks.py
- Command line
-
hf download hf://spaces/ManiacWallnut/ManiTaskGen/src/scripts/02a_gen_process_based_tasks.py
-
curl -L -o 02a_gen_process_based_tasks.py https://huggingface.co/spaces/ManiacWallnut/ManiTaskGen/resolve/main/src/scripts/02a_gen_process_based_tasks.py
6.72 kB
| # %% | |
| import os | |
| import sys | |
| import sapien | |
| import argparse | |
| # sys.path.append("d:/workplace/scene_graph/task_generation/") | |
| from src.preprocessing import ( | |
| visualize_scene_sapien, | |
| RawSceneParserFactory, | |
| renaming_engine, | |
| ) | |
| from src.geometry.convex_hull_processor import ConvexHullProcessor_2d | |
| from src.utils.image_renderer import image_render_processor | |
| from src.utils import visualization_tools | |
| from src.utils.config_manager import ConfigManager | |
| from src.core import gen_scene_graph, process_based_task_generation, benchmark_executor | |
| from src.core.task_feasibility_evaluator import TaskFeasibilityEvaluator | |
| from src.vlm_interaction import vlm_interactor | |
| import pickle | |
| import random | |
| import time | |
| from enum import Enum | |
| import colorama | |
| from colorama import Fore, Style | |
| import glog | |
| import copy | |
| import json | |
| # %% | |
| # scene = sapien.Scene() | |
| # import ipdb; ipdb.set_trace() | |
| # script_dir = os.path.dirname(os.path.abspath(__file__)) | |
| 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( | |
| "--atomic_task_pkl_load_path", | |
| type=str, | |
| default=None, | |
| help="Path to load atomic task pickle file", | |
| ) | |
| parser.add_argument( | |
| "--rename_dict_path", | |
| type=str, | |
| default=None, | |
| help="Path to load atomic task pickle file", | |
| ) | |
| parser.add_argument( | |
| "--scene_graph_pkl_load_path", | |
| type=str, | |
| default=None, | |
| help="Path to load scene graph pickle file, overrides config if provided.", | |
| ) | |
| parser.add_argument( | |
| "--process_tasks_pkl_path", | |
| type=str, | |
| default=None, | |
| help="Path to save process-based task pickle file", | |
| ) | |
| parser.add_argument( | |
| "--process_tasks_txt_path", | |
| type=str, | |
| default=None, | |
| help="Path to save process-based task text file", | |
| ) | |
| 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): | |
| print("Starting process-based task generation...") | |
| os.environ["KMP_DUPLICATE_LIB_OK"] = "True" | |
| current_path = os.path.dirname(os.path.abspath(__file__)) | |
| # 0. Initialize the configuration manager | |
| # We only need to load the config once. | |
| config_path = args.config | |
| print(f"Using config file: {config_path}") | |
| config_manager = ConfigManager( | |
| config_file_path=config_path, run_dir=args.output_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( | |
| os.path.join( | |
| config_manager.config_file_export_dir, | |
| f"used_config_{int(time.time())}.yaml", | |
| ) | |
| ) | |
| main_config = config_manager.config | |
| entity_json_path = main_config.entity_json_path | |
| t = time.perf_counter() | |
| if main_config.scene_graph_pkl_load_path is not None and os.path.exists( | |
| main_config.scene_graph_pkl_load_path | |
| ): | |
| glog.info(f"Loading scene graph from {main_config.scene_graph_pkl_load_path}") | |
| with open(main_config.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 = {} | |
| if main_config.use_renaming_engine: | |
| if main_config.rename_dict_path is not None and os.path.exists( | |
| main_config.rename_dict_path | |
| ): | |
| glog.info( | |
| f"Using provided renaming dictionary {main_config.rename_dict_path} to rename the objects." | |
| ) | |
| rename_dict = json.load(open(main_config.rename_dict_path, "r")) | |
| else: | |
| glog.warning("No renaming dict found, using empty dict.") | |
| rename_dict = {} | |
| scene_graph.rename_all_features(rename_dict) | |
| # scene_graph.rename_all_features(rename_dict) | |
| original_scene_graph = copy.deepcopy(scene_graph) | |
| # random.seed(123) | |
| chained_task = process_based_task_generation.TaskGeneration(scene_graph) | |
| chained_task.generate_task() | |
| if main_config.process_based_task_pkl_save_path is not None: | |
| os.makedirs( | |
| os.path.dirname(main_config.process_based_task_pkl_save_path), exist_ok=True | |
| ) | |
| os.makedirs( | |
| os.path.dirname(main_config.process_based_task_txt_save_path), exist_ok=True | |
| ) | |
| with open(main_config.process_based_task_pkl_save_path, "wb") as f: | |
| pickle.dump(chained_task, f) | |
| with open(main_config.process_based_task_txt_save_path, "w") as f: | |
| for i, task in enumerate(chained_task.tasks): | |
| print(f"Task {i}: {task.__repr_rough__()}", file=f) | |
| print(f"Goal State: ", file=f) | |
| for j in range(len(task.subtask_list)): | |
| print( | |
| f" Subtask {j}: {task.subtask_list[j].__repr_rough__()}", | |
| file=f, | |
| ) | |
| print( | |
| f" Goal Info: {task.subtask_list[j].goal_primitive_expression.to_pddl_goal()}", | |
| file=f, | |
| ) | |
| scene_graph = original_scene_graph | |
| glog.info(f"Time cost for task generation: {time.perf_counter() - t:.2f} seconds") | |
| # import ipdb | |
| # ipdb.set_trace() | |
| # for i, task in enumerate(atomic_task.tasks): | |
| # print(f"Task {i}: {task.__repr_rough__()}", file=open(os.path.join(main_config.output_dir, 'tasks.txt'), 'a')) | |
| task_sample = chained_task.tasks | |
| task_sample_ids = [chained_task.tasks.index(task) for task in task_sample] | |
| if __name__ == "__main__": | |
| args = parse_arguments() | |
| main(args) | |
| # Example usage | |