Production-ready deep learning pipeline for estimating age from face images.
Train, evaluate, and deploy in minutes β on CPU or GPU.
- β¨ Key Features
- π Quick Start
- π Results & Benchmarks
- ποΈ Architecture
- π Project Structure
- π¦ Installation
- π Dataset
- π Training
- π Evaluation
- π₯οΈ Gradio Demo
- π€ Export Model
- π Literature Review
- πΊοΈ Roadmap
- π Citation
- π€ Contributing
- π₯ 5 Backbones β MobileNetV3-Large, EfficientNet-B0/B3, ResNet-50, ViT-S/14 DINOv2
- π L1 + variance objective β models age as a distribution; a documented variant inspired by mean-variance methods, with float32 central moments
- β‘ AMP + AdamW β mixed-precision training with differential learning rates and OneCycleLR warmup; atomic best/last checkpoints and epoch-boundary resume
- ποΈ Clean package layout β
src/,scripts/,demo/,tests/,configs/following standard PyTorch practices - ποΈ OmegaConf config β change any hyperparameter from the CLI without editing code
- π Bias analysis β per-epoch breakdown of MAE by age group, gender, and ethnicity
- π₯οΈ Gradio demo β upload a photo β see predicted age + full probability distribution
- π€ Export ready β TorchScript (trace) and ONNX with dynamic batch support
- β 377 tests + GitHub Actions CI (GPU/demo checks are conditional)
# 1. Clone
git clone https://github.com/Ebimsv/Facial_Age_estimation_PyTorch.git
cd Facial_Age_estimation_PyTorch
# 2. Install (set up PyTorch for your CUDA version first β see Installation section)
pip install .
# 3. Download images without Kaggle, then prepare a fresh split
python scripts/download_data.py
# Or point --dataset-dir below at images you already have
python scripts/prepare_data.py --dataset-dir dataset/utkcropped \
--csv-dir csv_dataset/protocol_v2 --check-decode --plot
# 4. Train on the new protocol (EfficientNet-B3, L1 + variance, 100 epochs, AMP)
python scripts/train.py data.csv_dir=csv_dataset/protocol_v2
# 5. Run the Gradio demo
python demo/app.py --checkpoint checkpoints/RUN_ID/best.pt # replace RUN_ID with the printed run folderπ‘ No GPU? The pipeline runs on CPU too β just set
training.use_amp=falseinconfigs/default.yaml.
New models have not yet been benchmarked under a published protocol. The committed UTKFace test CSV contains 4,654 images, ages 1β84; regenerating splits can change it. The legacy row below is historical validation, not a comparable test-set result. A byte-hash check of the pinned mirror found 174 duplicate-content groups crossing the committed historical split boundaries. Preserve those CSVs for historical reference and use a freshly prepared, deduplicated protocol for new experiments. See data protocol evidence.
| Model | #Params | Input Size | Loss | MAE β | CS@5 β | Inference |
|---|---|---|---|---|---|---|
| MobileNetV3-Large | 5.5M | 224Γ224 | L1 + variance | TBD | TBD | β |
| EfficientNet-B0 | 5.3M | 224Γ224 | L1 + variance | TBD | TBD | β |
| EfficientNet-B3 | 12M | 224Γ224 | L1 + variance | TBD | TBD | β |
| ResNet-50 | 25.6M | 224Γ224 | L1 + variance | TBD | TBD | β |
| ViT-S/14 DINOv2 | 22M | 224Γ224 | L1 + variance | TBD | TBD | β |
| ResNet-50 (legacy validation only) | 25.6M | 128Γ128 | L1 | 4.73 | β | β |
MAE = Mean Absolute Error in years (lower is better).
CS@5 = Cumulative Score β fraction of predictions within 5 years of ground truth (higher is better).
Results for the new models will be added after training. Runpython scripts/evaluate.py --checkpoint <path> --biasto reproduce.
graph TD
A[Face Image] --> B[Preprocessing\n224Γ224 Β· Normalize]
B --> C{Backbone}
C --> D1[MobileNetV3-Large\n5.5M params]
C --> D2[EfficientNet-B3\n12M params β
]
C --> D3[ResNet-50\n25.6M params]
C --> D4[ViT-S/14 DINOv2\n22M params]
D1 & D2 & D3 & D4 --> E[Head\nDropout β Linear β GELU β Linear]
E --> F[101 Logits\nage classes 0 to 100]
F --> G[Softmax\nAge Probability Distribution]
G --> H[E_age = sum of p_i times i\nPredicted Age]
The distribution head outputs logits over a configurable age support (0β100 by default). Training uses the repository's L1 plus central-variance variant:
p = softmax(logits)
mu = sum(p_j * age_j)
variance = sum(p_j * (age_j - mu)^2)
loss = lambda_mean * mean(abs(mu - target)) + lambda_variance * mean(variance)
loss.type=l1_mean_variance is the default; mean_variance is a compatible alias.
This differs from the published CVPR 2018 joint objective. No measured MAE
advantage over L1/MSE is claimed. See loss definitions, precision and gradient
policies for the reference comparison and numerical tests.
For scalar regression, the model returns raw values during training; the modern inference decoder applies its saved [0,100] bound. The historical raw-output profile remains separate.
Facial_Age_estimation_PyTorch/
βββ configs/
β βββ default.yaml # All hyperparameters β edit here, not in code
βββ src/
β βββ data/
β β βββ dataset.py # UTKDataset (side-effect free, importable)
β β βββ transforms.py # Train / val / test transform factories
β βββ models/
β β βββ age_estimator.py # AgeEstimationModel (5 backbones)
β βββ training/
β β βββ losses.py # MeanVarianceLoss + L1AgeLoss
β β βββ trainer.py # Trainer: AMP, AdamW, OneCycleLR, logging
β βββ utils/
β βββ metrics.py # AverageMeter, MAE, CS@5
β βββ seed.py # set_seed() for reproducibility
βββ scripts/
β βββ prepare_data.py # Stratified CSV split from raw UTKFace
β βββ train.py # Training entry point (CLI + OmegaConf)
β βββ evaluate.py # Test-set eval + bias breakdown
β βββ inference.py # Single-image / batch inference
β βββ export_model.py # TorchScript & ONNX export
βββ demo/
β βββ app.py # Gradio web demo
βββ tests/ # 377 tests (pytest)
βββ notebooks/ # EDA notebooks (coming soon)
βββ pics/ # Images used in README
βββ checkpoints/ # Saved model weights (git-ignored)
βββ csv_dataset/ # Pre-split CSV files
βββ .github/workflows/ci.yml # GitHub Actions CI
βββ pyproject.toml
βββ requirements.txt
Step 1 β Clone the repo
git clone https://github.com/Ebimsv/Facial_Age_estimation_PyTorch.git
cd Facial_Age_estimation_PyTorchStep 2 β Install PyTorch (match your CUDA version from pytorch.org)
# CUDA 12.8 (RTX 40/50 series)
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu128
# CUDA 11.8
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
# CPU only
pip install torch torchvisionStep 3 β Install project dependencies
pip install .
# For the Gradio demo + W&B logging:
pip install ".[demo,wandb]"
# For development and packaging tests (includes pytest and build):
pip install ".[dev]"For checkpoint schema v1, runtime overrides, and the tracked historical ResNet50 weight, see checkpoint contracts and legacy loading. The existing legacy weight can be used without downloading pretrained backbones:
python scripts/inference.py --checkpoint checkpoints/epoch-16-loss_valid-4.73.pt \
--legacy-resnet50 --device cpu --image img_test/30_1_2.jpgThis is a historical compatibility example; 4.73 denotes validation history,
not a newly verified held-out test score. Modern checkpoints carry their own
preprocessing and decoder metadata; inference restores those settings automatically.
requirements.txt remains available for dependency-only installation. Installing
the project also provides train-age, eval-age, and export-age. The installed
package includes its default YAML configuration and works outside the checkout.
See installation and verified environments for Conda usage,
configuration paths, and the distinction between API minimums and tested versions.
Downloader settings are separate from experiments: configs/download.yaml.
For the completed P1.1βP1.6 work, evidence and two workflow diagrams, see the
Phase 1 implementation report (Persian).
Step 4 β Verify
python -m pytest tests/ -v -k "not vit_small"
# Includes model, loss, transform, dataset, and packaging checks.
# Packaging checks require the dev extra and use a temporary install without downloads.This project uses the UTKFace dataset β 20,000+ face images labelled with age (0β116), gender, and ethnicity.
| Dataset | Images | Age Range | Labels |
|---|---|---|---|
| UTKFace | ~23,000 | 0 β 116 | Age, Gender, Ethnicity |
| MORPH | ~55,000 | 16 β 77 | Age, Gender, Race |
| FG-NET | 1,002 | 0 β 69 | Age |
Download: The default is a pinned public Hugging Face cropped mirror. Kaggle remains an explicit alternative.
# Default: pinned Hugging Face tar.gz, no SDK/account/token installation
python scripts/download_data.py
# Show separate download config and resolved URL without downloading
python scripts/download_data.py --print-config
# Read-only local sanity check
python scripts/download_data.py --check-onlyThe source, revision, SHA-256 and size live in configs/download.yaml.
Archive bytes are verified before safe extraction; images remain in dataset/utkcropped.
Existing targets and unrelated files are preserved. There is no automatic Kaggle fallback.
For the optional Kaggle route, use --source kaggle with .[data] installed explicitly.
See download configuration, provenance and limits.
# Creates a fresh protocol, manifests and rejection report; never overwrites legacy CSVs
# Age-stratified splits with exact-byte duplicate groups excluded
python scripts/prepare_data.py \
--dataset-dir dataset/utkcropped \
--csv-dir csv_dataset/protocol_v2 --check-decode \
--train-ratio 0.80 \
--val-ratio 0.10 \
--plot # saves split_distributions.pngTrain with data.csv_dir=csv_dataset/protocol_v2 to use those new splits. The
committed CSVs retain their historical 15,822 / 2,793 / 4,654 split; changing training
ratios never regenerates them. Sparse age classes fail explicitly. Byte hashes
are checked before training; identity and near-duplicate leakage remain unknown.
See data validation, provenance and reproducibility for
source/version flags, rejection policy, optional hashing/decode settings and limits.
The visualizations below are historical and are not regenerated by the audit.
# Select a freshly prepared protocol; bare defaults point to historical CSVs
python scripts/train.py data.csv_dir=csv_dataset/protocol_v2Any value in configs/default.yaml can be overridden from the command line:
Overrides are validated together against a typed schema. Unknown keys and
incompatible model/loss settings fail before model construction. Partial YAML
files are supported; inspect the resolved settings with train-age --print-config.
See configuration contracts and migration.
# Change backbone
python scripts/train.py model.name=resnet50
# Regression mode (L1 loss, 1 output node; legacy checkpoint migration is separate)
python scripts/train.py model.output_dim=1 loss.type=l1
# Longer training with W&B logging
python scripts/train.py training.epochs=150 logging.use_wandb=true
# Lower memory (if < 8GB VRAM)
python scripts/train.py training.batch_size=48 data.img_size=192
# Lightweight edge model
python scripts/train.py model.name=mobilenet_v3 training.batch_size=128Fresh runs write checkpoints/RUN_ID/{best,last}.pt and isolate logs under the run ID.
Use the actual path printed by training:
train-age --resume checkpoints/RUN_ID/last.pt
# Optional local tracking without upload:
train-age --resume checkpoints/RUN_ID/last.pt logging.use_wandb=true logging.wandb_mode=offlineResume keeps the saved epoch budget/OneCycle schedule, checks data/loader/config contracts, and restores optimizer/scaler/RNG at an epoch boundary. A partial epoch is replayed. Older inference-only artifacts cannot be resumed. See training, atomic-write recovery and logging for the supported scope, best/last pairing and concurrency lock.
model:
name: "efficientnet_b3" # mobilenet_v3 | efficientnet_b0/b3 | resnet50 | vit_small
output_dim: 101 # 101 = Mean-Variance Loss | 1 = plain regression
training:
epochs: 100
batch_size: 96 # EfficientNet-B3 @ 224px, 8GB VRAM
use_amp: true # Mixed precision (strongly recommended)
seed: 42
deterministic: false # Optional strict PyTorch algorithms
loss:
type: "l1_mean_variance" # mean_variance alias | l1tensorboard --logdir runs/# Standard evaluation on the test split
python scripts/evaluate.py --checkpoint checkpoints/RUN_ID/best.pt
# Demographic bias breakdown (age group, gender, ethnicity)
python scripts/evaluate.py --checkpoint checkpoints/RUN_ID/best.pt --bias
# Save per-image predictions to CSV
python scripts/evaluate.py --checkpoint checkpoints/RUN_ID/best.pt \
--bias --save-csv results/predictions.csvIllustrative output format (not a measured benchmark):
==================================================
Split : test (n=2041)
MAE : 4.12 years
CS@5 : 0.713 (71.3% within 5 years)
CS@10 : 0.941 (94.1% within 10 years)
==================================================
ββ MAE by Gender ββββββββββββββββββββββββββββββββ
Female 4.05
Male 4.19
ββ MAE by Ethnicity βββββββββββββββββββββββββββββ
Asian 3.91
Black 4.44
Indian 4.28
White 4.02
python scripts/inference.py \
--checkpoint checkpoints/RUN_ID/best.pt \
--image img_test/30_1_2.jpg \
--output img_test/output.jpg# Local demo
python demo/app.py --checkpoint checkpoints/RUN_ID/best.pt
# Share publicly via Gradio tunnel
python demo/app.py --checkpoint checkpoints/RUN_ID/best.pt --shareThe demo shows the predicted age and the distribution over the checkpoint's age support. This distribution is not calibrated confidence; regression displays a scalar age.
π A live demo on Hugging Face Spaces is coming soon!
# TorchScript (fastest for deployment β uses torch.jit.trace)
python scripts/export_model.py --checkpoint checkpoints/RUN_ID/best.pt
# ONNX (for ONNX Runtime, TensorRT, OpenVINO, etc.)
python scripts/export_model.py --checkpoint checkpoints/RUN_ID/best.pt --format onnxClick to expand β 7 key papers reviewed
| Paper | Summary | Key Idea | Code |
|---|---|---|---|
| SwinFace (2023) | Multi-task Swin Transformer for face analysis | Multi-Level Channel Attention | GitHub |
| Unraveling Age Estimation Puzzle (2022) | Benchmark & analysis of what actually matters | Data quality > model choice | GitHub |
| MiVOLO (2023) | ViT-based age + gender; works on occluded faces | Uses body context, not just face | GitHub |
| CORAL (2020) | Ordinal regression for age estimation | Rank-consistent binary subtasks | GitHub |
| Deep Regression Forests (2018) | End-to-end DRF for heterogeneous age features | Joint data partition + abstraction | GitHub |
| Mean-Variance Loss (2018) | Distribution learning for age estimation | Inspires our L1 + variance variant; not a reproduction | Paper |
| FaceXFormer (2024) | Unified transformer for all face analysis tasks | Token-based multi-task decoder | GitHub |
Supported Datasets
| Dataset | Images | Age Range | Notes |
|---|---|---|---|
| UTKFace (used here) | ~23,000 | 0β116 | In-the-wild, diverse |
| MORPH | 55,134 | 16β77 | Mugshot style |
| CACD | 163,446 | β | Celebrity, web-collected |
| FG-NET | 1,002 | 0β69 | Small, cross-age pairs |
| Adience | 26,580 | Age groups | 8 coarse age groups |
- Professional package structure β
src/,scripts/,demo/,tests/,configs/ - OmegaConf YAML config β all hyperparameters in one file, CLI overrides
- 5 backbone support β MobileNetV3-Large, EfficientNet-B0/B3, ResNet-50, ViT-S/14 DINOv2
- L1 + central variance β explicit variant, float32 moments, raw regression gradients and bounded modern inference
- Modern training pipeline β AMP, AdamW with differential LRs, OneCycleLR, gradient clipping
- Stratified data splits β stratified train/val/test CSV generation
- 377 tests β including data manifests, worker repeatability, regression gradients, checkpoint contracts, and installed packaging
- GitHub Actions CI β automated lint + test on every push
- Gradio demo β with age probability distribution visualisation
- TorchScript + ONNX export
- Bias evaluation β per age-group, gender, and ethnicity MAE breakdown
- Single-image + batch inference script
- pyproject.toml β proper packaging with optional extras
- Comprehensive
.gitignoreβ no accidental weight/dataset commits
- Train EfficientNet-B3 on UTKFace and populate the results table with real MAE numbers
- Hugging Face Spaces demo β live, always-on demo accessible from the README
- GradCAM / Attention heatmaps β visualise which facial regions drive age predictions
- Real-time webcam inference β
scripts/webcam.pywith OpenCV face detection - Cross-dataset evaluation β train on UTKFace, test on MORPH / FG-NET
- EDA Jupyter Notebook β
notebooks/01_EDA.ipynbwith interactive plots - Pre-commit hooks β ruff + formatting checks enforced on commit
- ONNX Runtime inference script β for CPU-only deployment
- Benchmark on MORPH dataset β broader reproducibility
If you use this repository in your research, please cite:
@misc{mousavi2024facial,
title = {Facial Age Estimation with PyTorch},
author = {Mousavi, Ebrahim},
year = {2024},
publisher = {GitHub},
url = {https://github.com/Ebimsv/Facial_Age_estimation_PyTorch}
}Related mean-variance reference (the implemented L1 variant differs; see docs/LOSSES.md):
@inproceedings{pan2018mean,
title = {Mean-Variance Loss for Deep Age Estimation from a Face},
author = {Pan, Hongyu and Han, Hu and Shan, Shiguang and Chen, Xilin},
booktitle = {Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition},
pages = {5285--5294},
year = {2018}
}Contributions are welcome! Whether it's a new backbone, a better loss function, a bug fix, or improved documentation.
# Fork, clone, create a branch
git checkout -b feat/your-feature
# Install dev dependencies
pip install -r requirements-dev.txt
pre-commit install
# Make changes, then verify
ruff check src/ scripts/ tests/
python -m pytest tests/ -v
# Open a Pull Request πSee CONTRIBUTING.md for detailed guidelines, including how to add a new backbone.
This project is licensed under the MIT License β see LICENSE for details.
If this project helped you, please consider giving it a β β it means a lot!
Made with β€οΈ by Ebi Mousavi





