-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpreprocess.py
More file actions
213 lines (175 loc) · 6.37 KB
/
preprocess.py
File metadata and controls
213 lines (175 loc) · 6.37 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
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
import argparse
import os
#######
# CelebA attributes:
# ------
# 5_o_Clock_Shadow 1
# Arched_Eyebrows 2
# Attractive 3
# Bags_Under_Eyes 4
# Bald 5
# Bangs 6
# Big_Lips 7
# Big_Nose 8
# Black_Hair 9
# Blond_Hair 10
# Blurry 11
# Brown_Hair 12
# Bushy_Eyebrows 13
# Chubby 14
# Double_Chin 15
# Eyeglasses 16
# Goatee 17
# Gray_Hair 18
# Heavy_Makeup 19
# High_Cheekbones 20
# Male 21
# Mouth_Slightly_Open 22
# Mustache 23
# Narrow_Eyes 24
# No_Beard 25
# Oval_Face 26
# Pale_Skin 27
# Pointy_Nose 28
# Receding_Hairline 29
# Rosy_Cheeks 30
# Sideburns 31
# Smiling 32
# Straight_Hair 33
# Wavy_Hair 34
# Wearing_Earrings 35
# Wearing_Hat 36
# Wearing_Lipstick 37
# Wearing_Necklace 38
# Wearing_Necktie 39
# Young 40
#######
def preprocess_celeba(args):
if not os.path.exists(args.dest):
os.mkdir(args.dest)
allA = []
allB = []
with open(args.attributes) as f:
lines = f.readlines()
if args.config == 'beard_glasses':
for line in lines[2:]:
line = line.split()
if male_no_5_oclock(line) and beard(line) and (not glasses(line)):
allA.append(line[0])
elif male_no_5_oclock(line) and (not beard(line)) and glasses(line):
allB.append(line[0])
if args.config == 'beard_smile':
for line in lines[2:]:
line = line.split()
if male_no_5_oclock(line) and beard(line) and (not smile(line)):
allA.append(line[0])
elif male_no_5_oclock(line) and (not beard(line)) and smile(line):
allB.append(line[0])
if args.config == "smile_glasses":
for line in lines[2:]:
line = line.split()
if smile(line) and (not glasses(line)):
allA.append(line[0])
elif (not smile(line)) and glasses(line):
allB.append(line[0])
if args.config == "male_female":
for line in lines[2:]:
line = line.split()
if int(line[21]) == 1:
allA.append(line[0])
else:
allB.append(line[0])
if args.config == "blond_black":
for line in lines[2:]:
line = line.split()
if blonde_hair(line) and (not hat(line)):
allA.append(line[0])
elif black_hair(line) and (not hat(line)):
allB.append(line[0])
testA = allA[:args.num_test_imgs]
testB = allB[:args.num_test_imgs]
trainA = allA[args.num_test_imgs:]
trainB = allB[args.num_test_imgs:]
with open(os.path.join(args.dest, 'testA.txt'), 'w') as f:
for i, _img in enumerate(testA):
if i == len(testA) - 1:
f.write("%s" % os.path.join(args.root, _img))
else:
f.write("%s\n" % os.path.join(args.root, _img))
with open(os.path.join(args.dest, 'testB.txt'), 'w') as f:
for i, _img in enumerate(testB):
if i == len(testB) - 1:
f.write("%s" % os.path.join(args.root, _img))
else:
f.write("%s\n" % os.path.join(args.root, _img))
with open(os.path.join(args.dest, 'trainA.txt'), 'w') as f:
for i, _img in enumerate(trainA):
if i == len(trainA) - 1:
f.write("%s" % os.path.join(args.root, _img))
else:
f.write("%s\n" % os.path.join(args.root, _img))
with open(os.path.join(args.dest, 'trainB.txt'), 'w') as f:
for i, _img in enumerate(trainB):
if i == len(trainB) - 1:
f.write("%s" % os.path.join(args.root, _img))
else:
f.write("%s\n" % os.path.join(args.root, _img))
def male_no_5_oclock(line):
return int(line[21]) == 1 and int(line[1]) == -1
def beard(line):
return int(line[23]) == 1 or int(line[17]) == 1 or int(line[25]) == -1
def glasses(line):
return int(line[16]) == 1
def smile(line):
return int(line[32]) == 1
def blonde_hair(line):
return int(line[10]) == 1
def black_hair(line):
return int(line[9]) == 1
def preprocess_folders(args):
if not os.path.exists(args.dest):
os.mkdir(args.dest)
trainA = os.listdir(os.path.join(args.root, 'trainA'))
trainB = os.listdir(os.path.join(args.root, 'trainB'))
testA = os.listdir(os.path.join(args.root, 'testA'))
testB = os.listdir(os.path.join(args.root, 'testB'))
with open(os.path.join(args.dest, 'testA.txt'), 'w') as f:
for i, _img in enumerate(testA):
if i == len(testA) - 1:
f.write("%s" % os.path.join(args.root, _img))
else:
f.write("%s\n" % os.path.join(args.root, _img))
with open(os.path.join(args.dest, 'testB.txt'), 'w') as f:
for i, _img in enumerate(testB):
if i == len(testB) - 1:
f.write("%s" % os.path.join(args.root, _img))
else:
f.write("%s\n" % os.path.join(args.root, _img))
with open(os.path.join(args.dest, 'trainA.txt'), 'w') as f:
for i, _img in enumerate(trainA):
if i == len(trainA) - 1:
f.write("%s" % os.path.join(args.root, _img))
else:
f.write("%s\n" % os.path.join(args.root, _img))
with open(os.path.join(args.dest, 'trainB.txt'), 'w') as f:
for i, _img in enumerate(trainB):
if i == len(trainB) - 1:
f.write("%s" % os.path.join(args.root, _img))
else:
f.write("%s\n" % os.path.join(args.root, _img))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--root", default="",
help="path to the celeba folder, or if you\'re using another dataset this should be the path to the root")
parser.add_argument("--dest", default="", help="path to the destination folder")
parser.add_argument("--attributes", default="", help="path to the attributes file")
parser.add_argument("--num_test_imgs", default=64, help="number of images in the test set")
parser.add_argument("--config", default="smile_glasses", help="configs available: glasses, mouth, beard")
parser.add_argument("--custom", default=32, help="use a custom celeba attribute")
parser.add_argument("--folders", action="store_true",
help="use custom folders, instead of celeba")
args = parser.parse_args()
if not args.folders:
preprocess_celeba(args)
else:
preprocess_folders(args)