A PyTorch implementation of age estimation using the Convolutional Block Attention Module (CBAM) integrated with ResNet backbones, trained on the APPA-REAL dataset.
This model replaces the standard ImageNet classification head of ResNet-CBAM with a custom regression head optimized for predicting apparent or real age.
- Backbone: ResNet (
resnet18,resnet34,resnet50,resnet101, orresnet152) modified with CBAM modules (Channel and Spatial Attention) at every residual block. - Regression Head:
AdaptiveAvgPool2d(1, 1)FlattenDropoutLinear(feat_dim, 512)->ReLU->Dropout->Linear(512, 1)
- Pre-trained Weights: Automatically loads ImageNet pre-trained weights from
torchvisioninto the CBAM backbone, resolving any SSL ormodel_zooissues associated with older CBAM repositories.
The project is designed to run on the APPA-REAL (Apparent and Real Age) dataset.
- Official Download Page: ChaLearn LAP APPA-REAL Dataset
Extract the dataset and point the --data_root path to your appa-real-release folder:
appa-real-release/
├── train/ # Train face images (and optionally *_face.jpg crops)
├── valid/ # Validation face images
├── test/ # Test face images
├── gt_avg_train.csv # Training ground truth
├── gt_avg_valid.csv # Validation ground truth
└── gt_avg_test.csv # Test ground truth
CSV files must contain file_name, real_age, and apparent_age_avg.
Install the required dependencies:
pip install -r requirements.txtTrain the model by providing the path to the APPA-REAL dataset. The training pipeline automatically runs on CUDA or Apple Silicon (MPS) if available, otherwise falling back to CPU.
# Minimal configuration (ResNet-50 backbone, apparent age regression)
python train.py --data_root ./appa-real-release
# Full custom configuration
python train.py \
--data_root ./appa-real-release \
--target apparent \
--backbone resnet50 \
--pretrained \
--epochs 50 \
--batch_size 32 \
--lr 1e-4 \
--weight_decay 1e-4 \
--dropout 0.3 \
--use_face \
--save_dir ./checkpoints \
--log_interval 20- Differential Learning Rates: Lower learning rate for the pre-trained backbone, higher learning rate for the fresh regressor head.
- Cosine Annealing LR Scheduler: Includes a linear warmup period (default 3 epochs) followed by a cosine decay schedule.
- Checkpoints: Saves
last.pth,best.pth, and training history (history.json) in the specified--save_dir.
Evaluate a trained model checkpoint on the test, validation, or training split:
python evaluate.py \
--data_root ./appa-real-release \
--checkpoint ./checkpoints/best.pth \
--split testThis will compute Mean Absolute Error (MAE), Root Mean Squared Error (RMSE), and Prediction Bias, saving the output to eval_<split>.json inside the checkpoint directory.
.
├── CBAM/
│ ├── data_loader/
│ │ ├── __init__.py
│ │ └── appa_real_dataset.py # Dataset class and transforms
│ ├── model/
│ │ ├── __init__.py
│ │ ├── cbam_age_model.py # ResNet-CBAM wrapping and regression head
│ │ └── resnet_cbam.py # Official CBAM modules & ResNet backbones
│ ├── evaluate.py # Model evaluation script
│ ├── train.py # Model training script
│ └── requirements.txt # Project dependencies
├── .gitignore
└── README.md # Project documentation