Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyun1 committed Jul 30, 2020
1 parent 68890e7 commit 120bfad
Show file tree
Hide file tree
Showing 145 changed files with 6,761 additions and 9,393 deletions.
547 changes: 547 additions & 0 deletions IResnet.py

Large diffs are not rendered by default.

11 changes: 0 additions & 11 deletions Makefile

This file was deleted.

56 changes: 42 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
In this repository you will learn to build your own neural-network from scratch. To get started make sure you have `git`, a C/C++ compiler, and `make` installed. Then run:
## 人脸识别
--------------------
这个仓库是基于 arcface 论文上完成的,其中主要分为四大块:人脸检测、人脸矫正、提取特征和特征比对。各个模块的大小和在 17 款 macbook-pro 的 CPU 上跑耗时如下:

- 人脸检测:使用的是 mtcnn 网络,模型大小约 1.9MB,耗时约 70ms;
- 人脸矫正:OpenCV 的仿射变换,耗时约 0.83ms;
- 提取特征:使用 MobileFaceNet 和 IResNet 网络;
- 特征比对:使用曼哈顿距离,单次搜索和完成比对耗时约 0.011 ms;

⏳ Contents
--------------------

- [x] support to load image,more details see [example01.cpp](https://github.com/YunYang1994/yynet/blob/master/examples/example01.cpp)
- [x] support to resize, copy and gray in image processing,more details see [example02.cpp](https://github.com/YunYang1994/yynet/blob/master/examples/example02.cpp)
- [x] implement a fully connected layer to classify mnist data
## 注册人脸

⚙️ Useage
--------------------
注册人脸的方式有两种,分别是:

1. 打开相机注册:

- download mnist data
```bashrc
$ wget https://pjreddie.com/media/files/mnist.tar.gz
$ tar xvzf mnist.tar.gz
$ python register_face.py -person Sam -camera
```
- make your example

`s` 键保存图片,需要在不同距离和角度拍摄 10 张图片或者按 `q` 退出。

2. 导入人脸图片:

保证文件的名字与注册人名相同,并且每张图片只能出现一张这个 ID 的人脸。


```bashrc
$ make example01
$ ./example01 images/sample.png 3
$ python register_face.py -person Jay
```

## 识别人脸

|Method | LFW(%) | CFP-FP(%) | AgeDB-30(%) | MegaFace(%)| TensorFlow | 权重链接 |
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
| MobileFaceNet | 99.50 | 88.94 | 95.91 | --- | 35ms | [提取码: xgmo](https://pan.baidu.com/s/1QIYpHYazaPMTI0E15WRGug)
| MobileFaceNet | 99.77 | 98.27 | 98.28 | 98.47 | 435ms | --



识别模型用的是 `MobileFaceNet` 网络,这里直接使用了 [insightface](https://github.com/deepinsight/insightface) 在 ms1m-refine-v1 三百万多万张人脸数据集上训练的模型。这部分工作在 `mxnet` 分支上,你可以通过 `git checkout mxnet` 进行切换。

由于该模型是 mxnet 格式,因此使用了 [mmdnn](https://github.com/microsoft/MMdnn) 导出了其模型权重 `mobilefacenet.npy`。接着使用了 `TF2` 自己手写了一个 `MobileFaceNet` 网络并导入权重,预测精度没有任何损失。这部分工作在 `master` 分支上。

最后,如果你要识别人脸,可以执行:

```bashrc
$ python main.py
```


Binary file added database/ChenHe/0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/10.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/11.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/12.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/13.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/14.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/15.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/17.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/18.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/19.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/20.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/21.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/22.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/23.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/24.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/25.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/26.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/27.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/28.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/29.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/30.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added database/ChenHe/5.jpg
Binary file added database/ChenHe/6.jpg
Binary file added database/ChenHe/7.jpg
Binary file added database/ChenHe/8.jpg
Binary file added database/ChenHe/9.jpg
Binary file added database/ChenHe/ChenHe.npy
Binary file not shown.
Binary file added database/Jay/0.jpg
Binary file added database/Jay/1.jpg
Binary file added database/Jay/10.jpg
Binary file added database/Jay/11.jpg
Binary file added database/Jay/12.jpg
Binary file added database/Jay/13.jpg
Binary file added database/Jay/14.jpg
Binary file added database/Jay/15.jpg
Binary file added database/Jay/16.jpg
Binary file added database/Jay/17.jpg
Binary file added database/Jay/18.jpg
Binary file added database/Jay/19.jpg
Binary file added database/Jay/2.jpg
Binary file added database/Jay/20.jpg
Binary file added database/Jay/22.jpg
Binary file added database/Jay/23.jpg
Binary file added database/Jay/24.jpg
Binary file added database/Jay/25.jpg
Binary file added database/Jay/26.jpg
Binary file added database/Jay/27.jpg
Binary file added database/Jay/29.jpg
Binary file added database/Jay/3.jpg
Binary file added database/Jay/30.jpg
Binary file added database/Jay/4.jpg
Binary file added database/Jay/5.jpg
Binary file added database/Jay/6.jpg
Binary file added database/Jay/7.jpg
Binary file added database/Jay/8.jpg
Binary file added database/Jay/9.jpg
Binary file added database/Jay/Jay.npy
Binary file not shown.
Binary file added database/KunLin/0.jpg
Binary file added database/KunLin/1.jpg
Binary file added database/KunLin/10.jpg
Binary file added database/KunLin/11.jpg
Binary file added database/KunLin/12.jpg
Binary file added database/KunLin/13.jpg
Binary file added database/KunLin/14.jpg
Binary file added database/KunLin/15.jpg
Binary file added database/KunLin/16.jpg
Binary file added database/KunLin/17.jpg
Binary file added database/KunLin/19.jpg
Binary file added database/KunLin/2.jpg
Binary file added database/KunLin/20.jpg
Binary file added database/KunLin/21.jpg
Binary file added database/KunLin/23.jpg
Binary file added database/KunLin/24.jpg
Binary file added database/KunLin/25.jpg
Binary file added database/KunLin/26.jpg
Binary file added database/KunLin/27.jpg
Binary file added database/KunLin/28.jpg
Binary file added database/KunLin/3.jpg
Binary file added database/KunLin/30.jpg
Binary file added database/KunLin/4.jpg
Binary file added database/KunLin/5.jpg
Binary file added database/KunLin/6.jpg
Binary file added database/KunLin/7.jpg
Binary file added database/KunLin/8.jpg
Binary file added database/KunLin/9.jpg
Binary file added database/KunLin/KunLin.npy
Binary file not shown.
Binary file added database/ZhangHanYu/0.jpg
Binary file added database/ZhangHanYu/1.jpg
Binary file added database/ZhangHanYu/10.jpg
Binary file added database/ZhangHanYu/11.jpg
Binary file added database/ZhangHanYu/12.jpg
Binary file added database/ZhangHanYu/13.jpg
Binary file added database/ZhangHanYu/14.jpg
Binary file added database/ZhangHanYu/16.jpg
Binary file added database/ZhangHanYu/17.jpg
Binary file added database/ZhangHanYu/18.jpg
Binary file added database/ZhangHanYu/19.jpg
Binary file added database/ZhangHanYu/2.jpg
Binary file added database/ZhangHanYu/20.jpg
Binary file added database/ZhangHanYu/22.jpg
Binary file added database/ZhangHanYu/23.jpg
Binary file added database/ZhangHanYu/26.jpg
Binary file added database/ZhangHanYu/27.jpg
Binary file added database/ZhangHanYu/28.jpg
Binary file added database/ZhangHanYu/29.jpg
Binary file added database/ZhangHanYu/3.jpg
Binary file added database/ZhangHanYu/30.jpg
Binary file added database/ZhangHanYu/4.jpg
Binary file added database/ZhangHanYu/5.jpg
Binary file added database/ZhangHanYu/6.jpg
Binary file added database/ZhangHanYu/7.jpg
Binary file added database/ZhangHanYu/8.jpg
Binary file added database/ZhangHanYu/9.jpg
Binary file added database/ZhangHanYu/ZhangHanYu.npy
Binary file not shown.
15 changes: 15 additions & 0 deletions deploy/convert_to_ir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#! /bin/bash

#================================================================
# Copyright (C) 2020 * Ltd. All rights reserved.
#
# Editor : VIM
# File name : convert_to_ir.sh
# Author : YunYang1994
# Created date: 2020-02-28 16:06:54
# Description :
#
#================================================================

python3 -m mmdnn.conversion._script.convertToIR -f mxnet -n model-symbol.json -w model-0000.params --inputShape 3,112,112 -o mobilefacenet
python3 -m mmdnn.conversion._script.IRToCode -f tensorflow --IRModelPath mobilefacenet.pb --IRWeightPath mobilefacenet.npy --dstModelPath tf_mobilefacenet.py
61 changes: 61 additions & 0 deletions deploy/convert_to_tflite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#! /usr/bin/env python
# coding=utf-8
#================================================================
# Copyright (C) 2020 * Ltd. All rights reserved.
#
# Editor : VIM
# File name : convert_to_tflite.py
# Author : YunYang1994
# Created date: 2020-02-15 14:47:28
# Description :
#
#================================================================

import sys
sys.path.append("../")

import numpy as np
import tensorflow as tf
from mtcnn import PNet, RNet, ONet

def load_weights(model, weights_file):
weights_dict = np.load(weights_file, encoding='latin1').item()
for layer_name in weights_dict.keys():
layer = model.get_layer(layer_name)
if "conv" in layer_name:
layer.set_weights([weights_dict[layer_name]["weights"], weights_dict[layer_name]["biases"]])
else:
prelu_weight = weights_dict[layer_name]['alpha']
try:
layer.set_weights([prelu_weight])
except:
layer.set_weights([prelu_weight[np.newaxis, np.newaxis, :]])
return True

pnet, rnet, onet = PNet(), RNet(), ONet()
pnet(tf.ones(shape=[1, 12, 12, 3]))
rnet(tf.ones(shape=[1, 24, 24 ,3]))
onet(tf.ones(shape=[1, 48, 48, 3]))
load_weights(pnet, "./det1.npy"), load_weights(rnet, "./det2.npy"), load_weights(onet, "./det3.npy")

pnet.predict(tf.ones(shape=[1, 12, 12, 3]))
pnet_converter = tf.lite.TFLiteConverter.from_keras_model(pnet)
pnet_converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
with open("pnet.tflite", "wb") as f:
pnet_tflite_model = pnet_converter.convert()
f.write(pnet_tflite_model)

rnet.predict(tf.ones(shape=[1, 24, 24, 3]))
rnet_converter = tf.lite.TFLiteConverter.from_keras_model(rnet)
rnet_converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
with open("rnet.tflite", "wb") as f:
rnet_tflite_model = rnet_converter.convert()
f.write(rnet_tflite_model)

onet.predict(tf.ones(shape=[1, 48, 48, 3]))
onet_converter = tf.lite.TFLiteConverter.from_keras_model(onet)
onet_converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
with open("onet.tflite", "wb") as f:
onet_tflite_model = onet_converter.convert()
f.write(onet_tflite_model)

Binary file added deploy/model-0000.params
Binary file not shown.
Loading

0 comments on commit 120bfad

Please sign in to comment.