-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluation_factory.py
399 lines (370 loc) · 18.1 KB
/
evaluation_factory.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
394
395
396
397
398
399
import numpy as np
import sys
import termios
import time
import torch
from util.xbox import XboxController
from util.keyboard import Keyboard
from util.colors import OKGREEN, FAIL, WARNING, ENDC
from util.reward_plotter import Plotter
def simple_eval(actor, env, episode_length_max=300):
"""Simply evaluating policy in visualization window and no user input
Args:
actor: Actor loaded outside this function. If Actor is None, this function will evaluate
noisy actions without any policy.
env: Environment instance for actor
episode_length_max (int, optional): Max length of episode for evaluation. Defaults to 500.
"""
with torch.no_grad():
state = env.reset()
done = False
episode_length = 0
episode_reward = []
if hasattr(actor, 'init_hidden_state'):
actor.init_hidden_state()
env.sim.viewer_init(fps = env.default_policy_rate)
render_state = env.sim.viewer_render()
while render_state:
start_time = time.time()
if not env.sim.viewer_paused():
state = torch.Tensor(state).float()
if actor is None:
action = np.random.uniform(-0.2, 0.2, env.action_size)
else:
action = actor(state).numpy()
state, reward, done, _ = env.step(action)
episode_length += 1
episode_reward.append(reward)
env.viewer_update_cop_marker()
render_state = env.sim.viewer_render()
delaytime = max(0, env.default_policy_rate/2000 - (time.time() - start_time))
time.sleep(delaytime)
if episode_length == episode_length_max or done:
print(f"Episode length = {episode_length}, Average reward is {np.mean(episode_reward)}.")
done = False
state = env.reset()
episode_length = 0
if hasattr(actor, 'init_hidden_state'):
actor.init_hidden_state()
# Seems like Mujoco only allows a single mjContext(), and it prefers one context
# with one window when modifying mjModel. So for onscreen dual window, we re-init
# the non-main window, ie egocentric view here.
if hasattr(env.sim, 'renderer'):
if env.sim.renderer is not None:
print("re-init non-primary screen renderer")
env.sim.renderer.close()
env.sim.init_renderer(offscreen=env.offscreen,
width=env.depth_image_dim[0], height=env.depth_image_dim[1])
def interactive_eval(actor, env, episode_length_max=300, critic=None, plot_rewards=False):
"""Simply evaluating policy in visualization window with user input
Args:
actor: Actor loaded outside this function. If Actor is None, this function will evaluate
noisy actions without any policy.
env: Environment instance for actor
episode_length_max (int, optional): Max length of episode for evaluation. Defaults to 500.
"""
if actor is None:
raise RuntimeError(F"{FAIL}Interactive eval requires a non-null actor network for eval")
print(f"{OKGREEN}Feeding keyboard inputs to policy for interactive eval mode.")
print(f"Type commands into the terminal window to avoid interacting with the mujoco viewer keybinds.{ENDC}")
keyboard = Keyboard()
if plot_rewards:
plotter = Plotter()
with torch.no_grad():
state = env.reset(interactive_evaluation=True)
done = False
episode_length = 0
episode_reward = []
if hasattr(actor, 'init_hidden_state'):
actor.init_hidden_state()
if hasattr(critic, 'init_hidden_state'):
critic.init_hidden_state()
env.sim.viewer_init(fps = env.default_policy_rate)
render_state = env.sim.viewer_render()
env.display_controls_menu()
env.display_control_commands()
while render_state:
start_time = time.time()
cmd = None
if keyboard.data():
cmd = keyboard.get_input()
if not env.sim.viewer_paused():
state = torch.Tensor(state).float()
action = actor(state).numpy()
state, reward, done, infos = env.step(action)
episode_length += 1
episode_reward.append(reward)
if plot_rewards:
plotter.add_data(infos, done or cmd == "quit")
if critic is not None:
if hasattr(env, 'get_privilege_state'):
critic_state = env.get_privilege_state()
else:
critic_state = state
# print(f"Critic value = {critic(torch.Tensor(critic_state)).numpy() if critic is not None else 'N/A'}")
env.viewer_update_cop_marker()
if cmd is not None:
env.interactive_control(cmd)
if cmd == "r":
done = True
if cmd == "menu":
env.display_control_commands(erase=True)
env.display_controls_menu()
env.display_control_commands()
render_state = env.sim.viewer_render()
delaytime = max(0, 1/env.default_policy_rate - (time.time() - start_time))
time.sleep(delaytime)
if done:
state = env.reset(interactive_evaluation=True)
env.display_control_commands(erase=True)
print(f"Episode length = {episode_length}, Average reward is {np.mean(episode_reward) if episode_reward else 0}.")
env.display_control_commands()
episode_length = 0
episode_reward = []
if hasattr(actor, 'init_hidden_state'):
actor.init_hidden_state()
done = False
keyboard.restore()
# clear terminal on ctrl+q
print(f"\033[{len(env.control_commands_dict) + 3 - 1}B\033[K")
termios.tcdrain(sys.stdout)
time.sleep(0.1)
termios.tcflush(sys.stdout, termios.TCIOFLUSH)
def interactive_xbox_eval(actor, env, episode_length_max=300, critic=None, plot_rewards=False):
"""Simply evaluating policy in visualization window with user input
Args:
actor: Actor loaded outside this function. If Actor is None, this function will evaluate
noisy actions without any policy.
env: Environment instance for actor
episode_length_max (int, optional): Max length of episode for evaluation. Defaults to 500.
"""
if actor is None:
raise RuntimeError(F"{FAIL}Interactive eval requires a non-null actor network for eval")
print(f"{OKGREEN}Feeding xbox inputs to policy for interactive eval mode.{ENDC}")
xbox = XboxController()
if plot_rewards:
plotter = Plotter()
with torch.no_grad():
state = env.reset(interactive_evaluation=True)
done = False
episode_length = 0
episode_reward = []
if hasattr(actor, 'init_hidden_state'):
actor.init_hidden_state()
if hasattr(critic, 'init_hidden_state'):
critic.init_hidden_state()
env.sim.viewer_init(fps = env.default_policy_rate)
render_state = env.sim.viewer_render()
env.display_xbox_controls_menu()
env.display_control_commands()
while render_state:
start_time = time.time()
if not env.sim.viewer_paused():
state = torch.Tensor(state).float()
action = actor(state).numpy()
state, reward, done, infos = env.step(action)
episode_length += 1
episode_reward.append(reward)
if plot_rewards:
plotter.add_data(infos, done)
if critic is not None:
if hasattr(env, 'get_privilege_state'):
critic_state = env.get_privilege_state()
else:
critic_state = state
# print(f"Critic value = {critic(torch.Tensor(critic_state)).numpy() if critic is not None else 'N/A'}")
env.viewer_update_cop_marker()
env.interactive_xbox_control(xbox)
if xbox.RightBumper == 1 and xbox.LeftBumper == 0:
if xbox.Start == 1 and not xbox.Start_pressed:
break
elif xbox.Start_pressed and xbox.Start == 0:
xbox.Start_pressed = False
if xbox.Back == 1 and not xbox.Back_pressed:
xbox.Back_pressed = True
state = env.reset(interactive_evaluation=True)
env.display_control_commands(erase=True)
print(f"Episode length = {episode_length}, Average reward is {np.mean(episode_reward) if episode_reward else 0}.")
env.display_control_commands()
episode_length = 0
episode_reward = []
if hasattr(actor, 'init_hidden_state'):
actor.init_hidden_state()
done = False
elif xbox.Back_pressed and xbox.Back == 0:
xbox.Back_pressed = False
if xbox.RightBumper == 0 and xbox.LeftBumper == 0:
if xbox.Start == 1 and not xbox.Start_pressed:
xbox.Start_pressed = True
env.sim.viewer.paused = not env.sim.viewer.paused
elif xbox.Start_pressed and xbox.Start == 0:
xbox.Start_pressed = False
if xbox.RightBumper == 1 and xbox.LeftBumper == 1:
if xbox.Start == 1 and not xbox.Start_pressed:
xbox.Start_pressed = True
env.display_control_commands(erase=True)
env.display_xbox_controls_menu()
env.display_control_commands()
elif xbox.Start_pressed and xbox.Start == 0:
xbox.Start_pressed = False
render_state = env.sim.viewer_render()
delaytime = max(0, 1/env.default_policy_rate - (time.time() - start_time))
time.sleep(delaytime)
if done:
state = env.reset(interactive_evaluation=True)
env.display_control_commands(erase=True)
print(f"Episode length = {episode_length}, Average reward is {np.mean(episode_reward) if episode_reward else 0}.")
env.display_control_commands()
episode_length = 0
episode_reward = []
if hasattr(actor, 'init_hidden_state'):
actor.init_hidden_state()
done = False
# clear terminal on ctrl+q
print(f"\033[{len(env.control_commands_dict) + 3 - 1}B\033[K")
termios.tcdrain(sys.stdout)
time.sleep(0.1)
termios.tcflush(sys.stdout, termios.TCIOFLUSH)
def slowmo_interactive_eval(actor, env, episode_length_max=300, slowmo=4, critic=None):
"""Simply evaluating policy in visualization window with user input
Args:
actor: Actor loaded outside this function. If Actor is None, this function will evaluate
noisy actions without any policy.
env: Environment instance for actor
episode_length_max (int, optional): Max length of episode for evaluation. Defaults to 500.
"""
if actor is None:
raise RuntimeError(F"{FAIL}Interactive eval requires a non-null actor network for eval")
hw_step_counter = int(env.sim.simulator_rate / env.default_policy_rate)
if slowmo > hw_step_counter:
print(f'{WARNING}The slowmo factor provided exceeds the maximum possible. Rounding down to the max possible: {hw_step_counter}\n')
elif hw_step_counter % slowmo != 0:
while hw_step_counter % slowmo != 0:
slowmo += 1
print(f'{WARNING}The slowmo factor provided was uneven to the fps value. It has been rounded up to: {slowmo}\n')
steps_per_save = int(hw_step_counter / slowmo)
print(f"{OKGREEN}Feeding keyboard inputs to policy for interactive eval mode.")
print("Type commands into the terminal window to avoid interacting with the mujoco viewer keybinds." + '\033[0m')
keyboard = Keyboard()
with torch.no_grad():
state = env.reset(interactive_evaluation=True)
done = False
episode_length = 0
episode_reward = []
if hasattr(actor, 'init_hidden_state'):
actor.init_hidden_state()
if hasattr(critic, 'init_hidden_state'):
critic.init_hidden_state()
env.sim.viewer_init()
render_state = env.sim.viewer_render()
env.display_controls_menu()
env.display_control_commands()
pol_counter = 0
render_counter = 0
state = torch.Tensor(env.get_state()).float()
action = actor(state).numpy()
while render_state:
start_time = time.time()
cmd = None
if keyboard.data():
cmd = keyboard.get_input()
if cmd is not None:
env.interactive_control(cmd)
if cmd == "menu":
env.display_control_commands(erase=True)
env.display_controls_menu()
env.display_control_commands()
if cmd == "quit":
done = True
if not env.sim.viewer_paused():
# If we've taken enough 2kHz steps for a hw_step
if pol_counter == hw_step_counter:
pol_counter = 0
# Check done here to ensure the keyboard command does not get overridden
if not done:
state = torch.Tensor(env.get_state()).float()
action = actor(state).numpy()
done = env.compute_done()
env.compute_reward(action)
reward = env.reward
episode_length += 1
episode_reward.append(reward)
env.hw_step()
if critic is not None:
if hasattr(env, 'get_privilege_state'):
critic_state = env.get_privilege_state()
else:
critic_state = state
# print(f"Critic value = {critic(torch.Tensor(critic_state)).numpy() if critic is not None else 'N/A'}")
if done:
state = env.reset(interactive_evaluation=True)
env.display_control_commands(erase=True)
print(f"Episode length = {episode_length}, Average reward is {np.mean(episode_reward) if episode_reward else 0}.")
env.display_control_commands()
episode_length = 0
if hasattr(actor, 'init_hidden_state'):
actor.init_hidden_state()
done = False
# If we've taken enough 2kHz steps to call render, and a frame is saved here if we're currently recording
if render_counter == steps_per_save:
render_counter = 0
render_state = env.sim.viewer_render()
env.viewer_update_cop_marker()
delaytime = max(0, (steps_per_save * 0.0005) - (time.time() - start_time))
time.sleep(delaytime)
# Take a single 2kHz step and update the counter variables
env.step_simulation(action, 1)
render_counter += 1
pol_counter += 1
else:
# If the sim is reset while paused, ensures that's properly handled without unpausing the sim
if done:
state = env.reset(interactive_evaluation=True)
env.display_control_commands(erase=True)
print(f"Episode length = {episode_length}, Average reward is {np.mean(episode_reward) if episode_reward else 0}.")
env.display_control_commands()
episode_length = 0
if hasattr(actor, 'init_hidden_state'):
actor.init_hidden_state()
done = False
# Ensures we get a new action and the counter is reset
pol_counter = hw_step_counter
render_state = env.sim.viewer_render()
delaytime = max(0, 1/env.default_policy_rate - (time.time() - start_time))
time.sleep(delaytime)
keyboard.restore()
# clear terminal on ctrl+q
print(f"\033[{len(env.control_commands_dict) + 3 - 1}B\033[K")
termios.tcdrain(sys.stdout)
time.sleep(0.1)
termios.tcflush(sys.stdout, termios.TCIOFLUSH)
def simple_eval_offscreen(actor, env, episode_length_max=300):
"""Simply evaluating policy without visualization
Args:
actor: Actor loaded outside this function. If Actor is None, this function will evaluate
noisy actions without any policy.
env: Environment instance for actor
episode_length_max (int, optional): Max length of episode for evaluation. Defaults to 500.
"""
with torch.no_grad():
state = env.reset()
done = False
episode_length = 0
episode_reward = []
if hasattr(actor, 'init_hidden_state'):
actor.init_hidden_state()
while True:
state = torch.Tensor(state).float()
if actor is None:
action = np.random.uniform(-0.2, 0.2, env.action_size)
else:
action = actor(state).numpy()
state, reward, done, _ = env.step(action)
episode_length += 1
episode_reward.append(reward)
if episode_length == episode_length_max or done:
print(f"Episode length = {episode_length}, Average reward is {np.mean(episode_reward)}.")
state = env.reset()
episode_length = 0
if hasattr(actor, 'init_hidden_state'):
actor.init_hidden_state()