Skip to content
ModelListedExternal
Download

ViTFERPP Vit Small Patch16 224.augreg In1k

This repository contains a Vision Transformer (ViT-Small) model fine-tuned for Facial Expression Recognition (FER).

KindOtherVersionRevision 75c1b11

Model source

Kind
Other
File size
82.7 MB
Version
vRevision 75c1b11
Updated
Sep 21, 2026
Downloads
0
Source
Hugging Face

Source description

ViT-Small for Facial Expression Recognition (FER++)

This repository contains a Vision Transformer (ViT-Small) model fine-tuned for Facial Expression Recognition (FER).

The model is initialized from the pretrained checkpoint vit_small_patch16_224.augreg_in1k from the timm library and fine-tuned on a 7-class emotion classification dataset (aligned with FER++ taxonomy).

Model Details

  • Model Architecture: Vision Transformer (ViT-Small)
  • Pretrained Base: vit_small_patch16_224.augreg_in1k
  • Number of Parameters: ~22M
  • Task: 7-class Facial Expression Classification
  • Classes:
    • 0: Angry
    • 1: Disgust
    • 2: Fear
    • 3: Happy
    • 4: Neutral
    • 5: Sad
    • 6: Surprise

Training Parameters & Hyperparameters

The model was fine-tuned using PyTorch and the timm library with the following setup:

HyperparameterValue
Epochs50
Batch Size128
Base Learning Rate5e-4
Weight Decay0.01
OptimizerAdamW
SchedulerCosine Annealing (with 5-epoch warmup starting at 1e-6, min LR 1e-5)
Image Resolution224 x 224 (Bicubic Interpolation)
Stochastic Depth (Drop Path)0.1
PrecisionMixed Precision (AMP)
Data AugmentationsRandAugment, Random Erasing (p=0.1)
Loss FunctionWeighted Cross-Entropy Loss
Class Weights[1.560, 3.737, 2.242, 0.541, 0.527, 0.970, 1.149] (mapping to classes 0 to 6)

Evaluation Results

Validation Set Performance

  • Accuracy: 83.83%
  • Precision (Macro): 0.7892
  • Recall (Macro): 0.7767
  • F1-Score (Macro): 0.7817
  • F1-Score (Weighted): 0.8378

Per-Class Validation Metrics:

Class IDClass NamePrecisionRecallF1-ScoreSupport
3Happy0.92470.93450.9295473
6Surprise0.86360.76610.8120124
4Neutral0.79430.81450.8043275
0Angry0.74120.84000.787575
5Sad0.77580.77110.7734166
2Fear0.80000.72730.761933
1Disgust0.62500.58330.603460

Test Set Performance

  • Accuracy: 83.68%
  • Precision (Macro): 0.7800
  • Recall (Macro): 0.7413
  • F1-Score (Macro): 0.7565
  • F1-Score (Weighted): 0.8348

Per-Class Test Metrics:

Class IDClass NamePrecisionRecallF1-ScoreSupport
3Happy0.91870.93780.9281482
6Surprise0.87930.79690.8361128
0Angry0.81580.84930.832273
5Sad0.86880.72770.7920191
4Neutral0.72400.87070.7906232
2Fear0.64710.50000.564122
1Disgust0.60660.50680.552273

How to Use

Here is how you can load the model and run inference on an image using PyTorch and timm:

import torch
import torch.nn as nn
from torchvision import transforms
from PIL import Image
from timm import create_model

# Define class mapping
class_mapping = {
    0: "Angry",
    1: "Disgust",
    2: "Fear",
    3: "Happy",
    4: "Neutral",
    5: "Sad",
    6: "Surprise"
}

# 1. Define image preprocessing matching validation/test set config
transform = transforms.Compose([
    transforms.Resize((224, 224), interpolation=transforms.InterpolationMode.BICUBIC),
    transforms.ToTensor(),
    transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
])

# 2. Instantiate and build the model structure
model = create_model('vit_small_patch16_224.augreg_in1k', pretrained=False, num_classes=7)

# 3. Load weight state dictionary
state_dict = torch.load('vitsmall2.pth', map_location='cpu')

# Clean state_dict if it was saved using DataParallel (remove 'module.' prefix)
if list(state_dict.keys())[0].startswith('module.'):
    state_dict = {k.replace('module.', ''): v for k, v in state_dict.items()}

