Skip to main content

Easy to use Face Recognition and Detection models.

Project description


Face Detection and Recognition

A PyTorch-based face detection and recognition system built with EfficientNet backbones and ArcFace loss, supporting live webcam detection and recognition, image inference, and easy embedding extraction.


Table of Contents


Overview

This repository implements a face detection and recognition pipeline consisting of:

  • DetectionModel: Uses EfficientNet-B0 as backbone, with a custom neck and heads for bounding box and objectness prediction.
  • RecognitionModel: Uses EfficientNet-B2 backbone, neck, pooling, and ArcFace for robust face embeddings and classification.

Model Architecture

DetectionModel

  • Backbone: EfficientNet-B0 feature extractor (pretrained weights).

  • Neck: Three Conv-BatchNorm-SiLU blocks reducing channel depth from 1280 to 96.

  • Heads:

    • BBox Head: Predicts bounding box coordinates (4 channels).
    • Objectness Head: Predicts objectness confidence (1 channel).
  • Output: Concatenated tensor of shape [B, 5, H, W] (bbox + objectness).

  • Decoding: Converts network outputs to bounding boxes and confidence scores using grid and stride calculations.

RecognitionModel

  • Backbone: EfficientNet-B2 feature extractor.
  • Neck: Conv-BatchNorm-SiLU block to reduce channels to embedding dimension (default 256).
  • Pooling: Adaptive average pooling to 1x1.
  • Embedding Head: BatchNorm and Dropout followed by normalization.
  • ArcFace: Angular margin softmax layer for face recognition classification.
  • Output: Either normalized embeddings or classification logits (if labels provided).

Installation

To install from PyPI:

pip install MementoML

Make sure you have Python 3.8+ and PyTorch installed. Install dependencies:

I used Python 3.12.7.

pip install torch torchvision numpy opencv-python matplotlib pillow

or

pip install -r requirements.txt

Place the pre-trained weight files in the working directory:

  • FaceDetectionWeights.pth
  • FaceRecognitionWeights.pth

Usage

Detection Model

Detect faces in a single image and plot bounding boxes:

from PIL import Image
import matplotlib.pyplot as plt

detector = DetectionModel(weights="FaceDetectionWeights.pth", device="cuda")
img = Image.open("test_face.jpg")
detector.face_and_plot(img, conf_thresh=0.5)

Run live webcam face detection:

detector = DetectionModel(weights="FaceDetectionWeights.pth", device="cuda")
detector.live_test(conf_thresh=0.8, frame_skip=0)

Recognition Model

Generate a face embedding from an image:

from PIL import Image

recognizer = RecognitionModel(weights="FaceRecognitionWeights.pth", device="cuda")
img = Image.open("face_crop.jpg")
embedding = recognizer.generate_emb(img)
print(embedding.shape)  # torch.Size([256])

Live Webcam Testing

Both detection and recognition models support live webcam testing individually:

Detection example shown above; for recognition, run your own scripts on cropped face images or saved crops.


Saving and Loading Weights

Save your model weights after training:

detector.save_weights("new_detection_weights.pth")
recognizer.save_weights("new_recognition_weights.pth")

Load weights:

detector = DetectionModel(weights="new_detection_weights.pth")
recognizer = RecognitionModel(weights="new_recognition_weights.pth")

Customizing Confidence Thresholds and Frame Skips

  • Confidence Threshold: Adjust detection sensitivity.
detector.live_test(conf_thresh=0.5)  # More sensitive, detect more faces
  • Frame Skip: Process every Nth frame in live webcam feed to reduce compute.
detector.live_test(frame_skip=5)  # Process every 5th frame

Contributing

Feel free to open issues or submit pull requests. Suggestions and improvements are welcome!


License

This project is licensed under the MIT License.


If you want me to generate examples for training, or detailed info about the ArcFace loss or anything else, just ask! My email: therazielmoesch@gmail.com


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

memento_face-0.1.1.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

memento_face-0.1.1-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file memento_face-0.1.1.tar.gz.

File metadata

  • Download URL: memento_face-0.1.1.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for memento_face-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bfa699974b015a3361a8111b98832b5aedb7e518c9f9e6cacc226e0d6e78bb91
MD5 6dddb242e312d655bc541808fe7c26f1
BLAKE2b-256 c7b7ff2fe35653ad98b1d4e5a739b5094c0800b5e548d4d14b35a126eb3a11fb

See more details on using hashes here.

File details

Details for the file memento_face-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: memento_face-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for memento_face-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 be0e5e2e82a03dacc5d1d95a6398e1ca3af9c8a71d204898f7508f5dbb601ab9
MD5 05744e3da4982a003ad78ff54b31f194
BLAKE2b-256 4d16cdaff6bbd5715c1bca9e62c293522ad8963ab38e4ad78fe32d9358f3b8b1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page