-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfunc_utils.py
151 lines (120 loc) · 4.4 KB
/
func_utils.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
from visualization import *
import numpy as np
import cv2
from keras import backend as K
from keras.preprocessing import image
import os
def ShowAttentionV1(model,image_path):
file_list = os.listdir(image_path)
file_list.sort()
for filename in file_list:
print (filename)
filepath=image_path+filename
image=load_image(filepath)
image = image/255.0
gender=1.0
gender=np.asarray(gender)
gender=np.expand_dims(gender,axis=0)
layer=K.function([model.layers[0].input],[model.layers[196].output])
FeatureMap=layer([image,gender])[0]
print (FeatureMap.shape)
FeatureMap = np.squeeze(FeatureMap, axis=0)
FeatureMap = np.abs(FeatureMap)
heatmap = np.mean(FeatureMap,axis=2)
heatmap = heatmap/np.max(heatmap)
heatmap = np.uint8(255*heatmap)
print (heatmap.shape)
heatmap = cv2.applyColorMap(heatmap,cv2.COLORMAP_JET)
SaveImg(filename,filepath,heatmap)
print ('********** Done ***********')
def GAPAttention(model,weights,image_path):
file_list = os.listdir(image_path)
file_list.sort()
for filename in file_list:
filepath=image_path+filename
print (filepath)
image=load_image(filepath)
image = image/255.0
gender=1.0
gender=np.asarray(gender)
gender=np.expand_dims(gender,axis=0)
layer=K.function([model.layers[0].input],[model.layers[1].get_output_at(-1),model.layers[-1].output])
GAP,prediction=layer([image,gender])
GAP=np.squeeze(GAP,axis=0)
print (GAP.shape)
index = np.argmax(prediction)
print (index)
# weight = weights[:,index]
weight =np.mean(weights[:,index-5:index+5],axis=1)
heatmap = np.zeros((GAP.shape[0],GAP.shape[1]))
for k in range(GAP.shape[2]):
heatmap = heatmap + weight[k]*GAP[:,:,k]
heatmap = heatmap/np.max(heatmap)
heatmap = np.uint8(255*heatmap)
print (heatmap.shape)
heatmap = cv2.applyColorMap(heatmap,cv2.COLORMAP_JET)
SaveImg(filename,filepath,heatmap)
print ('********** Done ***********')
def SaveImg(filename,filepath,heatmap):
img = cv2.imread(filepath)
heatmap = cv2.resize(heatmap,(img.shape[1],img.shape[0]))
AttentionImg =0.5* heatmap + img
cv2.imwrite('heatmap/'+filename,heatmap)
cv2.imwrite('AttentionImg/'+filename,AttentionImg)
def load_image(path):
img = cv2.imread(path)
img = cv2.resize(img,(300,300))
x = np.asarray(img, dtype=np.float32)
# img = image.load_img(path, target_size=(448, 448))
# print (img.shape)
# x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
return x
def softlabel(label,num_class):
softlabel=np.zeros((len(label),num_class))
ratio = 1.0/50
for i in range(len(label)):
for j in range(num_class):
softlabel[i,j]=1.0 - ratio*np.abs(j-label[i])
softlabel = np.maximum(softlabel,0)
return softlabel
def GaussLabel(label,num_class):
sigma=15.0
GaussLabel = np.zeros((len(label),num_class))
x = np.array(range(num_class))+1
for k in range(len(label)):
GaussLabel[k,:]=np.exp(-(x-label[k])**2/(2.0*sigma**2))
return GaussLabel
def TestMAE(model,test_data,test_label,test_gender):
test_gender = np.array(test_gender)
test_gender = np.expand_dims(test_gender,axis=1)
layer=K.function([model.layers[0].input,model.layers[3].input],[model.layers[-1].output])
predictions=layer([test_data,test_gender])
predictions = np.array(predictions)
predictions = np.squeeze(predictions,axis=0)
print (predictions.shape)
predict_label = np.argmax(predictions,axis=1)
test_label = np.argmax(test_label,axis=1)
print (predict_label)
print (test_label)
TestMAE = np.mean(np.abs(predict_label-test_label))
return TestMAE
def DataAugment(x_train):
x_train_Aug = np.zeros(x_train.shape)
for i in range(x_train.shape[0]):
for j in range(3):
img = x_train[i,:,:,j]
img = RandomMask(img)
img = RandomMask(img)
if np.random.random()>-1:
x_train_Aug[i,:,:,j]=img
else:
x_train_Aug[i,:,:,j]=x_train[i,:,:,j]
return x_train_Aug
def RandomMask(img):
m,n=img.shape
m=int(m/6)
n=int(n/6)
i,j = np.random.randint(0,6,2)
img[i*m:(i+1)*m,j*n:(j+1)*n]=np.random.random()
return img