基于 caffe 的性别和年龄估计 – 论文阅读 – 基于CNN的年龄和性别分类
Github 中基于 Tensorflow 的性别和年龄估计 - Github项目 - Age-Gender-Estimate-TF 年龄性别估计这里学习下 Github 中基于 Keras 的性别和年龄估计
Github - age-gender-estimation 项目路径
基于 Keras 的人脸年龄和性别估计.
基于 IMDB-WIKI dataset 训练.
1. 依赖环境
- Python3.5+
- Keras2.0+
- scipy, numpy, Pandas, tqdm, tables, h5py
- dlib (for demo)
- OpenCV3
测试环境:
[1] - Ubuntu 16.04, Python 3.5.2, Keras 2.0.3, Tensorflow(-gpu) 1.0.1, Theano 0.9.0, CUDA 8.0, cuDNN 5.0
- CPU: i7-7700 3.60GHz, GPU: GeForce GTX1080
[2] - macOS Sierra, Python 3.6.0, Keras 2.0.2, Tensorflow 1.0.0, Theano 0.9.0
2. 采用预训练模型测试 Demo
需要 Web Cam:
python3 demo.py
采用 Tensorflow 后端的 Keras 会自动下载预训练模型到 pretrained_models
路径.
3. 模型训练
- 下载 IMDB-WIKI 数据集, 并解压到
data
路径.
./download.sh
- 创建训练数据
过滤噪声数据, 并将训练图片和对应标签序列化到 mat 文件.
数据集细节可参考 check_dataset.ipynb.
python3 create_db.py --output data/imdb_db.mat --db imdb --img_size 64
create_db.py [-h] --output OUTPUT [--db DB] [--img_size IMG_SIZE] [--min_score MIN_SCORE]
使用方法:
该脚本清理噪声数据, 并创建训练数据集
-h, --help - 查看 [help] 信息
--output OUTPUT, -o OUTPUT - 输出数据集 mat 文件的路径, 默认为 None.
--db DB - dataset名, wiki 或 imdb, 默认为 wiki
--img_size IMG_SIZE - 输出图片尺寸, 默认为 32
--min_score MIN_SCORE - 最小化 face_score, 默认为 1.0
- 网络训练
采用创建好的训练数据集进行网络训练:
python3 train.py --input data/imdb_db.mat
每个 epoch 训练的权重weights 文件保存为 checkpoints/weights.*.hdf5
.
train.py [-h] --input INPUT [--batch_size BATCH_SIZE] [--nb_epochs NB_EPOCHS] [--depth DEPTH] [--width WIDTH] [--validation_split VALIDATION_SPLIT] [--aug]
使用方法:
-h, --help - 查看 [help] 信息
--input INPUT, -i INPUT - 输入的数据集 mat 文件, 默认为 None.
--batch_size BATCH_SIZE - batch size, 默认为 32
--nb_epochs NB_EPOCHS - 训练的 epochs 数, 默认为 30
--depth DEPTH - 网络深度 depth of network (值应该为 10, 16, 22, 28, ...), 默认为 16
--width WIDTH - 网络宽度 width of network, 默认为 8
--validation_split VALIDATION_SPLIT - 验证集比例, 默认为 0.1
--aug - 设为 True 时, 采用数据增强, 默认为 False
- 采用数据增强方法训练网络
近期的数据增强方法,如 mixup 和 Random Erasing, 可以在 --aug
参数项进行设置:
python3 train.py --input data/imdb_db.mat --aug
具体实现可参考 - mixup-generator
经测试, 数据增强可以防止过拟合, 提高 validation loss.
[1] - mixup: Beyond Empirical Risk Minimization - 2017
[2] - Random Erasing Data Augmentation - 2017
4. 训练网络的使用
可以选取训练好的最佳模型权重进行测试 - checkpoints/weights.*.hdf5
.
python3 demo.py
demo.py [-h] [--weight_file WEIGHT_FILE] [--depth DEPTH] [--width WIDTH]
使用方法:
该脚本从 Web Cam 输入的图片检测人脸, 并对检测人脸进行年龄和性别估计.
-h, --help - 查看 [help] 信息
--weight_file WEIGHT_FILE - 权重文件路径, 如weights.18-4.06.hdf5, 默认为 None
--depth DEPTH - 网络深度 depth of network, 默认为 16
--width WIDTH - 网络宽度 width of network, 默认为 8
5. 训练日志
画出历史文件曲线
如:
python3 plot_history.py --input models/history_16_8.h5
- Results without data augmentation:
Results with data augmentation
最佳的 val_loss 从 3.969 降低为 3.731:
Without data augmentation: 3.969
With standard data augmentation: 3.799
With mixup and random erasing: 3.731
采用数据增强后, 即使学习率很小, 也没出现过拟合现象(epoch>15).
6. 网络结构
原始论文采用的是预训练的 VGG 网络.
[1] - DEX: Deep EXpectation of apparent age from a single image - ICCV2015
[2] - Deep expectation of real and apparent age from a single image without facial landmarks - IJCV2016
这里采用从零开始训练 Wide Residual Network (WideResNet).
添加了两个分类层(年龄和性别)到 WideResNet 尾部.
在原始论文实现中是采用两个 CNN 模型分别独立实现的.
这里采用单个 CNN 同时预测年龄和性别. 相当于 Multi-tasks.
7. Results
在 IMDB 上训练, WIKI 上测试.
8. 评测
在 APPA-REAL dataset (validation) 评估训练的模型.
python3 evaluate_appa_real.py
APPL-REAL 数据集细节可见 appa-real
得到的结果如下:
MAE Apparent: 6.06
MAE Real: 7.38
MAE Apparent: 4.08
MAE Real: 5.30