DigitRecognition / ACCURACY_GUIDE.md
Nausad's picture
Upload 9 files
d7ab363 verified
|
Raw History Blame Contribute Delete
7.38 kB

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

# 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:

# 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:

from tensorflow.keras.applications import MobileNetV2
# Use transfer learning for better performance

Option 3: Ensemble Predictions

Train multiple models and average their predictions:

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

# 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


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! 🎯