Age and Gender Recognition¶
Age and gender recognition, developed based on the insightface feature module, supports simultaneous detection and recognition of multiple faces.
Source Code Address: https://github.com/yeyupiaoling/Age-Gender-MXNET
Environment¶
- Install MXNet, supporting versions 1.3~1.6. The installation command is as follows:
```shell script
pip install mxnet-cu101==1.5.0
# Dataset
- The following three datasets are supported by default. Download and extract these three datasets into the `dataset` directory.
1. http://afad-dataset.github.io/
2. http://mmlab.ie.cuhk.edu.hk/projects/MegaAge/
3. https://ibug.doc.ic.ac.uk/resources/agedb/
- Generate the data list:
```shell script
python create_dataset.py
- If you want to train a custom dataset, simply generate a data list similar to the following:
```shell script
dataset/AgeDB/0_MariaCallas_35_f.jpg,0,35
dataset/AgeDB/10000_GlennClose_62_f.jpg,0,62
dataset/AgeDB/10001_GoldieHawn_23_f.jpg,0,23
dataset/AgeDB/10003_GoldieHawn_24_f.jpg,0,24
dataset/AgeDB/10004_GoldieHawn_27_f.jpg,0,27
dataset/AgeDB/10005_GoldieHawn_28_f.jpg,0,28
dataset/AgeDB/10006_GoldieHawn_29_f.jpg,0,29
To view the age distribution, execute `show_age_distribution.py` to generate an age distribution chart.

# Training
Start training. For specific parameters, refer to the code. Here, we introduce the `network` parameter, which selects the model. When set to `r50`, ResNet is chosen as the feature extraction model; when set to `m50`, MobileNet is used as the feature extraction model.
```shell script
python train.py
Training output results:
gpu num: 1
num_layers 50
data_shape [3, 112, 112]
Called with argument: Namespace(batch_size=128, color=0, ctx_num=1, cutoff=0, data_dir='dataset', data_shape='3,112,112', end_epoch=200, gpu_ids='0', image_channel=3, image_h=112, image_w=112, lr=0.1, lr_steps='10,30,80,150,200', network='m50', num_layers=50, prefix='temp/model', pretrained='', rand_mirror=1, rescale_threshold=0, version_input=1, version_output='GAP')
1 GAP 32
INFO:root:loading recordio dataset\train.rec...
INFO:root:dataset\train.rec Data size: 303018
INFO:root:Randomly flip images: 1
INFO:root:loading recordio dataset\val.rec...
INFO:root:dataset\val.rec Data size: 1032
INFO:root:Randomly flip images: False
call reset()
Training started...
INFO:root:Epoch[0] Batch [0-20] Speed: 520.85 samples/sec acc=0.572545 MAE=10.734747 CUM_5=0.240699
INFO:root:Epoch[0] Batch [20-40] Speed: 518.95 samples/sec acc=0.589844 MAE=9.351172 CUM_5=0.289844
INFO:root:Epoch[0] Batch [40-60] Speed: 516.86 samples/sec acc=0.603125 MAE=9.184766 CUM_5=0.303906
INFO:root:Epoch[0] Batch [60-80] Speed: 508.44 samples/sec acc=0.609766 MAE=8.759375 CUM_5=0.336719
INFO:root:Epoch[0] Batch [80-100] Speed: 461.26 samples/sec acc=0.656250 MAE=8.224609 CUM_5=0.361328
INFO:root:Epoch[0] Batch [100-120] Speed: 518.43 samples/sec acc=0.696875 MAE=7.611328 CUM_5=0.400391
INFO:root:Epoch[0] Batch [120-140] Speed: 514.88 samples/sec acc=0.715234 MAE=7.224609 CUM_5=0.426172
INFO:root:Epoch[0] Batch [140-160] Speed: 517.80 samples/sec acc=0.722266 MAE=6.976172 CUM_5=0.437500
Evaluation¶
After training, run the following command to evaluate the model’s recognition accuracy:
```shell script
python eval.py
Output results:
```shell
100%|██████████| 1032/1032 [00:06<00:00, 153.75it/s]
Gender accuracy: 0.972868
Age accuracy: 0.761628
Prediction¶
Use the trained model or the model provided by the author to perform age and gender recognition by specifying the image file path:
```shell script
python infer.py –image=test.jpg
Recognition output results:
```shell
Face 1, Position (160, 32, 204, 84), Gender: Male, Age: 30
Face 2, Position (545, 162, 579, 206), Gender: Female, Age: 31
Face 3, Position (632, 118, 666, 158), Gender: Male, Age: 28
Face 4, Position (91, 159, 151, 237), Gender: Male, Age: 38
Face 5, Position (723, 123, 760, 169), Gender: Male, Age: 26
Face 6, Position (263, 120, 317, 191), Gender: Male, Age: 27
Face 7, Position (438, 134, 481, 190), Gender: Male, Age: 46
Face 8, Position (908, 160, 963, 224), Gender: Male, Age: 35
Face 9, Position (39, 51, 81, 102), Gender: Female, Age: 31
Face 10, Position (807, 148, 847, 196), Gender: Female, Age: 26
Face 11, Position (449, 40, 485, 84), Gender: Male, Age: 29
Face 12, Position (378, 46, 412, 86), Gender: Female, Age: 33
Face 13, Position (534, 46, 567, 83), Gender: Male, Age: 30
Face 14, Position (272, 20, 311, 67), Gender: Male, Age: 28
Face 15, Position (358, 216, 375, 237), Gender: Male, Age: 27
Effect diagram:
