embedding-calculator/README.md
This is a component of CompreFace. CompreFace is a service for face recognition: upload images with faces of known people, then upload a new image, and the service will recognize faces in it.
Not needed if only running containers:
$ python -m pip install -r requirements.txt -e srcext/insightface/python-package
$ imageio_download_bin freeimage
Only needed if using tools:
$ make tools/tmp
$ chmod +x tools/test_memory_constraints.sh
$ export FLASK_ENV=development
$ python -m src.app
There are some pre-build images on https://hub.docker.com/r/exadel/compreface-core. To use it run:
$ docker run -p 3000:3000 exadel/compreface-core:latest
| Tag | Scanner | Build arguments | Comment |
|---|---|---|---|
| :0.5.1 :latest | Facenet2018 | ||
| :0.5.1-insightface | InsightFace | FACE_DETECTION_PLUGIN=insightface.FaceDetector | |
| CALCULATION_PLUGIN=insightface.Calculator | |||
| :0.5.1-insightface-gpu | InsightFace | FACE_DETECTION_PLUGIN=insightface.FaceDetector | |
| CALCULATION_PLUGIN=insightface.Calculator | |||
| GPU_IDX=0 | CORE_GPU_IDX - index of GPU-device |
Builds container (also runs main tests during the build):
$ docker build -t embedding-calculator .
To skip tests during build, use:
$ docker build -t embedding-calculator --build-arg SKIP_TESTS=true .
$ docker run -p 3000:3000 embedding-calculator
Unit tests
$ pytest -m "not integration and not performance" src tools
Integration tests
$ pytest -m integration src tools
Performance tests
$ pytest -m performance src tools
Lint checks
$ python -m pylama --options pylama.ini src tools
If DockerHub images is not enough, build an image with only the necessary set of plugins.
For changing default plugins pass needed plugin names in build arguments and build your own image.
Set plugins by build arguments FACE_DETECTION_PLUGIN and CALCULATION_PLUGIN
| Plugin name | Slug | Backend | Framework | GPU support |
|---|---|---|---|---|
| facenet.FaceDetector | detector | MTCNN | Tensorflow | |
| facenet.Calculator | calculator | Facenet | Tensorflow | |
| insightface.FaceDetector | detector | insightface | MXNet | + |
| insightface.Calculator | calculator | insightface | MXNet | + |
Pass to EXTRA_PLUGINS comma-separated names of plugins.
| Plugin name | Slug | Backend | Framework | GPU support |
|---|---|---|---|---|
| agegender.AgeDetector | age | agegender | Tensorflow | |
| agegender.GenderDetector | gender | agegender | Tensorflow | |
| insightface.AgeDetector | age | insightface | MXNet | + |
| insightface.GenderDetector | gender | insightface | MXNet | + |
| facenet.LandmarksDetector | landmarks | Facenet | Tensorflow | + |
| insightface.LandmarksDetector | landmarks | insightface | MXNet | + |
| insightface.Landmarks2d106Detector | landmarks2d106 | insightface | MXNet | + |
| facenet.facemask.MaskDetector | mask | facemask | Tensorflow | + |
| insightface.facemask.MaskDetector | mask | facemask | MXNet | + |
| facenet.PoseEstimator | pose | Facenet | Tensorflow | + |
| insightface.PoseEstimator | pose | insightface | MXNet | + |
Notes:
facenet.LandmarksDetector and insightface.LandmarksDetector extract landmarks
from results of FaceDetector plugin without additional processing. Returns 5 points of eyes, nose and mouth.insightface.Landmarks2d106Detector detects 106 points of facial landmark.
Points mark-upFACE_DETECTION_PLUGIN=facenet.FaceDetector
CALCULATION_PLUGIN=facenet.Calculator
EXTRA_PLUGINS=facenet.LandmarksDetector,agegender.GenderDetector,agegender.AgeDetector,facenet.facemask.MaskDetector,facenet.PoseEstimator
Some plugins have several pre-trained models.
To use an additional model pass a name of the model after a plugin name with a separator @. For example:
FACE_DETECTION_PLUGIN=insightface.FaceDetector@retinaface_mnet025_v1
List of pre-trained models:
facenet.Calculator
insightface.FaceDetector
insightface.Calculator
facenet.facemask.MaskDetector
insightface.facemask.MaskDetector
There are two build arguments for optimization:
GPU_IDX - id of NVIDIA GPU device, starts from 0 (empty or -1 for disable)INTEL_OPTIMIZATION - enable Intel MKL optimization (true/false)Install the nvidia-docker2 package and dependencies on the host machine:
sudo apt-get update
sudo apt-get install -y nvidia-docker2
sudo systemctl restart docker
docker build . -t embedding-calculator-cuda -f gpu.Dockerfile
docker build . -t embedding-calculator-gpu --build-arg GPU_IDX=0 --build-arg BASE_IMAGE=embedding-calculator-cuda
docker run -p 3000:3000 --gpus all embedding-calculator-gpu
Finds faces in a given image, puts bounding boxes and saves the resulting image.
$ export IMG_NAMES=015_6.jpg
$ python -m tools.scan
Tests the accuracy of face detection.
$ make tools/benchmark_detection/tmp
$ python -m tools.benchmark_detection
Tests whether service crashes with various parameters under given RAM constraints.
$ docker build -t embedding-calculator .
$ tools/test_memory_constraints.sh $(pwd)/sample_images
Optimizes face detection library parameters with a given annotated image dataset.
$ mkdir tmp
$ python -m tools.optimize_detection_params
Perform the following steps:
embedding-calculator with the needed scanner backend and CPU/GPU supportsdocker exec embedding-calculator ./benchmarkcd .embedding-calculator && ./benchmark.sh (require exposing API at localhost:3000): invalid optionCRLF file endings may cause this. To fix, run $ dos2unix *.
requirements.txt in a local environment crashesPackage uWSGI is not supported on Windows. Workaround is to temporarily delete the line with the package name from requirements.txt and install without it.
Check that the component is in valid state: run tests, build container, start it
$ make
$ make up
Get project line counts per file type
$ which tokei >/dev/null || conda install -y -c conda-forge tokei && tokei --exclude srcext/