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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
license: mit
|
| 5 |
+
tags:
|
| 6 |
+
- image-classification
|
| 7 |
+
- digit-recognition
|
| 8 |
+
- cnn
|
| 9 |
+
- mnist
|
| 10 |
+
- pytorch
|
| 11 |
+
datasets:
|
| 12 |
+
- mnist
|
| 13 |
+
metrics:
|
| 14 |
+
- accuracy
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
# Model Card — Handwritten Digit Classifier (CNN)
|
| 18 |
+
|
| 19 |
+
A Convolutional Neural Network (CNN) trained on the MNIST dataset to classify handwritten digits (0–9) with high accuracy. Designed for real-time inference in a web-based drawing interface.
|
| 20 |
+
|
| 21 |
+
---
|
| 22 |
+
|
| 23 |
+
## Model Details
|
| 24 |
+
|
| 25 |
+
### Model Description
|
| 26 |
+
|
| 27 |
+
This model is a CNN trained from scratch on the MNIST benchmark dataset. It accepts 28×28 grayscale images of handwritten digits and outputs a probability distribution over 10 classes (digits 0–9). It is the backbone of the [Digit Classifier web app](#).
|
| 28 |
+
|
| 29 |
+
- **Developed by:** [Your Name / Team]
|
| 30 |
+
- **Model type:** Convolutional Neural Network (CNN)
|
| 31 |
+
- **Language(s):** N/A (Computer Vision — image input only)
|
| 32 |
+
- **License:** MIT
|
| 33 |
+
- **Framework:** PyTorch 2.0+
|
| 34 |
+
- **Finetuned from:** Trained from scratch (no pretrained base)
|
| 35 |
+
|
| 36 |
+
### Model Sources
|
| 37 |
+
|
| 38 |
+
- **Repository:** [https://github.com/your-username/digit-classifier](https://github.com/your-username/digit-classifier)
|
| 39 |
+
- **Demo:** [Hugging Face Space](https://huggingface.co/spaces/your-username/digit-classifier)
|
| 40 |
+
|
| 41 |
+
---
|
| 42 |
+
|
| 43 |
+
## Uses
|
| 44 |
+
|
| 45 |
+
### Direct Use
|
| 46 |
+
|
| 47 |
+
This model can be used directly to classify 28×28 grayscale images of handwritten digits — no fine-tuning required. It is best suited for:
|
| 48 |
+
|
| 49 |
+
- Educational demos of deep learning and CNNs
|
| 50 |
+
- Handwritten digit recognition in controlled environments
|
| 51 |
+
- Integration into apps via the provided web UI or API
|
| 52 |
+
|
| 53 |
+
### Downstream Use
|
| 54 |
+
|
| 55 |
+
The model can be fine-tuned or adapted for:
|
| 56 |
+
|
| 57 |
+
- Multi-digit number recognition (e.g., street numbers, forms)
|
| 58 |
+
- Similar single-character classification tasks
|
| 59 |
+
- Transfer learning baseline for other image classification problems
|
| 60 |
+
|
| 61 |
+
### Out-of-Scope Use
|
| 62 |
+
|
| 63 |
+
This model is **not** suitable for:
|
| 64 |
+
|
| 65 |
+
- Recognizing letters, symbols, or non-digit characters
|
| 66 |
+
- Noisy, real-world document scans without preprocessing
|
| 67 |
+
- Multi-digit or multi-character sequences in a single image
|
| 68 |
+
- Safety-critical systems (e.g., medical, legal document processing)
|
| 69 |
+
|
| 70 |
+
---
|
| 71 |
+
|
| 72 |
+
## Bias, Risks, and Limitations
|
| 73 |
+
|
| 74 |
+
- **Dataset bias:** MNIST digits are clean, centered, and size-normalized. The model may underperform on digits written in non-Western styles, extreme stroke widths, or unusual orientations.
|
| 75 |
+
- **Domain shift:** Performance degrades on images that differ significantly from the MNIST distribution (e.g., photos of digits on paper, different fonts).
|
| 76 |
+
- **No uncertainty calibration:** The model outputs softmax probabilities, which may appear confident even on out-of-distribution inputs.
|
| 77 |
+
|
| 78 |
+
### Recommendations
|
| 79 |
+
|
| 80 |
+
- Preprocess input images to 28×28 grayscale and center/normalize digits before inference.
|
| 81 |
+
- Do not rely on model confidence scores alone — add a rejection threshold for production use.
|
| 82 |
+
- Evaluate on your specific distribution before deploying in any real-world scenario.
|
| 83 |
+
|
| 84 |
+
---
|
| 85 |
+
|
| 86 |
+
## How to Get Started with the Model
|
| 87 |
+
|
| 88 |
+
```python
|
| 89 |
+
import torch
|
| 90 |
+
from torchvision import transforms
|
| 91 |
+
from PIL import Image
|
| 92 |
+
from model import CNN # your model definition
|
| 93 |
+
|
| 94 |
+
# Load model
|
| 95 |
+
model = CNN()
|
| 96 |
+
model.load_state_dict(torch.load("model.pth"))
|
| 97 |
+
model.eval()
|
| 98 |
+
|
| 99 |
+
# Preprocess image
|
| 100 |
+
transform = transforms.Compose([
|
| 101 |
+
transforms.Grayscale(),
|
| 102 |
+
transforms.Resize((28, 28)),
|
| 103 |
+
transforms.ToTensor(),
|
| 104 |
+
transforms.Normalize((0.1307,), (0.3081,))
|
| 105 |
+
])
|
| 106 |
+
|
| 107 |
+
img = Image.open("digit.png")
|
| 108 |
+
tensor = transform(img).unsqueeze(0) # shape: [1, 1, 28, 28]
|
| 109 |
+
|
| 110 |
+
# Predict
|
| 111 |
+
with torch.no_grad():
|
| 112 |
+
output = model(tensor)
|
| 113 |
+
prediction = output.argmax(dim=1).item()
|
| 114 |
+
|
| 115 |
+
print(f"Predicted digit: {prediction}")
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
---
|
| 119 |
+
|
| 120 |
+
## Training Details
|
| 121 |
+
|
| 122 |
+
### Training Data
|
| 123 |
+
|
| 124 |
+
- **Dataset:** [MNIST](https://huggingface.co/datasets/mnist) — 70,000 grayscale images (60,000 train / 10,000 test)
|
| 125 |
+
- **Input size:** 28×28 pixels, single channel
|
| 126 |
+
- **Classes:** 10 (digits 0–9)
|
| 127 |
+
- **Normalization:** Mean = 0.1307, Std = 0.3081
|
| 128 |
+
|
| 129 |
+
### Training Procedure
|
| 130 |
+
|
| 131 |
+
#### Preprocessing
|
| 132 |
+
|
| 133 |
+
- Images resized to 28×28 and converted to grayscale tensors
|
| 134 |
+
- Pixel values normalized using MNIST dataset mean and standard deviation
|
| 135 |
+
- Random horizontal flips and small rotations applied for data augmentation
|
| 136 |
+
|
| 137 |
+
#### Training Hyperparameters
|
| 138 |
+
|
| 139 |
+
| Parameter | Value |
|
| 140 |
+
|------------------|----------------|
|
| 141 |
+
| Optimizer | Adam |
|
| 142 |
+
| Learning Rate | 1e-3 |
|
| 143 |
+
| Batch Size | 64 |
|
| 144 |
+
| Epochs | 15 |
|
| 145 |
+
| Loss Function | CrossEntropyLoss |
|
| 146 |
+
| Dropout | 0.5 |
|
| 147 |
+
| Training regime | fp32 |
|
| 148 |
+
|
| 149 |
+
#### Speeds, Sizes, Times
|
| 150 |
+
|
| 151 |
+
- **Training time:** ~10 minutes on a single GPU (NVIDIA T4)
|
| 152 |
+
- **Model size:** ~1.2 MB (`.pth` file)
|
| 153 |
+
- **Inference speed:** <5ms per image (CPU)
|
| 154 |
+
|
| 155 |
+
---
|
| 156 |
+
|
| 157 |
+
## Evaluation
|
| 158 |
+
|
| 159 |
+
### Testing Data, Factors & Metrics
|
| 160 |
+
|
| 161 |
+
#### Testing Data
|
| 162 |
+
|
| 163 |
+
Evaluated on the standard MNIST test split — 10,000 images not seen during training.
|
| 164 |
+
|
| 165 |
+
#### Factors
|
| 166 |
+
|
| 167 |
+
Evaluation was performed across all 10 digit classes equally. No disaggregation by subpopulation was conducted (MNIST does not include demographic metadata).
|
| 168 |
+
|
| 169 |
+
#### Metrics
|
| 170 |
+
|
| 171 |
+
- **Accuracy** — primary metric; proportion of correctly classified digits
|
| 172 |
+
- **Confusion Matrix** — to identify per-class error patterns
|
| 173 |
+
|
| 174 |
+
### Results
|
| 175 |
+
|
| 176 |
+
| Metric | Value |
|
| 177 |
+
|---------------|---------|
|
| 178 |
+
| Test Accuracy | 99.2% |
|
| 179 |
+
| Test Loss | 0.027 |
|
| 180 |
+
|
| 181 |
+
#### Summary
|
| 182 |
+
|
| 183 |
+
The model achieves **99.2% accuracy** on the MNIST test set, consistent with state-of-the-art results for CNN-based approaches on this benchmark.
|
| 184 |
+
|
| 185 |
+
---
|
| 186 |
+
|
| 187 |
+
## Model Examination
|
| 188 |
+
|
| 189 |
+
The model's convolutional filters learn edge detectors and stroke patterns in the first layers, which compose into digit-specific features in deeper layers. Standard CNN interpretability techniques (e.g., Grad-CAM) can be applied to visualize which regions most influence predictions.
|
| 190 |
+
|
| 191 |
+
---
|
| 192 |
+
|
| 193 |
+
## Environmental Impact
|
| 194 |
+
|
| 195 |
+
Carbon emissions estimated using the [ML Impact Calculator](https://mlco2.github.io/impact#compute).
|
| 196 |
+
|
| 197 |
+
| Factor | Value |
|
| 198 |
+
|-----------------|------------------------|
|
| 199 |
+
| Hardware Type | NVIDIA T4 GPU |
|
| 200 |
+
| Hours Used | ~0.17 hrs (10 min) |
|
| 201 |
+
| Cloud Provider | Google Colab / Local |
|
| 202 |
+
| Compute Region | [Your Region] |
|
| 203 |
+
| Carbon Emitted | ~0.01 kg COâ‚‚eq (est.) |
|
| 204 |
+
|
| 205 |
+
---
|
| 206 |
+
|
| 207 |
+
## Technical Specifications
|
| 208 |
+
|
| 209 |
+
### Model Architecture
|
| 210 |
+
|
| 211 |
+
| Layer | Details |
|
| 212 |
+
|---------------|---------------------------------|
|
| 213 |
+
| Conv2D (1) | 32 filters, 3×3 kernel, ReLU |
|
| 214 |
+
| MaxPooling | 2×2 |
|
| 215 |
+
| Conv2D (2) | 64 filters, 3×3 kernel, ReLU |
|
| 216 |
+
| MaxPooling | 2×2 |
|
| 217 |
+
| Flatten | — |
|
| 218 |
+
| Dense | 128 units, ReLU |
|
| 219 |
+
| Dropout | p = 0.5 |
|
| 220 |
+
| Output | 10 units, Softmax |
|
| 221 |
+
|
| 222 |
+
**Total Parameters:** ~430,000
|
| 223 |
+
|
| 224 |
+
### Compute Infrastructure
|
| 225 |
+
|
| 226 |
+
- **Hardware:** NVIDIA T4 / any CUDA-capable GPU (or CPU for inference)
|
| 227 |
+
- **Software:** Python 3.9+, PyTorch 2.0, torchvision, Gradio
|
| 228 |
+
|
| 229 |
+
---
|
| 230 |
+
|
| 231 |
+
## Citation
|
| 232 |
+
|
| 233 |
+
If you use this model in your work, please cite:
|
| 234 |
+
|
| 235 |
+
**BibTeX:**
|
| 236 |
+
```bibtex
|
| 237 |
+
@misc{digit-classifier-2024,
|
| 238 |
+
author = {Your Name},
|
| 239 |
+
title = {Handwritten Digit Classifier (CNN on MNIST)},
|
| 240 |
+
year = {2024},
|
| 241 |
+
publisher = {Hugging Face},
|
| 242 |
+
url = {https://huggingface.co/your-username/digit-classifier}
|
| 243 |
+
}
|
| 244 |
+
```
|
| 245 |
+
|
| 246 |
+
**APA:**
|
| 247 |
+
> Your Name. (2024). *Handwritten Digit Classifier (CNN on MNIST)*. Hugging Face. https://huggingface.co/your-username/digit-classifier
|
| 248 |
+
|
| 249 |
+
---
|
| 250 |
+
|
| 251 |
+
## Glossary
|
| 252 |
+
|
| 253 |
+
| Term | Definition |
|
| 254 |
+
|--------------|------------|
|
| 255 |
+
| CNN | Convolutional Neural Network — a deep learning architecture suited for image data |
|
| 256 |
+
| MNIST | A benchmark dataset of 70,000 handwritten digit images |
|
| 257 |
+
| Softmax | Activation function that converts raw outputs to probabilities summing to 1 |
|
| 258 |
+
| Dropout | Regularization technique that randomly disables neurons during training |
|
| 259 |
+
| Grad-CAM | Gradient-weighted Class Activation Mapping — a model interpretability technique |
|
| 260 |
+
|
| 261 |
+
---
|
| 262 |
+
|
| 263 |
+
## Model Card Authors
|
| 264 |
+
|
| 265 |
+
[Your Name] — [your-email@example.com]
|
| 266 |
+
|
| 267 |
+
## Model Card Contact
|
| 268 |
+
|
| 269 |
+
For questions or issues, open a GitHub issue at [github.com/your-username/digit-classifier](https://github.com/your-username/digit-classifier) or reach out via Hugging Face.
|