-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathload_data.py
More file actions
105 lines (83 loc) · 3.83 KB
/
Copy pathload_data.py
File metadata and controls
105 lines (83 loc) · 3.83 KB
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
import os
import numpy as np
import pandas as pd
class CelebA():
'''Wraps the celebA dataset, allowing an easy way to:
- Select the features of interest,
- Split the dataset into 'training', 'test' or 'validation' partition.
'''
def __init__(self, main_folder='../CelebA/', selected_features=None, drop_features=[]):
self.main_folder = main_folder
self.images_folder = os.path.join(main_folder, 'img_align_celeba/img_align_celeba/')
self.attributes_path = os.path.join(main_folder, 'list_attr_celeba.csv')
self.partition_path = os.path.join(main_folder, 'list_eval_partition.csv')
self.selected_features = selected_features
self.features_name = []
self.__prepare(drop_features)
def __prepare(self, drop_features):
'''do some preprocessing before using the data: e.g. feature selection'''
# attributes:
if self.selected_features is None:
self.attributes = pd.read_csv(self.attributes_path)
self.num_features = 40
else:
self.num_features = len(self.selected_features)
self.selected_features = self.selected_features.copy()
self.selected_features.append('image_id')
self.attributes = pd.read_csv(self.attributes_path)[self.selected_features]
for feature in drop_features:
if feature in self.attributes:
self.attributes = self.attributes.drop(feature, axis=1)
self.num_features -= 1
self.attributes.replace(to_replace=-1, value=0, inplace=True)
self.features_name = list(self.attributes.columns)[:-1]
# load ideal partitioning:
self.partition = pd.read_csv(self.partition_path)
def split(self, name='training', drop_zero=False):
'''Returns the ['training', 'validation', 'test'] split of the dataset'''
# select partition split:
if name is 'training':
to_drop = self.partition.where(lambda x: x != 0).dropna()
elif name is 'validation':
to_drop = self.partition.where(lambda x: x != 1).dropna()
elif name is 'test': # test
to_drop = self.partition.where(lambda x: x != 2).dropna()
else:
raise ValueError('CelebA.split() => `name` must be one of [training, validation, test]')
partition = self.partition.drop(index=to_drop.index)
# join attributes with selected partition:
joint = partition.join(self.attributes.drop('image_id', axis=1), how='inner').drop('partition', axis=1)
if drop_zero is True:
# select rows with all zeros values
return joint.loc[(joint[self.features_name] == 1).any(axis=1)]
elif 0 <= drop_zero <= 1:
zero = joint.loc[(joint[self.features_name] == 0).all(axis=1)]
zero = zero.sample(frac=drop_zero)
return joint.drop(index=zero.index)
return joint
def load_celeba(path):
data = np.load(os.path.join(path, "data.npy"))
data = data.astype(float)
data = data / 255.0 #0~1
return data.astype('float32')
def split(path):
celebA = CelebA(
main_folder = path,
drop_features=[
'Attractive',
'Pale_Skin',
'Blurry',
])
dataset = load_celeba(path)
features = pd.read_csv(os.path.join(path, "label_set.csv"), index_col='index')
train_set = celebA.split()
train_set = train_set[train_set.index.isin(features.index)]
test_set = celebA.split('test')
test_set = test_set[test_set.index.isin(features.index)]
labels = features['hair_style']
train_labels = labels[train_set.index]
test_labels = labels[test_set.index]
return (dataset[train_set.index], train_labels.to_numpy()), (dataset[test_set.index], test_labels.to_numpy())
if __name__ == "__main__":
test = load_celeba("../CelebA/")
print(len(test))