forked from OpenGHz/Imitate-All
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy_evaluate.py
393 lines (356 loc) · 13.3 KB
/
policy_evaluate.py
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import torch
import numpy as np
import os, time, logging, pickle, inspect
from typing import Dict
from tqdm import tqdm
from utils import set_seed, save_eval_results
from task_configs.config_tools.basic_configer import basic_parser, get_all_config
from policies.common.maker import make_policy
from envs.common_env import get_image, CommonEnv
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def main(args):
all_config = get_all_config(args, "eval")
set_seed(all_config["seed"])
ckpt_names = all_config["ckpt_names"]
# make environment
env_config = all_config["environments"]
env_maker = env_config.pop("environment_maker")
env = env_maker(all_config) # use all_config for more flexibility
assert env is not None, "Environment is not created..."
results = []
# multiple ckpt evaluation
for ckpt_name in ckpt_names:
success_rate, avg_return = eval_bc(all_config, ckpt_name, env)
results.append([ckpt_name, success_rate, avg_return])
for ckpt_name, success_rate, avg_return in results:
logger.info(f"{ckpt_name}: {success_rate=} {avg_return=}")
print()
def get_ckpt_path(ckpt_dir, ckpt_name, stats_path):
ckpt_path = os.path.join(ckpt_dir, ckpt_name)
raw_ckpt_path = ckpt_path
if not os.path.exists(ckpt_path):
ckpt_dir = os.path.dirname(ckpt_dir) # check the upper dir
ckpt_path = os.path.join(ckpt_dir, ckpt_name)
logger.warning(f"Warning: not found ckpt_path: {raw_ckpt_path}, try {ckpt_path}...")
if not os.path.exists(ckpt_path):
ckpt_dir = os.path.dirname(stats_path)
ckpt_path = os.path.join(ckpt_dir, ckpt_name)
logger.warning(f"Warning: also not found ckpt_path: {ckpt_path}, try {ckpt_path}...")
return ckpt_path
def eval_bc(config, ckpt_name, env: CommonEnv):
# TODO: eval only contains the logic, data flow and visualization
# remove other not general processing code outside in policy and env maker
# 显式获得配置
ckpt_dir = config["ckpt_dir"]
stats_path = config["stats_path"]
save_dir = config["save_dir"]
max_timesteps = config["max_timesteps"]
camera_names = config["camera_names"]
max_rollouts = config["num_rollouts"]
policy_config: dict = config["policy_config"]
state_dim = policy_config["state_dim"]
action_dim = policy_config["action_dim"]
temporal_agg = policy_config["temporal_agg"]
num_queries = policy_config["num_queries"] # i.e. chunk_size
dt = 1 / config["fps"]
image_mode = config.get("image_mode", 0)
arm_velocity = config.get("arm_velocity", 6)
save_all = config.get("save_all", False)
save_time_actions = config.get("save_time_actions", False)
filter_type = config.get("filter", None)
ensemble: dict = config.get("ensemble", None)
save_dir = save_dir if save_dir != "AUTO" else ckpt_dir
result_prefix = "result_" + ckpt_name.split(".")[0]
# TODO: remove this
ckpt_path = get_ckpt_path(ckpt_dir, ckpt_name, stats_path)
policy_config["ckpt_path"] = ckpt_path
# make and configure policies
policies: Dict[str, list] = {}
if ensemble is None:
logger.info("policy_config:", policy_config)
# if ensemble is not None:
policy_config["max_timesteps"] = max_timesteps # TODO: remove this
policy = make_policy(policy_config, "eval")
policies["Group1"] = (policy,)
else:
logger.info("ensemble config:", ensemble)
ensembler = ensemble.pop("ensembler")
for gr_name, gr_cfgs in ensemble.items():
policies[gr_name] = []
for index, gr_cfg in enumerate(gr_cfgs):
policies[gr_name].append(
make_policy(
gr_cfg["policies"][index]["policy_class"],
)
)
# add action filter
# TODO: move to policy maker as wrappers
if filter_type is not None:
# init filter
from OneEuroFilter import OneEuroFilter
config = {
"freq": config["fps"], # Hz
"mincutoff": 0.01, # Hz
"beta": 0.05,
"dcutoff": 0.5, # Hz
}
filters = [OneEuroFilter(**config) for _ in range(action_dim)]
# init pre/post process functions
# TODO: move to policy maker as wrappers
use_stats = True
if use_stats:
with open(stats_path, "rb") as f:
stats = pickle.load(f)
pre_process = lambda s_qpos: (s_qpos - stats["qpos_mean"]) / stats["qpos_std"]
post_process = lambda a: a * stats["action_std"] + stats["action_mean"]
else:
pre_process = lambda s_qpos: s_qpos
post_process = lambda a: a
# evaluation loop
if hasattr(policy, "eval"): policy.eval()
env_max_reward = 0
episode_returns = []
highest_rewards = []
num_rollouts = 0
policy_sig = inspect.signature(policy).parameters
prediction_freq = 100000
for rollout_id in range(max_rollouts):
# evaluation loop
all_time_actions = torch.zeros(
[max_timesteps, max_timesteps + num_queries, action_dim]
).cuda()
qpos_history = torch.zeros((1, max_timesteps, state_dim)).cuda()
image_list = [] # for visualization
qpos_list = []
action_list = []
rewards = []
with torch.inference_mode():
logger.info("Reset environment...")
env.reset(sleep_time=1)
logger.info(f"Current rollout: {rollout_id} for {ckpt_name}.")
v = input(f"Press Enter to start evaluation or z and Enter to exit...")
if v == "z":
break
ts = env.reset()
if hasattr(policy, "reset"): policy.reset()
try:
for t in tqdm(range(max_timesteps)):
start_time = time.time()
image_list.append(ts.observation["images"])
# pre-process current observations
curr_image = get_image(ts, camera_names, image_mode)
qpos_numpy = np.array(ts.observation["qpos"])
logger.debug(f"raw qpos: {qpos_numpy}")
qpos = pre_process(qpos_numpy) # normalize qpos
logger.debug(f"pre qpos: {qpos}")
qpos = torch.from_numpy(qpos).float().cuda().unsqueeze(0)
qpos_history[:, t] = qpos
logger.debug(f"observation time: {time.time() - start_time}")
start_time = time.time()
# wrap policy
target_t = t % num_queries
if temporal_agg or target_t == 0:
# (1, chunk_size, 7) for act
all_actions: torch.Tensor = policy(qpos, curr_image)
all_time_actions[[t], t : t + num_queries] = all_actions
index = 0 if temporal_agg else target_t
raw_action = all_actions[:, index]
# post-process predicted action
# dim: (1,7) -> (7,)
raw_action = (
raw_action.squeeze(0).cpu().numpy()
)
logger.debug(f"raw action: {raw_action}")
action = post_process(raw_action) # de-normalize action
logger.debug(f"post action: {action}")
if filter_type is not None: # filt action
for i, filter in enumerate(filters):
action[i] = filter(action[i], time.time())
time.sleep(max(0, 1/prediction_freq - (time.time() - start_time)))
logger.debug(f"prediction time: {time.time() - start_time}")
# step the environment
sleep_time = dt
# sleep_time = max(0, dt - (time.time() - start_time))
ts = env.step(action, sleep_time=sleep_time, arm_vel=arm_velocity)
# for visualization
qpos_list.append(qpos_numpy)
action_list.append(action)
rewards.append(ts.reward)
# debug
# input(f"Press Enter to continue...")
# break
except KeyboardInterrupt:
logger.info(f"Current roll out: {rollout_id} interrupted by CTRL+C...")
continue
else:
num_rollouts += 1
rewards = np.array(rewards)
episode_return = np.sum(rewards[rewards != None])
episode_returns.append(episode_return)
episode_highest_reward = np.max(rewards)
highest_rewards.append(episode_highest_reward)
logger.info(
f"Rollout {rollout_id}\n{episode_return=}, {episode_highest_reward=}, {env_max_reward=}, Success: {episode_highest_reward==env_max_reward}"
)
# saving evaluation results
if save_dir != "":
dataset_name = f"{result_prefix}_{rollout_id}"
save_eval_results(
save_dir,
dataset_name,
rollout_id,
image_list,
qpos_list,
action_list,
camera_names,
dt,
all_time_actions,
save_all=save_all,
save_time_actions=save_time_actions,
)
if num_rollouts > 0:
success_rate = np.mean(np.array(highest_rewards) == env_max_reward)
avg_return = np.mean(episode_returns)
summary_str = f"\nSuccess rate: {success_rate}\nAverage return: {avg_return}\n\n"
for r in range(env_max_reward + 1):
more_or_equal_r = (np.array(highest_rewards) >= r).sum()
more_or_equal_r_rate = more_or_equal_r / num_rollouts
summary_str += f"Reward >= {r}: {more_or_equal_r}/{num_rollouts} = {more_or_equal_r_rate*100}%\n"
logger.info(summary_str)
# save success rate to txt
if save_dir != "":
with open(os.path.join(save_dir, dataset_name + ".txt"), "w") as f:
f.write(summary_str)
f.write(repr(episode_returns))
f.write("\n\n")
f.write(repr(highest_rewards))
logger.info(
f'Success rate and average return saved to {os.path.join(save_dir, dataset_name + ".txt")}'
)
else:
success_rate = 0
avg_return = 0
return success_rate, avg_return
if __name__ == "__main__":
parser = basic_parser()
# change roll out num
parser.add_argument(
"-nr",
"--num_rollouts",
action="store",
type=int,
help="Maximum number of evaluation rollouts",
required=False,
)
# change max time steps
parser.add_argument(
"-mts",
"--max_timesteps",
action="store",
type=int,
help="max_timesteps",
required=False,
)
# robot config #TODO: move to robot config
parser.add_argument(
"-can",
"--can_buses",
action="store",
nargs="+",
type=str,
help="can_bus",
default=("can0", "can1"),
required=False,
)
parser.add_argument(
"-rn",
"--robot_name",
action="store",
type=str,
help="robot_name",
required=False,
)
parser.add_argument(
"-em",
"--eef_mode",
action="store",
nargs="+",
type=str,
help="eef_mode",
default=("gripper", "gripper"),
)
parser.add_argument(
"-bat",
"--bigarm_type",
action="store",
nargs="+",
type=str,
help="bigarm_type",
default=("OD", "OD"),
)
parser.add_argument(
"-fat",
"--forearm_type",
action="store",
nargs="+",
type=str,
help="forearm_type",
default=("DM", "DM"),
)
parser.add_argument(
"-ci",
"--camera_indices",
action="store",
nargs="+",
type=str,
help="camera_indices",
default=("0",),
)
parser.add_argument(
"-av",
"--arm_velocity",
action="store",
type=float,
help="arm_velocity",
required=False,
)
# habitat TODO: remove this
parser.add_argument(
"-res",
"--habitat",
action="store",
type=str,
help="habitat",
required=False,
)
# check_images
parser.add_argument("-cki", "--check_images", action="store_true")
# set time_stamp
parser.add_argument(
"-ts",
"--time_stamp",
action="store",
type=str,
help="time_stamp",
required=False,
)
# save
parser.add_argument(
"-sd", "--save_dir", action="store", type=str, help="save_dir", required=False
)
parser.add_argument("-sa", "--save_all", action="store_true", help="save_all")
parser.add_argument(
"-sta", "--save_time_actions", action="store_true", help="save_time_actions"
)
# action filter type TODO: move to post process; and will use obs filter?
parser.add_argument(
"-ft", "--filter", action="store", type=str, help="filter_type", required=False
)
args = parser.parse_args()
args_dict = vars(args)
# TODO: put unknown key-value pairs into args_dict
# unknown = vars(unknown)
# args.update(unknown)
# print(unknown)
main(args_dict)