model.load_state_dict(state_dict)
model.eval()

# 4. Perform Inference
image_path = "path_to_facial_image.jpg"
image = Image.open(image_path).convert('RGB')
input_tensor = transform(image).unsqueeze(0)  # Add batch dimension

with torch.no_grad():
    outputs = model(input_tensor)
    probabilities = torch.softmax(outputs, dim=1)[0]
    predicted_class_id = torch.argmax(probabilities).item()

print(f"Predicted emotion: {class_mapping[predicted_class_id]} ({probabilities[predicted_class_id].item():.2%})")

Sources

1 source
Hugging FacePrimaryVersion 75c1b11cdf25259014b34269b14275fb99e60159
huggingface.co/peepeeyanto/ViTFERPP_vit_small_patch16_224.augreg_in1k/tree/75c1b11cdf25259014b34269b14275fb99e60159

Verified Sep 10

Model artifacts

1 artifact

vitsmall2.pth

pth · 82.7 MB · SHA-256 bc3993eb6db7…ff42 · Hugging Face

Download

Source excerpts

2 excerpts

--- library_name: timm tags: - vision - image-classification - facial-expression-recognition - vit - vision-transformer - fer - ferplus datasets: - ferplus metrics: - accuracy - f1 - precision - recall pipeline_tag: image-classification --- # ViT-Small for Facial Expression Recognition (FER++) This repository contains a **Vision Transformer (ViT-Small)** model fine-tuned for **Facial Expression Recognition (FER)**. The model is initialized from the pretrained checkpoint `vit_small_patch16_224.augreg_in1k` from the `timm` library and fine-tuned on a 7-class emotion classification dataset (aligned with FER++ taxonomy). ## Model Details - **Model Architecture:** Vision Transformer (ViT-Small) - **Pretrained Base:** `vit_small_patch16_224.augreg_in1k` - **Number of Parameters:** ~22M - **Task:** 7-class Facial Expression Classification - **Classes:** - `0`: Angry - `1`: Disgust - `2`: Fear - `3`: Happy - `4`: Neutral - `5`: Sad - `6`: Surprise ## Training Parameters & Hyperparameters The model was fine-tuned using PyTorch and the `timm` library with the following setup: | Hyperparameter | Value | | :--- | :--- | | **Epochs** | 50 | | **Batch Size** | 128 | | **Base Learning Rate** | 5e-4 | | **Weight Decay** | 0.01 | | **Optimizer** | AdamW | | **Scheduler** | Cosine Annealing (with 5-epoch warmup starting at 1e-6, min LR 1e-5) | | **Image Resolution** | 224 x 224 (Bicubic Interpolation) | | **Stochastic Depth (Drop Path)** | 0.1 | | **Precision** | Mixed Precision (AMP) | | **Data Augmentations** | RandAugment, Random Erasing (p=0.1) | | **Loss Function** | Weighted Cross-Entropy Loss | | **Class Weights** | `[1.560, 3.737, 2.242, 0.541, 0.527, 0.970, 1.149]` (mapping to classes 0 to 6) | --- ## Evaluation Results ### Validation Set Performance - **Accuracy:** 83.83% - **Precision (Macro):** 0.7892 - **Recall (Macro):** 0.7767 - **F1-Score (Macro):** 0.7817 - **F1-Score (Weighted):** 0.8378 #### Per-Class Validation Metrics: | Class ID | Class Name | Precision | Recall | F1-Score | Support | | :---: | :--- | :---: | :---: | :---: | :---: | | 3 | Happy | 0.9247 | 0.9345 | 0.9295 | 473 | | 6 | Surprise | 0.8636 | 0.7661 | 0.8120 | 124 | | 4 | Neutral | 0.7943 | 0.8145 | 0.8043 | 275 | | 0 | Angry | 0.7412 | 0.8400 | 0.7875 | 75 | | 5 | Sad | 0.7758 | 0.7711 | 0.7734 | 166 | | 2 | Fear | 0.8000 | 0.7273 | 0.7619 | 33 | | 1 | Disgust | 0.6250...

Source context: 0 downloads · 0 likes · Pipeline image-classification · Library timm · Repo peepeeyanto/ViTFERPP_vit_small_patch16_224.augreg_in1k