This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataset.py
317 lines (261 loc) · 9.7 KB
/
dataset.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
import itertools
import random
from typing import (
Union,
Optional,
Tuple,
List,
Dict,
Callable,
TypeVar,
Generic,
)
from pickle import UnpicklingError
from collections import defaultdict
from pathlib import Path
import numpy as np
import torch
import torch.utils.data as data
from torch.nn import functional as F
import torchvision.transforms as transforms
import torchvision.transforms.functional as transforms_f
import einops
from utils import Instructions, Sample, Camera
T = TypeVar("T")
U = TypeVar("U")
class Cache(Generic[T, U]):
def __init__(self, size: int, loader: Callable[[T], U]):
self._size = size
self._loader = loader
self._keys: List[T] = []
self._cache: Dict[T, U] = {}
def __call__(self, args: T) -> U:
if args in self._cache:
index = self._keys.index(args)
del self._keys[index]
self._keys.append(args)
return self._cache[args]
# print(args, len(self._keys), self._size)
value = self._loader(args)
if len(self._keys) == self._size and self._keys != []:
key = self._keys[0]
del self._cache[key]
del self._keys[0]
if len(self._keys) < self._size:
self._keys.append(args)
self._cache[args] = value
return value
def data_transform(scales, **kwargs: torch.Tensor) -> Dict[str, torch.Tensor]:
"""
Expect tensors as T, N, C, H, W
"""
keys = list(kwargs.keys())
if len(keys) == 0:
raise RuntimeError("No args")
# Continuous range of scales
sc = np.random.uniform(*scales)
t, n, c, raw_h, raw_w = kwargs[keys[0]].shape
kwargs = {n: arg.flatten(0, 1) for n, arg in kwargs.items()}
resized_size = [int(raw_h * sc), int(raw_w * sc)]
# Resize based on randomly sampled scale
kwargs = {
n: transforms_f.resize(
arg,
resized_size,
transforms.InterpolationMode.NEAREST
# if "pc" in n
# else transforms.InterpolationMode.BILINEAR,
)
for n, arg in kwargs.items()
}
# Adding padding if crop size is smaller than the resized size
if raw_h > resized_size[0] or raw_w > resized_size[1]:
right_pad, bottom_pad = max(raw_h - resized_size[1], 0), max(
raw_w - resized_size[0], 0
)
kwargs = {
n: transforms_f.pad(
arg,
padding=[0, 0, right_pad, bottom_pad],
padding_mode="reflect",
)
for n, arg in kwargs.items()
}
# Random Cropping
i, j, h, w = transforms.RandomCrop.get_params(
kwargs[keys[0]], output_size=(raw_h, raw_w)
)
kwargs = {n: transforms_f.crop(arg, i, j, h, w) for n, arg in kwargs.items()}
kwargs = {
n: einops.rearrange(arg, "(t n) c h w -> t n c h w", t=t)
for n, arg in kwargs.items()
}
return kwargs
def loader(file: Path) -> Optional[np.ndarray]:
try:
return np.load(file, allow_pickle=True)
except UnpicklingError as e:
print(f"Can't load {file}: {e}")
return None
class DataTransform(object):
def __init__(self, scales):
self.scales = scales
def __call__(self, **kwargs: torch.Tensor) -> Dict[str, torch.Tensor]:
"""
Except tensors as T, N, C, H, W
"""
keys = list(kwargs.keys())
if len(keys) == 0:
raise RuntimeError("No args")
# Continuous range of scales
sc = np.random.uniform(*self.scales)
t, n, c, raw_h, raw_w = kwargs[keys[0]].shape
kwargs = {n: arg.flatten(0, 1) for n, arg in kwargs.items()}
resized_size = [int(raw_h * sc), int(raw_w * sc)]
# Resize based on randomly sampled scale
kwargs = {
n: transforms_f.resize(
arg,
resized_size,
transforms.InterpolationMode.NEAREST
# if "pc" in n
# else transforms.InterpolationMode.BILINEAR,
)
for n, arg in kwargs.items()
}
# Adding padding if crop size is smaller than the resized size
if raw_h > resized_size[0] or raw_w > resized_size[1]:
right_pad, bottom_pad = max(raw_h - resized_size[1], 0), max(
raw_w - resized_size[0], 0
)
kwargs = {
n: transforms_f.pad(
arg,
padding=[0, 0, right_pad, bottom_pad],
padding_mode="reflect",
)
for n, arg in kwargs.items()
}
# Random Cropping
i, j, h, w = transforms.RandomCrop.get_params(
kwargs[keys[0]], output_size=(raw_h, raw_w)
)
kwargs = {n: transforms_f.crop(arg, i, j, h, w) for n, arg in kwargs.items()}
kwargs = {
n: einops.rearrange(arg, "(t n) c h w -> t n c h w", t=t)
for n, arg in kwargs.items()
}
return kwargs
class RLBenchDataset(data.Dataset):
"""
RLBench dataset, 10 tasks
"""
def __init__(
self,
root: Union[Path, str, List[Path], List[str]],
taskvar: List[Tuple[str, int]],
instructions: Instructions,
max_episode_length: int,
cache_size: int,
max_episodes_per_taskvar: int,
num_iters: Optional[int] = None,
cameras: Tuple[Camera, ...] = ("wrist", "left_shoulder", "right_shoulder"),
training: bool = True,
):
self._cache = Cache(cache_size, loader)
self._cameras = cameras
self._max_episode_length = max_episode_length
self._max_episodes_per_taskvar = max_episodes_per_taskvar
self._num_iters = num_iters
self._training = training
self._taskvar = taskvar
if isinstance(root, (Path, str)):
root = [Path(root)]
self._root: List[Path] = [Path(r).expanduser() for r in root]
# We keep only useful instructions to save mem
self._instructions: Instructions = defaultdict(dict)
for task, var in taskvar:
self._instructions[task][var] = instructions[task][var]
self._transform = DataTransform((0.75, 1.25))
self._data_dirs = []
self._episodes = []
self._num_episodes = 0
for root, (task, var) in itertools.product(self._root, taskvar):
data_dir = root / f"{task}+{var}"
if not data_dir.is_dir():
raise ValueError(f"Can't find dataset folder {data_dir}")
episodes = [(task, var, ep) for ep in data_dir.glob("*.npy")]
episodes = episodes[: self._max_episodes_per_taskvar]
num_episodes = len(episodes)
if num_episodes == 0:
raise ValueError(f"Can't find episodes at folder {data_dir}")
self._data_dirs.append(data_dir)
self._episodes += episodes
self._num_episodes += num_episodes
print("Num ep.", self._num_episodes)
def __getitem__(self, episode_id: int) -> Optional[Sample]:
episode_id %= self._num_episodes
task, variation, file = self._episodes[episode_id]
episode = self._cache(file)
if episode is None:
return None
frame_ids = episode[0]
num_ind = len(frame_ids)
pad_len = max(0, self._max_episode_length - num_ind)
states: torch.Tensor = torch.stack([episode[1][i].squeeze(0) for i in frame_ids])
if states.shape[-1] != 128 or states.shape[-2] != 128:
raise ValueError(f"{states.shape} {self._episodes[episode_id]}")
pad_vec = [0] * (2 * states.dim())
pad_vec[-1] = pad_len
states = F.pad(states, pad_vec)
cameras = list(episode[3][0].keys())
assert all(c in cameras for c in self._cameras)
index = torch.tensor([cameras.index(c) for c in self._cameras])
states = states[:, index]
rgbs = states[:, :, 0]
pcds = states[:, :, 1]
attns = torch.Tensor([])
for i in frame_ids:
attn_cams = torch.Tensor([])
for cam in self._cameras:
u, v = episode[3][i][cam]
attn = torch.zeros((1, 1, 128, 128))
if not (u < 0 or u > 127 or v < 0 or v > 127):
attn[0, 0, v, u] = 1
attn_cams = torch.cat([attn_cams, attn])
attns = torch.cat([attns, attn_cams.unsqueeze(0)])
pad_vec = [0] * (2 * attns.dim())
pad_vec[-1] = pad_len
attns = F.pad(attns, pad_vec)
rgbs = torch.cat([rgbs, attns], 2)
if self._training:
modals = self._transform(rgbs=rgbs, pcds=pcds)
rgbs = modals["rgbs"]
pcds = modals["pcds"]
action = torch.cat([episode[2][i] for i in frame_ids])
shape = [0, 0] * action.dim()
shape[-1] = pad_len
action = F.pad(action, tuple(shape), value=0)
mask = torch.tensor([True] * num_ind + [False] * pad_len)
instr: torch.Tensor = random.choice(self._instructions[task][variation])
gripper = torch.cat([episode[4][i] for i in frame_ids])
shape = [0, 0] * gripper.dim()
shape[-1] = pad_len
gripper = F.pad(gripper, tuple(shape), value=0)
tframe_ids = torch.tensor(frame_ids)
tframe_ids = F.pad(tframe_ids, (0, pad_len), value=-1)
return {
"frame_id": tframe_ids,
"task": task,
"variation": variation,
"rgbs": rgbs,
"pcds": pcds,
"action": action,
"padding_mask": mask,
"instr": instr,
"gripper": gripper,
}
def __len__(self):
if self._num_iters is not None:
return self._num_iters
return self._num_episodes