-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuman_similarity_calculator.py
More file actions
executable file
·276 lines (226 loc) · 9.05 KB
/
human_similarity_calculator.py
File metadata and controls
executable file
·276 lines (226 loc) · 9.05 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import argparse
import numpy as np
import torch
import glob
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
from collections import deque
import os
import sys
import re
sys.path.append("human_similarity")
sys.path.append("RL")
sys.path.append("VQVAE")
sys.path.append("IQL")
import gym
import d4rl
import json
import csv
from fastdtw import fastdtw
from scipy.spatial.distance import euclidean
import pprint
import pandas as pd
import matplotlib.pyplot as plt
import os
import moviepy.video.io.ImageSequenceClip
import pickle
from human_similarity.utils import *
import glob
from tensorboard.backend.event_processing import event_accumulator
sys.path.append('../offline_data')
from human_similarity_utils import (
run_agent_episode,
evaluate_agent_performance,
evaluate_saved_predictions,
calculate_dtw_distances,
calculate_wasserstein_distances,
calculate_min_dtw_distances
)
DEFAULT_SEQLEN_IQL = 3
DEFAULT_K_IQL = 16
MAQ_IQL_CSV_FILENAME = "experiment_short.csv"
# --- Environment Horizon Constant ---
# Set this to None to use environment's default horizon, or set to a specific number
# If set, episodes will terminate early if success is achieved before the horizon
ENVIRONMENT_HORIZON = None # You can change this value (e.g., 1000, 500, etc.)
def load_trajectories(file):
"""從檔案加載 trajectory 數據"""
with open(file, 'rb') as f:
return pickle.load(f)
def get_d4rl_dataset(env: gym.Env, clip_to_eps: bool = True, eps: float = 1e-5):
dataset = d4rl.qlearning_dataset(gym.make(env))
dones_float = np.zeros_like(dataset['rewards'])
for i in range(len(dones_float) - 1):
if np.linalg.norm(dataset['observations'][i + 1] - dataset['next_observations'][i]) > 1e-6 or dataset['terminals'][i] == 1.0:
dones_float[i] = 1
else:
dones_float[i] = 0
dones_float[-1] = 1
return dataset['observations'].astype(np.float32), dataset['actions'].astype(np.float32), dataset['rewards'].astype(np.float32), dones_float.astype(np.float32), dataset['next_observations'].astype(np.float32)
def normalized_score(env,score):
return env.get_normalized_score(score)*100
def Calc_Distance(A, B):
# get L1 distance
assert A.shape == B.shape
return np.sum(np.abs(A - B))
def compute_dtw_distance(traj1, traj2):
assert traj1[0].shape == traj2[0].shape
distance, _ = fastdtw(traj1, traj2, dist=euclidean)
return distance
def compute_euclidean_distance(traj1, traj2):
assert len(traj1) == len(traj2), "The trajectories must have the same length."
for t1, t2 in zip(traj1, traj2):
assert t1.shape == t2.shape, "Each step in the trajectories must have the same shape."
total_distance = sum(np.linalg.norm(t1 - t2) for t1, t2 in zip(traj1, traj2))
return total_distance
def similarity_metrics(env, env_id, testing_dataset, eval_seqlen, eval_episodes, report_file,
agents = [],
render=False,
horizon=None):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
metrics_to_calculate = {
'dtw': calculate_dtw_distances,
'wasserstein': calculate_wasserstein_distances,
'min_dtw': calculate_min_dtw_distances
}
# --- Evaluate Each Agent ---
for agent in agents:
agent_seed = agent.get_seed()
agent_type = agent.get_agent_type()
human_traj_states = []
human_traj_actions = []
test_trajectories = load_trajectories(f"offline_data/{testing_dataset}")
for traj in test_trajectories:
human_traj_states.append(traj['observations'])
human_traj_actions.append(traj['actions'])
print(f"Loaded {len(human_traj_states)} human trajectories for comparison (seed {agent_seed}).")
print(f"Processing Agent: {agent_type}, Seed: {agent_seed}")
evaluate_agent_performance(
agent=agent,
env_id=env_id,
eval_episodes=eval_episodes,
human_trajs_states=human_traj_states,
human_trajs_actions=human_traj_actions,
metric_fns=metrics_to_calculate,
base_seed=agent_seed, # Use agent's seed for its eval episodes
render=render,
results_prefix="agent_performance", # Specific prefix for performance results
horizon=horizon # Pass horizon parameter
)
print("\n--- similarity_metrics function finished ---")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Evaluating methods.')
parser.add_argument('--env', type=str,help='')
parser.add_argument('--gpuid', type=str,help='',default='3')
parser.add_argument('--eval_agent', type=str,help='',default='')
parser.add_argument('--render', type=bool, default=False, help='Render and save video of agent performance')
parser.add_argument('--suffix', type=str,help='',default='')
parser.add_argument('--model_path', type=str,help='',default='')
parser.add_argument('--seed', type=str,help='',default='')
parser.add_argument('--training_dataset', type=str,help='',default='')
parser.add_argument('--testing_dataset', type=str,help='',default='')
args = parser.parse_args()
if args.gpuid != '':
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpuid
# Parameters
eval_seqlen = 3
eval_episodes = 100
if args.render:
eval_episodes = 5
seed = args.seed
env_id = args.env
method = args.eval_agent
training_dataset = args.training_dataset
testing_dataset = args.testing_dataset
suffix = args.suffix
if not method in ["MAQ+DSAC","MAQ+RLPD","MAQ+IQL","SAC","RLPD","IQL"]:
print("Method must be one of MAQ+DSAC, MAQ+RLPD, MAQ+IQL, SAC, RLPD, IQL")
exit(0)
generic_agent_type = None
if method == "MAQ+RLPD":
base_paths = [f"RLPD_MAQ/log/exp_{suffix}"]
target_keywords = ["RLPDMAQ"]
print(f"Base Paths: {base_paths}")
agents = get_best_MAQ_agent(
base_paths, # Use the constructed base paths
args.env,
env_id,
agent_type=RLPDMAQAgent,
target_keywords=target_keywords,
tag=suffix,
seed=str(seed)
)
generic_agent_type=RLPDMAQAgent
elif method == "MAQ+DSAC":
base_paths = [f"RLPD_MAQ/log/exp_{suffix}"]
target_keywords = ["DSACMAQ"]
print(f"Base Paths: {base_paths}")
agents = get_best_MAQ_agent(
base_paths, # Use the constructed base paths
args.env,
env_id,
agent_type=DSACMAQAgent,
target_keywords=target_keywords,
tag=suffix,
seed=str(seed)
)
generic_agent_type=DSACMAQAgent
elif method == "MAQ+IQL":
generic_agent_type=MAQIQLAgent
if args.model_path == '':
base_paths = [
f"IQL/log/{env_id}/MAQ_iql{suffix}_seed{seed}",
]
print(f"Base Paths: {base_paths}")
agents = get_best_MAQ_IQL_agent(
base_paths,
args.env,
env_id,
tag=suffix
)
elif method == "SAC":
generic_agent_type=SB3Agent
if args.model_path == '':
agents = []
ckpt = find_best_checkpoint_SAC(f"SAC/log/{args.env}/SAC_seed{seed}")
agents.append(FlexibleAgentInterface(SB3Agent, ckpt, env_id))
elif method == "RLPD":
os.environ["XLA_PYTHON_CLIENT_PREALLOCATE"] = "false"
import d4rl.gym_mujoco
import d4rl.locomotion
import dmcgym
from rlpd.wrappers import wrap_gym
from human_similarity.agent_rlpd import *
agents = []
#checkpoint_0.orbax-checkpoint-tmp-0
ckpt = f"/workspace/rlpd/log_door_seed1/s1_0pretrain_LN/checkpoints/checkpoint_1000000"
agents.append(FlexibleAgentInterface(RLPDAgent, ckpt, env_id))
generic_agent_type=RLPDAgent
elif method == "IQL":
generic_agent_type=IQLAgent
if args.model_path == '':
agents = get_best_IQL_agent(
[
f"IQL/log/{args.env}/iql_seed{seed}"
],
args.env,
env_id
)
if args.model_path != '':
# load specific model path
agents = []
agents.append(FlexibleAgentInterface(generic_agent_type, args.model_path, env_id))
if len(agents) == 0:
print("No agents found!")
exit(0)
# print(">>>",agents)
# tmp_check = []
# for agent in agents:
# s = agent.test()
# tmp_check.append(s)
# agent.close()
# print("\n".join(tmp_check))
# print("total agents:",len(agents)) # current version do not support multiple agents at same time
assert len(agents) == 1 # must equals to one
horizon = ENVIRONMENT_HORIZON
similarity_metrics(args.env, env_id, testing_dataset, eval_seqlen, eval_episodes, f"total_human_sim_report_{args.env}.csv", agents, render=args.render, horizon=horizon)