Git Product home page Git Product logo

facial-landmark-detection-hrnet's Introduction

facial-landmark-detection-hrnet

A TensorFlow implementation of HRNet for facial landmark detection.

ms_marvel

Watch this demo video: HRNet Facial Landmark Detection (bilibili).

Features

  • Support multiple public dataset: WFLW, IBUG, etc.
  • Advanced model architecture: HRNet v2
  • Data augmentation: randomly scale/rotate/flip
  • Model optimization: quantization, pruning

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

TensorFlow TensorFlow Model Optimizer Toolkit OpenCV Numpy

Installing

Get the source code for training

# From your favorite development directory
git clone --recursive https://github.com/yinguobing/facial-landmark-detection-hrnet.git

Generate the training data

There are multiple public facial mark datasets available which can be used to generate training heatmaps we need. For this training process the images will be augmented. The first step is transforming the dataset into a more uniform distribution that is easier to process. You can do this yourself or, use this repo:

# From your favorite development directory
git clone https://github.com/yinguobing/face-mesh-generator.git

# Checkout the desired branch
git checkout features/export_for_mark_regression

Use the module generate_mesh_dataset.py to generate training data. Popular public datasets like IBUG, 300-W, WFLW are supported. Checkout the full list here: facial-landmark-dataset.

Training

Deep neural network training can be complicated as you have to make sure everything is ready like datasets, checkpoints, logs, etc. But do not worry. Following these steps you should be fine.

Setup the model.

In the module train.py, setup your model's name and the number of marks.

# What is the model's name?
name = "hrnetv2"

# How many marks are there for a single face sample?
number_marks = 98

Set the training and testing datasets

These files do not change frequently so set them in the source code. Take WFLW as an example.

# Training data.
train_files_dir = "/path/to/wflw_train"

# Testing data.
test_files_dir = "/path/to/wflw_test"

Set the validation datasets

The loss value from this dataset will be used to decide which checkpoint should be preserved. Set None if no files available. Then about 512 of the training files will be used as validation samples.

# Validation data.
val_files_dir = None

Provide a sanity check image

This sample image will be logged into TensorBoard with detected marks drawing on it. In this way you can check the model's behavior visually during training.

sample_image = "docs/face.jpg"

Start training

Set the hyper parameters in the command line.

python3 train.py --epochs=80 --batch_size=32

Training checkpoints can be found in directory checkpoints. Before training started, this directory will be checked and the model will be restored if any checkpoint is available. Only the best model (smallest validation loss) will be saved.

Resume training

If training was interrupted, resume it by providing --initial_epoch argument.

python3 train.py --epochs=80 --initial_epoch=61

Monitor the training process

Use TensorBoard. The log and profiling files are in directory logs

tensorboard --logdir /path/to/facial-landmark-detection-hrnet/logs

Training speedup

You can download this checkpoint file to speedup the training process.

Evaluation

A quick evaluation on validation datasets will be performed automatically after training. For a full evaluation, please run the evaluate.py file. The NME value will be printed after evaluation.

python3 evaluate.py

Export

Even though the model wights are saved in the checkpoint, it is better to save the entire model so you won't need the source code to restore it. This is useful for inference and model optimization later.

For cloud/PC applications

Exported model will be saved in saved_model format in directory exported. You can restore the model with Keras directly. Loading the model in OpenCV is also supported.

python3 train.py --export_only=True

For Android phones, embedded and IoT devices

TensorFlow lite and TensorFlow Model Optimization Toolkit will help you to get a optimized model for these applications. Please follow the instructions of the later section Optimization.

For iPhone

Apple has developed a conversion tool named coremltools which can convert and quantize the TensorFlow model into the native model format supported and accelrated by iPhone's Neural Engine.

# Install the package
pip install --upgrade coremltools

# Do the conversion.
python3 coreml_conversion.py

Inference

Check out module predict.py for details.

A pre-trained model is provided in case you want to try it in no time, or do not have adequate equipments to train it yourself.

URL: https://pan.baidu.com/s/1EQsB0LnSkfvoNjMvkFV5dQ
Access code: qg5e

Optimization

Optimize the model so it can run on mobile, embedded, and IoT devices. TensorFlow supports post-training quantization, quantization aware training, pruning, and clustering.

Post training quantization

There are multiple means for post training quantization: dynamic range, integer only, float16. To quantize the model, run:

python3 quantization.py

Quantized tflite file will be find in the optimized directory.

Pruning

Model pruning could dramatically reduce the model size while minimize the side effects on model accuracy. There is a demo video showing the performance of a pruned model with 80% of weights pruned (set to zero): TensorFlow model pruning (bilibili)

