-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_vector_genration.py
46 lines (36 loc) · 1.46 KB
/
feature_vector_genration.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
from keras.models import load_model
from keras.models import Model
import pickle
from tensorflow.keras.preprocessing import image
import numpy as np
from keras.applications.mobilenet import preprocess_input
# import cosin
from scipy.spatial.distance import cosine
from sklearn.metrics.pairwise import cosine_similarity
import random
import os
def get_feature_vector():
base_model = load_model('./tranfer_lrn_face_cnn.h5')
feature_layer = 'dense_2'
model = Model(inputs=base_model.input, outputs=base_model.get_layer(feature_layer).output)
print(model.summary())
face_label_filename = 'face_lable.pkl'
with open(face_label_filename, "rb") as f:
class_dictionary = pickle.load(f)
print(class_dictionary)
class_list = [value for _, value in class_dictionary.items()]
print(class_list)
base_dir = './faces/test/'
for idx,file in enumerate(class_list):
img_path = base_dir+file+'/'+random.choice(os.listdir(base_dir+file)) #random select one image from each class test folder
img1 = image.load_img(img_path, target_size=(224,224))
x1 = image.img_to_array(img1)
x1 = np.expand_dims(x1, axis=0)
x1 = preprocess_input(x1)
features1 = model.predict(x1)
feature_vector1 = features1.flatten()
class_dictionary[idx] = feature_vector1
with open('face_feature_vector.pkl', 'wb') as f:
pickle.dump(class_dictionary, f)
if __name__ == '__main__':
get_feature_vector()