File size: 3,116 Bytes
bff785d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# This Gradio app provides a simple interface to evaluate a trained drone hovering policy.
# It allows users to select a checkpoint and optionally record a video of the evaluation.
# Note: The `torch` library is not available, so the code has been modified to remove the dependency.

import gradio as gr
import os
import pickle

# Assuming the `evaluate` function from the provided code is available here.
# The `evaluate` function is responsible for running the trained policy in a single environment with visualization.

def evaluate_policy(exp_name, ckpt, record):
    # Load the environment and policy configuration saved during training
    log_dir = f"logs/{exp_name}"
    if not os.path.exists(log_dir):
        raise FileNotFoundError(f"Log directory '{log_dir}' does not exist.  Did you run training?")
    env_cfg, obs_cfg, reward_cfg, command_cfg, train_cfg = pickle.load(open(f"{log_dir}/cfgs.pkl", "rb"))
    
    # For evaluation, we disable reward scaling (pure inference)
    reward_cfg["reward_scales"] = {}
    
    # Always visualize the target during evaluation
    env_cfg["visualize_target"] = True
    
    # Optionally set up a camera for recording
    env_cfg["visualize_camera"] = record
    env_cfg["max_visualize_FPS"] = 60
    
    # Build a single-environment instance with viewer
    env = HoverEnv(num_envs=1, env_cfg=env_cfg, obs_cfg=obs_cfg, reward_cfg=reward_cfg, command_cfg=command_cfg, show_viewer=True)
    runner = OnPolicyRunner(env, train_cfg, log_dir, device=gs.device)
    
    # Load the specified checkpoint
    resume_path = os.path.join(log_dir, f"model_{ckpt}.pt")
    runner.load(resume_path)
    
    # Get the inference policy
    policy = runner.get_inference_policy(device=gs.device)
    
    # Reset the environment
    obs, _ = env.reset()
    
    # Number of simulation steps equal to the episode duration times FPS
    max_sim_step = int(env_cfg["episode_length_s"] * env_cfg["max_visualize_FPS"])
    
    if record and env.cam is not None:
        env.cam.start_recording()
        for _ in range(max_sim_step):
            actions = policy(obs)
            obs, rews, dones, infos = env.step(actions)
            env.cam.render()
        env.cam.stop_recording(save_to_filename="video.mp4", fps=env_cfg["max_visualize_FPS"])
    else:
        for _ in range(max_sim_step):
            actions = policy(obs)
            obs, rews, dones, infos = env.step(actions)
    
    return "Evaluation completed successfully."

# Create a Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# Drone Hovering Policy Evaluation")
    
    with gr.Row():
        exp_name = gr.Textbox(label="Experiment Name", placeholder="drone-hovering")
        ckpt = gr.Number(label="Checkpoint Index", value=300, precision=0)
        record = gr.Checkbox(label="Record Video", value=False)
    
    with gr.Row():
        evaluate_btn = gr.Button("Evaluate Policy")
    
    output = gr.Textbox(label="Evaluation Status")
    
    evaluate_btn.click(fn=evaluate_policy, inputs=[exp_name, ckpt, record], outputs=output)

# Launch the interface
demo.launch(show_error=True)