-
Notifications
You must be signed in to change notification settings - Fork 2
/
minigrid_gail_training_script.py
134 lines (102 loc) · 3.38 KB
/
minigrid_gail_training_script.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
import copy
import os
import gym
import gym_minigrid
from gym_minigrid import wrappers
import numpy as np
import pickle5 as pickle
import torch
from imitation.rewards.discrim_nets import ActObsMLP
from imitation.algorithms import adversarial
from imitation.data import rollout
from imitation.util import logger, util
from imitation.algorithms import bc
from stable_baselines3 import PPO
from stable_baselines3.common.policies import ActorCriticCnnPolicy, ActorCriticPolicy
from utils.env_utils import minigrid_get_env
from cnn_modules.cnn_discriminator import ActObsCNN
import argparse
import matplotlib.pyplot as plt
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--env",
"-e",
help="minigrid gym environment to train on",
default="MiniGrid-Empty-6x6-v0",
)
parser.add_argument("--run", "-r", help="Run name", default="sample_run")
parser.add_argument(
"--save-name", "-s", help="BC weights save name", default="saved_testing"
)
parser.add_argument("--traj-name", "-t", help="Run name", default="saved_testing")
parser.add_argument(
"--seed", type=int, help="random seed to generate the environment with", default=1
)
parser.add_argument(
"--nepochs", type=int, help="number of epochs to train till", default=50
)
parser.add_argument(
"--flat",
"-f",
default=False,
help="Partially Observable FlatObs or Fully Observable Image ",
action="store_true",
)
parser.add_argument(
"--vis-trained",
default=False,
help="Render 10 traj of trained BC",
action="store_true",
)
args = parser.parse_args()
save_path = "./logs/" + args.env + "/gail/" + args.run + "/"
os.makedirs(save_path, exist_ok=True)
traj_dataset_path = "./traj_datasets/" + args.traj_name + ".pkl"
print(f"Expert Dataset: {args.traj_name}")
with open(traj_dataset_path, "rb") as f:
trajectories = pickle.load(f)
transitions = rollout.flatten_trajectories(trajectories)
train_env = minigrid_get_env(args.env, 1, args.flat)
if args.flat:
discrim_type = ActObsMLP(
action_space=train_env.action_space,
observation_space=train_env.observation_space,
hid_sizes=(32, 32),
)
policy_type = ActorCriticPolicy
else:
discrim_type = ActObsCNN(
action_space=train_env.action_space,
observation_space=train_env.observation_space,
)
policy_type = ActorCriticCnnPolicy
base_ppo = PPO(policy_type, train_env, verbose=1, batch_size=64, n_steps=50)
logger.configure(save_path)
gail_trainer = adversarial.GAIL(
train_env,
expert_data=transitions,
expert_batch_size=64,
gen_algo=base_ppo,
n_disc_updates_per_round=1,
normalize_reward=False,
normalize_obs=False,
disc_opt_kwargs = {"lr":0.0001},
discrim_kwargs={"discrim_net": discrim_type},
)
total_timesteps = 100000
gail_trainer.train(total_timesteps=total_timesteps)
# gail_trainer.gen_algo.save("gens/gail_gen_"+str(i))
# with open('discrims/gail_discrim'+str(i)+'.pkl', 'wb') as handle:
# pickle.dump(gail_trainer.discrim, handle, protocol=pickle.HIGHEST_PROTOCOL)
if args.vis_trained:
for traj in range(10):
obs = train_env.reset()
train_env.render()
for i in range(20):
action, _ = gail_trainer.gen_algo.predict(obs, deterministic=True)
obs, reward, done, info = train_env.step(action)
train_env.render()
if done:
break
print("done")