Skip to main content

Face analysis PyTorch framework.

Project description

facetorch

PyPI - License PyPI Conda

Facetorch is a Python library that can detect faces and analyze facial features like expressions using artificial neural networks. The goal is to gather open-source face analysis models from the community, optimize them for performance using TorchScript, and combine them to create a single face analysis tool that one can:

  1. configure using Hydra (OmegaConf)
  2. reproduce with conda-lock and Docker
  3. accelerate on CPU and GPU with TorchScript
  4. extend by uploading a model file to Google Drive and adding a config yaml file to the repository

Please, use the library responsibly with caution and follow the ethics guidelines for Trustworthy AI from European Commission. The models are not perfect and may be biased.

Install

PyPI

pip install facetorch

Conda

conda install -c conda-forge facetorch

Usage

Prerequisites

Docker Compose builds an image that can be used to run the FaceAnalyzer.

Configure

The project is configured by files located in conf with the main file conf/config.yaml.

Run docker example

  • CPU: docker compose run facetorch python ./scripts/example.py
  • GPU: docker compose run facetorch-gpu python ./scripts/example.py analyzer.device=cuda

Components

FaceAnalyzer is the main class of Facetorch as it is the orchestrator responsible for initializing and running the following components:

  1. Reader - reads the image and returns an ImageData object containing the image tensor.
  2. Detector - wrapper around a neural network that detects faces.
  3. Unifier - processor that unifies sizes of all faces and normalizes them between 0 and 1.
  4. Predictor dict - set of wrappers around neural networks trained to analyze facial features.

Documentation provides more detailed information about the facetorch modules.

Structure

analyzer
    ├── reader
    ├── detector
    ├── unifier
    └── predictor
            ├── embed
            ├── fer
            └── deepfake

Available models

Detector

|    detector   |   source  |   license   | version |
| ------------- | --------- | ----------- | ------- |
|   RetinaFace  |  biubug6  | MIT license |    1    |
  1. biubug6

Predictor

Facial representation learning (embed)

|       embed       |   source   |   license   | version |  
| ----------------- | ---------- | ----------- | ------- |
|  ResNet-50 VGG 1M |  1adrianb  | MIT license |    1    |
  1. 1adrianb

Facial expression recognition (FER)

|        fer        |      source    |       license      | version |  
| ----------------- | -------------- | ------------------ | ------- |
| EfficientNet B0 7 | HSE-asavchenko | Apache License 2.0 |    1    |
| EfficientNet B2 8 | HSE-asavchenko | Apache License 2.0 |    2    |
  1. HSE-asavchenko

Deepfake detection

Deepfake detection is a difficult task and these models are not perfect.

|       deepfake       |      source      |   license   | version |
| -------------------- | ---------------- | ----------- | ------- |
|    EfficientNet B7   |     selimsef     | MIT license |    1    |
  1. selimsef

Model download

Models are downloaded during runtime automatically to the models directory. You can also download the models manually from a public Google Drive folder.

Execution time

The analyzer can analyze test.jpg (4 faces) in about 400ms and test3.jpg (25 faces) in about 1.1s on NVIDIA Tesla T4 GPU once the models are pre heated to the initial image size 1024x1024. One can monitor the execution times in logs using the DEBUG level.

Detailed test.jpg execution times:

analyzer
    ├── reader: 27 ms
    ├── detector: 230 ms
    ├── unifier: 1 ms
    └── predictor
            ├── embed: 8 ms
            ├── fer: 22 ms
            └── deepfake: 109 ms

Development

Run the Docker container:

  • CPU: docker compose -f docker-compose.dev.yml run facetorch-dev bash
  • GPU: docker compose -f docker-compose.dev.yml run facetorch-dev-gpu bash

Add new predictor

Prerequisites

  1. File of the TorchScript model
  2. Google Drive file ID of the model

Facetorch works with models that were exported from PyTorch to TorchScript. You can apply torch.jit.trace function to compile a PyTorch model as a TorchScript module.

The first models are hosted on my public Google Drive folder. You can either send the new model for upload to me, host the model on your Google Drive or host it somewhere else and add your own downloader object to the codebase.

Configuration

