-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrendering.py
68 lines (52 loc) · 2.11 KB
/
rendering.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
from EnvironmentWrapper import CustomEnvWrapper
import tqdm
from stable_baselines3 import SAC
import imageio
import os
experiment_name = "Sharpening_AO_system_easy"
eval_episodes = 10
eval_steps = 100
# evaluate no agent
env = CustomEnvWrapper(name=experiment_name)
average_reward = []
obs = env.reset()
for episode in tqdm.tqdm(range(eval_episodes)):
rewards = []
obs = env.reset()
for step in range(eval_steps):
action = 0
obs, reward, done, info = env.step(action)
rewards.append(reward)
env.render(episode=episode, iteration = step, tot_rewards = average_reward, loc='no_agent')
# keep track of rewards
average_reward.append(sum(rewards)/len(rewards))
# evaluate agent
model_name = 'SAC-1.7rms-21act-100000buf-2'
model = SAC.load(f"models/{model_name}")
average_reward = []
for episode in tqdm.tqdm(range(eval_episodes)):
rewards = []
obs = env.reset()
for step in range(eval_steps):
action, _states = model.predict(obs, deterministic=True)
obs, reward, done, info = env.step(action)
rewards.append(reward)
env.render(episode=episode, iteration = step, tot_rewards = average_reward, loc=f'{model_name}')
# keep track of rewards
average_reward.append(sum(rewards)/len(rewards))
# make gifs
images = []
filenames = os.listdir('figures/animations/no_agent')
filenames.sort(key=lambda x: (int(x.split('_')[0]), int(x.split('_')[1].split('.')[0])))
for filename in filenames:
if filename.startswith('3') or filename.startswith('4'):
images.append(imageio.imread('figures/animations/no_agent/' + filename))
imageio.mimsave('figures/animations/no_agent.gif', images, duration=1000/5)
# make a gif for the agent
images = []
filenames = os.listdir(f'figures/animations/{model_name}')
filenames.sort(key=lambda x: (int(x.split('_')[0]), int(x.split('_')[1].split('.')[0])))
for filename in filenames:
if filename.startswith('3') or filename.startswith('5'):
images.append(imageio.imread(f'figures/animations/{model_name}/' + filename))
imageio.mimsave(f'figures/animations/{model_name}.gif', images, duration=1000/5)