-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdataset.py
48 lines (40 loc) · 1.58 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
from torch.utils.data import Dataset
from torchvision import transforms
import numpy as np
import imageio
import cv2
import os
class AFADDataset(Dataset):
def __init__(self, data_root, index_root, input_size, augment):
self.data = []
self.data_root = data_root
self.input_size = input_size
self.augment = augment
with open(index_root, 'r', encoding='utf-8') as f:
for line in f.readlines():
path = os.path.join(self.data_root, line[2:-1])
if os.path.exists(path) and 'Thumbs' not in path:
self.data.append(line[2:-1])
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
image = cv2.resize(imageio.imread(os.path.join(self.data_root, self.data[idx])), (self.input_size, self.input_size))
image = preprocess(image, self.input_size, self.augment)
gender = np.array(int(self.data[idx][5]) - 1, dtype=np.int64)
age = np.array(int(self.data[idx][:2]), dtype=np.float32)
return image, gender, age
def preprocess(image, input_size, augmentation=True):
if augmentation:
crop_transform = transforms.Compose([
transforms.Resize(input_size // 4 * 5),
transforms.RandomHorizontalFlip(0.5),
transforms.RandomCrop(input_size),
transforms.RandomRotation(10)])
else:
crop_transform = transforms.CenterCrop(input_size)
result = transforms.Compose([
transforms.ToPILImage(),
crop_transform,
transforms.ToTensor(),
])(image)
return result