File size: 4,476 Bytes
43e3729
 
1c26a44
 
 
 
 
 
 
430dbd7
 
 
 
 
43e3729
 
1c26a44
 
 
 
 
 
 
 
 
 
43e3729
 
 
 
4a40985
 
 
 
 
43e3729
4a40985
 
 
756209a
43e3729
4a40985
1c26a44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65bdfec
 
 
 
 
 
 
 
 
 
1c26a44
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
---
license: apache-2.0
datasets:
- prithivMLmods/Age-Classification-Set
language:
- en
base_model:
- google/siglip2-base-patch16-512
library_name: transformers
pipeline_tag: image-classification
tags:
- Age-Detection
- SigLIP2
- Image
---

![3.png](https://cdn-uploads.huggingface.co/production/uploads/65bb837dbfb878f46c77de4c/oN-EYfwR1vvdgHkVKAOKP.png)

# open-age-detection

> `open-age-detection` is a vision-language encoder model fine-tuned from `google/siglip2-base-patch16-512` for **multi-class image classification**. It is trained to classify the estimated age group of a person from an image. The model uses the `SiglipForImageClassification` architecture.

> \[!note]
> *SigLIP 2: Multilingual Vision-Language Encoders with Improved Semantic Understanding, Localization, and Dense Features*
> [https://arxiv.org/pdf/2502.14786](https://arxiv.org/pdf/2502.14786)

```py
Classification Report:
                  precision    recall  f1-score   support

      Child 0-12     0.9827    0.9859    0.9843      2193
  Teenager 13-20     0.9663    0.8713    0.9163      1779
     Adult 21-44     0.9669    0.9884    0.9775      9999
Middle Age 45-64     0.9665    0.9538    0.9601      3785
        Aged 65+     0.9737    0.9706    0.9722      1260

        accuracy                         0.9691     19016
       macro avg     0.9713    0.9540    0.9621     19016
    weighted avg     0.9691    0.9691    0.9688     19016
```

![download.png](https://cdn-uploads.huggingface.co/production/uploads/65bb837dbfb878f46c77de4c/ngGPmqGLlqQnfwRhy8phZ.png)

---

## Label Space: 5 Age Groups

```
Class 0: Child 0–12  
Class 1: Teenager 13–20  
Class 2: Adult 21–44  
Class 3: Middle Age 45–64  
Class 4: Aged 65+
```

---

## Install Dependencies

```bash
pip install -q transformers torch pillow gradio hf_xet
```

---

## Inference Code

```python
import gradio as gr
from transformers import AutoImageProcessor, SiglipForImageClassification
from PIL import Image
import torch

# Load model and processor
model_name = "prithivMLmods/open-age-detection"  # Updated model name
model = SiglipForImageClassification.from_pretrained(model_name)
processor = AutoImageProcessor.from_pretrained(model_name)

# Updated label mapping
id2label = {
    "0": "Child 0-12",
    "1": "Teenager 13-20",
    "2": "Adult 21-44",
    "3": "Middle Age 45-64",
    "4": "Aged 65+"
}

def classify_image(image):
    image = Image.fromarray(image).convert("RGB")
    inputs = processor(images=image, return_tensors="pt")

    with torch.no_grad():
        outputs = model(**inputs)
        logits = outputs.logits
        probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()

    prediction = {
        id2label[str(i)]: round(probs[i], 3) for i in range(len(probs))
    }

    return prediction

# Gradio Interface
iface = gr.Interface(
    fn=classify_image,
    inputs=gr.Image(type="numpy"),
    outputs=gr.Label(num_top_classes=5, label="Age Group Detection"),
    title="open-age-detection",
    description="Upload a facial image to estimate the age group: Child, Teenager, Adult, Middle Age, or Aged."
)

if __name__ == "__main__":
    iface.launch()
```

---

## Demo Inference

![Screenshot 2025-05-20 at 21-04-41 open-age-detection.png](https://cdn-uploads.huggingface.co/production/uploads/65bb837dbfb878f46c77de4c/V-kG5yBm3F1uixB501XML.png)
![Screenshot 2025-05-20 at 21-49-28 open-age-detection.png](https://cdn-uploads.huggingface.co/production/uploads/65bb837dbfb878f46c77de4c/n6HyF0E_hvvV1QjMRCxd3.png)
![Screenshot 2025-05-20 at 21-50-03 open-age-detection.png](https://cdn-uploads.huggingface.co/production/uploads/65bb837dbfb878f46c77de4c/8c5n2UcC7ICmfui6q5k6F.png)
![Screenshot 2025-05-20 at 21-56-22 open-age-detection.png](https://cdn-uploads.huggingface.co/production/uploads/65bb837dbfb878f46c77de4c/I1l7x55WvlibUKmKQNvGn.png)
![Screenshot 2025-05-20 at 21-58-09 open-age-detection.png](https://cdn-uploads.huggingface.co/production/uploads/65bb837dbfb878f46c77de4c/5KHykSEnBtJy1cZePfveO.png)

---

## Intended Use

`open-age-detection` is designed for:

* **Demographic Analysis** – Estimate age groups for statistical or analytical applications.
* **Smart Personalization** – Age-based content or product recommendation.
* **Access Control** – Assist systems requiring age verification.
* **Social Research** – Study age-related trends in image datasets.
* **Surveillance and Security** – Profile age ranges in monitored environments.