Downloads · 30 days
9
16% of all-time downloads
abhshkp/footfall-analysis-vit-stage1
footfall-analysis-vit-stage1 is a image classification model from abhshkp. Use it when you need a label for an image. The card lists the license as apache-2.0.
This repository contains the stage-1 weights for a fine-tuned Vision Transformer (ViT) model optimized for binary gender classification (female and male). The model is designed to be integrated into footfall analysis,…
Downloads · 30 days
9
16% of all-time downloads
All-time downloads
58
Public
Repo size
346 MB
Likes
1
Public
Click a slice to open those files.
.bin346 MB · 100%
From the Hugging Face model README
This repository contains the stage-1 weights for a fine-tuned Vision Transformer (ViT) model optimized for binary gender classification (female and male). The model is designed to be integrated into footfall analysis, retail analytics, and demographic tracking pipelines.
The model utilizes the google/vit-base-patch16-224 architecture as its backbone, which was pre-trained on ImageNet-21k at a resolution of 224x224 pixels. It wraps the ViT backbone in a custom PyTorch class (GenderClassifier) that extracts the representation of the [CLS] token and passes it to a linear classifier layer to produce binary logits.
female and male), which does not capture the full diversity of gender identities.Below is a detailed guide on how to preprocess inputs and run inference using PyTorch and the Hugging Face transformers library.
Ensure the custom GenderClassifier class matches the structure expected by the saved pytorch_model.bin weights:
import torch
import torch.nn as nn
from transformers import ViTModel
class GenderClassifier(nn.Module):
def __init__(self, config):
super().__init__()
self.vit = ViTModel.from_pretrained(config.vit_model_name)
self.classifier = nn.Linear(self.vit.config.hidden_size, config.num_labels)
def forward(self, pixel_values):
outputs = self.vit(pixel_values=pixel_values)
# Extract the sequence output for the CLS token
sequence_output = outputs.last_hidden_state[:, 0, :]
# Pass to classification head
logits = self.classifier(sequence_output)
return logits
import torch
from transformers import ViTConfig
# Path to the directory containing config.json and pytorch_model.bin
model_path = "./path_to_model_directory"
# 1. Load the configuration
config = ViTConfig.from_pretrained(model_path)
# 2. Instantiate the model skeleton
model = GenderClassifier(config)
# 3. Load the fine-tuned weights
model.load_state_dict(torch.load(f"{model_path}/pytorch_model.bin", map_location="cpu"))
model.eval()
from PIL import Image
import torch
import torch.nn.functional as F
from transformers import ViTImageProcessor
# Initialize the image processor corresponding to the base ViT model
processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224")
# Load and preprocess an image (crop of a person)
image_path = "path_to_person_crop.jpg"
image = Image.open(image_path).convert("RGB")
# Preprocess image to tensor matching expected resolution (224x224) and normalization
inputs = processor(images=image, return_tensors="pt")
# Run inference
with torch.no_grad():
logits = model(pixel_values=inputs.pixel_values)
probabilities = F.softmax(logits, dim=-1)
predicted_class_id = torch.argmax(probabilities, dim=-1).item()
# Map prediction to label
id2label = config.id2label # {0: "female", 1: "male"}
print(f"Predicted class: {id2label[str(predicted_class_id)]} ({probabilities[0][predicted_class_id].item():.4f})")
The model was fine-tuned on person crops extracted from pedestrian detection datasets, annotated with binary gender labels.
When deploying automated gender classification models, care must be taken to: