-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
367 lines (275 loc) · 10 KB
/
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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python3
# Author: Armit
# Create Time: 2023/12/27
import json
import random
from enum import Enum
import pandas as pd
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as T
from utils import *
DATA_PATH = BASE_PATH / 'data'
DATA_EMOTION6_PATH = DATA_PATH / 'Emotion6'
DATA_ABSTRACT_PATH = DATA_PATH / 'testImages_abstract'
DATA_ARTPHOTO_PATH = DATA_PATH / 'testImages_artphoto'
DATA_TWEETERI_PATH = DATA_PATH / 'Twitter_PCNN'
DATA_FI_PATH = DATA_PATH / 'emotion_dataset'
DATA_OASIS_PATH = DATA_PATH / 'OASIS_database_2016'
DATA_EMOSET_PATH = DATA_PATH / 'EmoSet-118K'
RESIZE = (224, 224)
IMAGENET_MEAN = (0.485, 0.456, 0.406)
IMAGENET_STD = (0.229, 0.224, 0.225)
# Just follow EmoSet: https://github.com/JingyuanYY/EmoSet/blob/main/EmoSet.py#L58
transform_train = T.Compose([
T.RandomResizedCrop(RESIZE),
T.RandomHorizontalFlip(),
T.ToTensor(),
T.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD)
])
transform_test = T.Compose([
T.Resize(RESIZE),
T.CenterCrop(RESIZE),
T.ToTensor(),
T.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD),
])
LABELS_MIKELS = ['anger', 'disgust', 'fear', 'amusement', 'sadness', 'excitement', 'contentment', 'awe']
LABELS_EKMAN = ['anger', 'disgust', 'fear', 'joy', 'sadness', 'surprise']
LABELS_EKMAN_N = ['anger', 'disgust', 'fear', 'joy', 'sadness', 'surprise', 'neutral']
LABELS_VA = ['valence', 'arousal']
LABELS_POLAR = ['neg', 'pos']
class HeadType(Enum):
Mikels = 'Mikels'
Ekman = 'Ekman'
EkmanN = 'EkmanN'
VA = 'VA'
Polar = 'Polar'
HEAD_CLASS_NAMES = {
# 'head_type': dim
'Mikels': LABELS_MIKELS, # 8
'EkmanN': LABELS_EKMAN_N, # 7
'Ekman': LABELS_EKMAN, # 6
'VA': LABELS_VA, # 2
'Polar': LABELS_POLAR, # 2
}
HEAD_DIMS = { k: len(v) for k, v in HEAD_CLASS_NAMES.items() }
HEAD_NAMES = list(HEAD_DIMS.keys())
is_clf = lambda head: head not in [HeadType.VA.value]
''' Base '''
class BaseDataset(Dataset):
root: Path = None
head: HeadType = None
is_ldl: bool = False
def __init__(self, split:str='train'):
assert split in ['train', 'valid', 'test']
self.split = split
self.img_root = self.root
self.transform = transform_train if split == 'train' else transform_test
self._metadata = []
@property
def class_names(self):
return HEAD_CLASS_NAMES[self.head.value]
@property
def metadata(self):
return self._metadata
def make_metadata(self, X:ndarray, Y:ndarray, split:str, split_ratio:float):
metadata = [(x, y) for x, y in zip(X, Y)]
import random
random.seed(114514)
random.shuffle(metadata)
cp = int(len(metadata) * split_ratio)
self._metadata = metadata[cp:] if split == 'train' else metadata[:cp]
def norm_VA(self, Y:ndarray) -> ndarray:
stats_fp = LOG_PATH / f'{self.__class__.__name__}_stats.npz'
if not stats_fp.exists():
stats_fp.parent.mkdir(exist_ok=True)
Y_avg = Y.mean(axis=0, keepdims=True)
Y_std = Y.std (axis=0, keepdims=True)
np.savez_compressed(stats_fp, avg=Y_avg, std=Y_std)
stats = np.load(stats_fp)
return (Y - stats['avg']) / stats['std']
def get_fps(self):
return [(self.img_root / mt[0]) for mt in self.metadata]
def __len__(self):
return len(self.metadata)
def shuffle(self):
random.shuffle(self._metadata)
''' EmoSet '''
class EmoSet(BaseDataset):
root = DATA_EMOSET_PATH
head = HeadType.Mikels
def __init__(self, split:str='train'):
super().__init__(split)
if self.split == 'valid': self.split = 'val'
assert self.split in ['train', 'val', 'test']
with open(self.root / f'{self.split}.json', 'r', encoding='utf-8') as fh:
self._metadata = json.load(fh)
def get_fps(self):
return [(self.root / mt[1]) for mt in self.metadata]
def __getitem__(self, idx:int):
lbl, rfp, annot = self.metadata[idx]
img = load_pil(self.root / rfp)
img = self.transform(img)
return img, self.class_names.index(lbl)
''' Emotion6 '''
class Emotion6BaseDataset(BaseDataset):
root = DATA_EMOTION6_PATH
def __init__(self, split:str='train'):
super().__init__(split)
self.img_root = self.root / 'images'
df = pd.read_csv(self.root / 'ground_truth.txt', sep='\t').to_numpy()
self.X = df[:, 0]
self.vals = df[:, 1:].astype(np.float32)
def get_img(self, idx:int) -> Tensor:
fn, _ = self.metadata[idx]
img = load_pil(self.img_root / fn)
img = self.transform(img)
return img
class Emotion6Dim7(Emotion6BaseDataset):
head = HeadType.EkmanN
is_ldl = True
def __init__(self, split:str='train', split_ratio:float=0.2):
super().__init__(split)
Y = self.vals[:, 2:9]
self.make_metadata(self.X, Y, split, split_ratio)
def __getitem__(self, idx:int):
img = self.get_img(idx)
_, prob = self.metadata[idx]
return img, prob
class Emotion6Dim6(Emotion6BaseDataset):
head = HeadType.Ekman
is_ldl = True
def __init__(self, split:str='train', split_ratio:float=0.2):
super().__init__(split)
Y = self.vals[:, 2:8]
Y /= Y.sum(axis=-1, keepdims=True) # re-norm to 1
self.make_metadata(self.X, Y, split, split_ratio)
def __getitem__(self, idx:int):
img = self.get_img(idx)
_, prob = self.metadata[idx]
return img, prob
class Emotion6VA(Emotion6BaseDataset):
head = HeadType.VA
def __init__(self, split:str='train', split_ratio:float=0.2):
super().__init__(split)
Y = self.norm_VA(self.vals[:, :2])
self.make_metadata(self.X, Y, split, split_ratio)
def __getitem__(self, idx:int):
img = self.get_img(idx)
_, va = self.metadata[idx]
return img, va
''' Abstract & ArtPhoto '''
class Abstract(BaseDataset):
root = DATA_ABSTRACT_PATH
head = HeadType.Mikels
is_ldl = True
def __init__(self, split:str='train', split_ratio:float=0.2):
super().__init__(split)
original_class_names = ['Amusement', 'Anger', 'Awe', 'Content', 'Disgust', 'Excitement', 'Fear', 'Sad']
tmp_class_names = [e.lower().replace('content', 'contentment').replace('sad', 'sadness') for e in original_class_names]
df = pd.read_csv(self.root / 'ABSTRACT_groundTruth.csv').to_numpy()
X, Y = df[:, 0], df[:, 1:].astype(np.float32)
X = [fn.strip("'") for fn in X]
Y = Y[:, [tmp_class_names.index(e) for e in self.class_names]] # re-order for mapping
Y /= Y.sum(axis=-1, keepdims=True) # freq to prob
self.make_metadata(X, Y, split, split_ratio)
def __getitem__(self, idx:int) -> int:
fn, prob = self.metadata[idx]
img = load_pil(self.img_root / fn)
img = self.transform(img)
return img, prob
class ArtPhoto(BaseDataset):
root = DATA_ARTPHOTO_PATH
head = HeadType.Mikels
def __init__(self, split:str='train', split_ratio:float=0.2):
super().__init__(split)
X = [fp.name for fp in self.root.iterdir() if fp.suffix == '.jpg']
Y = [self.class_names.index(x.split('_')[0].replace('sad', 'sadness')) for x in X]
self.make_metadata(X, Y, split, split_ratio)
def __getitem__(self, idx:int) -> int:
fn, lbl = self.metadata[idx]
img = load_pil(self.img_root / fn)
img = self.transform(img)
return img, lbl
''' Tweeter-I & FI-23k (Flickr-Instagram) '''
class TweeterI(BaseDataset):
root = DATA_TWEETERI_PATH
head = HeadType.Polar
is_ldl = True
def __init__(self, split:str='train', split_ratio:float=0.2):
super().__init__(split)
self.img_root = self.root / 'Agg_AMT_Candidates'
df = pd.read_csv(self.root / 'amt_result.csv').to_numpy()
X = df[:, 0]
Y = np.stack([df[:, 2] / df[:, 1], df[:, 3] / df[:, 1]], axis=-1).astype(np.float32)
self.make_metadata(X, Y, split, split_ratio)
def __getitem__(self, idx:int) -> int:
fn, prob = self.metadata[idx]
img = load_pil(self.img_root / fn)
img = self.transform(img)
return img, prob
class FI(BaseDataset):
root = DATA_FI_PATH
head = HeadType.Mikels
def __init__(self, split:str='train', split_ratio:float=0.2):
super().__init__(split)
X, Y = [], []
for emo_dp in self.root.iterdir():
for fp in emo_dp.iterdir():
X.append(fp)
Y.append(self.class_names.index(emo_dp.name))
self.make_metadata(X, Y, split, split_ratio)
def get_fps(self):
return [mt[0] for mt in self._metadata]
def __getitem__(self, idx:int) -> int:
fp, prob = self.metadata[idx]
img = load_pil(fp)
img = self.transform(img)
return img, prob
''' others '''
class OASIS(BaseDataset):
root = DATA_OASIS_PATH
head = HeadType.VA
def __init__(self, split:str='train', split_ratio:float=0.2):
super().__init__(split)
self.img_root = self.root / 'images'
df = pd.read_csv(self.root / 'OASIS.csv')
X = [f'{name.strip()}.jpg' for name in df['Theme']]
Y = np.stack([
df['Valence_mean'].to_numpy(),
df['Arousal_mean'].to_numpy(),
], axis=-1).astype(np.float32)
Y = self.norm_VA(Y)
self.make_metadata(X, Y, split, split_ratio)
def __getitem__(self, idx:int) -> int:
fn, va = self.metadata[idx]
img = load_pil(self.img_root / fn)
img = self.transform(img)
return img, va
DATASETS = [k for k, v in globals().items() if type(v) == type(BaseDataset) and issubclass(v, BaseDataset) and v not in [BaseDataset, Emotion6BaseDataset]]
def get_dataset_cls(name:str) -> 'BaseDataset':
assert name in DATASETS
return globals()[name]
if __name__ == '__main__':
keys = list(globals().keys())
for k in keys:
v = globals()[k]
if v in [BaseDataset, Emotion6BaseDataset]: continue
if not type(v) == type(BaseDataset): continue
if not issubclass(v, BaseDataset): continue
try:
dataset: BaseDataset = v('train')
print(f'>> [{k}] len={len(dataset)} ({dataset.head.value})')
for x, y in iter(dataset):
print(f' x.shape: {tuple(x.shape)}')
if isinstance(y, int):
print(f' y: {y}')
elif isinstance(y, ndarray):
print(f' y.shape: {tuple(y.shape)}' if isinstance(y, ndarray) else f' y: {y}')
print(f' y: {[round(e, 4) for e in y]}, sum: {sum(y):.4f}')
else: raise TypeError(type(y))
break
print()
except:
from traceback import print_exc
print_exc()
print(f'>> [{k}] failed!')