Spaces:
Running
Running
File size: 2,364 Bytes
da66501 | 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 | """
Shared CNN architecture for both ASL and ISL sign language models.
Uses MobileNetV2 pretrained on ImageNet with a custom classifier head.
"""
import torch
import torch.nn as nn
from torchvision import models
def build_model(num_classes: int, pretrained: bool = True) -> nn.Module:
"""
Build a MobileNetV2-based classifier for sign language recognition.
Args:
num_classes: Number of output classes (29 for ASL, 35 for ISL).
pretrained: Whether to load ImageNet pretrained weights.
Returns:
A PyTorch model ready for fine-tuning or inference.
"""
weights = models.MobileNet_V2_Weights.DEFAULT if pretrained else None
model = models.mobilenet_v2(weights=weights)
# Freeze all backbone layers initially
for param in model.parameters():
param.requires_grad = False
# Unfreeze the last 3 InvertedResidual blocks + classifier for fine-tuning
trainable_layers = list(model.features.children())[-4:]
for layer in trainable_layers:
for param in layer.parameters():
param.requires_grad = True
# Replace the classifier head
in_features = model.classifier[1].in_features # 1280 for MobileNetV2
model.classifier = nn.Sequential(
nn.Dropout(p=0.3),
nn.Linear(in_features, 512),
nn.ReLU(),
nn.Dropout(p=0.2),
nn.Linear(512, num_classes),
)
return model
def load_model(checkpoint_path: str, num_classes: int, device: torch.device) -> nn.Module:
"""
Load a trained model from a checkpoint.
Args:
checkpoint_path: Path to the .pth file.
num_classes: Number of output classes.
device: torch device to load onto.
Returns:
Model in eval mode on the specified device.
"""
model = build_model(num_classes=num_classes, pretrained=False)
checkpoint = torch.load(checkpoint_path, map_location=device)
model.load_state_dict(checkpoint["model_state_dict"])
model.to(device)
model.eval()
print(f"[OK] Loaded model from {checkpoint_path} | Classes: {num_classes} | Device: {device}")
return model
# ImageNet normalization constants (used for both train & inference)
NORMALIZE_MEAN = [0.485, 0.456, 0.406]
NORMALIZE_STD = [0.229, 0.224, 0.225]
INPUT_SIZE = 224
|