Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
Facial_recognition_for_access_control
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Wael Khlifi
Facial_recognition_for_access_control
Commits
403467ca
Commit
403467ca
authored
11 months ago
by
Wael Khlifi
Browse files
Options
Downloads
Patches
Plain Diff
added_another_evaluation_script_LFW
parent
ce6cf47b
Branches
master
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
test_model/evaluate_model.py
+71
-0
71 additions, 0 deletions
test_model/evaluate_model.py
with
71 additions
and
0 deletions
test_model/evaluate_model.py
0 → 100644
+
71
−
0
View file @
403467ca
import
numpy
as
np
import
tensorflow
as
tf
from
sklearn.datasets
import
fetch_lfw_pairs
from
sklearn.metrics
import
accuracy_score
from
sklearn.preprocessing
import
normalize
import
tensorflow
as
tf
import
os
import
random
import
numpy
as
np
import
cv2
from
facial_recognition.Siamese_model
import
image_embedder
from
facial_recognition.functions
import
split_dataset
,
create_triplets
import
seaborn
as
sns
import
matplotlib.pyplot
as
plt
from
keras.applications.inception_v3
import
preprocess_input
from
pathlib
import
Path
# Load the LFW pairs dataset
lfw_pairs
=
fetch_lfw_pairs
(
subset
=
'
test
'
,
color
=
True
,
resize
=
0.5
)
# Prepare the images and labels
pairs
=
lfw_pairs
.
pairs
labels
=
lfw_pairs
.
target
# Preprocess images
def
preprocess_image
(
image
):
image
=
tf
.
image
.
resize
(
image
,
(
224
,
224
))
#image = tf.cast(image, tf.float32) / 255.0 # Normalize to [0, 1]
return
image
# Load your pre-trained face recognition model
def
load_model
():
# Replace this with the code to load your model
model
=
tf
.
keras
.
models
.
load_model
(
r
'
C:\Users\waelk\PycharmProjects\facial_recognition\facial_recognition\test_model\model\encoder.keras
'
)
return
model
encoder
=
image_embedder
((
224
,
224
,
3
))
encoder
.
load_weights
(
os
.
path
.
join
(
'
model
'
,
'
encoder.keras
'
))
model
=
encoder
# Compute embeddings for pairs of images
def
compute_embeddings
(
model
,
pairs
):
embeddings
=
[]
for
i
in
range
(
pairs
.
shape
[
0
]):
img1
=
preprocess_image
(
pairs
[
i
,
0
])
img2
=
preprocess_image
(
pairs
[
i
,
1
])
embedding1
=
model
.
predict
(
np
.
expand_dims
(
img1
,
axis
=
0
))
embedding2
=
model
.
predict
(
np
.
expand_dims
(
img2
,
axis
=
0
))
embeddings
.
append
((
embedding1
,
embedding2
))
return
embeddings
embeddings
=
compute_embeddings
(
model
,
pairs
)
# Compute distance between embeddings and predict labels
def
compute_distances
(
embeddings
):
distances
=
[]
for
embedding1
,
embedding2
in
embeddings
:
distance
=
np
.
linalg
.
norm
(
embedding1
-
embedding2
)
distances
.
append
(
distance
)
return
np
.
array
(
distances
)
distances
=
compute_distances
(
embeddings
)
# Threshold to classify pairs as same or different
threshold
=
1.2
# You can tune this threshold based on a validation set
predicted_labels
=
distances
<
threshold
accuracy
=
accuracy_score
(
labels
,
predicted_labels
)
print
(
f
'
Accuracy:
{
accuracy
:
.
4
f
}
'
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment