Views
No views yet
a to z)pip install torch torchvision huggingface_hub1import torch
2from torchvision import models
3from huggingface_hub import hf_hub_download
4
5# Download model weights
6model_path = hf_hub_download("sanjeevan7/emnist-letters-eng-resnet18-v2", filename="pytorch_model.bin")
7
8# Load model architecture
9model = models.resnet18()
10model.fc = torch.nn.Linear(model.fc.in_features, 26) # 26 letters
11
12# Load weights
13model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
14model.eval()1import torchvision.transforms as transforms
2import torchvision.transforms.functional as TF
3
4transform = transforms.Compose([
5 transforms.Resize((224, 224)),
6 transforms.Grayscale(3),
7 transforms.ToTensor(),
8 transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
9])1from PIL import Image
2
3# Load and preprocess image
4image = Image.open("your_image.png").convert('L') # Grayscale
5input_tensor = transform(image).unsqueeze(0)
6
7# Predict
8with torch.no_grad():
9 outputs = model(input_tensor)
10 _, predicted = torch.max(outputs, 1)
11
12predicted_class = predicted.item()
13predicted_char = chr(predicted_class + 97) # 0->a, 1->b, ...
14
15print(f"Predicted Character: {predicted_char}")| File | Description |
|---|---|
pytorch_model.bin | Model weights |
config.json | Model metadata (architecture info) |