-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
24 lines (19 loc) · 802 Bytes
/
dataset.py
File metadata and controls
24 lines (19 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from torch.utils.data import Dataset
from torchvision import transforms
from PIL import Image
import torch
class FaceDataset(Dataset):
def __init__(self, samples, transform=None):
self.samples = samples
self.transform = transform if transform else transforms.ToTensor()
class_names = sorted(set(label for _, label in samples))
self.class_to_idx = {name: idx for idx, name in enumerate(class_names)}
def __len__(self):
return len(self.samples)
def __getitem__(self, idx):
img_path, label_str = self.samples[idx]
image = Image.open(img_path).convert('RGB')
if self.transform:
image = self.transform(image)
label = torch.tensor(self.class_to_idx[label_str], dtype=torch.long)
return image, label