Create yaml file
  1. Create new folder with a short name of the task in predictor configuration directory /conf/analyzer/predictor/ following the FER example in /conf/analyzer/predictor/fer/
  2. Copy the yaml file /conf/analyzer/predictor/fer/efficientnet_b2_8.yaml to the new folder /conf/analyzer/predictor/<predictor_name>/
  3. Change the yaml file name to the model you want to use: /conf/analyzer/predictor/<predictor_name>/<model_name>.yaml
Edit yaml file
  1. Change the Google Drive file ID to the ID of the model.
  2. Select the preprocessor (or implement a new one based on BasePredPreProcessor) and specify it's parameters e.g. image size and normalization in the yaml file to match the requirements of the new model.
  3. Select the postprocessor (or implement a new one based on BasePredPostProcessor) and specify it's parameters e.g. labels in the yaml file to match the requirements of the new model.
Configure tests
  1. Add a new predictor to the main config.yaml and all tests.config..yaml files. Alternatively, create a new config file e.g. tests.config..yaml and add it to the /tests/conftest.py file.
  2. Write a test for the new predictor in /tests/test_<predictor_name>.py

Test and submit

  1. Run linting test: flake8 --config=.flake8
  2. Run tests and check coverage: pytest tests --verbose --cov-report html:coverage --cov facetorch
  3. Add the new predictor to the README model table.
  4. Submit a pull request to add the new predictor to the main codebase.

Update environment

  • Add packages with corresponding versions to environment.yml file
  • Lock the environment: conda lock -p linux-64 -f environment.yml
  • Install the locked environment: conda-lock install --name env conda-lock.yml

Generate documentation

  • Generate documentation from docstrings using pdoc3: pdoc --html facetorch --output-dir docs --force --template-dir pdoc/templates/

Profiling

  1. Run profiling of the example script: python -m cProfile -o profiling/example.prof scripts/example.py
  2. Open profiling file in the browser: snakeviz profiling/example.prof

Acknowledgements

I want to thank the open source code community and the researchers who have published the models. This project would not be possible without their work.

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

facetorch-0.0.1.tar.gz (20.7 kB view details)

Uploaded Source

Built Distributions

facetorch-0.0.1-py3-none-any.whl (29.7 kB view details)

Uploaded Python 3

facetorch-0.0.1-1-py3-none-any.whl (29.6 kB view details)

Uploaded Python 3

File details

Details for the file facetorch-0.0.1.tar.gz.

File metadata

  • Download URL: facetorch-0.0.1.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.3

File hashes

Hashes for facetorch-0.0.1.tar.gz
Algorithm Hash digest
SHA256 0de068fb04f0843ebc88a7db33190dbb10ceed7edd028e9e1d1791331e0c300f
MD5 2182dafa5a5be410b6fdbfca63396e42
BLAKE2b-256 a3aaf67cd55622667a9719a7ed9584eca7f9c1baf9dfbe768b96607ff5470abe

See more details on using hashes here.

Provenance

File details

Details for the file facetorch-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: facetorch-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 29.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.3

File hashes

Hashes for facetorch-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 764ec35c6963806a8e7d3a564ac8268b9c92ee5d755674a9784450a9599817a1
MD5 4826df409964a969790e55e40af88dca
BLAKE2b-256 c488f807a4b6b2cd76b9b9cc6063cc0c5a39e0681f38511f302578b4b45b4c9d

See more details on using hashes here.

Provenance

File details

Details for the file facetorch-0.0.1-1-py3-none-any.whl.

File metadata

  • Download URL: facetorch-0.0.1-1-py3-none-any.whl
  • Upload date:
  • Size: 29.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.3

File hashes

Hashes for facetorch-0.0.1-1-py3-none-any.whl
Algorithm Hash digest
SHA256 78dac44d646c8545c1c5ddef5235984a0c1a051c5ad12d0ce28b0a45e18bc215
MD5 248d77edcbe978b0b78ce579cd475146
BLAKE2b-256 cce423b22c3b4763f241db934d110f7da2e2c75f0cd4f7cfd7de4846f9fa146a

See more details on using hashes here.

Provenance

Supported by

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