Age and Gender Deep Learning with TensorFlow实践

编程入门 行业动态 更新时间:2024-10-18 06:10:45

Age and <a href=https://www.elefans.com/category/jswz/34/1444251.html style=Gender Deep Learning with TensorFlow实践"/>

Age and Gender Deep Learning with TensorFlow实践

mark一下,感谢作者分享!

按照作者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模型。需要另外下载。
下载地址:

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

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


模型下载
因为被墙,在这提供百度网盘下载链接,下载后解压到本地工程里面的22801目录

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 (非常难下载,下载不断中断,我是翻墙尝试了一下午,才下载成功一次)

训练集和验证集图片文件F2下载
.html (由百度网盘可以下载)

首先要对训练集进行预处理
官方的处理方法如下,这个目录要根据你的实际目录进行调整

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:

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

更多推荐

Age and Gender Deep Learning with TensorFlow实践

本文发布于:2024-03-11 14:20:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1729169.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Gender   Age   Deep   TensorFlow   Learning

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!