CNN MNIST Digit Recognition
A compact CNN trained on MNIST for handwritten digit recognition.
Performance
- Test Accuracy: 99.63%
- Parameters: 175,594
- Training: 20 epochs on Apple M5 (MPS) in ~12 minutes
Architecture
- 3 convolutional blocks with BatchNorm + Dropout
- Global average pooling → FC classifier
- Input: 28×28 grayscale images
- Output: 10 classes (digits 0-9)
Per-class Accuracy
| Digit | Accuracy |
|---|---|
| 0 | 100.00% |
| 1 | 99.91% |
| 2 | 99.71% |
| 3 | 99.90% |
| 4 | 99.49% |
| 5 | 99.22% |
| 6 | 99.48% |
| 7 | 99.42% |
| 8 | 99.59% |
| 9 | 99.50% |
Usage
import torch
from PIL import Image
from torchvision.transforms import Compose, ToTensor, Normalize, Resize, Grayscale
# Load model
checkpoint = torch.load("model.pt", map_location="cpu")
# Preprocess (28x28 grayscale, normalized)
transform = Compose([
Grayscale(1),
Resize((28, 28)),
ToTensor(),
Normalize((0.1307,), (0.3081,)),
])
image = Image.open("digit.png")
tensor = transform(image).unsqueeze(0)
# Predict
model.eval()
with torch.no_grad():
logits = model(tensor)
prediction = logits.argmax(dim=1).item()
confidence = torch.softmax(logits, dim=1).max().item()
print(f"Predicted digit: {prediction} (confidence: {confidence:.2%})")
Training Details
- Dataset: MNIST (60K train / 10K test)
- Optimizer: AdamW (lr=1e-3, weight_decay=1e-4)
- Scheduler: OneCycleLR (cosine annealing)
- Augmentation: Random rotation ±15°, affine transforms (translate, scale, shear)
- Regularization: Dropout (0.25 conv, 0.5 FC) + BatchNorm
Generated by ML Intern
This model repository was generated by ML Intern, an agent for machine learning research and development on the Hugging Face Hub.
- Try ML Intern: https://smolagents-ml-intern.hf.space
- Source code: https://github.com/huggingface/ml-intern
- Downloads last month
- 1
Dataset used to train wenyun/cnn-mnist-digit-recognition
Evaluation results
- Accuracy on MNISTself-reported99.630