-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
61 lines (51 loc) · 1.47 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
from typing import NamedTuple, Optional
import torch
import numpy as np
class WallSample(NamedTuple):
states: torch.Tensor
locations: torch.Tensor
actions: torch.Tensor
class WallDataset:
def __init__(
self,
data_path,
probing=False,
device="cuda",
):
self.device = device
self.states = np.load(f"{data_path}/states.npy", mmap_mode="r")
self.actions = np.load(f"{data_path}/actions.npy")
if probing:
self.locations = np.load(f"{data_path}/locations.npy")
else:
self.locations = None
def __len__(self):
return len(self.states)
def __getitem__(self, i):
states = torch.from_numpy(self.states[i]).float().to(self.device)
actions = torch.from_numpy(self.actions[i]).float().to(self.device)
if self.locations is not None:
locations = torch.from_numpy(self.locations[i]).float().to(self.device)
else:
locations = torch.empty(0).to(self.device)
return WallSample(states=states, locations=locations, actions=actions)
def create_wall_dataloader(
data_path,
probing=False,
device="cuda",
batch_size=64,
train=True,
):
ds = WallDataset(
data_path=data_path,
probing=probing,
device=device,
)
loader = torch.utils.data.DataLoader(
ds,
batch_size,
shuffle=train,
drop_last=True,
pin_memory=False,
)
return loader