Age-X Age Detection Model

This is a ResNet18-based age prediction model trained to detect and predict the age of a person from a facial image. It is designed with safety and high performance in mind, originally built for the Age-X Next-Generation Age Verification Platform.

By open-sourcing this model, we hope to enable developers to build safer, age-aware applications.

πŸš€ Model Details

  • Architecture: ResNet18 (Regression)
  • Framework: PyTorch
  • Task: Age Estimation
  • License: MIT (Open Source)

πŸ’» How to use in Python

You can download the model weights and use them directly with PyTorch. For best results, use a face detector (like OpenCV) to crop the face first, as shown in the age_service.py file included in this repository.

Here is a minimal example of how to load the model and run an inference on a cropped face:

import torch
import torch.nn as nn
from torchvision import models, transforms
from PIL import Image

# 1. Initialize the ResNet18 Model
model = models.resnet18(weights=None)
model.fc = nn.Linear(model.fc.in_features, 1) # Set output to 1 for age regression
model.load_state_dict(torch.load("age_model.pth", map_location="cpu"))
model.eval()

# 2. Prepare the Image
transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

# NOTE: The image should be a tightly cropped face for accurate predictions
image = Image.open("path_to_cropped_face.jpg").convert("RGB")
input_tensor = transform(image).unsqueeze(0)

# 3. Predict Age
with torch.no_grad():
    predicted_age = model(input_tensor).item()

print(f"Predicted Age: {max(1, min(100, predicted_age)):.1f}")

πŸ—οΈ Interactive Demo (Gradio)

We have also included an app.py built with Gradio! You can deploy this exact repository to Hugging Face Spaces (select Gradio as the SDK) to get an instant web UI where you can upload photos and see the prediction.

Alternatively, you can run the demo locally:

pip install -r requirements.txt
python app.py

⚠️ Limitations & Bias

This model provides age estimation based on facial features. Results may vary significantly depending on lighting conditions, camera angles, and demographics. It relies on standard ResNet18 architecture which is optimized for general image processing but may exhibit standard biases present in its training dataset. Always use fallback safety mechanisms in production environments.

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support