-
Notifications
You must be signed in to change notification settings - Fork 124
/
data.py
188 lines (157 loc) · 8.9 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
import os
import pytorch_lightning as pl
from torch.utils.data import DataLoader
from torchvision import transforms
import numpy as np
import pandas as pd
import evaluate_utils
from dataset.image_folder_dataset import CustomImageFolderDataset
from dataset.five_validation_dataset import FiveValidationDataset
from dataset.record_dataset import AugmentRecordDataset
class DataModule(pl.LightningDataModule):
def __init__(self, **kwargs):
super().__init__()
self.output_dir = kwargs['output_dir']
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']
self.swap_color_channel = kwargs['swap_color_channel']
self.use_mxrecord = kwargs['use_mxrecord']
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,
self.swap_color_channel,
self.use_mxrecord,
self.output_dir
)
if 'faces_emore' in self.train_data_path and self.train_data_subset:
# subset ms1mv2 dataset for reproducing the same setup in AdaFace ablation experiments.
with open('assets/ms1mv2_train_subset_index.txt', 'r') as f:
subset_index = [int(i) for i in f.read().split(',')]
self.subset_ms1mv2_dataset(subset_index)
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 subset_ms1mv2_dataset(self, subset_index):
# 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
def train_dataset(data_root, train_data_path,
low_res_augmentation_prob,
crop_augmentation_prob,
photometric_augmentation_prob,
swap_color_channel,
use_mxrecord,
output_dir):
train_transform = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
if use_mxrecord:
train_dir = os.path.join(data_root, train_data_path)
train_dataset = AugmentRecordDataset(root_dir=train_dir,
transform=train_transform,
low_res_augmentation_prob=low_res_augmentation_prob,
crop_augmentation_prob=crop_augmentation_prob,
photometric_augmentation_prob=photometric_augmentation_prob,
swap_color_channel=swap_color_channel,
output_dir=output_dir)
else:
train_dir = os.path.join(data_root, train_data_path, 'imgs')
train_dataset = CustomImageFolderDataset(root=train_dir,
transform=train_transform,
low_res_augmentation_prob=low_res_augmentation_prob,
crop_augmentation_prob=crop_augmentation_prob,
photometric_augmentation_prob=photometric_augmentation_prob,
swap_color_channel=swap_color_channel,
output_dir=output_dir
)
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