πŸ“– ResNet-18 Fine-Tuned on EMNIST Letters

This repository contains a ResNet-18 model fine-tuned on the EMNIST Letters dataset for handwritten character recognition (a-z).


πŸ“ Model Details

  • Architecture: ResNet-18 (pretrained on ImageNet)

  • Dataset: EMNIST Letters (https://www.nist.gov/itl/products-and-services/emnist-dataset)

  • Input Size: 224x224 (grayscale, converted to 3 channels)

  • Classes: 26 lowercase letters (a to z)

  • Transformations:

    • Resize to 224x224
    • Convert to grayscale with 3 channels
    • Rotate 90Β° clockwise
    • Flip vertically
    • Normalize with ImageNet mean & std

πŸ› οΈ Usage

1️⃣ Install Required Libraries

pip install torch torchvision huggingface_hub

2️⃣ Load the Model

import torch
from torchvision import models
from huggingface_hub import hf_hub_download

# Download model weights
model_path = hf_hub_download("sanjeevan7/emnist-letters-eng-resnet18-v2", filename="pytorch_model.bin")

# Load model architecture
model = models.resnet18()
model.fc = torch.nn.Linear(model.fc.in_features, 26)  # 26 letters

# Load weights
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
model.eval()

🎨 Input Preprocessing

import torchvision.transforms as transforms
import torchvision.transforms.functional as TF

transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.Grayscale(3),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

πŸ§ͺ Inference Example

from PIL import Image

# Load and preprocess image
image = Image.open("your_image.png").convert('L')  # Grayscale
input_tensor = transform(image).unsqueeze(0)

# Predict
with torch.no_grad():
    outputs = model(input_tensor)
    _, predicted = torch.max(outputs, 1)

predicted_class = predicted.item()
predicted_char = chr(predicted_class + 97)  # 0->a, 1->b, ...

print(f"Predicted Character: {predicted_char}")

πŸ“Š Performance

  • Accuracy: ~94% on EMNIST Letters test set

  • Known Limitations:

    • Requires correct image orientation (preprocessing handles this)

πŸ“₯ Files

File Description
pytorch_model.bin Model weights
config.json Model metadata (architecture info)

πŸ“§ Contact

For any questions, please contact Sanjeevan.


Downloads last month
2
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support