-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_siamese_network.py
63 lines (55 loc) · 1.95 KB
/
test_siamese_network.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
import config
import utils
from keras.models import load_model
from imutils.paths import list_images
import numpy as np
import cv2
import tensorflow as tf
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
print("[INFO] loading test dataset...")
# testImagePaths = testX
testImagePaths = list(list_images('examples'))
print(testImagePaths)
np.random.seed(42)
pairs = np.random.choice(testImagePaths, size=(10, 2))
print(pairs)
# # load the model from disk
# print("[INFO] loading siamese model...")
model = load_model(config.MODEL_PATH)
for (i, (pathA, pathB)) in enumerate(pairs):
# load both the images and convert them to grayscale
imageA = cv2.imread(pathA, 0)
imageB = cv2.imread(pathB, 0)
# create a copy of both the images for visualization purpose
origA = imageA.copy()
origB = imageB.copy()
#resizing the image according to the model's input requirement
imageA = cv2.resize(imageA, (28,28))
imageB = cv2.resize(imageB, (28,28))
# add channel a dimension to both the images
imageA = np.expand_dims(imageA, axis=-1)
imageB = np.expand_dims(imageB, axis=-1)
# add a batch dimension to both images
imageA = np.expand_dims(imageA, axis=0)
imageB = np.expand_dims(imageB, axis=0)
# scale the pixel values to the range of [0, 1]
imageA = imageA / 255.0
imageB = imageB / 255.0
# use our siamese model to make predictions on the image pair,
# # indicating whether or not the images belong to the same class
preds = model.predict([imageA, imageB])
proba = preds[0][0]
fig = plt.figure("Pair #{}".format(i + 1), figsize=(4, 2))
plt.suptitle("Similarity: {:.2f}".format(proba))
# show first image
ax = fig.add_subplot(1, 2, 1)
plt.imshow(origA, cmap=plt.cm.gray)
plt.axis("off")
# show the second image
ax = fig.add_subplot(1, 2, 2)
plt.imshow(origB, cmap=plt.cm.gray)
plt.axis("off")
# show the plot
plt.show()