AdityaManojShinde commited on
Commit
ca7a55d
·
verified ·
1 Parent(s): 96396c6

updated model card

Browse files

updated the model and added details related to the model

Files changed (1) hide show
  1. README.md +198 -3
README.md CHANGED
@@ -1,3 +1,198 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: en
3
+ tags:
4
+ - image-classification
5
+ - mnist
6
+ - emnist
7
+ - digit-recognition
8
+ - pytorch
9
+ - resnet
10
+ license: mit
11
+ datasets:
12
+ - mnist
13
+ - emnist
14
+ pipeline_tag: image-classification
15
+ ---
16
+
17
+ # Handwritten Digit Classifier
18
+
19
+ A PyTorch image classification model that recognizes handwritten digits (0–9), built on a **pretrained ResNet-18** backbone (ImageNet weights) fine-tuned on a combined **MNIST + EMNIST** dataset with aggressive data augmentation. Achieves **99.46% accuracy** on the combined test set.
20
+
21
+ ---
22
+
23
+ ## Model Details
24
+
25
+ | Property | Value |
26
+ |-----------------------|-------------------------------------------------|
27
+ | **Architecture** | ResNet-18 (pretrained on ImageNet) |
28
+ | **Framework** | PyTorch |
29
+ | **Task** | Image Classification (10 classes, digits 0–9) |
30
+ | **Input Size** | 32 × 32 (grayscale, converted to 3-channel) |
31
+ | **Output** | Softmax probabilities over digits 0–9 |
32
+ | **Test Accuracy** | **99.46%** |
33
+ | **Training Device** | CUDA (GPU) |
34
+ | **Epochs** | 7 |
35
+ | **Batch Size** | 256 |
36
+ | **Optimizer** | Adam (differential learning rates) |
37
+ | **Loss Function** | CrossEntropyLoss |
38
+ | **LR Scheduler** | StepLR (step=2, gamma=0.5) |
39
+
40
+ ---
41
+
42
+ ## Architecture
43
+
44
+ The model uses a **ResNet-18** backbone pretrained on ImageNet, with the default classification head replaced by a custom fully-connected head:
45
+
46
+ ```
47
+ ResNet-18 Backbone (pretrained on ImageNet1K)
48
+
49
+ Linear(512 → 128)
50
+
51
+ ReLU()
52
+
53
+ Dropout(0.3)
54
+
55
+ Linear(128 → 10)
56
+
57
+ Softmax (at inference)
58
+ ```
59
+
60
+ **Differential learning rates** were used to preserve pretrained features while allowing the new head to learn faster:
61
+ - Pretrained backbone layers: `lr = 0.0001`
62
+ - New classification head (last 4 param groups): `lr = 0.001`
63
+
64
+ The dropout layer (p=0.3) reduces overfitting given the simplicity of digit images relative to the model's capacity.
65
+
66
+ ---
67
+
68
+ ## Dataset
69
+
70
+ The model was trained on a **combined MNIST + EMNIST (digits split)** dataset for greater diversity and robustness.
71
+
72
+ ### MNIST
73
+ | Property | Value |
74
+ |------------------|----------------------------|
75
+ | **Classes** | 10 (digits 0–9) |
76
+ | **Training set** | 60,000 grayscale images |
77
+ | **Test set** | 10,000 grayscale images |
78
+ | **Image size** | 28 × 28 pixels |
79
+ | **Source** | [yann.lecun.com/exdb/mnist](http://yann.lecun.com/exdb/mnist/) |
80
+
81
+ ### EMNIST (digits split)
82
+ | Property | Value |
83
+ |------------------|----------------------------|
84
+ | **Classes** | 10 (digits 0–9) |
85
+ | **Training set** | 240,000 grayscale images |
86
+ | **Test set** | 40,000 grayscale images |
87
+ | **Image size** | 28 × 28 pixels |
88
+ | **Source** | [NIST Special Database 19](https://www.nist.gov/itl/products-and-services/emnist-dataset) |
89
+
90
+ **Combined total:** 300,000 training images and 50,000 test images.
91
+
92
+ ---
93
+
94
+ ## Training
95
+
96
+ The model was trained for **7 epochs** on CUDA with a StepLR scheduler (halving LR every 2 epochs). Loss decreased consistently across all epochs.
97
+
98
+ | Epoch | Loss |
99
+ |-------|--------|
100
+ | 1 | 0.1732 |
101
+ | 2 | 0.0635 |
102
+ | 3 | 0.0446 |
103
+ | 4 | 0.0409 |
104
+ | 5 | 0.0340 |
105
+ | 6 | 0.0307 |
106
+ | 7 | 0.0279 |
107
+
108
+ **Final Test Accuracy: 99.46%**
109
+
110
+ ---
111
+
112
+ ## Data Augmentation
113
+
114
+ Aggressive augmentation was applied during training to improve generalization to real-world handwriting styles:
115
+
116
+ | Augmentation | Parameters |
117
+ |-------------------------|-----------------------------------------|
118
+ | Random Rotation | ±15° |
119
+ | Random Affine (translate)| ±15% horizontal and vertical |
120
+ | Random Affine (shear) | 10° |
121
+ | Random Perspective | distortion scale 0.3, p=0.3 |
122
+ | Color Jitter | brightness ±0.3, contrast ±0.3 |
123
+ | Normalization | mean (0.5, 0.5, 0.5), std (0.5, 0.5, 0.5) |
124
+
125
+ No augmentation was applied to the test set (only resize + normalize).
126
+
127
+ ---
128
+
129
+ ## Preprocessing
130
+
131
+ At inference, input images go through the following pipeline:
132
+
133
+ 1. Convert to **grayscale**
134
+ 2. **Invert** colors (white background → black background to match MNIST format)
135
+ 3. **Resize** to 32 × 32
136
+ 4. Convert to **3-channel** (grayscale replicated across RGB channels for ResNet compatibility)
137
+ 5. **Normalize** with mean `(0.5, 0.5, 0.5)` and std `(0.5, 0.5, 0.5)`
138
+
139
+ ---
140
+
141
+ ## Usage
142
+
143
+ ```python
144
+ import torch
145
+ import torch.nn as nn
146
+ from torchvision import transforms, models
147
+ from huggingface_hub import hf_hub_download
148
+ from PIL import Image
149
+ import numpy as np
150
+
151
+ # Load model
152
+ model = models.resnet18(weights=None)
153
+ model.fc = nn.Sequential(
154
+ nn.Linear(512, 128), nn.ReLU(), nn.Dropout(0.3), nn.Linear(128, 10)
155
+ )
156
+
157
+ weights_path = hf_hub_download(
158
+ repo_id="AdityaManojShinde/handwritten_digit_classifier",
159
+ filename="mnist_model.pth"
160
+ )
161
+ model.load_state_dict(torch.load(weights_path, map_location="cpu"))
162
+ model.eval()
163
+
164
+ # Preprocessing pipeline
165
+ transform = transforms.Compose([
166
+ transforms.Grayscale(num_output_channels=3),
167
+ transforms.Resize((32, 32)),
168
+ transforms.ToTensor(),
169
+ transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
170
+ ])
171
+
172
+ # Inference
173
+ image = Image.open("your_digit.png").convert("L")
174
+ img_array = 255 - np.array(image) # invert: white bg → black bg
175
+ image = Image.fromarray(img_array)
176
+ img_tensor = transform(image).unsqueeze(0)
177
+
178
+ with torch.no_grad():
179
+ output = model(img_tensor)
180
+ probs = torch.nn.functional.softmax(output, dim=1)[0]
181
+ predicted = probs.argmax().item()
182
+
183
+ print(f"Predicted digit: {predicted} ({probs[predicted]*100:.1f}% confidence)")
184
+ ```
185
+
186
+ ---
187
+
188
+ ## Limitations
189
+
190
+ - Works best with **centered, clearly written** single digits on a plain background.
191
+ - Not suitable for multi-digit recognition or digit detection in natural scenes.
192
+ - May struggle with highly stylized or non-standard digit handwriting not represented in MNIST/EMNIST.
193
+
194
+ ---
195
+
196
+ ## License
197
+
198
+ This model is released under the [MIT License](https://opensource.org/licenses/MIT).