Code Monkey home page Code Monkey logo

face_toolbox_keras's Introduction

face-toolbox-keras

A collection of deep learning frameworks ported to Keras for face detection, face segmentation, face parsing, iris detection, and face verification.

Descriptions

This repository contains deep learning frameworks that we collected and ported to Keras. We wrapped those models into separate modules that aim to provide their functionality to users within 3 lines of code.

*Each module follows the license of their source repo. Notice that some models were trained on dataset with non-commercial license.

Usage

Open In Colab (Please run pip install keras==2.2.4 before initializaing models.)

This colab demo requires a GPU instance. It demonstrates all face analysis functionalities above.

1. Face detection

models.detector.face_detector.FaceAlignmentDetector(fd_weights_path=..., lmd_weights_path=..., fd_type="s3fd")

Arguments

  • fd_weights_path: A string. Path to weights file of the face detector model.
  • lmd_weights_path: A string. Path to weights file of the landmarks detector model.
  • fd_type: A string. Face detector backbone model of either s3fd or mtcnn.

Example

from models.detector import face_detector

im = cv2.imread(PATH_TO_IMAGE)[..., ::-1]
fd = face_detector.FaceAlignmentDetector()
bboxes = fd.detect_face(im, with_landmarks=False)

2. Face landmarks detection

The default model is 2DFAN-4. Lite models of 2DFAN-1 and 2DFAN-2 are also provided.

GPU 2DFAN-1 2DFAN-2 2DFAN-4
K80 74.3ms 92.2ms 133ms

Example

from models.detector import face_detector

im = cv2.imread(PATH_TO_IMAGE)[..., ::-1]
fd = face_detector.FaceAlignmentDetector()
bboxes, landmarks = fd.detect_face(im, with_landmarks=True)

3. Face parsing

models.parser.face_parser.FaceParser(path_bisenet_weights=...)

Arguments

  • path_bisenet_weights: A string. Path to weights file of the model.

Example

from models.parser import face_parser

im = cv2.imread(PATH_TO_IMAGE)[..., ::-1]
fp = face_parser.FaceParser()
# fp.set_detector(fd) # fd = face_detector.FaceAlignmentDetector()
parsing_map = fp.parse_face(im, bounding_box=None, with_detection=False)

4. Eye region landmarks detection

models.detector.iris_detector.IrisDetector()

Faster face detection using MTCNN can be found in this repo.

Example

from models.detector import iris_detector

im = cv2.imread(PATH_TO_IMAGE)[..., ::-1]
idet = iris_detector.IrisDetector()
idet.set_detector(fd) # fd = face_detector.FaceAlignmentDetector()
eye_landmarks = idet.detect_iris(im)

5. Face verification

models.verifier.face_verifier.FaceVerifier(extractor="facenet", classes=512)

Argument

  • extractor: A string, one of facenet, insightface, ir50_ms1m, or ir50_asia.
  • classes: An integer. Dimension of output embeddings.

Example

from models.verifier import face_verifier

im1 = cv2.imread(PATH_TO_IMAGE1)[..., ::-1]
im2 = cv2.imread(PATH_TO_IMAGE2)[..., ::-1]
fv = face_verifier.FaceVerifier(extractor="facenet")
# fv.set_detector(fd) # fd = face_detector.FaceAlignmentDetector()
result, distance = fv.verify(im1, im2, threshold=0.5, with_detection=False, with_alignment=False, return_distance=True)

6. Gender and age estimation

models.estimator.gender_age_estimator.GenderAgeEstimator(model_type="insightface")

Arguments

  • model_type: A string, only insightface is supported now.

Example

from models.estimator import gender_age_estimator

im = cv2.imread(PATH_TO_IMAGE)[..., ::-1]
gae = gender_age_estimator.GenderAgeEstimator()
gae.set_detector(fd) # fd = face_detector.FaceAlignmentDetector()
gender, age = gae.predict_gender_age(im, with_detection=True)

Ported model weights

Known issues

It works fine on Colab at this point (2019/06/11) but for certain Keras/TensorFlow version, it throws errors loading 2DFAN-1_keras.h5 or 2DFAN-2_keras.h5.

Requirements

  • Keras 2.2.4
  • TensorFlow 1.12.0 or 1.13.1

Acknowledgments

We learnt a lot from 1adrianb/face-alignment, zllrunning/face-parsing.PyTorch, swook/GazeML, deepinsight/insightface, davidsandberg/facenet, and ZhaoJ9014/face.evoLVe.PyTorch.

face_toolbox_keras's People

Contributors

shaoanlu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

face_toolbox_keras's Issues

Face detection problem

Hi shaoanlu, first of all thank you for this project!

I used your project to detect face with my image but it didn't detect correctly comparing with the face-alignment (from 1adrianb).

Would you please help to fix this problem?

You can see the attachments for more details. The 3.jpeg is one of the file that the project doesn't detect correctly.

1adrianb_face-alignment
shaoanlu_face_toolbox_keras
3

Error in Colab Demo

Currently, I can't make the demo works in colab ...

The line fd = face_detector.FaceAlignmentDetector(lmd_weights_path="./models/detector/FAN/2DFAN-4_keras.h5")
return the error AttributeError: module 'tensorflow' has no attribute 'get_default_graph' ...

From several forum sources, it seems to be a tensorflow/keras version problem (some people advice to change from keras to from tensorflow.keras) but I can't really figure how and where to debug it as I don't really know how all the code works for now

Do you have any ideas on how to make it work ?

Thank you

Confusion with ir50 preprocessing

Hello @shaoanlu, first of all thank you for a great job porting the models to keras.
I'm unsure of what exactly is this step doing. I think that corresponds to a center crop of a tensor with shape (b,128,128,3) resulting in a tensor with shape (b,112,112,3), but the ir50 preprocessing layer is resizing the tensor to 112 x 112 beforehand.
Could this be a mistake?

Edit: I can confirm this shouldn't be in the preprocessing step, at least in combination with alignment.
Evaluation on LFW using face-alignment with MTCNN landmarks increased from 99.62% to 99.82% after removing that step.

H5 parser => Core ML

Hi shaoanlu, I really like to try this on iOS app, but have hard time to properly convert to mlmodel. Any help?

Speed

Does this run faster than using the original face-alignment? Is face detection using multiprocessing on the GPU? I'm using 1adrianb's face-alignment for bounding box and landmark detection, and would like to find a way to speed it up.

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'

Error in Demo


AttributeError Traceback (most recent call last)
in
1 fd = face_detector.FaceAlignmentDetector(
----> 2 lmd_weights_path="./models/detector/FAN/2DFAN-4_keras.h5"# 2DFAN-4_keras.h5, 2DFAN-1_keras.h5
3 )

7 frames
/usr/local/lib/python3.7/dist-packages/keras/backend/tensorflow_backend.py in get_uid(prefix)
72 """
73 global _GRAPH_UID_DICTS
---> 74 graph = tf.compat.v1.get_default_graph()
75 if graph not in _GRAPH_UID_DICTS:
76 _GRAPH_UID_DICTS[graph] = defaultdict(int)

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.