forked from xucong-zhang/ETH-XGaze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_loader.py
143 lines (113 loc) · 5.29 KB
/
data_loader.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import numpy as np
import h5py
import torch
from torchvision import transforms
from torch.utils.data import Dataset, DataLoader
import os
import json
import random
from typing import List
trans_train = transforms.Compose([
transforms.ToPILImage(),
transforms.ToTensor(), # this also convert pixel value from [0,255] to [0,1]
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
trans = transforms.Compose([
transforms.ToPILImage(),
transforms.ToTensor(), # this also convert pixel value from [0,255] to [0,1]
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
def get_train_loader(data_dir,
batch_size,
num_workers=4,
is_shuffle=True):
# load dataset
refer_list_file = os.path.join(data_dir, 'train_test_split.json')
print('load the train file list from: ', refer_list_file)
with open(refer_list_file, 'r') as f:
datastore = json.load(f)
# there are three subsets for ETH-XGaze dataset: train, test and test_person_specific
# train set: the training set includes 80 participants data
# test set: the test set for cross-dataset and within-dataset evaluations
# test_person_specific: evaluation subset for the person specific setting
sub_folder_use = 'train'
train_set = GazeDataset(dataset_path=data_dir, keys_to_use=datastore[sub_folder_use], sub_folder=sub_folder_use,
transform=trans, is_shuffle=is_shuffle, is_load_label=True)
train_loader = DataLoader(train_set, batch_size=batch_size, num_workers=num_workers)
return train_loader
def get_test_loader(data_dir,
batch_size,
num_workers=4,
is_shuffle=True):
# load dataset
refer_list_file = os.path.join(data_dir, 'train_test_split.json')
print('load the train file list from: ', refer_list_file)
with open(refer_list_file, 'r') as f:
datastore = json.load(f)
# there are three subsets for ETH-XGaze dataset: train, test and test_person_specific
# train set: the training set includes 80 participants data
# test set: the test set for cross-dataset and within-dataset evaluations
# test_person_specific: evaluation subset for the person specific setting
sub_folder_use = 'test'
test_set = GazeDataset(dataset_path=data_dir, keys_to_use=datastore[sub_folder_use], sub_folder=sub_folder_use,
transform=trans, is_shuffle=is_shuffle, is_load_label=False)
test_loader = DataLoader(test_set, batch_size=batch_size, num_workers=num_workers)
return test_loader
class GazeDataset(Dataset):
def __init__(self, dataset_path: str, keys_to_use: List[str] = None, sub_folder='', transform=None, is_shuffle=True,
index_file=None, is_load_label=True):
self.path = dataset_path
self.hdfs = {}
self.sub_folder = sub_folder
self.is_load_label = is_load_label
# assert len(set(keys_to_use) - set(all_keys)) == 0
# Select keys
# TODO: select only people with sufficient entries?
self.selected_keys = [k for k in keys_to_use]
assert len(self.selected_keys) > 0
for num_i in range(0, len(self.selected_keys)):
file_path = os.path.join(self.path, self.sub_folder, self.selected_keys[num_i])
self.hdfs[num_i] = h5py.File(file_path, 'r', swmr=True)
# print('read file: ', os.path.join(self.path, self.selected_keys[num_i]))
assert self.hdfs[num_i].swmr_mode
# Construct mapping from full-data index to key and person-specific index
if index_file is None:
self.idx_to_kv = []
for num_i in range(0, len(self.selected_keys)):
n = self.hdfs[num_i]["face_patch"].shape[0]
self.idx_to_kv += [(num_i, i) for i in range(n)]
else:
print('load the file: ', index_file)
self.idx_to_kv = np.loadtxt(index_file, dtype=np.int)
for num_i in range(0, len(self.hdfs)):
if self.hdfs[num_i]:
self.hdfs[num_i].close()
self.hdfs[num_i] = None
if is_shuffle:
random.shuffle(self.idx_to_kv) # random the order to stable the training
self.hdf = None
self.transform = transform
def __len__(self):
return len(self.idx_to_kv)
def __del__(self):
for num_i in range(0, len(self.hdfs)):
if self.hdfs[num_i]:
self.hdfs[num_i].close()
self.hdfs[num_i] = None
def __getitem__(self, idx):
key, idx = self.idx_to_kv[idx]
self.hdf = h5py.File(os.path.join(self.path, self.sub_folder, self.selected_keys[key]), 'r', swmr=True)
assert self.hdf.swmr_mode
# Get face image
image = self.hdf['face_patch'][idx, :]
image = image[:, :, [2, 1, 0]] # from BGR to RGB
image = self.transform(image)
# Get labels
if self.is_load_label:
gaze_label = self.hdf['face_gaze'][idx, :]
gaze_label = gaze_label.astype('float')
return image, gaze_label
else:
return image