forked from goncamateus/Planning-the-path-with-rl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jit_models.py
53 lines (45 loc) · 1.39 KB
/
jit_models.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
import os
import envs
import gymnasium as gym
import numpy as np
import torch
from methods.sac import GaussianPolicy
from utils.experiment import strtobool
def load_model(model_path):
model = torch.load(model_path, map_location="cpu")
return model
def jit_model(env_id, caps):
env = gym.make(env_id, render_mode="human")
path = env_id + "-caps" if caps else env_id
state_dict = load_model(f"trained_models/{path}/actor.pt")
num_inputs = np.array(env.observation_space.shape).prod()
num_actions = np.array(env.action_space.shape).prod()
actor = GaussianPolicy(
num_inputs,
num_actions,
log_sig_min=-5,
log_sig_max=2,
hidden_dim=256,
epsilon=1e-6,
action_space=env.action_space,
)
print(f"Action scale: {actor.action_scale}")
print(f"Action bias: {actor.action_bias}")
actor.load_state_dict(state_dict)
actor.eval()
actor.to("cpu")
obs, _ = env.reset()
traced_script_module = torch.jit.trace(actor, torch.Tensor(obs.reshape(1, -1)))
traced_script_module.save(f"trained_models/{path}/actor_jit.pt")
if __name__ == "__main__":
ids = [
"Baseline-v0",
"Baseline-v1",
"Enhanced-v0",
"Enhanced-v1",
"Obstacle-v0",
"Obstacle-v1",
]
for gym_id in ids:
jit_model(env_id=gym_id, caps=False)
jit_model(env_id=gym_id, caps=True)