Skip to content

About

Age Estimation with PyTorch: Deep Learning for Predicting Age

Topics

Resources

Contributing

Stars

89 stars

Watchers

1 watching

Forks

Latest commit

Β 

History

191 Commits

Folders and files

Repository files navigation

Facial Age Estimation Banner

πŸŽ‚ Facial Age Estimation β€” PyTorch

Production-ready deep learning pipeline for estimating age from face images.
Train, evaluate, and deploy in minutes β€” on CPU or GPU.

GitHub Stars GitHub Forks License: MIT Python 3.10+ PyTorch 2.3+ CI


πŸ“‹ Table of Contents


✨ Key Features

  • πŸ”₯ 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)

πŸš€ Quick Start

# 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=false in configs/default.yaml.


πŸ“Š Results & Benchmarks

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. Run python scripts/evaluate.py --checkpoint <path> --bias to reproduce.

Inference example

πŸ—οΈ Architecture

How It Works

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]
Loading

L1 + Variance Objective

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.


πŸ“ Project Structure

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

πŸ“¦ Installation

Step 1 β€” Clone the repo

git clone https://github.com/Ebimsv/Facial_Age_estimation_PyTorch.git
cd Facial_Age_estimation_PyTorch

Step 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 torchvision

Step 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.jpg

This 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.

πŸ“‚ Dataset

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.

Managed Download (No Kaggle Required)

# 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-only

The 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.

Dataset Preparation

# 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.png

Train 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.

Data Visualisation

Age Distribution Train / Val / Test Splits
age dist split dist
Gender Distribution Ethnicity Distribution
gender ethnicity

πŸŽ“ Training

Default Training

# Select a freshly prepared protocol; bare defaults point to historical CSVs
python scripts/train.py data.csv_dir=csv_dataset/protocol_v2

CLI Overrides

Any 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=128

Resume and Checkpoint Safety

Fresh 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=offline

Resume 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.

Key Config Options (configs/default.yaml)

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 | l1

TensorBoard

tensorboard --logdir runs/
TensorBoard training curves

πŸ” Evaluation

# 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.csv

Illustrative 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

Single Image Inference

python scripts/inference.py \
    --checkpoint checkpoints/RUN_ID/best.pt \
    --image img_test/30_1_2.jpg \
    --output img_test/output.jpg

πŸ–₯️ Gradio Demo

# 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 --share

The 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!


πŸ“€ Export Model

# 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 onnx

πŸ“š Literature Review

Click 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

πŸ—ΊοΈ Roadmap

βœ… Completed

  • 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

πŸ”„ In Progress

  • 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

πŸ“Œ Planned

  • GradCAM / Attention heatmaps β€” visualise which facial regions drive age predictions
  • Real-time webcam inference β€” scripts/webcam.py with OpenCV face detection
  • Cross-dataset evaluation β€” train on UTKFace, test on MORPH / FG-NET
  • EDA Jupyter Notebook β€” notebooks/01_EDA.ipynb with 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

πŸ“ Citation

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}
}

🀝 Contributing

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.


πŸ“„ License

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

About

Age Estimation with PyTorch: Deep Learning for Predicting Age

Topics

Resources

Contributing

Stars

89 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages