Skip to content

neelalohith/CBAM-Implementation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

CBAM Age Estimator on APPA-REAL

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.


Architecture Overview

  1. Backbone: ResNet (resnet18, resnet34, resnet50, resnet101, or resnet152) modified with CBAM modules (Channel and Spatial Attention) at every residual block.
  2. Regression Head:
    • AdaptiveAvgPool2d(1, 1)
    • Flatten
    • Dropout
    • Linear(feat_dim, 512) -> ReLU -> Dropout -> Linear(512, 1)
  3. Pre-trained Weights: Automatically loads ImageNet pre-trained weights from torchvision into the CBAM backbone, resolving any SSL or model_zoo issues associated with older CBAM repositories.

Dataset

The project is designed to run on the APPA-REAL (Apparent and Real Age) dataset.

Dataset Structure

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.


Getting Started

1. Installation

Install the required dependencies:

pip install -r requirements.txt

2. Training

Train 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

Key Training Features:

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

3. Evaluation

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 test

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


Directory Structure

.
├── 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

About

The implementation of CBAM to predict age of a person throgh their image

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages