backupforme / VLABench /debug.py
czxlovesu's picture
Add files using upload-large-folder tool
2016dc0 verified
import numpy as np
import tensorflow_datasets as tfds
from VLABench.evaluation.model.policy.openvla import OpenVLA
# RLDS 训练集路径(留出以便替换)
rlds_path = "/home/zhao.bai/ArenaVlaSafety/rlds_out_backup/rlds_out/sem_pour_water_electronics/1.0.0"
# 模型与 LoRA
model_ckpt = "/home/zhao.bai/arena/openvla-oft/runs/arena_single/sem_pour_water_electronics_rlds/openvla-7b-oft-finetuned-10000_chkpt"
lora_ckpt = model_ckpt + "/lora_adapter"
norm_cfg = "/tmp/zhao.bai/work/repo/VLABench/VLABench/configs/model/openvla_config.json"
unnorm_key = "sem_pour_water_electronics_delta"
# 加载数据
ds = tfds.builder_from_directory(rlds_path).as_dataset(split="train")
policy = OpenVLA(
model_ckpt=model_ckpt,
lora_ckpt=lora_ckpt,
norm_config_file=norm_cfg,
debug_actions=True, # 会打印当前使用的 norm_stats
device="cuda:0",
)
max_episodes = 5 # 只取少量样本做对比
sample_print = 10 # 打印若干步详细对比
total_l2, total_l1, total_count = 0.0, 0.0, 0
printed = 0
for ex_id, ex in enumerate(tfds.as_numpy(ds.take(max_episodes))):
for step in ex["steps"]:
rgb = np.stack(
[step["observation"][k] for k in ("image_0", "image_1", "front", "wrist")],
axis=0,
)
obs = {
"rgb": rgb,
# 用真实 ee_state(pos+euler+grip),让模型预测基于相同的归一化参考
"ee_state": step["observation"]["ee_state"].astype(np.float32),
"instruction": step["language_instruction"].decode(),
}
# 获取模型预测的 delta(已经反归一化)
inputs = policy.process_observation(obs, unnorm_key=unnorm_key)
pred = np.array(policy.model.predict_action(**inputs, unnorm_key=unnorm_key, do_sample=False))
gt = np.array(step["action"], dtype=np.float32)
# 对齐尺度:只比较前 6 维(pos+euler),gripper 可以单独看
diff = pred[:6] - gt[:6]
total_l2 += np.linalg.norm(diff)
total_l1 += np.abs(diff).sum()
total_count += 1
if printed < sample_print:
print(f"[sample {printed}] instr='{obs['instruction']}'")
print(f" pred: {np.round(pred, 6)}")
print(f" gt : {np.round(gt, 6)}")
print(f" diff(0:6): {np.round(diff, 6)} (l2={np.linalg.norm(diff):.6f})")
print(f" gripper pred={pred[-1]:.3f}, gt={gt[-1]:.3f}")
printed += 1
mean_l2 = total_l2 / max(1, total_count)
mean_l1 = total_l1 / max(1, total_count)
print(f"\nCompared {total_count} steps across {max_episodes} episodes.")
print(f"Mean L2 diff per step: {mean_l2:.6f}")
print(f"Mean L1 diff per step: {mean_l1:.6f}")