-
Notifications
You must be signed in to change notification settings - Fork 327
/
roboset.py
365 lines (330 loc) · 16.3 KB
/
roboset.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import annotations
import importlib.util
import os.path
import shutil
import tempfile
from contextlib import nullcontext
from pathlib import Path
from typing import Callable
import torch
from tensordict import PersistentTensorDict, TensorDict
from torchrl._utils import (
KeyDependentDefaultDict,
logger as torchrl_logger,
print_directory_tree,
)
from torchrl.data.datasets.common import BaseDatasetExperienceReplay
from torchrl.data.datasets.utils import _get_root_dir
from torchrl.data.replay_buffers.samplers import Sampler
from torchrl.data.replay_buffers.storages import TensorStorage
from torchrl.data.replay_buffers.writers import ImmutableDatasetWriter, Writer
_has_tqdm = importlib.util.find_spec("tqdm", None) is not None
_has_h5py = importlib.util.find_spec("h5py", None) is not None
_has_hf_hub = importlib.util.find_spec("huggingface_hub", None) is not None
_NAME_MATCH = KeyDependentDefaultDict(lambda key: key)
_NAME_MATCH["observations"] = "observation"
_NAME_MATCH["rewards"] = "reward"
_NAME_MATCH["actions"] = "action"
_NAME_MATCH["env_infos"] = "info"
class RobosetExperienceReplay(BaseDatasetExperienceReplay):
"""Roboset experience replay dataset.
This class downloads the H5 data from roboset and processes it in a mmap
format, which makes indexing (and therefore sampling) faster.
Learn more about roboset here: https://sites.google.com/view/robohive/roboset
The data format follows the :ref:`TED convention <TED-format>`.
Args:
dataset_id (str): the dataset to be downloaded. Must be part of RobosetExperienceReplay.available_datasets.
batch_size (int): Batch-size used during sampling. Can be overridden by `data.sample(batch_size)` if
necessary.
Keyword Args:
root (Path or str, optional): The Roboset dataset root directory.
The actual dataset memory-mapped files will be saved under
`<root>/<dataset_id>`. If none is provided, it defaults to
``~/.cache/torchrl/roboset`.
download (bool or str, optional): Whether the dataset should be downloaded if
not found. Defaults to ``True``. Download can also be passed as ``"force"``,
in which case the downloaded data will be overwritten.
sampler (Sampler, optional): the sampler to be used. If none is provided
a default RandomSampler() will be used.
writer (Writer, optional): the writer to be used. If none is provided
a default :class:`~torchrl.data.replay_buffers.writers.ImmutableDatasetWriter` will be used.
collate_fn (callable, optional): merges a list of samples to form a
mini-batch of Tensor(s)/outputs. Used when using batched
loading from a map-style dataset.
pin_memory (bool): whether pin_memory() should be called on the rb
samples.
prefetch (int, optional): number of next batches to be prefetched
using multithreading.
transform (Transform, optional): Transform to be executed when sample() is called.
To chain transforms use the :class:`~torchrl.envs.transforms.transforms.Compose` class.
split_trajs (bool, optional): if ``True``, the trajectories will be split
along the first dimension and padded to have a matching shape.
To split the trajectories, the ``"done"`` signal will be used, which
is recovered via ``done = truncated | terminated``. In other words,
it is assumed that any ``truncated`` or ``terminated`` signal is
equivalent to the end of a trajectory.
Defaults to ``False``.
Attributes:
available_datasets: a list of accepted entries to be downloaded.
Examples:
>>> import torch
>>> torch.manual_seed(0)
>>> from torchrl.envs.transforms import ExcludeTransform
>>> from torchrl.data.datasets import RobosetExperienceReplay
>>> d = RobosetExperienceReplay("FK1-v4(expert)/FK1_MicroOpenRandom_v2d-v4", batch_size=32,
... transform=ExcludeTransform("info", ("next", "info"))) # excluding info dict for conciseness
>>> for batch in d:
... break
>>> # data is organised by seed and episode, but stored contiguously
>>> print(f"{batch['seed']}, {batch['episode']}")
tensor([2, 1, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 2, 2, 2, 1, 1, 2, 0, 2, 0, 2, 2, 1,
0, 2, 0, 0, 1, 1, 2, 1]) tensor([17, 20, 18, 9, 6, 1, 12, 6, 2, 6, 8, 15, 8, 21, 17, 3, 9, 20,
23, 12, 3, 16, 19, 16, 16, 4, 4, 12, 1, 2, 15, 24])
>>> print(batch)
TensorDict(
fields={
action: Tensor(shape=torch.Size([32, 9]), device=cpu, dtype=torch.float64, is_shared=False),
done: Tensor(shape=torch.Size([32, 1]), device=cpu, dtype=torch.bool, is_shared=False),
episode: Tensor(shape=torch.Size([32]), device=cpu, dtype=torch.int64, is_shared=False),
index: Tensor(shape=torch.Size([32]), device=cpu, dtype=torch.int64, is_shared=False),
next: TensorDict(
fields={
done: Tensor(shape=torch.Size([32, 1]), device=cpu, dtype=torch.bool, is_shared=False),
observation: Tensor(shape=torch.Size([32, 75]), device=cpu, dtype=torch.float64, is_shared=False),
reward: Tensor(shape=torch.Size([32, 1]), device=cpu, dtype=torch.float64, is_shared=False),
terminated: Tensor(shape=torch.Size([32, 1]), device=cpu, dtype=torch.bool, is_shared=False),
truncated: Tensor(shape=torch.Size([32, 1]), device=cpu, dtype=torch.bool, is_shared=False)},
batch_size=torch.Size([32]),
device=cpu,
is_shared=False),
observation: Tensor(shape=torch.Size([32, 75]), device=cpu, dtype=torch.float64, is_shared=False),
seed: Tensor(shape=torch.Size([32]), device=cpu, dtype=torch.int64, is_shared=False),
terminated: Tensor(shape=torch.Size([32, 1]), device=cpu, dtype=torch.bool, is_shared=False),
time: Tensor(shape=torch.Size([32]), device=cpu, dtype=torch.float64, is_shared=False)},
truncated: Tensor(shape=torch.Size([32, 1]), device=cpu, dtype=torch.bool, is_shared=False)},
batch_size=torch.Size([32]),
device=cpu,
is_shared=False)
"""
available_datasets = [
"DAPG(expert)/door_v2d-v1",
"DAPG(expert)/relocate_v2d-v1",
"DAPG(expert)/hammer_v2d-v1",
"DAPG(expert)/pen_v2d-v1",
"DAPG(human)/door_v2d-v1",
"DAPG(human)/relocate_v2d-v1",
"DAPG(human)/hammer_v2d-v1",
"DAPG(human)/pen_v2d-v1",
"FK1-v4(expert)/FK1_MicroOpenRandom_v2d-v4",
"FK1-v4(expert)/FK1_Knob2OffRandom_v2d-v4",
"FK1-v4(expert)/FK1_LdoorOpenRandom_v2d-v4",
"FK1-v4(expert)/FK1_SdoorOpenRandom_v2d-v4",
"FK1-v4(expert)/FK1_Knob1OnRandom_v2d-v4",
"FK1-v4(human)/human_demos_by_playdata",
"FK1-v4(human)/human_demos_by_task/human_demo_singleTask_Fixed-v4",
"FK1-v4(human)/human_demos_by_task/FK1_SdoorOpenRandom_v2d-v4",
"FK1-v4(human)/human_demos_by_task/FK1_LdoorOpenRandom_v2d-v4",
"FK1-v4(human)/human_demos_by_task/FK1_Knob2OffRandom_v2d-v4",
"FK1-v4(human)/human_demos_by_task/FK1_Knob1OnRandom_v2d-v4",
"FK1-v4(human)/human_demos_by_task/FK1_MicroOpenRandom_v2d-v4",
]
def __init__(
self,
dataset_id,
batch_size: int,
*,
root: str | Path | None = None,
download: bool = True,
sampler: Sampler | None = None,
writer: Writer | None = None,
collate_fn: Callable | None = None,
pin_memory: bool = False,
prefetch: int | None = None,
transform: "torchrl.envs.Transform" | None = None, # noqa-F821
split_trajs: bool = False,
**env_kwargs,
):
if not _has_h5py or not _has_hf_hub:
raise ImportError(
"h5py and huggingface_hub are required for Roboset datasets."
)
if dataset_id not in self.available_datasets:
raise ValueError(
f"The dataset_id {dataset_id} isn't part of the accepted datasets. "
f"To check which dataset can be downloaded, call `{type(self)}.available_datasets`."
)
self.dataset_id = dataset_id
if root is None:
root = _get_root_dir("roboset")
os.makedirs(root, exist_ok=True)
self.root = root
self.split_trajs = split_trajs
self.download = download
if self.download == "force" or (self.download and not self._is_downloaded()):
if self.download == "force":
try:
if os.path.exists(self.data_path_root):
shutil.rmtree(self.data_path_root)
if self.data_path != self.data_path_root:
shutil.rmtree(self.data_path)
except FileNotFoundError:
pass
storage = self._download_and_preproc()
elif self.split_trajs and not os.path.exists(self.data_path):
storage = self._make_split()
else:
storage = self._load()
storage = TensorStorage(storage)
if writer is None:
writer = ImmutableDatasetWriter()
super().__init__(
storage=storage,
sampler=sampler,
writer=writer,
collate_fn=collate_fn,
pin_memory=pin_memory,
prefetch=prefetch,
transform=transform,
batch_size=batch_size,
)
def _download_from_huggingface(self, tempdir):
try:
from huggingface_hub import hf_hub_download, HfApi
except ImportError:
raise ImportError(
f"huggingface_hub is required for downloading {type(self)}'s datasets."
)
dataset = HfApi().dataset_info("jdvakil/RoboSet_Sim")
h5_files = []
datapath = Path(tempdir) / "data"
for sibling in dataset.siblings:
if sibling.rfilename.startswith(
self.dataset_id
) and sibling.rfilename.endswith(".h5"):
path = Path(sibling.rfilename)
local_path = hf_hub_download(
"jdvakil/RoboSet_Sim",
subfolder=str(path.parent),
filename=str(path.parts[-1]),
repo_type="dataset",
cache_dir=str(datapath),
)
h5_files.append(local_path)
return sorted(h5_files)
def _download_and_preproc(self):
with tempfile.TemporaryDirectory() as tempdir:
h5_data_files = self._download_from_huggingface(tempdir)
return self._preproc_h5(h5_data_files)
def _preproc_h5(self, h5_data_files):
td_data = TensorDict()
total_steps = 0
torchrl_logger.info(
f"first read through data files {h5_data_files} to create data structure..."
)
episode_dict = {}
h5_datas = []
for seed, h5_data_name in enumerate(h5_data_files):
torchrl_logger.info(f"\nReading {h5_data_name}")
h5_data = PersistentTensorDict.from_h5(h5_data_name)
h5_datas.append(h5_data)
for i, (episode_key, episode) in enumerate(h5_data.items()):
episode_num = int(episode_key[len("Trial") :])
episode_len = episode["actions"].shape[0]
episode_dict[(seed, episode_num)] = (episode_key, episode_len)
# Get the total number of steps for the dataset
total_steps += episode_len
torchrl_logger.info(f"total_steps {total_steps}")
if i == 0 and seed == 0:
td_data.set("episode", 0)
td_data.set("seed", 0)
for key, val in episode.items():
match = _NAME_MATCH[key]
if key in ("observations", "env_infos", "done"):
td_data.set(("next", match), torch.zeros_like(val[0]))
td_data.set(match, torch.zeros_like(val[0]))
elif key not in ("rewards",):
td_data.set(match, torch.zeros_like(val[0]))
else:
td_data.set(
("next", match),
torch.zeros_like(val[0].unsqueeze(-1)),
)
# give it the proper size
td_data["next", "done"] = td_data["next", "done"].unsqueeze(-1)
td_data["done"] = td_data["done"].unsqueeze(-1)
td_data["next", "terminated"] = td_data["next", "done"]
td_data["next", "truncated"] = td_data["next", "done"]
td_data["terminated"] = td_data["done"]
td_data["truncated"] = td_data["done"]
td_data = td_data.expand(total_steps)
# save to designated location
torchrl_logger.info(f"creating tensordict data in {self.data_path_root}: ")
td_data = td_data.memmap_like(self.data_path_root)
# torchrl_logger.info(f"tensordict structure: {td_data}")
torchrl_logger.info(
f"Local dataset structure: {print_directory_tree(self.data_path_root)}"
)
torchrl_logger.info(f"Reading data from {len(episode_dict)} episodes")
index = 0
if _has_tqdm:
from tqdm import tqdm
else:
tqdm = None
with tqdm(total=total_steps) if _has_tqdm else nullcontext() as pbar:
# iterate over episodes and populate the tensordict
for seed, episode_num in sorted(episode_dict, key=lambda key: key[1]):
h5_data = h5_datas[seed]
episode_key, steps = episode_dict[(seed, episode_num)]
episode = h5_data.get(episode_key)
idx = slice(index, (index + steps))
data_view = td_data[idx]
data_view.fill_("episode", episode_num)
data_view.fill_("seed", seed)
for key, val in episode.items():
match = _NAME_MATCH[key]
if steps != val.shape[0]:
raise RuntimeError(
f"Mismatching number of steps for key {key}: was {steps} but got {val.shape[0]}."
)
if key in (
"observations",
"env_infos",
):
data_view["next", match][:-1].copy_(val[1:])
data_view[match].copy_(val)
elif key not in ("rewards", "done", "terminated", "truncated"):
data_view[match].copy_(val)
elif key in ("done", "terminated", "truncated"):
data_view[match].copy_(val.unsqueeze(-1))
data_view[("next", match)].copy_(val.unsqueeze(-1))
else:
data_view[("next", match)].copy_(val.unsqueeze(-1))
data_view["next", "terminated"].copy_(data_view["next", "done"])
if pbar is not None:
pbar.update(steps)
pbar.set_description(
f"index={index} - episode num {episode_num} - seed {seed}"
)
index += steps
return td_data
def _make_split(self):
from torchrl.collectors.utils import split_trajectories
td_data = TensorDict.load_memmap(self.data_path_root)
td_data = split_trajectories(td_data).memmap_(self.data_path)
return td_data
def _load(self):
return TensorDict.load_memmap(self.data_path)
@property
def data_path(self):
if self.split_trajs:
return Path(self.root) / (self.dataset_id + "_split")
return self.data_path_root
@property
def data_path_root(self):
return Path(self.root) / self.dataset_id
def _is_downloaded(self):
return os.path.exists(self.data_path_root)