# Improving Digit Recognition Accuracy The updated version includes several improvements to increase recognition accuracy. Here's what changed and how to get the best results: ## ๐Ÿ”ง Model Improvements ### Better Neural Network Architecture - **CNN (Convolutional Neural Network)** instead of simple Dense layers - Better at learning spatial patterns in images - Multiple convolutional blocks with max pooling - **More training epochs**: 20 epochs instead of 10 - Longer training time but better accuracy - **Data augmentation**: - Rotations (ยฑ20ยฐ) - Shifts (ยฑ10%) - Shear transformations - Zoom variations - Helps model generalize to different handwriting styles - **Batch normalization**: Stabilizes training - **Higher dropout rates**: Better regularization ### Expected Model Accuracy - **On MNIST test set**: ~99% - **On real handwriting**: 70-95% (varies by writing style) --- ## ๐Ÿ“ธ Image Processing Improvements The backend now applies advanced preprocessing: 1. **Gaussian Blur**: Reduces noise in the drawing 2. **Adaptive Thresholding**: Better binarization (black and white conversion) 3. **Contour Detection**: Finds and isolates the digit 4. **Automatic Centering**: Centers the digit in the image 5. **High-quality Resizing**: LANCZOS interpolation for sharp 28ร—28 image This matches how MNIST dataset was prepared! --- ## โœ๏ธ Drawing Tips for Best Results ### 1. **Draw Large & Bold** - Fill as much of the canvas as possible - Use thick, confident strokes - The model expects digits that fill most of the space ### 2. **Keep It Simple** - Draw one digit at a time - Don't add decorations or flourishes - Stick to standard digit shapes ### 3. **Clean Strokes** - Use smooth, continuous lines - Avoid shaky or jerky movements - Keep thickness consistent ### 4. **Centered Position** - Keep the digit roughly centered - Leave some margin around edges - The model will auto-center anyway ### 5. **Standard Style** - Write digits as you normally would - Not too slanted or rotated - Avoid unusual personal styles ### 6. **Proper Timing** - Draw at a normal, comfortable pace - Not too fast or slow - Smooth pressure on mouse --- ## ๐ŸŽฏ Digit-Specific Tips ### Easy to Recognize - **0, 1, 4, 7**: Simple, distinct shapes - **6, 9**: Distinct loop patterns - **2, 5**: Unique curves ### Harder to Recognize - **3, 8**: Can look similar to each other - **5, 6**: Similar curves and loops - **3, 5**: Similar bends **Tip**: Make distinctions clear: - **3 vs 5**: Make 3's curves more open - **8 vs 6**: Make 8's loops clearly separated - **6 vs 9**: Clear which way the loop faces --- ## ๐Ÿงช Testing & Troubleshooting ### If accuracy is still low: **1. Check Model Training** ```bash # Delete old model to retrain rm digit_model.h5 python app.py ``` The model will retrain and might perform better. **2. Verify Preprocessing** - Check that the digit is white on a black background - Canvas should now be pure black (was blue) - White strokes are critical **3. Clear Drawing Issues** - If canvas looks faded, refresh the page - Ensure drawing is crisp and clean - Use a mouse, not a touchpad if possible **4. Compare Different Digits** - Try drawing multiple digits - See which ones are recognized well - This helps identify writing style issues --- ## ๐Ÿ“Š Model Architecture (Improved) ``` Input (28ร—28ร—1) โ†“ Conv2D 32 filters + BatchNorm + ReLU โ†“ Conv2D 32 filters + BatchNorm + ReLU โ†“ MaxPool (2ร—2) + Dropout(0.25) โ†“ Conv2D 64 filters + BatchNorm + ReLU โ†“ Conv2D 64 filters + BatchNorm + ReLU โ†“ MaxPool (2ร—2) + Dropout(0.25) โ†“ Conv2D 128 filters + BatchNorm + ReLU โ†“ MaxPool (2ร—2) + Dropout(0.25) โ†“ Flatten โ†“ Dense 256 + BatchNorm + ReLU + Dropout(0.5) โ†“ Dense 128 + BatchNorm + ReLU + Dropout(0.5) โ†“ Dense 10 (Softmax output for 0-9) ``` **Total Parameters**: ~650K **Training Time**: 5-10 minutes first run --- ## ๐Ÿ”„ If You Want Even Better Accuracy ### Option 1: Train on Custom Data Collect samples of your own handwriting and retrain: ```python # Add your custom training data custom_digits = [...] # Your digit images custom_labels = [...] # Their labels # Combine with MNIST x_combined = np.concatenate([x_train, custom_digits]) y_combined = np.concatenate([y_train, custom_labels]) # Retrain model model.fit(x_combined, y_combined, epochs=5, ...) ``` ### Option 2: Use Pre-trained Model Download a pre-trained model from TensorFlow Hub or Keras Applications: ```python from tensorflow.keras.applications import MobileNetV2 # Use transfer learning for better performance ``` ### Option 3: Ensemble Predictions Train multiple models and average their predictions: ```python model1 = train_model() model2 = train_model_different_seed() model3 = train_model_different_seed() # Average predictions avg_prediction = ( model1.predict(x) + model2.predict(x) + model3.predict(x) ) / 3 ``` --- ## ๐Ÿ“ˆ Performance Expectations | Drawing Style | Expected Accuracy | |---|---| | Clean, printed-like | 95-99% | | Normal handwriting | 80-90% | | Cursive/decorative | 60-75% | | Very sloppy | 40-60% | **Remember**: The model is trained on the MNIST dataset, which has specific digit styles. The more your handwriting matches those styles, the better the accuracy. --- ## ๐Ÿ› Common Issues & Solutions ### Issue: Low accuracy on certain digits **Solution**: - Make that digit more distinct - Use clearer shapes - Avoid mixing with other digits ### Issue: Model says wrong digit with high confidence **Solution**: - Your drawing might not match training style - Try different writing style - Retrain model with your handwriting samples ### Issue: Confidence is always low **Solution**: - Drawing might be too faint - Use bolder, darker strokes - Fill more of the canvas - Check canvas background is pure black ### Issue: Same drawing predicts different digits **Solution**: - Drawing might be ambiguous - Make it clearer and more distinct - Avoid overlapping strokes - Draw more centered --- ## โœ… Checklist for Best Results - [ ] Canvas background is pure black - [ ] Drawing strokes are bright white - [ ] Digit fills most of the canvas - [ ] Strokes are smooth and continuous - [ ] Digit is roughly centered - [ ] No extra marks or decorations - [ ] Using standard digit shapes - [ ] Not too slanted or rotated - [ ] Drawing at normal pace - [ ] Model has been trained (first run takes 5-10 min) --- ## ๐Ÿš€ Quick Start with Improvements ```bash # 1. Install dependencies (including opencv-python) pip install --default-timeout=1000 -r requirements.txt # 2. Run the app (will train improved model) python app.py # 3. Open http://localhost:5000 # 4. Draw with white on black background # 5. Click predict and get results! ``` --- ## ๐Ÿ“š Technical References - **CNN for digit recognition**: https://keras.io/examples/vision/mnist_convnet/ - **MNIST dataset**: http://yann.lecun.com/exdb/mnist/ - **Image preprocessing**: https://en.wikipedia.org/wiki/Thresholding_(image_processing) - **Data augmentation**: https://www.tensorflow.org/guide/keras/preprocessing_layers --- **Most Important**: The model works best when you write naturally but clearly. Don't overthink it! The improvements in this version should handle most handwriting styles well. Good luck! ๐ŸŽฏ