MNIST Handwritten Digit Recognition -
Documentation
Table of Contents
1. Overview
2. Technical Architecture
3. Dataset Information
4. Neural Network Design
5. Implementation Guide
6. API Reference
7. Performance Metrics
8. Troubleshooting
9. Advanced Usage
Overview
This project implements a handwritten digit recognition system using the MNIST dataset and
[Link]. The system can classify handwritten digits (0-9) with high accuracy using a
convolutional neural network.
Key Features
Real-time digit recognition
Interactive drawing canvas
Pre-trained model with 98%+ accuracy
Data visualization and metrics
Cross-platform web-based interface
Requirements
Modern web browser with JavaScript enabled
Internet connection (for [Link] CDN)
Minimum 2GB RAM recommended
Technical Architecture
System Components
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Input Layer │ │ Hidden Layers │ │ Output Layer │
│ (28x28x1) │───▶│ CNN + Dense │───▶│ (10 classes) │
│ │ │ │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
Technology Stack
Frontend: HTML5, CSS3, JavaScript (ES6+)
ML Framework: [Link] 4.10.0
Canvas API: For drawing interface
Web APIs: File API, Performance API
Dataset Information
MNIST Dataset Overview
Training Images: 60,000 samples
Test Images: 10,000 samples
Image Size: 28×28 pixels (grayscale)
Classes: 10 digits (0-9)
Format: Normalized pixel values (0-1)
Data Preprocessing
// Normalization
const normalizedData = [Link](255.0);
// Reshaping for CNN
const reshapedData = [Link]([-1, 28, 28, 1]);
// One-hot encoding for labels
const oneHotLabels = [Link](labels, 10);
Neural Network Design
Architecture Details
Model Structure
Input Layer: 28×28×1 (784 neurons)
Conv2D Layer 1: 32 filters, 3×3 kernel, ReLU activation
MaxPool2D: 2×2 pool size
Conv2D Layer 2: 64 filters, 3×3 kernel, ReLU activation
MaxPool2D: 2×2 pool size
Flatten: Converts to 1D
Dense Layer 1: 128 neurons, ReLU activation
Dropout: 0.2 rate
Dense Layer 2: 10 neurons, Softmax activation (output)
Layer Configuration
const model = [Link]({
layers: [
// Convolutional layers
[Link].conv2d({
inputShape: [28, 28, 1],
filters: 32,
kernelSize: 3,
activation: 'relu'
}),
[Link].maxPooling2d({ poolSize: 2 }),
[Link].conv2d({
filters: 64,
kernelSize: 3,
activation: 'relu'
}),
[Link].maxPooling2d({ poolSize: 2 }),
// Dense layers
[Link](),
[Link]({
units: 128,
activation: 'relu'
}),
[Link]({ rate: 0.2 }),
[Link]({
units: 10,
activation: 'softmax'
})
]
});
Training Configuration
Optimizer: Adam (learning rate: 0.001)
Loss Function: Categorical Crossentropy
Metrics: Accuracy
Batch Size: 32
Epochs: 10-15
Validation Split: 20%
Implementation Guide
Step 1: Environment Setup
<!DOCTYPE html>
<html lang="en">
<head>
<script
src="[Link]
script>
</head>
Step 2: Data Loading
async function loadMNISTData() {
const MNIST_IMAGES_SPRITE_PATH =
'[Link]
const MNIST_LABELS_PATH =
'[Link]
;
const img = new Image();
const canvas = [Link]('canvas');
const ctx = [Link]('2d');
return new Promise((resolve) => {
[Link] = () => {
// Process image data
resolve(processedData);
};
[Link] = MNIST_IMAGES_SPRITE_PATH;
});
}
Step 3: Model Creation
function createModel() {
const model = [Link]({
layers: [
[Link].conv2d({
inputShape: [28, 28, 1],
filters: 32,
kernelSize: 3,
activation: 'relu'
}),
// ... additional layers
]
});
[Link]({
optimizer: 'adam',
loss: 'categoricalCrossentropy',
metrics: ['accuracy']
});
return model;
}
Step 4: Training Process
async function trainModel(model, trainData, trainLabels) {
const history = await [Link](trainData, trainLabels, {
epochs: 10,
batchSize: 32,
validationSplit: 0.2,
callbacks: {
onEpochEnd: (epoch, logs) => {
[Link](`Epoch ${epoch}: loss = ${[Link](4)}`);
}
}
});
return history;
}
Step 5: Prediction
function predict(model, imageData) {
const prediction = [Link](imageData);
const probabilities = [Link]();
const predictedClass = [Link](-1).dataSync()[0];
return {
class: predictedClass,
confidence: probabilities[predictedClass],
probabilities: [Link](probabilities)
};
}
API Reference
Core Functions
loadMNISTData()
Loads and preprocesses the MNIST dataset.
Returns: Promise<{images, labels}>
Usage: const data = await loadMNISTData();
createModel()
Creates and compiles the CNN model.
Returns: [Link]
Usage: const model = createModel();
trainModel(model, trainData, trainLabels, options)
Trains the model with provided data.
Parameters:
o model: [Link] model
o trainData: Training images tensor
o trainLabels: Training labels tensor
o options: Training configuration object
Returns: Promise<History>
predict(model, imageData)
Makes predictions on input data.
Parameters:
o model: Trained model
o imageData: Input image tensor (28×28×1)
Returns: Prediction object with class and probabilities
Canvas Drawing API
initializeCanvas(canvasId)
Sets up the drawing canvas for digit input.
Parameters: canvasId - Canvas element ID
Returns: Canvas context and drawing functions
preprocessCanvasImage(canvas)
Converts canvas drawing to model input format.
Parameters: canvas - Canvas element
Returns: Preprocessed tensor (28×28×1)
Performance Metrics
Training Metrics
Training Accuracy: ~99.2%
Validation Accuracy: ~98.5%
Training Loss: ~0.03
Validation Loss: ~0.05
Training Time: ~5-10 minutes (CPU)
Inference Performance
Prediction Time: <50ms per image
Model Size: ~2.5MB
Memory Usage: ~100MB during training
Accuracy by Digit Class
Digit 0: 99.1% Digit 5: 98.2%
Digit 1: 99.4% Digit 6: 98.8%
Digit 2: 98.7% Digit 7: 98.6%
Digit 3: 98.9% Digit 8: 97.9%
Digit 4: 98.5% Digit 9: 98.1%
Troubleshooting
Common Issues
Low Training Accuracy
Problem: Model accuracy below 95% Solutions:
Increase training epochs
Adjust learning rate
Add data augmentation
Check data preprocessing
Slow Training
Problem: Training takes too long Solutions:
Reduce batch size
Use GPU acceleration
Optimize model architecture
Reduce dataset size for testing
Canvas Recognition Issues
Problem: Drawn digits not recognized well Solutions:
Ensure proper image preprocessing
Check canvas-to-tensor conversion
Verify image normalization (0-1 range)
Center digits in canvas
Memory Errors
Problem: Out of memory during training Solutions:
Reduce batch size
Use [Link]() to free memory
Process data in smaller chunks
Close browser tabs
Debugging Tips
Enable Verbose Logging
[Link]('DEBUG', true);
Monitor Memory Usage
[Link]('Memory info:', [Link]());
Validate Data Shapes
[Link]('Data shape:', [Link]);
[Link]('Labels shape:', [Link]);
Advanced Usage
Custom Model Architecture
function createAdvancedModel() {
const model = [Link]({
layers: [
// Batch normalization
[Link]({ inputShape: [28, 28, 1] }),
// Multiple conv blocks
[Link].conv2d({ filters: 32, kernelSize: 3, activation:
'relu' }),
[Link].conv2d({ filters: 32, kernelSize: 3, activation:
'relu' }),
[Link].maxPooling2d({ poolSize: 2 }),
[Link]({ rate: 0.25 }),
[Link].conv2d({ filters: 64, kernelSize: 3, activation:
'relu' }),
[Link].conv2d({ filters: 64, kernelSize: 3, activation:
'relu' }),
[Link].maxPooling2d({ poolSize: 2 }),
[Link]({ rate: 0.25 }),
// Dense layers with regularization
[Link](),
[Link]({
units: 256,
activation: 'relu',
kernelRegularizer: [Link].l2({ l2: 0.001 })
}),
[Link]({ rate: 0.5 }),
[Link]({ units: 10, activation: 'softmax' })
]
});
return model;
}
Data Augmentation
function augmentData(images) {
return [Link](images, [32, 32])
.div(255.0)
.expandDims(-1);
}
Model Saving and Loading
// Save model
await [Link]('downloads://my-mnist-model');
// Load model
const loadedModel = await [Link]('path/to/[Link]');
Batch Prediction
function batchPredict(model, imagesBatch) {
const predictions = [Link](imagesBatch);
return [Link](-1).dataSync();
}
Performance Optimization
// Use GPU backend if available
[Link]('webgl');
// Warm up the model
const dummyInput = [Link]([1, 28, 28, 1]);
[Link](dummyInput).dispose();
[Link]();
Conclusion
This MNIST digit recognition system provides a complete implementation of a convolutional
neural network for handwritten digit classification. The system achieves high accuracy while
maintaining good performance for real-time predictions.
For additional support or advanced features, refer to the [Link] documentation or modify
the code according to your specific requirements.