注:篇幅较长,持续更新状态
2019.4.10
阶段一:基于MTCNN的人脸检测
前期已有实现,遂不再重复。github代码持续更新,现更新到version 0.2,博客由于代码更新幅度较大且仅提供入门参考遂不再更新,如有更新那就是我有时间和节操了~。
version 0.1博客地址:人脸检测与识别:MTCNN人脸检测
github地址:https://github.com/friedhelm739/MTCNN-tensorflow
阶段二:基于insightface的人脸识别
项目环境及配置:ubuntu16.04+2*GTX 1080ti+Python3.6+Anaconda5.2.0+Tensorflow1.7-gpu
本阶段是对《ArcFace: Additive Angular Margin Loss for Deep Face Recognition》论文的复现,网上解读文章很多,大家可以择优选读,关于代码解读有一系列比较好的解读,对入门理解源码有一定的帮助。
博客地址:人脸检测与识别:基于MTCNN与insightface的人脸打卡系统
github地址:https://github.com/friedhelm739/Insightface-tensorflow
本阶段代码参考:
- https://github.com/deepinsight/insightface
- https://github.com/luckycallor/InsightFace-tensorflow (非常感谢)
- https://github.com/auroua/InsightFace_TF
- https://github.com/tensorflow/models
在此对其表示衷心的感谢。
1、数据获取与处理
本文数据可以很轻松的从源代码的Dataset Zoo内获取,本文使用CASIA数据集。
下载后解压成如图1形式:
其中训练用的是train.idx和train.rec,其他的bin文件都是验证用的。
解压后需要使用/data/gen_tfrecord_mxdata.py将原MXNet训练数据格式转换为tensorflow的TFRecord格式,代码直接抄袭,如下所示:
# -*- coding: utf-8 -*-
"""
@author: friedhelm
"""
import tensorflow as tf
import mxnet as mx
import os
import io
import numpy as np
import cv2
import time
from scipy import misc
import argparse
from core import config
def arg_parse():
parser=argparse.ArgumentParser()
parser.add_argument("--read_dir",default=config.mxdata_dir,type=str, help='directory to read data')
parser.add_argument("--save_dir",default=config.tfrecord_dir,type=str, help='path to save TFRecord file')
return parser
def main():
with tf.python_io.TFRecordwriter(save_dir) as writer:
idx_path = os.path.join(read_dir, 'train.idx')
bin_path = os.path.join(read_dir, 'train.rec')
imgrec = mx.recordio.MXIndexedRecordIO(idx_path, bin_path, 'r')
s = imgrec.read_idx(0)
header, _ = mx.recordio.unpack(s)
imgidx = list(range(1, int(header.label[0])))
labels = []
for i in imgidx:
img_info = imgrec.read_idx(i)
header, img = mx.recordio.unpack(img_info)
label = int(header.label)
labels.append(label)
img = io.BytesIO(img)
img = misc.imread(img).astype(np.uint8)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
#img = cv2.resize(img, (112,112))
img_raw = img.tobytes()
example=tf.train.Example(features=tf.train.Features(feature={
"img" : tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])),
"label" : tf.train.Feature(int64_list=tf.train.Int64List(value=[label])),
}))
writer.write(example.SerializeToString())
if i % 10000 == 0:
print('%d pics processed' % i,"time: ", time.time()-begin)
if __name__ == "__main__":
parser=arg_parse()
save_dir=parser.save_dir
read_dir=parser.read_dir
begin=time.time()
main()
测试数据直接使用,如想转换请参考上述代码。测试数据参照源码,是使用MTCNN检测人脸得到的,下面以lfw为例,代码路径:/data/gen_lfw_data.py。代码基本为搬运源码,由于数据集的特性,MTCNN会检测出多张未标注人脸,或者同一人脸多检测框的情况,这时源码使用以图片中心为准的思路,只取与图片中心点偏差最小的人脸框,。
# -*- coding: utf-8 -*-
"""
@author: friedhelm
"""
import sys
sys.path.append("../")
from core.MTCNN.mtcnn_detector import MTCNN_Detector
from core.MTCNN.MTCNN_model import Pnet_model,Rnet_model,Onet_model
import numpy as np
import os
from collections import namedtuple
from easydict import EasyDict as edict
from scipy import misc
import cv2
from collections import namedtuple
from core import config
import argparse
from core.tool import preprocess
def arg_parse():
parser=argparse.ArgumentParser()
parser.add_argument("--input_dir",default=config.lfw_dir,type=str, help='directory to read lfw data')
parser.add_argument("--output_dir",default=c