-
Notifications
You must be signed in to change notification settings - Fork 32
/
cleaba_atter_cal_and_dataSet_create.py
153 lines (119 loc) · 7.82 KB
/
cleaba_atter_cal_and_dataSet_create.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
import os
from shutil import copyfile
from tqdm import tqdm
import argparse
argparse
parser = argparse.ArgumentParser()
parser.add_argument('--celeba_path', type=str, default='./datasets/CelebA', help='Path to celeba dataset.')
parser.add_argument('--celeba_anno_path', type=str, default='/Anno/list_attr_celeba.txt', help='path to celebe annotation file')
parser.add_argument('--celeba_path_list_eval_partition', type=str, default='/Eval/list_eval_partition.txt', help='path to celebe list_eval_partition')
parser.add_argument('--main_tag', type=str, default='Eyeglasses', help='main annotation to split celeba data by, one of: \'Male\' \'Blond_Hair\' \'Eyeglasses\' \'Gray_Hair\' \'Black_Hair\' \'Brown_Hair\'')
parser.add_argument('--sub_tag', type=str, default='Eyeglasses', help='sub annotation to split celeba data by, one of: \'Male\' \'Blond_Hair\' \'Eyeglasses\' \'Gray_Hair\' \'Black_Hair\' \'Brown_Hair\'')
opts = parser.parse_args()
path = opts.celeba_path
DataPath = os.path.join(path, 'Img/img_align_celeba')
f = open(path + opts.celeba_anno_path, "r")
f_eval = open(path + opts.celeba_path_list_eval_partition, "r")
num_of_images = int(f.readline())
tags = f.readline().split()
print('num of images: ' + str(num_of_images))
print('tags:')
print(str(tags))
tag_to_use_main = opts.main_tag
tag_to_use_sub = opts.sub_tag # 'Male' # 'Blond_Hair' # 'Eyeglasses' # 'Gray_Hair' # 'Black_Hair' #' Brown_Hair'
index_main = tags.index(tag_to_use_main) + 1
index_sub = tags.index(tag_to_use_sub) + 1
if not os.path.exists(os.path.join(path, tag_to_use_main, tag_to_use_sub, tag_to_use_main, tag_to_use_sub)):
os.makedirs(os.path.join(path, tag_to_use_main, tag_to_use_sub, tag_to_use_main, tag_to_use_sub))
if not os.path.exists(os.path.join(path, tag_to_use_main, tag_to_use_sub, tag_to_use_main,'not_' + tag_to_use_sub)):
os.makedirs(os.path.join(path, tag_to_use_main, tag_to_use_sub, tag_to_use_main, 'not_' + tag_to_use_sub))
if not os.path.exists(os.path.join(path, tag_to_use_main, tag_to_use_sub, 'not_' + tag_to_use_main, tag_to_use_sub)):
os.makedirs(os.path.join(path, tag_to_use_main, tag_to_use_sub, 'not_' + tag_to_use_main, tag_to_use_sub))
if not os.path.exists(os.path.join(path, tag_to_use_main, tag_to_use_sub, 'not_' + tag_to_use_main , 'not_' + tag_to_use_sub)):
os.makedirs(os.path.join(path, tag_to_use_main, tag_to_use_sub, 'not_' + tag_to_use_main, 'not_' + tag_to_use_sub))
if not os.path.exists(os.path.join(path, tag_to_use_main, tag_to_use_sub, tag_to_use_main, tag_to_use_sub + '_test')):
os.makedirs(os.path.join(path, tag_to_use_main, tag_to_use_sub, tag_to_use_main, tag_to_use_sub + '_test'))
if not os.path.exists(os.path.join(path, tag_to_use_main,tag_to_use_sub, tag_to_use_main, 'not_' + tag_to_use_sub + '_test')):
os.makedirs(os.path.join(path, tag_to_use_main, tag_to_use_sub, tag_to_use_main, 'not_' + tag_to_use_sub + '_test'))
if not os.path.exists(os.path.join(path, tag_to_use_main, tag_to_use_sub, 'not_' + tag_to_use_main, tag_to_use_sub + '_test')):
os.makedirs(os.path.join(path, tag_to_use_main, tag_to_use_sub, 'not_' + tag_to_use_main, tag_to_use_sub + '_test'))
if not os.path.exists(os.path.join(path, tag_to_use_main, tag_to_use_sub, 'not_' + tag_to_use_main, 'not_' + tag_to_use_sub + '_test')):
os.makedirs(os.path.join(path, tag_to_use_main, tag_to_use_sub, 'not_' + tag_to_use_main, 'not_' + tag_to_use_sub + '_test'))
f_A_1 = open(os.path.join(path, tag_to_use_main, tag_to_use_sub, tag_to_use_main, tag_to_use_sub, tag_to_use_main + '_' + tag_to_use_sub + ".txt"), "w")
f_A_2 = open(os.path.join(path, tag_to_use_main, tag_to_use_sub, tag_to_use_main, tag_to_use_sub, tag_to_use_main + '_not_' + tag_to_use_sub + ".txt"), "w")
f_B_1 = open(os.path.join(path, tag_to_use_main, tag_to_use_sub, 'not_' + tag_to_use_main, tag_to_use_sub, 'not_' + tag_to_use_main + '_' + tag_to_use_sub + ".txt"), "w")
f_B_2 = open(os.path.join(path, tag_to_use_main, tag_to_use_sub, 'not_' + tag_to_use_main, tag_to_use_sub, 'not_' + tag_to_use_main + '_not_' + tag_to_use_sub + ".txt"), "w")
f_all_info = open(os.path.join(path,tag_to_use_main, tag_to_use_sub, tag_to_use_main + '_' + tag_to_use_sub + "_info.txt"), "w")
# print(index_main)
# print(index_sub)
numOf_A_1 = 0
numOf_A_2 = 0
numOf_B_1 = 0
numOf_B_2 = 0
for line in tqdm(f, total=num_of_images):
curr_line = line.split()
eval_partition = int(f_eval.readline().split()[1])
addTest = ''
add_eval = ''
if eval_partition != 0:
addTest = '_test'
add_eval = str(eval_partition) + '_'
fileEnder = 'png'
file_name = curr_line[0].split('.')[0] + '.' + fileEnder
srcFile = DataPath + '/' + file_name
if int(curr_line[index_main]) == 1:
if int(curr_line[index_sub]) == 1:
destFile = os.path.join(path, tag_to_use_main, tag_to_use_sub, tag_to_use_main, tag_to_use_sub + addTest, add_eval + curr_line[0] + '.' + fileEnder)
numOf_A_1 += 1
f_A_1.write(curr_line[0] + '\n')
copyfile(srcFile, destFile)
else:
destFile = os.path.join(path, tag_to_use_main, tag_to_use_sub, tag_to_use_main, 'not_' + tag_to_use_sub + addTest, add_eval + curr_line[0] + '.' + fileEnder)
numOf_A_2 += 1
f_A_2.write(curr_line[0] + '\n')
copyfile(srcFile, destFile)
else:
if int(curr_line[index_sub]) == 1:
destFile = os.path.join(path, tag_to_use_main, tag_to_use_sub, 'not_' + tag_to_use_main, tag_to_use_sub + addTest, add_eval + curr_line[0] + '.' + fileEnder)
numOf_B_1 += 1
f_B_1.write(curr_line[0] + '\n')
copyfile(srcFile, destFile)
else:
destFile = os.path.join(path, tag_to_use_main, tag_to_use_sub, 'not_' + tag_to_use_main, 'not_' + tag_to_use_sub + addTest, add_eval + curr_line[0] + '.' + fileEnder)
numOf_B_2 += 1
f_B_1.write(curr_line[0] + '\n')
copyfile(srcFile, destFile)
tot_A = numOf_A_1 + numOf_A_2
tot_B = numOf_B_1 + numOf_B_2
print('Total ' + tag_to_use_main + ': ' + str(tot_A))
f_all_info.write('Total ' + tag_to_use_main + ': ' + str(tot_A) + '\n')
print('Total not_' + tag_to_use_main + ': ' + str(tot_B))
f_all_info.write('Total_not ' + tag_to_use_main + ': ' + str(tot_B) + '\n')
print('Total ' + tag_to_use_main + ' + ' + tag_to_use_sub + ': ' + str(numOf_A_1))
f_all_info.write('Total ' + tag_to_use_main + ' + ' + tag_to_use_sub + ': ' + str(numOf_A_1) + '\n')
print('Total ' + tag_to_use_main + ' + not_' + tag_to_use_sub + ': ' + str(numOf_A_2))
f_all_info.write('Total ' + tag_to_use_main + ' + not_' + tag_to_use_sub + ': ' + str(numOf_A_2) + '\n')
print('Total not_' + tag_to_use_main + ' + ' + tag_to_use_sub + ': ' + str(numOf_B_1))
f_all_info.write('Total not_' + tag_to_use_main + ' + ' + tag_to_use_sub + ': ' + str(numOf_B_1) + '\n')
print('Total not_' + tag_to_use_main + ' + not_' + tag_to_use_sub + ': ' + str(numOf_B_2))
f_all_info.write('Total not_' + tag_to_use_main + ' + not_' + tag_to_use_sub + ': ' + str(numOf_B_2) + '\n')
ratio_A_1 = numOf_A_1 / tot_A
ratio_A_2 = numOf_A_2 / tot_A
ratio_B_1 = numOf_B_1 / tot_B
ratio_B_2 = numOf_B_2 / tot_B
ratio_A_1_str = "{:.3f}".format(ratio_A_1)
ratio_A_2_str = "{:.3f}".format(ratio_A_2)
ratio_B_1_str = "{:.3f}".format(ratio_B_1)
ratio_B_2_str = "{:.3f}".format(ratio_B_2)
f_all_info.write('---------------------------\n')
print(tag_to_use_main + ' : ' + tag_to_use_sub + '/not_' + tag_to_use_sub + ' : ' + ratio_A_1_str + '/' + ratio_A_2_str)
f_all_info.write(tag_to_use_main + ' : ' + tag_to_use_sub + '/not_' + tag_to_use_sub + ' : ' + ratio_A_1_str + '/' + ratio_A_2_str + '\n')
print('not_' + tag_to_use_main + ' : ' + tag_to_use_sub + '/not_' + tag_to_use_sub + ' : ' + ratio_B_1_str + '/' + ratio_B_2_str)
f_all_info.write('not_' + tag_to_use_main + ' : ' + tag_to_use_sub + '/not_' + tag_to_use_sub + ' : ' + ratio_B_1_str + '/' + ratio_B_2_str + '\n')
f.close()
f_eval.close()
f_A_1.close()
f_A_2.close()
f_B_1.close()
f_B_2.close()
f_all_info.close()