【前沿】见人识面,TensorFlow实现人脸性别/年龄识别

时间:2022-05-08
本文章向大家介绍【前沿】见人识面,TensorFlow实现人脸性别/年龄识别,主要内容包括【导读】近期,浙江大学学生Boyuan Jiang使用TensorFlow实现了一个人脸年龄和性别识别的工具,首先使用dlib来检测和对齐图片中的人脸,然后使用CNN深度网络来估计年龄和性别。代码已经在Github上开源,让我们来看下。、TensorFlow实现的人脸性别/年龄识别、安装python依赖包、使用方法、模型训练、测试模型、One picture demo、从摄像头获取的图片、待办、引用和声明、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

【导读】近期,浙江大学学生Boyuan Jiang使用TensorFlow实现了一个人脸年龄和性别识别的工具,首先使用dlib来检测和对齐图片中的人脸,然后使用CNN深度网络来估计年龄和性别。代码已经在Github上开源,让我们来看下。

TensorFlow实现的人脸性别/年龄识别

这是一个人脸年龄和性别识别的TensorFlow工具,首先使用dlib来检测和对齐图片中的人脸,然后使用CNN深度网络来估计年龄和性别。如下所示,该项目可以同时估计一张照片中的多个人脸 。

安装python依赖包

本项目需要以下依赖包,已经在CenotOS7系统上的Python2.7.14环境中测试过。

  • tensorflow==1.4
  • dlib==19.7.99
  • cv2
  • matplotlib==2.1.0
  • imutils==0.4.3
  • numpy==1.13.3
  • pandas==0.20.3

使用方法

编译 tfrecords

为了训练你自己的模型,你首先需要下载IMDB和wiki 两个数据集,下载地址分别为https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/static/imdb_crop.tar和 https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/static/wiki_crop.tar 。 然后将下载好的打包文件解压到data目录中。解压后images目录应该如下所示:

/path/to/project/data/imdb_crop/00/somepictures /path/to/project/data/imdb_crop/01/somepictures …. /path/to/project/data/imdb_crop/99/somepictures

然后你可以运行

> python convert_to_records_multiCPU.py --imdb --nworks 8

来将图片转化成tfrecords格式. —imdb 使用imdb数据集,—nworks 8 表示8核心的cpu并行转换数据。因为我们首先需要进行非常耗时的人脸检测和对齐步棸,所以我们建议使用尽可能多的核心数。Intel E5-2667 v4 带有 32 个核心运行完需要大概50分钟。

模型训练

一旦完成图片到tfrecords的转换,你应该会得到以下的目录:

/path/to/project/data/train/train-000.tfrecords …… /path/to/project/data/test/test-000.tfrecords ……

我们的CNN深度网络采用基于inception-resnet-v1的FaceNet架构来提取特征,为了加快训练速度,我们使用预训练好的模型权重(https://github.com/davidsandberg/facenet ),并已经微调权重来适应我们的模型,你可以下载这个预训练的facenet权重文件(https://mega.nz/#!4G4yxbAL!D9QG48yzCeFegCFhZfpCgOyLYbfDdU6lt2k2kK9n23gmodels**目录。),并解压到**

/path/to/project/models/checkpoint /path/to/project/models/model.ckpt-0.data-00000-of-00001 /path/to/project/models/model.ckpt-0.index /path/to/project/models/model.ckpt-0.meta

注意: 这一步是可选的,你可以从头开始训练自己的模型,运行以下命令来开始训练。

> python train.py --lr 1e-3 --weight_decay 1e-5 --epoch 6 --batch_size 128 --keep_prob 0.8 --cuda

NOTE: 使用 --cuda 将在GPU上训练模型.

使用 tensorboard 来可视化训练

> tensorboard --logdir=./train_log

测试模型

你可以通过以下命令在测试数据集上来检查训练好的模型

> python test.py --images "./data/test" --model_path "./models" --batch_size 128 --choose_best --cuda

--cuda 表示使用GPU来测试;—choose_best 表示测试所有的训练模型并选出最好的一个;如果你只想测试最新的一个训练模型就不用加这个参数。

> python test.py --images "./data/test" --model_path "./models" --batch_size 128 --cuda

One picture demo

如果你想在自己的图片上测试模拟

> python eval.py --I "./demo/demo.jpg" --M "./models/" --font_scale 1 --thickness 1

--I表示图片位置;如果图像上的文本太大或者太小,你可以使用不同的 --font_scale 1--thickness 1 值来调整文本大小和粗细。我们提供一个预训练的模型,你可以从(https://mega.nz/#!BfglkI7A!YBvFyxgKhvUnnNRu9FL-ACjdo18SmOZ-YSz9QghQRzE)或者 (https://pan.baidu.com/s/1bpllJg7models**)下载,并解压到** 路径中.

从摄像头获取的图片

首先从(https://mega.nz/#!BfglkI7A!YBvFyxgKhvUnnNRu9FL-ACjdo18SmOZ-YSz9QghQRzE)或者 (https://pan.baidu.com/s/1bpllJg7models**)下载,并解压到** 路径中。从摄像头中获得图片运行以下命令时出现问题,你需要卸载你的cv2并从源码重新安装(https://www.scivision.co/anaconda-python-opencv3/)

> python demo.py 

待办

  • x 项目版本一
  • x 代码检查
  • x 增加readme
  • 尝试使用其他轻量级的 CNN网络
  • x 增加从摄像头获取图片的演示

引用和声明

这个项目是我在浙大机器学习课程上的课程作业,以下是论文和代码引用说明

  1. Rothe R, Timofte R, Van Gool L. Dex: Deep expectation of apparent age from a single imageC//Proceedings of the IEEE International Conference on Computer Vision Workshops. 2015: 10-15.
  2. Rothe R, Timofte R, Van Gool L. Deep expectation of real and apparent age from a single image without facial landmarksJ. International Journal of Computer Vision, 2016: 1-14.
  3. IMDB-WIKI – 500k+ face images with age and gender labels(https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/)
  4. yu4u/age-gender-estimation(https://github.com/yu4u/age-gender-estimation)
  5. davidsandberg/facenet(https://github.com/davidsandberg/facenet)
  6. Face Alignment with OpenCV and Python(https://www.pyimagesearch.com/2017/05/22/face-alignment-with-opencv-and-python/)

参考链接:https://github.com//BoyuanJiang/Age-Gender-Estimate-TF

作者主页:https://www.aiboy.pub/