-
Notifications
You must be signed in to change notification settings - Fork 1
/
Celeba_Handle.py
118 lines (98 loc) · 3.09 KB
/
Celeba_Handle.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
import numpy as np
import glob
from math import sqrt
def multipl(a, b):
sumofab = 0.0
for i in range(len(a)):
temp = a[i] * b[i]
sumofab += temp
return sumofab
def corrcoef(x, y):
n = len(x)
# 求和
sum1 = sum(x)
sum2 = sum(y)
# 求乘积之和
sumofxy = multipl(x, y)
# 求平方和
sumofx2 = sum([pow(i, 2) for i in x])
sumofy2 = sum([pow(j, 2) for j in y])
num = sumofxy - (float(sum1) * float(sum2) / n)
# 计算皮尔逊相关系数
den = sqrt((sumofx2 - float(sum1 ** 2) / n) * (sumofy2 - float(sum2 ** 2) / n))
return num / den
def GetImagesByAttribute(attributeIndex=16):
f = open("C:\CommonData\list_attr_celeba.txt")
glass = []
noGlass = []
lines = f.readlines()
del lines[0]
del lines[0]
for line in lines:
array = line.split()
if (array[attributeIndex] == "-1"):
new_context = array[0]
str1 = "C:\CommonData\img_celeba2\\"+new_context
glass.append(str1)
else:
new_context = array[0]
str1 = "C:\CommonData\img_celeba2\\"+new_context
noGlass.append(str1)
return glass,noGlass
def GetImagesByAttribute_Array(attributeArray,count):
totalArr = []
for i in range(len(attributeArray)):
index = attributeArray[i]
_,attributes = GetImagesByAttribute(index)
attributes = attributes[0:count]
totalArr.append(attributes)
return totalArr
def MeanVector(zglass_p,znoglass_p,count):
dim_z = 256
tb1 = np.zeros(dim_z)
tb2 = np.zeros(dim_z)
batch_size = 64
count2 = int(count / batch_size) * batch_size
for i in range(dim_z):
for j in range(count2):
tb1[i] = tb1[i] + zglass_p[j, i]
tb2[i] = tb2[i] + znoglass_p[j, i]
# mean vector for glass to no glass
tb1 = tb1 / count
tb2 = tb2 / count
return tb1,tb2
def MeanVector_One(zglass_p,count):
dim_z = 256
tb1 = np.zeros(dim_z)
tb2 = np.zeros(dim_z)
batch_size = 64
count2 = int(count / batch_size) * batch_size
for i in range(dim_z):
for j in range(count2):
tb1[i] = tb1[i] + zglass_p[j, i]
# mean vector for glass to no glass
tb1 = tb1 / count
return tb1
def GetCeleBa_WithAttribute():
# load dataset
img_path = glob.glob('C:/commonData/img_celeba2/*.jpg') # 获取新文件夹下所有图片
data_files = img_path
data_files = sorted(data_files)
data_files = np.array(data_files) # for tl.iterate.minibatches
n_examples = 202599
f = open("C:\CommonData\list_attr_celeba.txt")
glass = []
noGlass = []
lines = f.readlines()
del lines[0]
del lines[0]
attribute = np.zeros((n_examples,40)).astype(int)
index = 0
for line in lines:
array = line.split()
del array[0]
array = np.array(array).astype(int)
attribute[index,:] = array
index = index + 1
return data_files,attribute
GetCeleBa_WithAttribute()