-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhf_dataset.py
52 lines (39 loc) · 1.54 KB
/
hf_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
from datasets import load_dataset
from torch.utils.data import Dataset
from torchvision import transforms
from web_dataset import SplitImages, actions_to_one_hot
class ImageDataset(Dataset):
def __init__(
self,
split: str,
return_actions: bool = False,
):
self.return_actions = return_actions
self.split = split
# Load dataset using Hugging Face datasets
self.dataset = load_dataset("Iker/GTAV-Driving-Dataset", split=split)
# Define transforms
self.transform = transforms.Compose(
[transforms.ToTensor(), SplitImages(), transforms.Resize((360, 640))]
)
print(f"Loaded dataset for {split} split with {len(self.dataset)} examples")
def __len__(self):
return len(self.dataset)
def __getitem__(self, idx):
sample = self.dataset[idx]
# Convert PIL image to tensor and apply transforms
img = self.transform(sample["jpg"])
if self.return_actions:
actions = actions_to_one_hot(sample["json"]["actions_int"])
return {"video": img, "actions": actions}
else:
return {"video": img}
def __iter__(self):
for sample in self.dataset:
# Convert PIL image to tensor and apply transforms
img = self.transform(sample["image"])
if self.return_actions:
actions = actions_to_one_hot(sample["json"]["actions_int"])
yield {"video": img, "actions": actions}
else:
yield {"video": img}