-
Notifications
You must be signed in to change notification settings - Fork 2
/
miniworld_gotoobj_train.py
189 lines (151 loc) · 6.21 KB
/
miniworld_gotoobj_train.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
import argparse
from datetime import datetime
from pdb import set_trace
from time import time
import gymnasium as gym
import miniworld
import numpy as np
import torch as th
import torch.nn as nn
from gymnasium.core import ObservationWrapper
from gymnasium.spaces import Box, Dict
from stable_baselines3 import PPO
from stable_baselines3.common.callbacks import CheckpointCallback
from stable_baselines3.common.torch_layers import BaseFeaturesExtractor
from miniworld_gotoobj_env import MiniworldGoToObjEnv
class GoToObjObsWrapper(ObservationWrapper):
def __init__(self, env):
"""A wrapper that makes image the only observation.
Args:
env: The environment to apply the wrapper
"""
super().__init__(env)
self.observation_space = Dict(
{
"image": env.observation_space,
"mission": Box(low=0.0, high=1.0, shape=(9,), dtype=np.float32),
}
)
self.color_one_hot_dict = {
"red": np.array([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
"green": np.array([0.0, 1.0, 0.0, 0.0, 0.0, 0.0]),
"blue": np.array([0.0, 0.0, 1.0, 0.0, 0.0, 0.0]),
"purple": np.array([0.0, 0.0, 0.0, 1.0, 0.0, 0.0]),
"yellow": np.array([0.0, 0.0, 0.0, 0.0, 1.0, 0.0]),
"grey": np.array([0.0, 0.0, 0.0, 0.0, 0.0, 1.0]),
}
self.obj_one_hot_dict = {
"ball": np.array([1.0, 0.0, 0.0]),
"box": np.array([0.0, 1.0, 0.0]),
"key": np.array([0.0, 0.0, 1.0]),
}
def observation(self, obs):
mission_array = np.concatenate(
[
self.color_one_hot_dict[self.target_color],
self.obj_one_hot_dict[self.target_obj_name],
]
)
wrapped_obs = {
"image": obs,
"mission": mission_array,
}
return wrapped_obs
class GoToObjEnvExtractor(BaseFeaturesExtractor):
def __init__(self, observation_space: Dict):
# We do not know features-dim here before going over all the items,
# so put something dummy for now. PyTorch requires calling
# nn.Module.__init__ before adding modules
super().__init__(observation_space, features_dim=1)
extractors = {}
total_concat_size = 0
# We need to know size of the output of this extractor,
# so go over all the spaces and compute output feature sizes
for key, subspace in observation_space.spaces.items():
if key == "image":
# We will just downsample one channel of the image by 4x4 and flatten.
# Assume the image is single-channel (subspace.shape[0] == 0)
cnn = nn.Sequential(
nn.Conv2d(3, 32, kernel_size=8, stride=4, padding=0),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=4, stride=2, padding=0),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=0),
nn.ReLU(),
nn.Flatten(),
)
# Compute shape by doing one forward pass
with th.no_grad():
n_flatten = cnn(
th.as_tensor(subspace.sample()[None]).float()
).shape[1]
linear = nn.Sequential(nn.Linear(n_flatten, 64), nn.ReLU())
extractors["image"] = nn.Sequential(*(list(cnn) + list(linear)))
total_concat_size += 64
elif key == "mission":
extractors["mission"] = nn.Linear(subspace.shape[0], 32)
total_concat_size += 32
self.extractors = nn.ModuleDict(extractors)
# Update the features dim manually
self._features_dim = total_concat_size
def forward(self, observations) -> th.Tensor:
encoded_tensor_list = []
# self.extractors contain nn.Modules that do all the processing.
for key, extractor in self.extractors.items():
encoded_tensor_list.append(extractor(observations[key]))
# Return a (B, self._features_dim) PyTorch tensor, where B is batch dimension.
return th.cat(encoded_tensor_list, dim=1)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--train", action="store_true", help="train the model")
parser.add_argument("--load_model", default="minigrid_door_20230418-174509")
parser.add_argument("--render", action="store_true", help="render trained models")
args = parser.parse_args()
policy_kwargs = dict(features_extractor_class=GoToObjEnvExtractor)
# Create time stamp of experiment
stamp = datetime.fromtimestamp(time()).strftime("%Y%m%d-%H%M%S")
if args.train:
env = MiniworldGoToObjEnv()
env = GoToObjObsWrapper(env)
checkpoint_callback = CheckpointCallback(
save_freq=1e5,
save_path=f"./models/ppo/miniworld_gotoobj_{stamp}/",
name_prefix="iter",
)
model = PPO(
"MultiInputPolicy",
env,
policy_kwargs=policy_kwargs,
verbose=1,
tensorboard_log="./logs/ppo/miniworld_gotoobj_tensorboard/",
)
model.learn(
2e5,
tb_log_name=f"{stamp}",
callback=checkpoint_callback,
)
else:
if args.render:
env = MiniworldGoToObjEnv(render_mode="human")
else:
env = MiniworldGoToObjEnv()
env = GoToObjObsWrapper(env)
ppo = PPO("MultiInputPolicy", env, policy_kwargs=policy_kwargs, verbose=1)
# add the experiment time stamp
ppo = ppo.load(f"models/ppo/{args.load_model}", env=env)
obs, info = env.reset()
rewards = 0
for i in range(2000):
action, _state = ppo.predict(obs, deterministic=True)
obs, reward, terminated, truncated, info = env.step(action)
rewards += reward
if terminated or truncated:
print(f"Test reward: {rewards}")
obs, info = env.reset()
rewards = 0
continue
print(f"Test reward: {rewards}")
env.close()
if __name__ == "__main__":
for i in range(10):
main()