forked from gchoi/face-recognition-using-siamese-network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsiamese-nework-face-prediction.py
69 lines (52 loc) · 2.08 KB
/
siamese-nework-face-prediction.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
#%% Import libraries
import os
import numpy as np
from keras.models import load_model
import siamese_network as SN
import helper
#%% Define some params
DATA_PATH = "./dataset/faces"
MODEL_PATH = './models'
MODEL_NAME = 'siamese-face-model.h5'
NUM_TRIALS = 500 ## Trials for Testing Accuracy
#%% Main
## load the model
model = load_model(os.path.join(MODEL_PATH, MODEL_NAME),
custom_objects={'contrastive_loss': SN.contrastive_loss})
## test with many randomly selected images
matching = 0
for tiral in range(NUM_TRIALS):
print("Now trial #%d of %d" % (tiral, NUM_TRIALS))
pathlist = os.listdir(DATA_PATH)
category = np.random.randint(len(pathlist))
cur_path = os.path.join(DATA_PATH, pathlist[category])
filelist = os.listdir(cur_path)
index = np.random.randint(len(filelist))
ref_image = helper.get_image(DATA_PATH, category, index)
results = []
for cat in range(len(pathlist)):
filelist = os.listdir(os.path.join(DATA_PATH, pathlist[cat]))
idx = np.random.randint(len(filelist))
cur_image = helper.get_image(DATA_PATH, cat, idx)
dist = model.predict([ref_image, cur_image])[0][0]
results.append(dist)
if category == np.argmin(results):
matching += 1
print("Accuracy: %5.2f %%\n" % (100.0 * matching / NUM_TRIALS))
## select an image randomly (with only 1 image)
print("\n.... Now predict with the randomly selected image ....")
pathlist = os.listdir(DATA_PATH)
category = np.random.randint(len(pathlist))
cur_path = os.path.join(DATA_PATH, pathlist[category])
filelist = os.listdir(cur_path)
index = np.random.randint(len(filelist))
ref_image = helper.get_image(DATA_PATH, category, index)
results = []
for cat in range(len(pathlist)):
filelist = os.listdir(os.path.join(DATA_PATH, pathlist[cat]))
idx = np.random.randint(len(filelist))
cur_image = helper.get_image(DATA_PATH, cat, idx)
dist = model.predict([ref_image, cur_image])[0][0]
results.append(dist)
print("Selected Category: %d" % (category))
print("Predicted Category: %d" % (np.argmin(results)))