To prune the model in this repo, run:

python3 pruning.py

Pruned model file will be find in the optimized directory.

Quantization aware training

Due to the conflict between pruning and quantization aware training, please checkout the other branch for details.

git checkout features/quantization-aware-training
python3 train.py --quantization=True

Authors

Yin Guobing (尹国冰) - yinguobing

wechat

License

GitHub

Acknowledgments

The HRNet authors and the dataset authors who made their work public.

facial-landmark-detection-hrnet's People

Contributors

yinguobing 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

facial-landmark-detection-hrnet's Issues

asking about dataset creation

Hello @yinguobing, thank for your code.

One thing I'm not really understand about the creation of dataset for training. From your description, we need use the code in repository: https://github.com/yinguobing/face-mesh-generator to generate the 'Cropped' images, which is from this line: https://github.com/yinguobing/face-mesh-generator/blob/master/generate_heatmap_dataset.py#L131. So, I need to write some piece of code to save the 'image_resized' and 'mark_resized' somewhere else to constructing the dataset, is it correct ?

I see that you use the cropped and centered images as training data. However, for the real image, the face will not be in the center, therefore, I think this preprocessing will reduce the capability of the model isn't it ?

No access to the pretrained-model

Hello author,

is it possible to gain access to the pre-trained model besides the pan.baidu link you have posted in the readme file?
Doesn't work if you are Greek (cant even create an account :P). Working on my thesis project! Thanks for your time

how to create dataset?

Hello author, I'm having trouble with the "Set the training and testing datasets" step ,i didn't find any file call "wflw_train" & "wflw_train" in WFLW_images file?how can i create these two file ? Thank you.

Model Training

Hello, thank you so much for sharing your work. Your examples are very helpful for me learning about keypoint detection.

I have a question about training this model, when I train on the WFLW dataset I get increasing validation error after only a few training epochs. Is this from not enough training data, or is there some error in my setup?

For the saved checkpoint you provided, how many epochs was this trained and on which data sets?

image

need your help

Hello thanks for your code.
i want to train my model on nails images.
how can i do this.
i was convert the dataset json to .pts format.
like this version: 1
n_points :4
{
328.15 136.54
332.96 174.04
370.46 175.0
375.27 135.58
}
now what can i do for next steps .
can i pass the direct .pts files on train.py?
can i convert to pts into other format?
please help !

Checkpoints on Drive or Dropbox

Hello,
Thank you for sharing the work with us. It is very useful.
Can you please share the pretrained checkpoints on Google Drive or DropBox or something else than Baidu?
Thank you

No module named 'face_detector.detector

大佬你好,我发现git下面的 face_detector文件夹下面的超链接是错的,导致我运行predict.py的时候报错 ModuleNotFoundError: No module named 'face_detector.detector', 请问能否修复一下该链接? 非常感谢!

Help to load HRNet model with OpenCV

Hi,

First congratulate you on your amazing project. I have trained the network without problems and I was able to run the predict.py script on my webcam to check the operation of the model.

However, now I wanted to do a proof of concept to load this model with OpenCV DNN module. I have tried the following:

  1. Load the model in SavedModel format directly using cv.dnn.readNetFromTensorflow ("./ exported")

  2. Freeze the model and load the protobuf file with cv.dnn.readNetFromTensorflow ("frozen_graph.pb")

  3. Convert the model to OpenVino format and load it with cv.dnn.readNetFromModelOptimizer ("./ frozen_graph.xml", "frozen_graph.bin")

All these attempts return the following error when executing net.forward ():

cv2.error: OpenCV (4.3.0-openvino) /home/suaro/libs/opencv/repo/opencv/modules/dnn/src/dnn.cpp:2887: error: (-215: Assertion failed)! layers [0 ] .outputBlobs.empty () in function 'allocateLayers'

I suppose the problem is that you have to apply some kind of modification to the model when optimizing it to be able to load it with OpenCV, but I am not sure where to start.

Could you help me?

Thanks a lot.

运行报错

