forked from mk-minchul/AdaFace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
330 lines (277 loc) · 14.5 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
import os
import torchvision.datasets as datasets
import pytorch_lightning as pl
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from torchvision.transforms import functional as F
import numpy as np
import torch
from PIL import Image
import cv2
import pandas as pd
import evaluate_utils
class DataModule(pl.LightningDataModule):
def __init__(self, **kwargs):
super().__init__()
self.data_root = kwargs['data_root']
self.train_data_path = kwargs['train_data_path']
self.val_data_path = kwargs['val_data_path']
self.batch_size = kwargs['batch_size']
self.num_workers = kwargs['num_workers']
self.train_data_subset = kwargs['train_data_subset']
self.low_res_augmentation_prob = kwargs['low_res_augmentation_prob']
self.crop_augmentation_prob = kwargs['crop_augmentation_prob']
self.photometric_augmentation_prob = kwargs['photometric_augmentation_prob']
concat_mem_file_name = os.path.join(self.data_root, self.val_data_path, 'concat_validation_memfile')
self.concat_mem_file_name = concat_mem_file_name
def prepare_data(self):
# call this once to convert val_data to memfile for saving memory
if not os.path.isdir(os.path.join(self.data_root, self.val_data_path, 'agedb_30', 'memfile')):
print('making validation data memfile')
evaluate_utils.get_val_data(os.path.join(self.data_root, self.val_data_path))
if not os.path.isfile(self.concat_mem_file_name):
# create a concat memfile
concat = []
for key in ['agedb_30', 'cfp_fp', 'lfw', 'cplfw', 'calfw']:
np_array, issame = evaluate_utils.get_val_pair(path=os.path.join(self.data_root, self.val_data_path),
name=key,
use_memfile=False)
concat.append(np_array)
concat = np.concatenate(concat)
evaluate_utils.make_memmap(self.concat_mem_file_name, concat)
def setup(self, stage=None):
# Assign Train/val split(s) for use in Dataloaders
if stage == 'fit' or stage is None:
print('creating train dataset')
self.train_dataset = train_dataset(self.data_root,
self.train_data_path,
self.low_res_augmentation_prob,
self.crop_augmentation_prob,
self.photometric_augmentation_prob,
)
# checking same list for subseting
if self.train_data_path == 'faces_emore/imgs' and self.train_data_subset:
with open('ms1mv2_train_subset_index.txt', 'r') as f:
subset_index = [int(i) for i in f.read().split(',')]
# remove too few example identites
self.train_dataset.samples = [self.train_dataset.samples[idx] for idx in subset_index]
self.train_dataset.targets = [self.train_dataset.targets[idx] for idx in subset_index]
value_counts = pd.Series(self.train_dataset.targets).value_counts()
to_erase_label = value_counts[value_counts<5].index
e_idx = [i in to_erase_label for i in self.train_dataset.targets]
self.train_dataset.samples = [i for i, erase in zip(self.train_dataset.samples, e_idx) if not erase]
self.train_dataset.targets = [i for i, erase in zip(self.train_dataset.targets, e_idx) if not erase]
# label adjust
max_label = np.max(self.train_dataset.targets)
adjuster = {}
new = 0
for orig in range(max_label+1):
if orig in to_erase_label:
continue
adjuster[orig] = new
new += 1
# readjust class_to_idx
self.train_dataset.targets = [adjuster[orig] for orig in self.train_dataset.targets]
self.train_dataset.samples = [(sample[0], adjuster[sample[1]]) for sample in self.train_dataset.samples]
new_class_to_idx = {}
for label_str, label_int in self.train_dataset.class_to_idx.items():
if label_int in to_erase_label:
continue
else:
new_class_to_idx[label_str] = adjuster[label_int]
self.train_dataset.class_to_idx = new_class_to_idx
print('creating val dataset')
self.val_dataset = val_dataset(self.data_root, self.val_data_path, self.concat_mem_file_name)
# Assign Test split(s) for use in Dataloaders
if stage == 'test' or stage is None:
self.test_dataset = test_dataset(self.data_root, self.val_data_path, self.concat_mem_file_name)
def train_dataloader(self):
return DataLoader(self.train_dataset, batch_size=self.batch_size, num_workers=self.num_workers, shuffle=True)
def val_dataloader(self):
return DataLoader(self.val_dataset, batch_size=self.batch_size, num_workers=self.num_workers, shuffle=False)
def test_dataloader(self):
return DataLoader(self.test_dataset, batch_size=self.batch_size, num_workers=self.num_workers, shuffle=False)
def train_dataset(data_root, train_data_path,
low_res_augmentation_prob,
crop_augmentation_prob,
photometric_augmentation_prob):
train_dir = os.path.join(data_root, train_data_path)
train_dataset = CustomImageFolderDataset(root=train_dir,
transform=transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
]),
low_res_augmentation_prob=low_res_augmentation_prob,
crop_augmentation_prob=crop_augmentation_prob,
photometric_augmentation_prob=photometric_augmentation_prob,
)
return train_dataset
def val_dataset(data_root, val_data_path, concat_mem_file_name):
val_data = evaluate_utils.get_val_data(os.path.join(data_root, val_data_path))
# theses datasets are already normalized with mean 0.5, std 0.5
age_30, cfp_fp, lfw, age_30_issame, cfp_fp_issame, lfw_issame, cplfw, cplfw_issame, calfw, calfw_issame = val_data
val_data_dict = {
'agedb_30': (age_30, age_30_issame),
"cfp_fp": (cfp_fp, cfp_fp_issame),
"lfw": (lfw, lfw_issame),
"cplfw": (cplfw, cplfw_issame),
"calfw": (calfw, calfw_issame),
}
val_dataset = FiveValidationDataset(val_data_dict, concat_mem_file_name)
return val_dataset
def test_dataset(data_root, val_data_path, concat_mem_file_name):
val_data = evaluate_utils.get_val_data(os.path.join(data_root, val_data_path))
# theses datasets are already normalized with mean 0.5, std 0.5
age_30, cfp_fp, lfw, age_30_issame, cfp_fp_issame, lfw_issame, cplfw, cplfw_issame, calfw, calfw_issame = val_data
val_data_dict = {
'agedb_30': (age_30, age_30_issame),
"cfp_fp": (cfp_fp, cfp_fp_issame),
"lfw": (lfw, lfw_issame),
"cplfw": (cplfw, cplfw_issame),
"calfw": (calfw, calfw_issame),
}
val_dataset = FiveValidationDataset(val_data_dict, concat_mem_file_name)
return val_dataset
class CustomImageFolderDataset(datasets.ImageFolder):
def __init__(self,
root,
transform=None,
target_transform=None,
loader=datasets.folder.default_loader,
is_valid_file=None,
low_res_augmentation_prob=0.0,
crop_augmentation_prob=0.0,
photometric_augmentation_prob=0.0,
):
super(CustomImageFolderDataset, self).__init__(root,
transform=transform,
target_transform=target_transform,
loader=loader,
is_valid_file=is_valid_file)
self.root = root
self.low_res_augmentation_prob = low_res_augmentation_prob
self.crop_augmentation_prob = crop_augmentation_prob
self.photometric_augmentation_prob = photometric_augmentation_prob
self.random_resized_crop = transforms.RandomResizedCrop(size=(112, 112),
scale=(0.2, 1.0),
ratio=(0.75, 1.3333333333333333))
self.photometric = transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0)
self.tot_rot_try = 0
self.rot_success = 0
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (sample, target) where target is class_index of the target class.
"""
path, target = self.samples[index]
sample = self.loader(path)
if 'WebFace' in self.root:
# swap rgb to bgr since image is in rgb for webface
sample = Image.fromarray(np.asarray(sample)[:,:,::-1])
sample, _ = self.augment(sample)
if self.transform is not None:
sample = self.transform(sample)
if self.target_transform is not None:
target = self.target_transform(target)
return sample, target
def augment(self, sample):
# crop with zero padding augmentation
if np.random.random() < self.crop_augmentation_prob:
# RandomResizedCrop augmentation
new = np.zeros_like(np.array(sample))
orig_W, orig_H = F._get_image_size(sample)
i, j, h, w = self.random_resized_crop.get_params(sample,
self.random_resized_crop.scale,
self.random_resized_crop.ratio)
cropped = F.crop(sample, i, j, h, w)
new[i:i+h,j:j+w, :] = np.array(cropped)
sample = Image.fromarray(new.astype(np.uint8))
crop_ratio = min(h, w) / max(orig_H, orig_W)
else:
crop_ratio = 1.0
# low resolution augmentation
if np.random.random() < self.low_res_augmentation_prob:
# low res augmentation
img_np, resize_ratio = low_res_augmentation(np.array(sample))
sample = Image.fromarray(img_np.astype(np.uint8))
else:
resize_ratio = 1
# photometric augmentation
if np.random.random() < self.photometric_augmentation_prob:
fn_idx, brightness_factor, contrast_factor, saturation_factor, hue_factor = \
self.photometric.get_params(self.photometric.brightness, self.photometric.contrast,
self.photometric.saturation, self.photometric.hue)
for fn_id in fn_idx:
if fn_id == 0 and brightness_factor is not None:
sample = F.adjust_brightness(sample, brightness_factor)
elif fn_id == 1 and contrast_factor is not None:
sample = F.adjust_contrast(sample, contrast_factor)
elif fn_id == 2 and saturation_factor is not None:
sample = F.adjust_saturation(sample, saturation_factor)
information_score = resize_ratio * crop_ratio
return sample, information_score
class FiveValidationDataset(Dataset):
def __init__(self, val_data_dict, concat_mem_file_name):
'''
concatenates all validation datasets from emore
val_data_dict = {
'agedb_30': (agedb_30, agedb_30_issame),
"cfp_fp": (cfp_fp, cfp_fp_issame),
"lfw": (lfw, lfw_issame),
"cplfw": (cplfw, cplfw_issame),
"calfw": (calfw, calfw_issame),
}
agedb_30: 0
cfp_fp: 1
lfw: 2
cplfw: 3
calfw: 4
'''
self.dataname_to_idx = {"agedb_30": 0, "cfp_fp": 1, "lfw": 2, "cplfw": 3, "calfw": 4}
self.val_data_dict = val_data_dict
# concat all dataset
all_imgs = []
all_issame = []
all_dataname = []
key_orders = []
for key, (imgs, issame) in val_data_dict.items():
all_imgs.append(imgs)
dup_issame = [] # hacky way to make the issame length same as imgs. [1, 1, 0, 0, ...]
for same in issame:
dup_issame.append(same)
dup_issame.append(same)
all_issame.append(dup_issame)
all_dataname.append([self.dataname_to_idx[key]] * len(imgs))
key_orders.append(key)
assert key_orders == ['agedb_30', 'cfp_fp', 'lfw', 'cplfw', 'calfw']
if isinstance(all_imgs[0], np.memmap):
self.all_imgs = evaluate_utils.read_memmap(concat_mem_file_name)
else:
self.all_imgs = np.concatenate(all_imgs)
self.all_issame = np.concatenate(all_issame)
self.all_dataname = np.concatenate(all_dataname)
assert len(self.all_imgs) == len(self.all_issame)
assert len(self.all_issame) == len(self.all_dataname)
def __getitem__(self, index):
x_np = self.all_imgs[index].copy()
x = torch.tensor(x_np)
y = self.all_issame[index]
dataname = self.all_dataname[index]
return x, y, dataname, index
def __len__(self):
return len(self.all_imgs)
def low_res_augmentation(img):
# resize the image to a small size and enlarge it back
img_shape = img.shape
side_ratio = np.random.uniform(0.2, 1.0)
small_side = int(side_ratio * img_shape[0])
interpolation = np.random.choice(
[cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_AREA, cv2.INTER_CUBIC, cv2.INTER_LANCZOS4])
small_img = cv2.resize(img, (small_side, small_side), interpolation=interpolation)
interpolation = np.random.choice(
[cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_AREA, cv2.INTER_CUBIC, cv2.INTER_LANCZOS4])
aug_img = cv2.resize(small_img, (img_shape[1], img_shape[0]), interpolation=interpolation)
return aug_img, side_ratio