预训练pytorch人脸检测与识别模型
facenet-pytorch的Python项目详细描述
使用pytorch进行人脸识别
System | Python | |
---|---|---|
Linux | 3.5, 3.6, 3.7 | |
macOS | 3.6, 3.7 | |
Windows | 3.5, 3.6, 3.7 |
这是Pythorch中inception resnet(v1)模型的存储库,在vggface2和casia webface上进行了预训练。
pytorch模型权重是使用david sandberg的tensorflow facenet repo中移植的参数初始化的。
该报告还包括一个高效的mtcnn的pytorch实现,用于推理前的人脸检测。这些模型也经过了预训练。
快速启动
- 使用pip安装:
pip install facenet-pytorch
或者克隆此repo,删除“-”以允许python导入:git clone https://github.com/timesler/facenet-pytorch.git facenet_pytorch
- 在python中,导入模块:
fromfacenet_pytorchimportMTCNN,InceptionResnetV1
- 如果需要,使用mtcnn创建面detection管道:
mtcnn=MTCNN(image_size=<image_size>,margin=<margin>)
- 创建初始资源网(在评估模式下):
resnet=InceptionResnetV1(pretrained='vggface2').eval()
- 处理图像:
fromPILimportImageimg=Image.open(<imagepath>)# Get cropped and prewhitened image tensorimg_cropped=mtcnn(img,save_path=<optionalsavepath>)# Calculate embedding (unsqueeze to add batch dimension)img_embedding=resnet(img_cropped.unsqueeze(0))# Or, if using for VGGFace2 classificationresnet.classify=Trueimg_probs=resnet(img_cropped.unsqueeze(0))
有关用法和实现的详细信息,请参见help(MTCNN)
和help(InceptionResnetV1)
。
预训练模型
见:models/inception_resnet_v1.py
以下型号已移植到Pythorch(带有下载Pythorch State的链接):
Model name | LFW accuracy (as listed here) | Training dataset |
---|---|---|
20180408-102900 (111MB) | 0.9905 | CASIA-Webface |
20180402-114759 (107MB) | 0.9965 | VGGFace2 |
无需手动下载预训练状态;它们在模型实例化时自动下载,并缓存在torch缓存中以备将来使用。要在pytorch中使用inception resnet(v1)模型进行面部识别/识别,请使用:
fromfacenet_pytorchimportInceptionResnetV1# For a model pretrained on VGGFace2model=InceptionResnetV1(pretrained='vggface2').eval()# For a model pretrained on CASIA-Webfacemodel=InceptionResnetV1(pretrained='casia-webface').eval()# For an untrained modelmodel=InceptionResnetV1().eval()# For an untrained 1001-class classifiermodel=InceptionResnetV1(classify=True,num_classes=1001).eval()
两个预先训练的模型都是在160x160px的图像上训练的,所以如果应用于调整到这个形状的图像,效果会最好。为了获得最佳效果,图像也应该使用mtcnn裁剪到面部(见下文)。
默认情况下,上述模型将返回512维图像嵌入。若要启用分类,请将classify=True
传递给模型构造函数,或者随后使用model.classify = True
设置对象属性。对于vggface2,预训练模型将输出长度为8631的概率向量,对于长度为10575的casia webface概率向量。
完整的检测和识别管道
人脸识别可以很容易地应用于原始图像,首先使用mtcnn检测人脸,然后使用初始resnet模型计算嵌入或概率。
位于models/utils/example.py的示例代码提供了利用数据集、数据加载器和可选gpu处理的完整示例管道。从repo目录中,可以使用python -c "import models.utils.example"
运行此命令。
注意,对于真实的数据集,应该修改代码以控制传递给resnet的批大小,特别是在gpu上处理时。此外,对于重复测试,最好将人脸检测(使用mtcnn)与嵌入或分类(使用inceptionresnetv1)分离,因为检测可以执行一次,并将检测到的人脸保存以备将来使用。
在您自己的git项目中使用此repo
要在您自己的git repo中使用pretrained mtcnn和inception resnet v1模型,我建议首先将此repo添加为一个子模块。请注意,在作为子模块克隆时,应删除repo名称中的短划线(“-”),因为它将在导入时中断python:
git submodule add https://github.com/timesler/facenet-pytorch.git facenet_pytorch
或者,可以使用pip:
pip install facenet-pytorch
然后,可以简单地使用以下命令实例化模型:
fromfacenet_pytorchimportMTCNN,InceptionResnetV1mtcnn=MTCNN()resnet=InceptionResnetV1(pretrained='vggface2').eval()
从tensorflow到pytorch的参数转换
见:models/utils/tensorflow2pytorch.py
请注意,使用此repo中的模型不需要此功能,它仅依赖于保存的pytorch state_dict
。
在pytorch模型实例化之后,从davidsandberg/facenet的预训练张量流模型中的等效层加载每个层的权重。
已测试了原始Tensorflow模型和Pythorch移植模型的输出的等效性,它们是相同的:
>>> compare_model_outputs(mdl, sess, torch.randn(5, 160, 160, 3).detach())
Passing test data through TF model
tensor([[-0.0142, 0.0615, 0.0057, ..., 0.0497, 0.0375, -0.0838],
[-0.0139, 0.0611, 0.0054, ..., 0.0472, 0.0343, -0.0850],
[-0.0238, 0.0619, 0.0124, ..., 0.0598, 0.0334, -0.0852],
[-0.0089, 0.0548, 0.0032, ..., 0.0506, 0.0337, -0.0881],
[-0.0173, 0.0630, -0.0042, ..., 0.0487, 0.0295, -0.0791]])
Passing test data through PT model
tensor([[-0.0142, 0.0615, 0.0057, ..., 0.0497, 0.0375, -0.0838],
[-0.0139, 0.0611, 0.0054, ..., 0.0472, 0.0343, -0.0850],
[-0.0238, 0.0619, 0.0124, ..., 0.0598, 0.0334, -0.0852],
[-0.0089, 0.0548, 0.0032, ..., 0.0506, 0.0337, -0.0881],
[-0.0173, 0.0630, -0.0042, ..., 0.0487, 0.0295, -0.0791]],
grad_fn=<DivBackward0>)
Distance 1.2874517096861382e-06
为了重新运行tensorflow参数到pytorch模型的转换,请确保使用子模块克隆此repo,作为davidsandberg/facenet repo是作为子模块包含的,部分子模块是转换所必需的。
参考文献
大卫桑德伯格的facenet回购协议:https://github.com/davidsandberg/facenet
F.Schroff、D.Kalenichenko、J.Philbin。facenet:用于人脸识别和聚类的统一嵌入,arxiv:1503.038322015。PDF
曹先生,沈先生,谢先生,帕克希先生,A.齐瑟曼先生。vggface2:一个跨姿势和年龄的人脸识别数据集,国际自动人脸和手势识别会议,2018年。PDF
易,雷,廖,李。casiawebface:learning face representation from scratch,arxiv:1411.79232014年。PDF
张先生,张先生,李先生和乔先生。使用多任务级联卷积网络的联合人脸检测和对齐,ieee信号处理快报,2016年。PDF