-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddppo_agents.py
206 lines (176 loc) · 7.01 KB
/
ddppo_agents.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import os
import random
from typing import Dict, Optional
import numba
import numpy as np
import torch
from gym.spaces import Box
from gym.spaces import Dict as SpaceDict
from gym.spaces import Discrete
import habitat
from corruptions.parser import get_corruptions_parser, apply_corruptions_to_config, get_runid_and_logfolder
from habitat import get_config
from habitat.config import Config
from habitat.core.agent import Agent
from habitat.core.simulator import Observations
from habitat_baselines.common.baseline_registry import baseline_registry
from habitat_baselines.common.obs_transformers import (
apply_obs_transforms_batch,
apply_obs_transforms_obs_space,
get_active_obs_transforms,
)
from habitat_baselines.config.default import get_config as get_baseline_config
from habitat_baselines.utils.common import batch_obs
from habitat_extensions.sensors.noise_models import GaussianNoiseModelTorch
from my_benchmark import MyChallenge
@numba.njit
def _seed_numba(seed: int):
random.seed(seed)
np.random.seed(seed)
class PPOAgent(Agent):
def __init__(self, config: Config) -> None:
image_size = config.RL.POLICY.OBS_TRANSFORMS.CENTER_CROPPER
if "ObjectNav" in config.TASK_CONFIG.TASK.TYPE:
OBJECT_CATEGORIES_NUM = 20
spaces = {
"objectgoal": Box(
low=0, high=OBJECT_CATEGORIES_NUM, shape=(1,), dtype=np.int64
),
"compass": Box(low=-np.pi, high=np.pi, shape=(1,), dtype=np.float32),
"gps": Box(
low=np.finfo(np.float32).min,
high=np.finfo(np.float32).max,
shape=(2,),
dtype=np.float32,
),
}
else:
spaces = {
"pointgoal": Box(
low=np.finfo(np.float32).min,
high=np.finfo(np.float32).max,
shape=(2,),
dtype=np.float32,
)
}
if config.INPUT_TYPE in ["depth", "rgbd"]:
spaces["depth"] = Box(
low=0,
high=1,
shape=(image_size.HEIGHT, image_size.WIDTH, 1),
dtype=np.float32,
)
if config.INPUT_TYPE in ["rgb", "rgbd"]:
spaces["rgb"] = Box(
low=0,
high=255,
shape=(image_size.HEIGHT, image_size.WIDTH, 3),
dtype=np.uint8,
)
observation_spaces = SpaceDict(spaces)
action_spaces = (
Discrete(6) if "ObjectNav" in config.TASK_CONFIG.TASK.TYPE else Discrete(4)
)
self.obs_transforms = get_active_obs_transforms(config)
observation_spaces = apply_obs_transforms_obs_space(
observation_spaces, self.obs_transforms
)
self.device = (
torch.device("cuda:{}".format(config.PTH_GPU_ID))
if torch.cuda.is_available()
else torch.device("cpu")
)
self.hidden_size = config.RL.PPO.hidden_size
random.seed(config.RANDOM_SEED)
np.random.seed(config.RANDOM_SEED)
_seed_numba(config.RANDOM_SEED)
torch.random.manual_seed(config.RANDOM_SEED)
if torch.cuda.is_available():
torch.backends.cudnn.deterministic = True # type: ignore
policy = baseline_registry.get_policy(config.RL.POLICY.name)
self.actor_critic = policy.from_config(
config, observation_spaces, action_spaces
)
self.actor_critic.to(self.device)
if config.MODEL_PATH:
ckpt = torch.load(config.MODEL_PATH, map_location=self.device)
# Filter only actor_critic weights
self.actor_critic.load_state_dict(
{
k[len("actor_critic."):]: v
for k, v in ckpt["state_dict"].items()
if "actor_critic" in k
}
)
else:
habitat.logger.error(
"Model checkpoint wasn't loaded, evaluating " "a random model."
)
self.test_recurrent_hidden_states: Optional[torch.Tensor] = None
self.not_done_masks: Optional[torch.Tensor] = None
self.prev_actions: Optional[torch.Tensor] = None
def reset(self) -> None:
self.test_recurrent_hidden_states = torch.zeros(
1,
self.actor_critic.net.num_recurrent_layers,
self.hidden_size,
device=self.device,
)
self.not_done_masks = torch.zeros(1, 1, device=self.device, dtype=torch.bool)
self.prev_actions = torch.zeros(1, 1, dtype=torch.long, device=self.device)
def act(self, observations: Observations) -> Dict[str, int]:
batch = batch_obs([observations], device=self.device)
batch = apply_obs_transforms_batch(batch, self.obs_transforms)
with torch.no_grad():
(_, actions, _, self.test_recurrent_hidden_states) = self.actor_critic.act(
batch,
self.test_recurrent_hidden_states,
self.prev_actions,
self.not_done_masks,
deterministic=False,
)
# Make masks not done till reset (end of episode) will be called
self.not_done_masks.fill_(True)
self.prev_actions.copy_(actions) # type: ignore
return {"action": actions[0][0].item()}
def main():
_ = GaussianNoiseModelTorch()
parser = get_corruptions_parser()
parser.add_argument(
"--input-type", default="blind", choices=["blind", "rgb", "depth", "rgbd"]
)
parser.add_argument(
"--evaluation", type=str, required=True, choices=["local", "remote"]
)
parser.add_argument("--model-path", default="", type=str)
args = parser.parse_args()
print(args)
if args.challenge_config_file:
config_paths = args.challenge_config_file
else:
config_paths = os.environ["CHALLENGE_CONFIG_FILE"]
task_config = get_config(config_paths)
apply_corruptions_to_config(args, task_config)
args.run_id, args.log_folder = get_runid_and_logfolder(args, task_config)
ddppo_config = get_baseline_config(
"config_files/ddppo/ddppo_pointnav_ORIGINAL.yaml", ["BASE_TASK_CONFIG_PATH", config_paths]
).clone()
ddppo_config.defrost()
ddppo_config.TASK_CONFIG = task_config
ddppo_config.PTH_GPU_ID = 0
ddppo_config.INPUT_TYPE = args.input_type
ddppo_config.MODEL_PATH = args.model_path
ddppo_config.RANDOM_SEED = task_config.RANDOM_SEED
ddppo_config.freeze()
agent = PPOAgent(ddppo_config)
if args.evaluation == "local":
challenge = MyChallenge(task_config, eval_remote=False, **args.__dict__)
else:
challenge = habitat.Challenge(eval_remote=True)
challenge.submit(agent, args.num_episodes)
if __name__ == "__main__":
main()