File size: 4,606 Bytes
f9c42e5 | 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 | import sys
sys.stdout = open(sys.stdout.fileno(), mode="w", buffering=1)
sys.stderr = open(sys.stderr.fileno(), mode="w", buffering=1)
import numpy as np
import os
import pathlib
import click
import hydra
import torch
import dill
import wandb
import json
import random
from omegaconf import open_dict, OmegaConf
# os.environ["LD_LIBRARY_PATH"] = "/hpc2hdd/home/jfeng644/.mujoco/mujoco210/bin:/usr/lib/nvidia:" + os.environ.get("LD_LIBRARY_PATH", "")
from unified_video_action.workspace.base_workspace import BaseWorkspace
from unified_video_action.utils.load_env import load_env_runner
@click.command()
@click.option("-c", "--checkpoint", required=True)
@click.option("-o", "--output_dir", required=True)
@click.option("-d", "--device", default="cuda:0")
@click.option("--config-override", default='unified_video_action/config/task/libero10.yaml', help="Path to additional config file to override settings")
@click.option("--view-key", default="agentview_340_image", help="View key for testing")
def main(checkpoint, output_dir, device, config_override, view_key):
pathlib.Path(output_dir).mkdir(parents=True, exist_ok=True)
# load checkpoint
payload = torch.load(open(checkpoint, "rb"), pickle_module=dill)
cfg = payload["cfg"]
# # Update config with additional config file if provided
# if config_override is not None:
# override_cfg = OmegaConf.load(config_override)
# # 确保task键存在,如果不存在就创建
# if 'task' not in cfg:
# cfg['task'] = OmegaConf.create({})
# # 递归地确保所有嵌套键都存在
# def ensure_keys_exist(base_cfg, override_cfg):
# for key, value in override_cfg.items():
# if key not in base_cfg:
# base_cfg[key] = OmegaConf.create({})
# if isinstance(value, dict) and isinstance(base_cfg[key], dict):
# ensure_keys_exist(base_cfg[key], value)
# else:
# base_cfg[key] = value
# ensure_keys_exist(cfg['task'], override_cfg)
# ensure_keys_exist(cfg['model']['policy']['shape_meta'], override_cfg['shape_meta'])
# print(f"Updated config with override file: {config_override}")
# config_2 = 'unified_video_action/config/vpp_libero10.yaml'
# override_cfg = OmegaConf.load(config_2)
# del override_cfg['defaults']
# ensure_keys_exist(cfg, override_cfg)
# config_3 = 'unified_video_action/config/model/vpp.yaml'
# override_cfg = OmegaConf.load(config_3)
# ensure_keys_exist(cfg['model'], override_cfg)
# print(f"Updated config with override file: {config_3}")
# print(f"Updated config with override file: {config_2}")
# set seed
seed = cfg.training.seed
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
with open_dict(cfg):
cfg.output_dir = output_dir
# 设置view_key参数
cfg.task.env_runner.view_key = view_key
# configure workspace
cls = hydra.utils.get_class(cfg.model._target_)
workspace = cls(cfg, output_dir=output_dir)
workspace: BaseWorkspace
print("Loaded checkpoint from %s" % checkpoint)
workspace.load_payload(payload, exclude_keys=None, include_keys=None)
# get policy from workspace
policy = workspace.ema_model
policy.to(device)
policy.eval()
env_runners = load_env_runner(cfg, output_dir)
if "libero" in cfg.task.name:
step_log = {}
for env_runner in env_runners:
runner_log = env_runner.run(policy)
step_log.update(runner_log)
print(step_log)
assert "test_mean_score" not in step_log
all_test_mean_score = {
k: v for k, v in step_log.items() if "test/" in k and "_mean_score" in k
}
step_log["test_mean_score"] = np.mean(list(all_test_mean_score.values()))
runner_log = step_log
else:
env_runner = env_runners
runner_log = env_runner.run(policy)
# dump log to json
json_log = dict()
for key, value in runner_log.items():
if isinstance(value, wandb.sdk.data_types.video.Video):
json_log[key] = value._path
else:
json_log[key] = value
for k, v in json_log.items():
print(k, v)
out_path = os.path.join(output_dir, f'eval_log_{checkpoint.split("/")[-1]}.json')
print("Saving log to %s" % out_path)
json.dump(json_log, open(out_path, "w"), indent=2, sort_keys=True)
if __name__ == "__main__":
main()
|