Age and Gender Deep Learning with TensorFlow实践

mark一下,感谢作者分享!
https://github.com/dpressel/rude-carnie

按照作者github上的步骤,我们很容易就可以用作者提供的预训练模型进行人脸年龄和性别进行预测。现进行详细介绍。

首先进入github,下载rude-carnie-master.zip。
这里写图片描述

请详细阅读README.md。

Running
There are several ways to use a pre-existing checkpoint to do age or gender classification. By default, the code will simply assume that the image you provided has a face in it, and will run that image through a multi-pass classification using the corners and center.

The –class_type parameter controls which task, and the –model_dir controls which checkpoint to restore. There are advanced parameters for the checkpoint basename (–checkpoint) and the requested step number if there are multiple checkpoints in the directory (–requested_step)

Here is a run using Age classification on the latest checkpoint in a directory using 12-look (all corners + center + resized, along with flipped versions) averaging:

$ python guess.py - -model_dir /home/dpressel/dev/work/AgeGenderDeepLearning/Folds/tf/age_test_fold_is_1/run-20854 - -filename /home/dpressel/Downloads/portraits/prince.jpg
model_dir:模型路径,run-20854为训练好的模型文件夹。若不对模型训练,则只需要下载训练好的模型,修改模型路径为下载后的路径,运行即可。

预训练的模型,此项目中涉及到的所有文件,我会上传整个工程,百度网盘或者github。

上述命令,假设传入的图片已经有人脸,且多个人脸时检测效果不好。后面会用到openCV做人脸检测后再进行年龄预测。

Here is a version using gender, where we restore the checkpoint from a specific step:
python guess.py - -model_dir /home/dpressel/dev/work/AgeGenderDeepLearning/Folds/tf/gen_test_fold_is_0/run-31376 - -class_type gender - -requested_step 9999 - -filename /home/dpressel/Downloads/portraits/prince.jpg

Face Detection
If you have an image with one or more frontal faces, you can run a face-detector upfront, and each detected face will be chipped out and run through classification individually. A variety of face detectors are supported including OpenCV, dlib and YOLO

OpenCV:
python guess.py - -model_type inception - -model_dir /data/xdata/rude-carnie/checkpoints/age/inception/22801 - -filename /home/dpressel/Downloads/portraits/p_and_d.jpg - -face_detection_model /usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml
此处–model_dir /data/xdata/rude-carnie/checkpoints/age/inception/22801。
22801即预训练的模型。
–face_detection_model /usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml
openCV需要用到的face_detection模型。需要另外下载。
下载地址:https://pan.baidu.com/s/1yRiobqg6hZdLATDhFj_Trg

Pre-trained Checkpoints
You can find a pre-trained age checkpoint for inception here:

https://drive.google.com/drive/folders/0B8N1oYmGLVGWbDZ4Y21GLWxtV1E

A pre-trained gender checkpoint for inception is available here:

https://drive.google.com/drive/folders/0B8N1oYmGLVGWemZQd3JMOEZvdGs
模型下载
因为被墙,在这提供百度网盘下载链接,下载后解压到本地工程里面的22801目录
http://pan.baidu.com/s/1mhLBIHy

Training
You can use your own training data if you wish. This is a little easier to do with gender, since there are many ways that you could come up with a training set for this, but it has been developed specifically with the Adience corpus in mind, and uses the pre-splits created by Levi and Hassner.

年龄段识别模型的训练过程
训练集和验证集列表文件F1下载
git clone https://github.com/GilLevi/AgeGenderDeepLearning (非常难下载,下载不断中断,我是翻墙尝试了一下午,才下载成功一次)
https://pan.baidu.com/s/1UzRyxAbSg02T3MGeyyKAhw
训练集和验证集图片文件F2下载
http://www.openu.ac.il/home/hassner/Adience/data.html (由百度网盘可以下载)
https://pan.baidu.com/s/11ZJRhkOh9UiQHkIhcfP_LA
首先要对训练集进行预处理
官方的处理方法如下,这个目录要根据你的实际目录进行调整

python preproc.py - -fold_dir /home/dpressel/dev/work/AgeGenderDeepLearning/Folds/train_val_txt_files_per_fold/test_fold_is_0 - -train_list age_train.txt - -valid_list age_val.txt - -data_dir /data/xdata/age-gender/aligned - -output_dir /home/dpressel/dev/work/AgeGenderDeepLearning/Folds/tf/age_test_fold_is_0
主要是 fold_dir、train_list、valid_list、data_dir、output_dir参数
fold_dir:训练集和验证集的F1基目录
train_list:训练集
valid_list:验证集
data_dir:图片文件目录,这里选的是aligned目录,也就是F2文件夹
output_dir:输出目录
我们先翻看下训练集的目录结构
这里写图片描述

上述示例中只处理了test_fold_is_0中的数据,实际上有0-4.

在预处理后,在tf/age_test_fold_is_0文件夹下会生成tfrecord文件。
这里写图片描述

Gender is done much the same way:
python preproc.py - -fold_dir /home/dpressel/dev/work/AgeGenderDeepLearning/Folds/train_val_txt_files_per_fold/test_fold_is_0 - -train_list gender_train.txt - -valid_list gender_val.txt - -data_dir /data/xdata/age-gender/aligned –output_dir /home/dpressel/dev/work/AgeGenderDeepLearning/Folds/tf/gen_test_fold_is_0

Train the model (Levi/Hassner)
Now that we have generated the training and validation shards, we can start training the program. Here is a simple way to call the driver program to run using SGD with momentum to train:
python train.py –train_dir /home/dpressel/dev/work/AgeGenderDeepLearning/Folds/tf/age_test_fold_is_0
会在tf/age_test_fold_is_0产生run-6352文件夹
这里写图片描述

这里写图片描述

Train the model (fine-tuned Inception)微调模型
Its also easy to use this codebase to fine-tune an pre-trained inception checkpoint for age or gender dectection. Here is an example for how to do this:
python train.py - -train_dir /home/dpressel/dev/work/AgeGenderDeepLearning/Folds/tf/age_test_fold_is_0 - -max_steps 15000 - -model_type inception - -batch_size 32 - -eta 0.001 - -dropout 0.5 - -pre_model /data/pre-trained/inception_v3.ckpt
You can get the inception_v3.ckpt like so:
https://github.com/tensorflow/models/tree/master/im2txt#download-the-inception-v3-checkpoint

Monitoring the training(另起一个终端,输入以下命令,在浏览器中输入终端显示的http地址)
tensorboard - -logdir /home/dpressel/dev/work/AgeGenderDeepLearning/Folds/tf/gen_test_fold_is_0/run-6352
run-6352为训练时候生成的模型文件夹。

Evaluate the model
The evaluation program is written to be run alongside the training or after the fact. If you run it after the fact, you can specify a list of checkpoint steps to evaluate in sequence. If you run while training is working, it will periodically rerun itself on the latest checkpoint.

Here is an example of running evaluation continuously. The –run_id will live in the –train_dir (run-) and is the product of a single run of training (the id is actually the PID used in training):

python eval.py - -run_id 15918 - -train_dir /home/dpressel/dev/work/AgeGenderDeepLearning/Folds/tf/gen_test_fold_is_0/ - -eval_dir /home/dpressel/dev/work/AgeGenderDeepLearning/Folds/tf/eval_gen_test_fold_is_0

Here is an after-the-fact run of eval that loops over the specified checkpoints and evaluates the performance on each:
python eval.py - -run_id 25079 - -train_dir /home/dpressel/dev/work/AgeGenderDeepLearning/Folds/tf/age_test_fold_is_0/ - -eval_dir /home/dpressel/dev/work/AgeGenderDeepLearning/Folds/tf/eval_age_test_fold_is_0 - -requested_step_seq 7000,8000,9000,9999

To monitor the fine-tuning of an inception model, the call is much the same. Just be sure to pass –model_type inception
python eval.py - -run_id 8128 - -train_dir /home/dpressel/dev/work/AgeGenderDeepLearning/Folds/tf/age_test_fold_is_0/ - -eval_dir /home/dpressel/dev/work/AgeGenderDeepLearning/Folds/tf/eval_age_test_fold_is_0 - -model_type inception

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值