Download Assignment4/eval_policy.py from TTangenty/embodied_ai: direct link, hf CLI and curl.
- Browser
- Download file 18.6 kB
-
https://huggingface.co/datasets/TTangenty/embodied_ai/resolve/main/Assignment4/eval_policy.py
- Command line
-
hf download hf://datasets/TTangenty/embodied_ai/Assignment4/eval_policy.py
-
curl -L -o eval_policy.py https://huggingface.co/datasets/TTangenty/embodied_ai/resolve/main/Assignment4/eval_policy.py
18.6 kB
| import sys | |
| import os | |
| import subprocess | |
| import multiprocessing as mp | |
| import queue | |
| import time | |
| sys.path.append("./") | |
| sys.path.append(f"./policy") | |
| sys.path.append("./description/utils") | |
| from envs import CONFIGS_PATH | |
| from envs.utils.create_actor import UnStableError | |
| import numpy as np | |
| from pathlib import Path | |
| from collections import deque | |
| import traceback | |
| import yaml | |
| from datetime import datetime | |
| import importlib | |
| import argparse | |
| import pdb | |
| import ast | |
| from generate_episode_instructions import * | |
| current_file_path = os.path.abspath(__file__) | |
| parent_directory = os.path.dirname(current_file_path) | |
| DEMO_PREFLIGHT_TIMEOUT = 300 | |
| def class_decorator(task_name): | |
| envs_module = importlib.import_module(f"envs.{task_name}") | |
| try: | |
| env_class = getattr(envs_module, task_name) | |
| env_instance = env_class() | |
| except: | |
| raise SystemExit("No Task") | |
| return env_instance | |
| def eval_function_decorator(policy_name, model_name): | |
| try: | |
| policy_model = importlib.import_module(policy_name) | |
| return getattr(policy_model, model_name) | |
| except ImportError as e: | |
| raise e | |
| def run_demo_preflight_once(task_name: str, args: dict, now_id: int, now_seed: int) -> tuple[str, dict | str]: | |
| task_env = None | |
| try: | |
| task_env = class_decorator(task_name) | |
| task_env.setup_demo(now_ep_num=now_id, seed=now_seed, is_test=True, **args) | |
| episode_info = task_env.play_once() | |
| return ( | |
| "ok", | |
| { | |
| "episode_info": {"info": episode_info["info"]}, | |
| "success": bool(task_env.plan_success and task_env.check_success()), | |
| }, | |
| ) | |
| except UnStableError as e: | |
| return "unstable", str(e) | |
| except Exception: | |
| return "error", traceback.format_exc() | |
| finally: | |
| if task_env is not None: | |
| try: | |
| task_env.close_env() | |
| except Exception: | |
| pass | |
| def demo_preflight_worker(task_name: str, request_queue, result_queue) -> None: | |
| while True: | |
| request = request_queue.get() | |
| if request is None: | |
| break | |
| request_id, args, now_id, now_seed = request | |
| status, payload = run_demo_preflight_once(task_name, args, now_id, now_seed) | |
| result_queue.put((request_id, status, payload)) | |
| class DemoPreflightRunner: | |
| def __init__(self, task_name: str, timeout: int = DEMO_PREFLIGHT_TIMEOUT) -> None: | |
| self.task_name = task_name | |
| self.timeout = timeout | |
| self.ctx = mp.get_context("spawn") | |
| self.request_id = 0 | |
| self.process = None | |
| self.request_queue = None | |
| self.result_queue = None | |
| self.start() | |
| def start(self) -> None: | |
| self.request_queue = self.ctx.Queue() | |
| self.result_queue = self.ctx.Queue() | |
| self.process = self.ctx.Process( | |
| target=demo_preflight_worker, | |
| args=(self.task_name, self.request_queue, self.result_queue), | |
| ) | |
| self.process.daemon = True | |
| self.process.start() | |
| def close(self, force: bool = False) -> None: | |
| if self.process is None: | |
| return | |
| if self.process.is_alive() and not force: | |
| try: | |
| self.request_queue.put(None) | |
| except Exception: | |
| pass | |
| self.process.join(5) | |
| if self.process.is_alive(): | |
| self.process.terminate() | |
| self.process.join(5) | |
| if self.process.is_alive(): | |
| self.process.kill() | |
| self.process.join() | |
| def restart(self, force: bool = False) -> None: | |
| self.close(force=force) | |
| self.start() | |
| def run(self, args: dict, now_id: int, now_seed: int) -> tuple[str, dict | str | None]: | |
| if self.process is None or not self.process.is_alive(): | |
| self.restart() | |
| self.request_id += 1 | |
| request_id = self.request_id | |
| self.request_queue.put((request_id, args, now_id, now_seed)) | |
| deadline = time.time() + self.timeout | |
| while True: | |
| try: | |
| result_id, status, payload = self.result_queue.get_nowait() | |
| if result_id == request_id: | |
| return status, payload | |
| except queue.Empty: | |
| if not self.process.is_alive(): | |
| exitcode = self.process.exitcode | |
| self.restart() | |
| return "error", f"preflight worker exited with code {exitcode}" | |
| if time.time() >= deadline: | |
| self.restart(force=True) | |
| return "timeout", None | |
| time.sleep(0.2) | |
| def get_camera_config(camera_type): | |
| camera_config_path = os.path.join(parent_directory, "../task_config/_camera_config.yml") | |
| assert os.path.isfile(camera_config_path), "task config file is missing" | |
| with open(camera_config_path, "r", encoding="utf-8") as f: | |
| args = yaml.load(f.read(), Loader=yaml.FullLoader) | |
| assert camera_type in args, f"camera {camera_type} is not defined" | |
| return args[camera_type] | |
| def get_embodiment_config(robot_file): | |
| robot_config_file = os.path.join(robot_file, "config.yml") | |
| with open(robot_config_file, "r", encoding="utf-8") as f: | |
| embodiment_args = yaml.load(f.read(), Loader=yaml.FullLoader) | |
| return embodiment_args | |
| def parse_bool(value): | |
| if isinstance(value, bool): | |
| return value | |
| if isinstance(value, (int, float)): | |
| return bool(value) | |
| if isinstance(value, str): | |
| value = value.strip().lower() | |
| if value in ("1", "true", "yes", "on"): | |
| return True | |
| if value in ("0", "false", "no", "off"): | |
| return False | |
| raise ValueError(f"cannot parse boolean value: {value!r}") | |
| def keep_task_static_cameras(embodiment_config, task_args): | |
| config = dict(embodiment_config) | |
| static_cameras = config.get("static_camera_list", []) | |
| allowed_names = set() | |
| if task_args.get("camera", {}).get("collect_head_camera", True): | |
| allowed_names.add("head_camera") | |
| config["static_camera_list"] = [ | |
| dict(camera) for camera in static_cameras if camera.get("name") in allowed_names | |
| ] | |
| return config | |
| def main(usr_args): | |
| current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
| task_name = usr_args["task_name"] | |
| task_config = usr_args["task_config"] | |
| ckpt_setting = usr_args["ckpt_setting"] | |
| # checkpoint_num = usr_args['checkpoint_num'] | |
| policy_name = usr_args["policy_name"] | |
| instruction_type = usr_args["instruction_type"] | |
| save_dir = None | |
| video_save_dir = None | |
| video_size = None | |
| get_model = eval_function_decorator(policy_name, "get_model") | |
| with open(f"./task_config/{task_config}.yml", "r", encoding="utf-8") as f: | |
| args = yaml.load(f.read(), Loader=yaml.FullLoader) | |
| if "eval_video_log" in usr_args: | |
| args["eval_video_log"] = parse_bool(usr_args["eval_video_log"]) | |
| if "eval_step_lim" in usr_args: | |
| args["eval_step_lim"] = int(usr_args["eval_step_lim"]) | |
| if "force_arm_tag" in usr_args: | |
| args["force_arm_tag"] = usr_args["force_arm_tag"] | |
| if "force_block_arm_tag" in usr_args: | |
| args["force_block_arm_tag"] = usr_args["force_block_arm_tag"] | |
| args['task_name'] = task_name | |
| args["task_config"] = task_config | |
| args["ckpt_setting"] = ckpt_setting | |
| embodiment_type = args.get("embodiment") | |
| embodiment_config_path = os.path.join(CONFIGS_PATH, "_embodiment_config.yml") | |
| with open(embodiment_config_path, "r", encoding="utf-8") as f: | |
| _embodiment_types = yaml.load(f.read(), Loader=yaml.FullLoader) | |
| def get_embodiment_file(embodiment_type): | |
| robot_file = _embodiment_types[embodiment_type]["file_path"] | |
| if robot_file is None: | |
| raise "No embodiment files" | |
| return robot_file | |
| with open(CONFIGS_PATH + "_camera_config.yml", "r", encoding="utf-8") as f: | |
| _camera_config = yaml.load(f.read(), Loader=yaml.FullLoader) | |
| head_camera_type = args["camera"]["head_camera_type"] | |
| args["head_camera_h"] = _camera_config[head_camera_type]["h"] | |
| args["head_camera_w"] = _camera_config[head_camera_type]["w"] | |
| if len(embodiment_type) == 1: | |
| args["left_robot_file"] = get_embodiment_file(embodiment_type[0]) | |
| args["right_robot_file"] = get_embodiment_file(embodiment_type[0]) | |
| args["dual_arm_embodied"] = True | |
| elif len(embodiment_type) == 3: | |
| args["left_robot_file"] = get_embodiment_file(embodiment_type[0]) | |
| args["right_robot_file"] = get_embodiment_file(embodiment_type[1]) | |
| args["embodiment_dis"] = embodiment_type[2] | |
| args["dual_arm_embodied"] = False | |
| else: | |
| raise "embodiment items should be 1 or 3" | |
| args["left_embodiment_config"] = keep_task_static_cameras(get_embodiment_config(args["left_robot_file"]), args) | |
| args["right_embodiment_config"] = keep_task_static_cameras(get_embodiment_config(args["right_robot_file"]), args) | |
| if len(embodiment_type) == 1: | |
| embodiment_name = str(embodiment_type[0]) | |
| else: | |
| embodiment_name = str(embodiment_type[0]) + "+" + str(embodiment_type[1]) | |
| save_dir = Path(f"eval_result/{task_name}/{policy_name}/{task_config}/{ckpt_setting}/{current_time}") | |
| save_dir.mkdir(parents=True, exist_ok=True) | |
| if args["eval_video_log"]: | |
| video_save_dir = save_dir | |
| camera_config = get_camera_config(args["camera"]["head_camera_type"]) | |
| video_size = str(camera_config["w"]) + "x" + str(camera_config["h"]) | |
| video_save_dir.mkdir(parents=True, exist_ok=True) | |
| args["eval_video_save_dir"] = video_save_dir | |
| # output camera config | |
| print("============= Config =============\n") | |
| print("\033[95mMessy Table:\033[0m " + str(args["domain_randomization"]["cluttered_table"])) | |
| print("\033[95mRandom Background:\033[0m " + str(args["domain_randomization"]["random_background"])) | |
| if args["domain_randomization"]["random_background"]: | |
| print(" - Clean Background Rate: " + str(args["domain_randomization"]["clean_background_rate"])) | |
| print("\033[95mRandom Light:\033[0m " + str(args["domain_randomization"]["random_light"])) | |
| if args["domain_randomization"]["random_light"]: | |
| print(" - Crazy Random Light Rate: " + str(args["domain_randomization"]["crazy_random_light_rate"])) | |
| print("\033[95mRandom Table Height:\033[0m " + str(args["domain_randomization"]["random_table_height"])) | |
| print("\033[95mRandom Head Camera Distance:\033[0m " + str(args["domain_randomization"]["random_head_camera_dis"])) | |
| print("\033[94mHead Camera Config:\033[0m " + str(args["camera"]["head_camera_type"]) + f", " + | |
| str(args["camera"]["collect_head_camera"])) | |
| print("\033[94mWrist Camera Config:\033[0m " + str(args["camera"]["wrist_camera_type"]) + f", " + | |
| str(args["camera"]["collect_wrist_camera"])) | |
| print("\033[94mEmbodiment Config:\033[0m " + embodiment_name) | |
| print("\n==================================") | |
| TASK_ENV = class_decorator(args["task_name"]) | |
| args["policy_name"] = policy_name | |
| usr_args["left_arm_dim"] = len(args["left_embodiment_config"]["arm_joints_name"][0]) | |
| usr_args["right_arm_dim"] = len(args["right_embodiment_config"]["arm_joints_name"][1]) | |
| seed = usr_args["seed"] | |
| st_seed = int(usr_args.get("fixed_eval_seed", 100000 * (1 + seed))) | |
| suc_nums = [] | |
| test_num = int(usr_args.get("eval_test_num", 100)) | |
| topk = 1 | |
| model = get_model(usr_args) | |
| st_seed, suc_num = eval_policy(task_name, | |
| TASK_ENV, | |
| args, | |
| model, | |
| st_seed, | |
| test_num=test_num, | |
| video_size=video_size, | |
| instruction_type=instruction_type) | |
| suc_nums.append(suc_num) | |
| topk_success_rate = sorted(suc_nums, reverse=True)[:topk] | |
| file_path = os.path.join(save_dir, f"_result.txt") | |
| with open(file_path, "w") as file: | |
| file.write(f"Timestamp: {current_time}\n\n") | |
| file.write(f"Instruction Type: {instruction_type}\n\n") | |
| # file.write(str(task_reward) + '\n') | |
| file.write("\n".join(map(str, np.array(suc_nums) / test_num))) | |
| print(f"Data has been saved to {file_path}") | |
| # return task_reward | |
| def eval_policy(task_name, | |
| TASK_ENV, | |
| args, | |
| model, | |
| st_seed, | |
| test_num=100, | |
| video_size=None, | |
| instruction_type=None): | |
| print(f"\033[34mTask Name: {args['task_name']}\033[0m") | |
| print(f"\033[34mPolicy Name: {args['policy_name']}\033[0m") | |
| TASK_ENV.suc = 0 | |
| TASK_ENV.test_num = 0 | |
| now_id = 0 | |
| succ_seed = 0 | |
| suc_test_seed_list = [] | |
| policy_name = args["policy_name"] | |
| eval_func = eval_function_decorator(policy_name, "eval") | |
| reset_func = eval_function_decorator(policy_name, "reset_model") | |
| now_seed = st_seed | |
| task_total_reward = 0 | |
| clear_cache_freq = args["clear_cache_freq"] | |
| args["eval_mode"] = True | |
| preflight_runner = DemoPreflightRunner(task_name) | |
| try: | |
| while succ_seed < test_num: | |
| render_freq = args["render_freq"] | |
| args["render_freq"] = 0 | |
| preflight_status, preflight_payload = preflight_runner.run(args, now_id, now_seed) | |
| if preflight_status == "timeout": | |
| print(f"demo preflight timeout after {DEMO_PREFLIGHT_TIMEOUT}s, skip seed {now_seed}") | |
| now_seed += 1 | |
| args["render_freq"] = render_freq | |
| continue | |
| if preflight_status == "unstable": | |
| now_seed += 1 | |
| args["render_freq"] = render_freq | |
| continue | |
| if preflight_status == "error": | |
| print(" -------------") | |
| print("Error: ", preflight_payload) | |
| print(" -------------") | |
| now_seed += 1 | |
| args["render_freq"] = render_freq | |
| print("error occurs !") | |
| continue | |
| episode_info = preflight_payload["episode_info"] | |
| if preflight_payload["success"]: | |
| succ_seed += 1 | |
| suc_test_seed_list.append(now_seed) | |
| else: | |
| now_seed += 1 | |
| args["render_freq"] = render_freq | |
| continue | |
| args["render_freq"] = render_freq | |
| TASK_ENV.setup_demo(now_ep_num=now_id, seed=now_seed, is_test=True, **args) | |
| episode_info_list = [episode_info["info"]] | |
| results = generate_episode_descriptions(args["task_name"], episode_info_list, test_num) | |
| instruction_choices = results[0].get(instruction_type, []) if results else [] | |
| instruction = np.random.choice(instruction_choices) if instruction_choices else "" | |
| TASK_ENV.set_instruction(instruction=instruction) # set language instruction | |
| if TASK_ENV.eval_video_path is not None: | |
| ffmpeg = subprocess.Popen( | |
| [ | |
| "ffmpeg", | |
| "-y", | |
| "-loglevel", | |
| "error", | |
| "-f", | |
| "rawvideo", | |
| "-pixel_format", | |
| "rgb24", | |
| "-video_size", | |
| video_size, | |
| "-framerate", | |
| "10", | |
| "-i", | |
| "-", | |
| "-pix_fmt", | |
| "yuv420p", | |
| "-vcodec", | |
| "libx264", | |
| "-crf", | |
| "23", | |
| f"{TASK_ENV.eval_video_path}/episode{TASK_ENV.test_num}.mp4", | |
| ], | |
| stdin=subprocess.PIPE, | |
| ) | |
| TASK_ENV._set_eval_video_ffmpeg(ffmpeg) | |
| succ = False | |
| reset_func(model) | |
| while TASK_ENV.take_action_cnt < TASK_ENV.step_lim: | |
| observation = TASK_ENV.get_obs() | |
| eval_func(TASK_ENV, model, observation) | |
| if TASK_ENV.eval_success: | |
| succ = True | |
| break | |
| # task_total_reward += TASK_ENV.episode_score | |
| if TASK_ENV.eval_video_path is not None: | |
| TASK_ENV._del_eval_video_ffmpeg() | |
| if succ: | |
| TASK_ENV.suc += 1 | |
| print("\033[92mSuccess!\033[0m") | |
| else: | |
| print("\033[91mFail!\033[0m") | |
| now_id += 1 | |
| TASK_ENV.close_env(clear_cache=((succ_seed + 1) % clear_cache_freq == 0)) | |
| if TASK_ENV.render_freq: | |
| TASK_ENV.viewer.close() | |
| TASK_ENV.test_num += 1 | |
| print( | |
| f"\033[93m{task_name}\033[0m | \033[94m{args['policy_name']}\033[0m | \033[92m{args['task_config']}\033[0m | \033[91m{args['ckpt_setting']}\033[0m\n" | |
| f"Success rate: \033[96m{TASK_ENV.suc}/{TASK_ENV.test_num}\033[0m => \033[95m{round(TASK_ENV.suc/TASK_ENV.test_num*100, 1)}%\033[0m, current seed: \033[90m{now_seed}\033[0m\n" | |
| ) | |
| # TASK_ENV._take_picture() | |
| now_seed += 1 | |
| finally: | |
| preflight_runner.close() | |
| return now_seed, TASK_ENV.suc | |
| def parse_args_and_config(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--config", type=str, required=True) | |
| parser.add_argument("--overrides", nargs=argparse.REMAINDER) | |
| args = parser.parse_args() | |
| with open(args.config, "r", encoding="utf-8") as f: | |
| config = yaml.safe_load(f) | |
| # Parse overrides | |
| def parse_override_pairs(pairs): | |
| if len(pairs) % 2 != 0: | |
| raise ValueError(f"overrides must be key/value pairs, got odd argument count: {pairs}") | |
| override_dict = {} | |
| for i in range(0, len(pairs), 2): | |
| key = pairs[i].lstrip("--") | |
| value = pairs[i + 1] | |
| try: | |
| value = ast.literal_eval(value) | |
| except (ValueError, SyntaxError): | |
| pass | |
| override_dict[key] = value | |
| return override_dict | |
| if args.overrides: | |
| overrides = parse_override_pairs(args.overrides) | |
| config.update(overrides) | |
| return config | |
| if __name__ == "__main__": | |
| from test_render import Sapien_TEST | |
| Sapien_TEST() | |
| usr_args = parse_args_and_config() | |
| main(usr_args) | |