-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
42 lines (31 loc) · 988 Bytes
/
util.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
import tensorflow as tf
import numpy as np
import pickle
def total_params(variables):
total = 0
for variable in variables:
total += np.prod(variable.shape.as_list())
return total
def preprocess():
lines = [line.rstrip() for line in open('./data/list_attr_celeba.txt', 'r')]
columns = lines[1].split()
indexes = []
labels = []
attr2idx = {}
for idx, attr in enumerate(columns):
attr2idx[attr] = idx
x2y = {}
lines = lines[2:]
for line in lines:
line_ls = line.split()
indexes.append(line_ls[0])
labels.append([int(_ == '1') for _ in line_ls[1:]] )
x2y[line_ls[0]] = [int(_ == '1') for _ in line_ls[1:]]
labels = np.array(labels)
with open('./data/attr2idx.pkl', 'wb') as f:
pickle.dump(attr2idx, f)
with open('./data/x2y.pkl', 'wb') as f:
pickle.dump(x2y, f)
print "Data preprocessed done......"
if __name__ == '__main__':
preprocess()