-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathdataset_utils.py
More file actions
365 lines (314 loc) · 12.5 KB
/
Copy pathdataset_utils.py
File metadata and controls
365 lines (314 loc) · 12.5 KB
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
# coding=utf-8
# Copyright 2026 The TensorFlow Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Library functions and types for the robomimic datasets."""
import dataclasses
import enum
from typing import Any, Dict, List, Mapping
from etils import epath
import h5py
import numpy as np
import tensorflow_datasets.public_api as tfds
import tree
class Task(enum.Enum):
LIFT = 'lift'
CAN = 'can'
SQUARE = 'square'
TRANSPORT = 'transport'
TOOL_HANG = 'tool_hang'
def __str__(self) -> str:
return self.value
class DataSource(enum.Enum):
PH = 'ph' # proficient human, i.e. expert demonstrations
MH = 'mh' # mixed-human, i.e. more suboptimal than proficient human
MG = 'mg' # machine generated, i.e. soft actor critic with dense reward
def __str__(self) -> str:
return self.value
class ObservationType(enum.Enum):
IMAGE = 'image' # image-based observations (i.e. exteroception)
LOW_DIM = 'low_dim' # internal state-based observations (i.e. proprioception)
def __str__(self) -> str:
return self.value
@dataclasses.dataclass
class BuilderConfig(tfds.core.BuilderConfig):
"""Configuration of the dataset versions."""
task: Task = Task.LIFT
dataset: DataSource = DataSource.PH
filename: ObservationType = ObservationType.LOW_DIM
horizon: int = 100
TASKS = {
Task.LIFT: {
'datasets': [DataSource.PH, DataSource.MH, DataSource.MG],
'object': 10,
'states': 32,
'horizon': 400,
'action_size': 7,
},
Task.CAN: {
'datasets': [DataSource.PH, DataSource.MH, DataSource.MG],
'object': 14,
'states': 71,
'horizon': 400,
'action_size': 7,
},
Task.SQUARE: {
'datasets': [DataSource.PH, DataSource.MH],
'object': 14,
'states': 45,
'horizon': 400,
'action_size': 7,
},
Task.TRANSPORT: {
'datasets': [DataSource.PH, DataSource.MH],
'object': 41,
'states': 115,
'horizon': 700,
'action_size': 14,
},
Task.TOOL_HANG: {
'datasets': [
DataSource.PH,
],
'object': 44,
'states': 58,
'horizon': 700,
'action_size': 7,
},
}
def make_builder_configs(dataset: DataSource):
"""Creates the PH build configs."""
configs = []
for task, details in TASKS.items():
if dataset in details['datasets']: # pyrefly: ignore[not-iterable]
for observation_type in [ObservationType.IMAGE, ObservationType.LOW_DIM]:
# pytype: disable=wrong-keyword-args
configs.append(
# name is inherited from base dataclass unbeknownst to the linter
BuilderConfig( # pylint: disable=unexpected-keyword-arg
name=f'{task}_{dataset}_{observation_type}',
task=task,
dataset=dataset,
filename=observation_type,
horizon=details['horizon'],
)
)
# pytype: enable=wrong-keyword-args
return configs
def _concat_obs(base_obs, extra_obs):
return np.append(base_obs, np.expand_dims(extra_obs, 0), axis=0)
def episode_metadata(
mask: Mapping[str, List[str]], episode_key: str
) -> Dict[str, bool]:
"""Builds the metadata of an episode.
Args:
mask: A dict that maps flags to the list of episodes for which this flag is
true.
episode_key: The key of an episode.
Returns:
Dictionary that maps the flags to the value corresponding to the given
episode.
"""
metadata = {}
for k in mask:
metadata[k] = episode_key in mask[k]
return metadata
def build_episode(steps: Mapping[str, Any]) -> Dict[str, Any]:
"""Builds a full episode dict.
Args:
steps: A dict with each key corresponding to an episode of that variable.
Returns:
A dict with data specific to one episode.
"""
# This is the base episode length without the end step, that step will get
# added manually. `[:]` notation is necessary to extract data from the hdf5
# object.
ep_length = steps['actions'][:].shape[0]
episode = {}
episode['is_first'] = np.append(True, np.zeros(ep_length, dtype=bool))
# The standard 'obs' needs to be extended with the last element from
# 'next_obs' to reconstruct the full sequence.
obs = tree.map_structure(np.array, dict(steps['obs']))
last_obs = tree.map_structure(lambda el: el[-1], dict(steps['next_obs']))
concat_obs = tree.map_structure(_concat_obs, obs, last_obs)
actions = steps['actions'][:]
dones = steps['dones'][:]
episode['observation'] = concat_obs
episode['action'] = np.append(
actions, np.expand_dims(np.zeros(actions[0].shape), 0), axis=0
)
episode['reward'] = np.append(steps['rewards'][:], 0)
episode['discount'] = np.append(np.ones(ep_length), 0)
# Offset `dones` by one as it corresponds to the `next_obs`:
# "done signal, equal to 1 if playing the corresponding action in the state
# should terminate the episode". Since playing another action may not end the
# episode, it is actually representing whether the following state is
# the terminal state.
episode['is_terminal'] = np.append(False, dones).astype(bool)
episode['is_last'] = np.append(np.zeros(ep_length, dtype=bool), True)
states = steps['states'][:]
episode['states'] = np.append(
states, np.expand_dims(np.zeros(states[0].shape), 0), axis=0
)
return episode
def tensor_feature(size: int, doc=None) -> tfds.features.Tensor:
return tfds.features.Tensor(
shape=(size,),
dtype=np.float64,
encoding=tfds.features.Encoding.ZLIB,
doc=doc,
)
def image_feature(size: int, doc=None) -> tfds.features.Image:
return tfds.features.Image(
shape=(size, size, 3), dtype=np.uint8, encoding_format='png', doc=doc
)
class RobomimicBuilder(tfds.core.GeneratorBasedBuilder, skip_registration=True):
"""DatasetBuilder for robomimic datasets."""
VERSION: tfds.core.Version
RELEASE_NOTES: Dict[str, str] # pyrefly: ignore[bad-override]
BUILDER_CONFIGS: List[tfds.core.BuilderConfig]
DATASET_NAME: str
DATASET_FILE_EXTENSION: str = ''
def _info(self) -> tfds.core.DatasetInfo:
"""Returns the dataset metadata."""
return self.dataset_info_from_configs(
features=self._get_features(),
supervised_keys=None,
homepage='https://arise-initiative.github.io/robomimic-web/',
)
def _get_features(self) -> tfds.features.FeaturesDict:
obs_dim = TASKS[self.builder_config.task]['object'] # pyrefly: ignore[missing-attribute]
states_dim = TASKS[self.builder_config.task]['states'] # pyrefly: ignore[missing-attribute]
action_size = TASKS[self.builder_config.task]['action_size'] # pyrefly: ignore[missing-attribute]
observation = {
'object': tensor_feature(
obs_dim, # pyrefly: ignore[bad-argument-type]
),
'robot0_eef_pos': tensor_feature(3, doc='End-effector position'),
'robot0_eef_quat': tensor_feature(4, doc='End-effector orientation'),
'robot0_eef_vel_ang': tensor_feature(
3, doc='End-effector angular velocity'
),
'robot0_eef_vel_lin': tensor_feature(
3, doc='End-effector cartesian velocity'
),
'robot0_gripper_qpos': tensor_feature(2, doc='Gripper position'),
'robot0_gripper_qvel': tensor_feature(2, doc='Gripper velocity'),
'robot0_joint_pos': tensor_feature(7, doc='7DOF joint positions'),
'robot0_joint_pos_cos': tensor_feature(7),
'robot0_joint_pos_sin': tensor_feature(7),
'robot0_joint_vel': tensor_feature(7, doc='7DOF joint velocities'),
}
if self.builder_config.task == Task.TRANSPORT: # pyrefly: ignore[missing-attribute]
observation['robot1_eef_pos'] = tensor_feature(
3, doc='End-effector position'
)
observation['robot1_eef_quat'] = tensor_feature(
4, doc='End-effector orientation'
)
observation['robot1_eef_vel_ang'] = tensor_feature(
3, doc='End-effector angular velocity'
)
observation['robot1_eef_vel_lin'] = tensor_feature(
3, doc='End-effector cartesian velocity'
)
observation['robot1_gripper_qpos'] = tensor_feature(
2, doc='Gripper position'
)
observation['robot1_gripper_qvel'] = tensor_feature(
2, doc='Gripper velocity'
)
observation['robot1_joint_pos'] = tensor_feature(
7, doc='7DOF joint positions'
)
observation['robot1_joint_pos_cos'] = tensor_feature(7)
observation['robot1_joint_pos_sin'] = tensor_feature(7)
observation['robot1_joint_vel'] = tensor_feature(
7, doc='7DOF joint velocities'
)
if self.builder_config.filename == ObservationType.IMAGE: # pyrefly: ignore[missing-attribute]
if self.builder_config.task == Task.TOOL_HANG:
observation['robot0_eye_in_hand_image'] = image_feature(240) # pyrefly: ignore[bad-assignment]
observation['sideview_image'] = image_feature(240) # pyrefly: ignore[bad-assignment]
elif self.builder_config.task == Task.TRANSPORT:
observation['robot0_eye_in_hand_image'] = image_feature(84) # pyrefly: ignore[bad-assignment]
observation['robot1_eye_in_hand_image'] = image_feature(84) # pyrefly: ignore[bad-assignment]
observation['shouldercamera0_image'] = image_feature(84) # pyrefly: ignore[bad-assignment]
observation['shouldercamera1_image'] = image_feature(84) # pyrefly: ignore[bad-assignment]
else:
observation['agentview_image'] = image_feature(84) # pyrefly: ignore[bad-assignment]
observation['robot0_eye_in_hand_image'] = image_feature(84) # pyrefly: ignore[bad-assignment]
# metadata depends on the quality type
metadata = self._get_metadata()
features = tfds.features.FeaturesDict({
'horizon': np.int32,
'episode_id': np.str_,
'steps': tfds.features.Dataset({ # pyrefly: ignore[bad-argument-type]
'action': tensor_feature(action_size), # pyrefly: ignore[bad-argument-type]
'observation': observation,
'reward': np.float64,
'is_first': np.bool_,
'is_last': np.bool_,
'is_terminal': np.bool_,
'discount': np.int32,
'states': tensor_feature(states_dim), # pyrefly: ignore[bad-argument-type]
}),
**metadata,
})
return features
def _get_metadata(self) -> Dict[Any, Any]:
return {}
def _split_generators(self, dl_manager: tfds.download.DownloadManager):
"""Returns SplitGenerators."""
# Machine generated datasets have an additional variant as they are trained
# using dense rather than sparse rewards. We currently are only interested
# in the sparse rewards, for consistency with the other datasets.
ext = self.DATASET_FILE_EXTENSION
filepath = (
# pyrefly: ignore[missing-attribute]
'http://downloads.cs.stanford.edu/downloads/rt_benchmark/'
f'{self.builder_config.task}/{self.builder_config.dataset}/'
f'{self.builder_config.filename}{ext}.hdf5'
)
path = dl_manager.download_and_extract({'file_path': filepath})
return {
'train': self._generate_examples(path),
}
def _generate_examples(self, path):
"""Yields examples."""
path = epath.Path(path['file_path'])
with path.open('rb') as f:
with h5py.File(f, 'r') as dataset_file:
data = dataset_file['data']
if 'mask' in dataset_file.keys():
mask = dataset_file['mask']
string_mask = {}
for k in mask:
string_mask[k] = []
for element in mask[k]:
string_mask[k].append(element.decode('UTF-8'))
mask = string_mask
for key in data:
yield key, {
'steps': build_episode(data[key]),
'horizon': self.builder_config.horizon, # pyrefly: ignore[missing-attribute]
'episode_id': key,
**episode_metadata(mask, key),
}
else:
for key in data:
yield key, {
'steps': build_episode(data[key]),
'horizon': self.builder_config.horizon, # pyrefly: ignore[missing-attribute]
'episode_id': key,
}