-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_learned_policy.py
47 lines (32 loc) · 1.31 KB
/
test_learned_policy.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
import torch
import gymnasium as gym
from PIL import Image
from core.agent import *
import hydra
from core.utils import pixel_replication
# TEST LEARNED POLICIES AND CREATE GIF OF ONE EPISODE
@hydra.main(config_path="config", config_name="transfer_config.yaml", version_base=None)
def main(args):
model_path = args.model_path
env = gym.make('core:MazEnv-v0', goal_mode=0)
model = Agent(env=env, args=args)
model.target_net.load_state_dict(torch.load(model_path))
model.policy_net.load_state_dict(torch.load(model_path))
state, _ = env.reset()
images = []
for i in range(100):
state = state.transpose((2, 0, 1))
action = model.target_net(torch.tensor(state, device=model.device))[0].argmax().item()
print(model.target_net(torch.tensor(state, device=model.device))[0])
image = Image.fromarray(pixel_replication(state.transpose((1, 2, 0)), 24), "RGB")
images.append(image)
print(action)
next_state, reward, terminated, truncated, _ = env.step(action)
done = terminated or truncated
if done:
break
state = next_state
images[0].save('out1.gif',
save_all=True, append_images=images[1:], optimize=False, duration=40, loop=0)
if __name__ == "__main__":
main()