您好,作为一个小白,看到您的博文后想运行一下代码,但是发生了错误。下边是报错情况和我觉得出问题的地方。
File "C:/Users/RPPG/Desktop/pyCode/facial-landmark-detection-hrnet-master/predict.py", line 37, in
model = tf.keras.models.load_model("assets/face_model/saved_model.pb")
File "C:\Users\RPPG\Anaconda3\envs\ippg\lib\site-packages\tensorflow\python\keras\saving\save.py", line 211, in load_model
loader_impl.parse_saved_model(filepath)
File "C:\Users\RPPG\Anaconda3\envs\ippg\lib\site-packages\tensorflow\python\saved_model\loader_impl.py", line 111, in parse_saved_model
raise IOError("SavedModel file does not exist at: %s/{%s|%s}" %
OSError: SavedModel file does not exist at: assets/face_model/saved_model.pb/{saved_model.pbtxt|saved_model.pb}

Restore the model.

model = tf.keras.models.load_model("./exported/hrnetv2")

因为在百度云盘下载后,我将其中的内容放在了asset中,也不知道对不对。
谢谢!

Multi-face detection

hello author,

Does this repo has multi-face keyoint detection support?
I want to ask wheter the COCO person dataset is specialized for multi-person?
i have trained a simple model using FCN for face keypoint regression. Can you please tell me how we can get heatmaps of muliple faces in the bottom-up approach. My current model takes input of 96x96x1 image and gives 96x96x15 size heatmaps for 15 keypoints. I trained my model using kaggle datset consisting of images with single face pre cropped to 96x96. Do i need the dataset with multiple faces? and do I need bouding box information or mask information too?

Please give me your advice
thank you

Training error

Hello @yinguobing

It's me again,
I have found there is a problem in the implementation, when I was trying to train the network with the 300VW dataset, the following problem occurred:
ValueError: could not broadcast input array from shape (19,19) into shape (20,19)

It turned out that the problem appear in here: https://github.com/yinguobing/facial-landmark-detection-hrnet/blob/master/mark_operator.py#L112-L113

The int() is not always do the same thing, as you can see in the debugging output below:
Screenshot 2020-12-10 at 18 55 16

It's easy to solve this problem, I have replaced the int function by the following:

def r_int(f_number):
    if f_number >= int(f_number) + 0.5:
        return int(f_number) + 1
    else:
        return int(f_number)

WFLW数据集使用

由于环境问题我无法使用提供的面网生成器去生成json文件。您是否能通过百度云/谷歌硬盘来提供一下用于训练的WFLW数据呢?麻烦您了

Stability and accuracy

Yet I am working on project with dlib in unity. I am using 68_face_landmarks.dat but its not enough stable and accurate. So face mesh seems very vibrating. Can you share me your facial landmarks module as .dat file or can you guide me how can I use this dataset in unity.

NME with original image dimensions

Hello,
Thank you @yinguobing for your work and blog, I am really enjoying them.

In the evaluation, before we compute NME, we to transform the marks back to the original image dimensions.
Do you know why the transform of the marks back to the original image dimensions is necessary for the NME ?

Thank you

edge TPU版本

作者您好,我使用你的开源代码去运行quantization.py,成功生成了5个tflite版本,我选取全整型的hrnet_quant_int_only.tflite,在ubuntu上用edgetpu_compiler optimized/hrnet_quant_int_only.tflite进行编译,尝试使用TPU运行模型。但是我得到了这样的报错

generate_mesh_dataset.py

after I use generate_mesh_dataset.py,A 300vw.record file was generated.But how should I use this file for model training.

Traceback (most recent call last):
  File "train.py", line 167, in <module>
    model.fit(dataset_train,
  File "/Users/liuyuhan/miniforge3/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1110, in fit
    raise ValueError('Expect x to be a non-empty array or dataset.')
ValueError: Expect x to be a non-empty array or dataset.

how come can use tfrecord by face-mesh-generator?

Hi, I finished wflw dataset using face-mesh-generator in colab environment. I just got tfrecord and what is that? How can I use it for training on hrnet? do I have to save heatmapped image indivisually like figs??

when I check train.py file, train dir path is wflw_cropped folder. how can I access crop? that means need to use face detector?

image_size

i have see your work, it is beautiful !
i have do all works before training, and i have see the HRNet lib for face landmark, it seem to have different size in input and output for images at current work ?

Need Help

(base) C:\Users\user\Desktop\facial-landmark-detection-hrnet\face-mesh-generator>python generate_mesh_dataset.py
Traceback (most recent call last):
File "C:\Users\user\Desktop\facial-landmark-detection-hrnet\face-mesh-generator\generate_mesh_dataset.py", line 342, in
ds_wflw.populate_dataset(wflw_dir)
File "C:\Users\user\Desktop\facial-landmark-detection-hrnet\face-mesh-generator\fmd\wflw.py", line 71, in populate_dataset
_read_mark_file(mark_file_train)
File "C:\Users\user\Desktop\facial-landmark-detection-hrnet\face-mesh-generator\fmd\wflw.py", line 60, in _read_mark_file
with open(mark_file) as fid:
FileNotFoundError: [Errno 2] No such file or directory: '/home/robin/data/facial-marks/wflw\WFLW_annotations\list_98pt_rect_attr_train_test\list_98pt_rect_attr_train.txt'

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.