-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdata.py
34 lines (26 loc) · 957 Bytes
/
data.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
import os
import numpy as np
from PIL import Image
from torch.utils.data import Dataset
class FaceDataset(Dataset):
def __init__(self, filepath_list, transform=None):
self.images = []
self.labels = []
for filepath in filepath_list:
basename = os.path.basename(filepath)
self.labels.append(int(basename[4:6]))
img = np.array(Image.open(filepath).convert('RGB'))
self.images.append(img)
self.images = np.array(self.images)
self.labels = np.array(self.labels)
self.transform = transform
def __len__(self):
return self.images.shape[0]
def __getitem__(self, index):
img = self.images[index]
# img = self.images[index].astype(np.float32)
label = self.labels[index]
if self.transform:
img = self.transform(img)
sample = {'image': img, 'label': label}
return sample