--- title: FaceEmbedding Model keywords: fastai sidebar: home_sidebar nb_path: "nbs/indexers.FaceRecognitionModel.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %}

The FaceEmbeddingModel recognizes face from photos. Behind the scenes it uses the arcface model from deepinsight to make predictions. The model does both face detection (creating bouding boxes around faces) and face verification (compare faces and check for similarity).

{% raw %}
{% endraw %} {% raw %}

class FaceEmbeddingModel[source]

FaceEmbeddingModel(*args, **kwargs)

Recognizes photos from faces.

{% endraw %} {% raw %}
{% endraw %}

Usage

You can initialize the Facerecognition model as follows:

{% raw %}
indexer = FaceEmbeddingModel()
[32, 16, 8] {'32': {'SCALES': (32, 16), 'BASE_SIZE': 16, 'RATIOS': (1.0,), 'ALLOWED_BORDER': 9999}, '16': {'SCALES': (8, 4), 'BASE_SIZE': 16, 'RATIOS': (1.0,), 'ALLOWED_BORDER': 9999}, '8': {'SCALES': (2, 1), 'BASE_SIZE': 16, 'RATIOS': (1.0,), 'ALLOWED_BORDER': 9999}}
use_landmarks True
{% endraw %} {% raw %}
photo = IPhoto.from_path(path=PYI_TESTDATA / "photos" / "facerecognition" / "celebs.jpg")
boxes, landmarks = indexer.predict_boundingboxes(photo)
{% endraw %} {% raw %}
photo.draw_boxes(boxes)
Plotting 12 face boundingboxes
{% endraw %} {% raw %}
crops = photo.get_crops(boxes, landmarks)
{% endraw %}

You can easily inspect the embeddings for those crops using the get_embedding function

{% raw %}
embedding = indexer.get_embedding(crops[0])
embedding.shape, embedding[:3]
((512,), array([-0.18354045, -0.9777582 , -1.5571648 ], dtype=float32))
{% endraw %}

You can inspect the crops using the show_images function

{% raw %}
show_images([crops[0], crops[1]])
{% endraw %}

And check if they are the same

{% raw %}
try:
    similarity =  indexer.compare(crops[0], crops[1])
    assert similarity < 0.5
finally:
    print("Not the same person")
Not the same person
{% endraw %}

Compute similarity for photo's of the same person

{% raw %}
ellen1 = IPhoto.from_path(path=PYI_TESTDATA / "photos" / "facerecognition" / "ellen1.png")
ellen2 = IPhoto.from_path(path=PYI_TESTDATA / "photos" / "facerecognition" / "ellen2.png")
{% endraw %} {% raw %}
show_images([ellen1.data, ellen2.data])
{% endraw %} {% raw %}
try:
    sim = indexer.compare(ellen1.data, ellen2.data)
    assert sim > 0.5
finally:
    print("Same person")
Same person
{% endraw %}

Plotting crops

When we plot the crops, we see that our model actually is doing some magic behind the scenes to normalize and scale our images.

{% raw %}
photo.plot_crops(boxes, landmarks)
{% endraw %}