-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
241 lines (207 loc) · 10.5 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import numpy as np
import random
from PIL import Image
from torch.utils.data import Dataset
import os
def make_dataset(image_list, label_list, au_relation=None):
len_ = len(image_list)
if au_relation is not None:
images = [(image_list[i].strip(), label_list[i, :],au_relation[i,:]) for i in range(len_)]
else:
if len(label_list.shape) == 1:
images = [(image_list[i].strip(), label_list[i]) for i in range(len_)]
else:
images = [(image_list[i].strip(), label_list[i, :]) for i in range(len_)]
return images
def pil_loader(path):
with open(path, 'rb') as f:
with Image.open(f) as img:
return img.convert('RGB')
def default_loader(path):
return pil_loader(path)
class UNBC(Dataset):
def __init__(self, root_path, train=True, fold = 1, transform=None, crop_size = 172, stage=1, loader=default_loader):
assert fold>0 and fold <=3, 'The fold num must be restricted from 1 to 3'
assert stage>0 and stage <=3, 'The stage num must be restricted from 1 to 3'
self._root_path = root_path
self._train = train
self._stage = stage
self._transform = transform
self.crop_size = crop_size
self.loader = loader
self.img_folder_path = os.path.join(root_path,'img' if crop_size == 172 else 'resized_img')
if self._train:
# img
train_image_list_path = os.path.join(root_path, 'list', 'UNBC_train_img_path_fold' + str(fold) +'.txt')
train_image_list = open(train_image_list_path).readlines()
# img labels
if self._stage == 3:
train_label_list_path = os.path.join(root_path, 'list', 'UNBC_train_pspi_fold' + str(fold) + '.txt')
else:
train_label_list_path = os.path.join(root_path, 'list', 'UNBC_train_label_fold' + str(fold) + '.txt')
train_label_list = np.loadtxt(train_label_list_path)
# AU relation
if self._stage == 2:
au_relation_list_path = os.path.join(root_path, 'list', 'UNBC_train_AU_relation_fold' + str(fold) + '.txt')
au_relation_list = np.loadtxt(au_relation_list_path)
self.data_list = make_dataset(train_image_list, train_label_list, au_relation_list)
else:
self.data_list = make_dataset(train_image_list, train_label_list)
else:
# img
test_image_list_path = os.path.join(root_path, 'list', 'UNBC_test_img_path_fold' + str(fold) + '.txt')
test_image_list = open(test_image_list_path).readlines()
# img labels
if self._stage == 3:
test_label_list_path = os.path.join(root_path, 'list', 'UNBC_test_pspi_fold' + str(fold) + '.txt')
else:
test_label_list_path = os.path.join(root_path, 'list', 'UNBC_test_label_fold' + str(fold) + '.txt')
test_label_list = np.loadtxt(test_label_list_path)
self.data_list = make_dataset(test_image_list, test_label_list)
def __getitem__(self, index):
if self._stage == 2 and self._train:
img, label, au_relation = self.data_list[index]
img = self.loader(os.path.join(self.img_folder_path, img))
w, h = img.size
offset_y = random.randint(0, h - self.crop_size)
offset_x = random.randint(0, w - self.crop_size)
flip = random.randint(0, 1)
if self._transform is not None:
img = self._transform(img, flip, offset_x, offset_y)
return img, label, au_relation
else:
img, label = self.data_list[index]
img = self.loader(os.path.join(self.img_folder_path, img))
if self._train:
w, h = img.size
offset_y = random.randint(0, h - self.crop_size)
offset_x = random.randint(0, w - self.crop_size)
flip = random.randint(0, 1)
if self._transform is not None:
img = self._transform(img, flip, offset_x, offset_y)
else:
if self._transform is not None:
img = self._transform(img)
return img, label
def __len__(self):
return len(self.data_list)
class BP4D(Dataset):
def __init__(self, root_path, train=True, fold = 1, transform=None, crop_size = 224, stage=1, loader=default_loader):
assert fold>0 and fold <=3, 'The fold num must be restricted from 1 to 3'
assert stage>0 and stage <=2, 'The stage num must be restricted from 1 to 2'
self._root_path = root_path
self._train = train
self._stage = stage
self._transform = transform
self.crop_size = crop_size
self.loader = loader
self.img_folder_path = os.path.join(root_path,'img')
if self._train:
# img
train_image_list_path = os.path.join(root_path, 'list', 'BP4D_train_img_path_fold' + str(fold) +'.txt')
train_image_list = open(train_image_list_path).readlines()
# img labels
train_label_list_path = os.path.join(root_path, 'list', 'BP4D_train_label_fold' + str(fold) + '.txt')
train_label_list = np.loadtxt(train_label_list_path)
# AU relation
if self._stage == 2:
au_relation_list_path = os.path.join(root_path, 'list', 'BP4D_train_AU_relation_fold' + str(fold) + '.txt')
au_relation_list = np.loadtxt(au_relation_list_path)
self.data_list = make_dataset(train_image_list, train_label_list, au_relation_list)
else:
self.data_list = make_dataset(train_image_list, train_label_list)
else:
# img
test_image_list_path = os.path.join(root_path, 'list', 'BP4D_test_img_path_fold' + str(fold) + '.txt')
test_image_list = open(test_image_list_path).readlines()
# img labels
test_label_list_path = os.path.join(root_path, 'list', 'BP4D_test_label_fold' + str(fold) + '.txt')
test_label_list = np.loadtxt(test_label_list_path)
self.data_list = make_dataset(test_image_list, test_label_list)
def __getitem__(self, index):
if self._stage == 2 and self._train:
img, label, au_relation = self.data_list[index]
img = self.loader(os.path.join(self.img_folder_path, img))
w, h = img.size
offset_y = random.randint(0, h - self.crop_size)
offset_x = random.randint(0, w - self.crop_size)
flip = random.randint(0, 1)
if self._transform is not None:
img = self._transform(img, flip, offset_x, offset_y)
return img, label, au_relation
else:
img, label = self.data_list[index]
img = self.loader(os.path.join(self.img_folder_path, img))
if self._train:
w, h = img.size
offset_y = random.randint(0, h - self.crop_size)
offset_x = random.randint(0, w - self.crop_size)
flip = random.randint(0, 1)
if self._transform is not None:
img = self._transform(img, flip, offset_x, offset_y)
else:
if self._transform is not None:
img = self._transform(img)
return img, label
def __len__(self):
return len(self.data_list)
class DISFA(Dataset):
def __init__(self, root_path, train=True, fold = 1, transform=None, crop_size = 224, stage=1, loader=default_loader):
assert fold>0 and fold <=3, 'The fold num must be restricted from 1 to 3'
assert stage>0 and stage <=2, 'The stage num must be restricted from 1 to 2'
self._root_path = root_path
self._train = train
self._stage = stage
self._transform = transform
self.crop_size = crop_size
self.loader = loader
self.img_folder_path = os.path.join(root_path,'img')
if self._train:
# img
train_image_list_path = os.path.join(root_path, 'list', 'DISFA_train_img_path_fold' + str(fold) + '.txt')
train_image_list = open(train_image_list_path).readlines()
# img labels
train_label_list_path = os.path.join(root_path, 'list', 'DISFA_train_label_fold' + str(fold) + '.txt')
train_label_list = np.loadtxt(train_label_list_path)
# AU relation
if self._stage == 2:
au_relation_list_path = os.path.join(root_path, 'list', 'DISFA_train_AU_relation_fold' + str(fold) + '.txt')
au_relation_list = np.loadtxt(au_relation_list_path)
self.data_list = make_dataset(train_image_list, train_label_list, au_relation_list)
else:
self.data_list = make_dataset(train_image_list, train_label_list)
else:
# img
test_image_list_path = os.path.join(root_path, 'list', 'DISFA_test_img_path_fold' + str(fold) + '.txt')
test_image_list = open(test_image_list_path).readlines()
# img labels
test_label_list_path = os.path.join(root_path, 'list', 'DISFA_test_label_fold' + str(fold) + '.txt')
test_label_list = np.loadtxt(test_label_list_path)
self.data_list = make_dataset(test_image_list, test_label_list)
def __getitem__(self, index):
if self._stage == 2 and self._train:
img, label, au_relation = self.data_list[index]
img = self.loader(os.path.join(self.img_folder_path, img))
w, h = img.size
offset_y = random.randint(0, h - self.crop_size)
offset_x = random.randint(0, w - self.crop_size)
flip = random.randint(0, 1)
if self._transform is not None:
img = self._transform(img, flip, offset_x, offset_y)
return img, label, au_relation
else:
img, label = self.data_list[index]
img = self.loader(os.path.join(self.img_folder_path,img))
if self._train:
w, h = img.size
offset_y = random.randint(0, h - self.crop_size)
offset_x = random.randint(0, w - self.crop_size)
flip = random.randint(0, 1)
if self._transform is not None:
img = self._transform(img, flip, offset_x, offset_y)
else:
if self._transform is not None:
img = self._transform(img)
return img, label
def __len__(self):
return len(self.data_list)