File size: 2,922 Bytes
f0c8b65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tensorflow as tf
import os
from PPO_Model import PPOAgent

gpus = tf.config.list_physical_devices('GPU')
if gpus:
    try:
        for gpu in gpus:
            tf.config.experimental.set_memory_growth(gpu, True)
        logical_gpus = tf.config.experimental.list_logical_devices('GPU')
        print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
    except RuntimeError as e:
        print(e)

agent_config = {
    "env_id": "CarRacing-v3",
    "num_envs": 21,
    "gamma": 0.99,
    "lam": 0.95,
    "clip_epsilon": 0.2,
    "actor_lr": 3e-4,
    "critic_lr": 3e-4,
    "ppo_epochs": 10,
    "minibatches": 4,
    "steps_per_batch": 1024,
    "num_stack_frames": 4,
    "resize_dim": (84, 84),
    "grayscale": True,
    "seed": 42,
    "log_dir": "./ppo_car_racing_logs",
    "entropy_coeff": 0.01,
    'save_interval_timesteps': 537600,
    'hidden_layer_sizes': [512, 512, 512]
}

if __name__ == "__main__":
    print("Initializing PPO Agent for evaluation...")
    
    agent = PPOAgent(**agent_config)

    root_log_dir = "./ppo_car_racing_logs"

    latest_log_run_dir = None
    if os.path.exists(root_log_dir):
        all_runs = [os.path.join(root_log_dir, d) for d in os.listdir(root_log_dir) if os.path.isdir(os.path.join(root_log_dir, d))]
        if all_runs:
    
            latest_log_run_dir = max(all_runs, key=os.path.getmtime)
            print(f"Found latest training run directory: {latest_log_run_dir}")
        else:
            print(f"No training run directories found in {root_log_dir}.")
    else:
        print(f"Log directory {root_log_dir} does not exist. Cannot find trained model.")


    model_to_load = None
    if latest_log_run_dir:

        final_model_path = os.path.join(latest_log_run_dir, "final_model.weights.h5")
        if os.path.exists(final_model_path):
            model_to_load = final_model_path
        else:
            print(f"Final model weights not found in {latest_log_run_dir}. Checking checkpoints...")
            
            checkpoint_dir = os.path.join(latest_log_run_dir, "checkpoints")
            if os.path.exists(checkpoint_dir):
                all_checkpoints = [os.path.join(checkpoint_dir, f) for f in os.listdir(checkpoint_dir) if f.endswith(".weights.h5")]
                if all_checkpoints:
                    model_to_load = max(all_checkpoints, key=os.path.getmtime)
                    print(f"Loading latest checkpoint: {model_to_load}")
                else:
                    print("No checkpoints found.")
            else:
                print("Checkpoints directory does not exist.")


    if model_to_load:
        
        print("\n--- Evaluation ---")
        agent.evaluate(num_episodes=10, render=True, model_path=model_to_load)
    else:
        print("No trained model found to evaluate. Please train an agent first.")