-
Notifications
You must be signed in to change notification settings - Fork 28
/
data_gen.py
48 lines (37 loc) · 1.27 KB
/
data_gen.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
import os
import pickle
from PIL import Image
from torch.utils.data import Dataset
from torchvision import transforms
from config import IMG_DIR, pickle_file
# Data augmentation and normalization for training
# Just normalization for validation
data_transforms = {
'train': transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(brightness=0.125, contrast=0.125, saturation=0.125),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
]),
'val': transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}
class ArcFaceDataset(Dataset):
def __init__(self, split):
with open(pickle_file, 'rb') as file:
data = pickle.load(file)
self.split = split
self.samples = data
self.transformer = data_transforms['train']
def __getitem__(self, i):
sample = self.samples[i]
filename = sample['img']
filename = os.path.join(IMG_DIR, filename)
img = Image.open(filename).convert('RGB')
img = self.transformer(img)
label = sample['label']
return img, label
def __len__(self):
return len(self.samples)