File size: 10,440 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
# %%
import os
import sys
import sapien
import argparse


scene = sapien.Scene()
script_dir = os.path.dirname(os.path.abspath(__file__))
# 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.outcome_based_task_generation import (
    OutcomeBasedTaskGenerator,
    VLMVoter,
    OutcomeBasedTaskPattern,
)
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

# %%


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.",
    )

    # Important global configuration parameters - these will override settings in config file
    parser.add_argument(
        "--input_json_path",
        type=str,
        default=None,
        help="Path to the input JSON scene file",
    )
    parser.add_argument(
        "--output_json_path",
        type=str,
        default=None,
        help="Path to the output JSON file",
    )
    parser.add_argument(
        "--entity_json_path",
        type=str,
        default=None,
        help="Path to the entity JSON file",
    )
    parser.add_argument(
        "--output_dir", type=str, default=None, help="Output directory for results"
    )

    # Task related configuration
    parser.add_argument(
        "--task_num", type=int, default=None, help="Number of tasks to generate"
    )

    # Model and mode configuration
    parser.add_argument(
        "--mode",
        type=str,
        default=None,
        choices=["online", "offline", "manual"],
        help="Mode: online, offline, or manual",
    )
    parser.add_argument(
        "--model_name", type=str, default=None, help="Model name for VLM interaction"
    )
    parser.add_argument(
        "--rename_dict_path",
        type=str,
        default=None,
        help="Path to load atomic task pickle file",
    )
    # File path configuration
    parser.add_argument(
        "--scene_graph_pkl_save_path",
        type=str,
        default=None,
        help="Path to save scene graph pickle file",
    )
    parser.add_argument(
        "--atomic_task_pkl_load_path",
        type=str,
        default=None,
        help="Path to load atomic task pickle 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):

    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

    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",
        )
    )

    # import ipdb
    # ipdb.set_trace()

    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
    sapien_scene_manager = visualize_scene_sapien.SapienSceneManager()
    scene = sapien_scene_manager.create_scene()

    sapien_scene_manager.load_objects_from_json(scene, json_file_path=output_json_path)

    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}")

    if main_config.process_based_task_pkl_load_path is not None and os.path.exists(
        main_config.process_based_task_pkl_load_path
    ):
        glog.info(
            f"Loading atomic tasks from {main_config.process_based_task_pkl_load_path}"
        )
        with open(main_config.process_based_task_pkl_load_path, "rb") as f:
            chained_task = pickle.load(f)
    else:
        glog.warning("No  pkl file found.")

    initial_atomic_task = copy.deepcopy(chained_task)
    initial_scene_graph = copy.deepcopy(scene_graph)

    task_sample = chained_task.tasks
    task_sample_ids = [chained_task.tasks.index(task) for task in task_sample]

    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 rename dict found, using empty dict.")
            rename_dict = {}

    scene_graph.rename_all_features(rename_dict)
    scene_graph.calculate_free_space()

    # 5 test tasks
    initial_atomic_task = copy.deepcopy(chained_task)
    initial_scene_graph = copy.deepcopy(scene_graph)
    result = []
    histories = []
    task_list = random.sample(
        range(len(task_sample_ids)),
        min(main_config.benchmark_task_num, len(task_sample_ids)),
    )
    total_score = 0
    total_sr = 0
    sapien_scene_manager = visualize_scene_sapien.SapienSceneManager()

    # import ipdb;
    # ipdb.set_trace()

    for i in task_list:

        task = task_sample[i]
        for j, subtask in enumerate(task.subtask_list):
            task.subtask_list[j].item = scene_graph.nodes[subtask.item.name]

        another_scene = sapien.Scene()
        another_scene.set_timestep(1 / 100)
        another_scene.add_ground(altitude=0)
        sapien_scene_manager.load_objects_from_json(
            another_scene, json_file_path=output_json_path
        )
        another_scene.set_ambient_light([0.5, 0.5, 0.5])
        another_scene.add_directional_light([0, 1, -1], [0.5, 0.5, 0.5])

        for j in range(1000):

            another_scene.step()
            another_scene.update_render()

        # description of task has moved into apply function.
        manual_vlm_interactor = vlm_interactor.VLMInteractor(
            mode=main_config.mode, model=main_config.benchmark_model_name
        )

        manual_vlm_interactor.MAX_INTERACTION_COUNT *= len(task.subtask_list)

        scene_graph.corresponding_scene = another_scene
        scene_graph.rename_all_features(rename_dict)
        scene_graph.corresponding_scene = scene
        scene_graph.rename_all_features(rename_dict)

        glog.info(task)

        # return TaskStatusCode.SUCCESS or TaskStatusCode.FAILURE
        intermediate_task, intermediate_task_id = None, None

        task = benchmark_executor.BenchmarkExecutor(
            task=task,
            task_id=i,
            intermediate_task=intermediate_task,
            intermediate_task_id=intermediate_task_id,
            scene_graph=scene_graph,
            scene=another_scene,
            vlm_interactor=manual_vlm_interactor,
            model_name=main_config.benchmark_model_name,
            generate_mistake_note=main_config.generate_mistake_note,
            use_mistake_note=main_config.use_mistake_note,
        )

        task.apply_action(state=benchmark_executor.InteractStates.NAVIGATION)
        result.append([task.status, task.partial_score])
        histories.append(task.action_history_list)
        scene = sapien.Scene()
        scene.set_timestep(1 / 100)
        scene.add_ground(altitude=0)

        sapien_scene_manager.load_objects_from_json(
            scene, json_file_path=output_json_path
        )
        scene.set_ambient_light([0.5, 0.5, 0.5])
        scene.add_directional_light([0, 1, -1], [0.5, 0.5, 0.5])

        for j in range(1000):
            scene.step()
            scene.update_render()

        scene_graph = copy.deepcopy(initial_scene_graph)
        atomic_task = copy.deepcopy(initial_atomic_task)

        # import ipdb; ipdb.set_trace()
        # start_task_msg_buffer = ""
        total_score += sum(task.out_of_order_partial_scores)
        total_sr += int(task.status == True)
        with open(main_config.result_file_path, "a") as f:
            f.write(f"Task {i}: {task.status}, Score: {task.partial_score}\n")
            f.write(
                f"Out-of-order Subtask Scores: {task.out_of_order_partial_scores}\n"
            )
            f.write(f"Task Info: {task.task.__repr_rough__()}\n")
            f.write(f"History: {task.action_history_list}\n")

    with open(main_config.result_file_path, "a") as f:
        f.write(f"Total Score: {total_score  / len(task_list)}\n")
        f.write(f"Total Success Rate: {total_sr / len(task_list)}\n")


# %%

if __name__ == "__main__":

    """
    Args:




    """

    args = parse_arguments()
    main(args)


# %%