Downloads · 30 days
9
6% of all-time downloads
aman-27/cnn-digit-recognition-tf-v1
cnn-digit-recognition-tf-v1 is a image classification model from aman-27. Use it when you need a label for an image. It is set up for keras. The card lists the license as mit.
A Convolutional Neural Network trained on the MNIST dataset to recognize handwritten digits (0–9). This project is just a fun learning project.
Downloads · 30 days
9
6% of all-time downloads
All-time downloads
143
Public
Repo size
2.1 MB
Likes
0
Public
Click a slice to open those files.
.keras2.1 MB · 100%
From the Hugging Face model README
A Convolutional Neural Network trained on the MNIST dataset to recognize handwritten digits (0–9). This project is just a fun learning project.
This model is an updated version of cnn-recognition-tf-v0.
Model: Convolutional Neural Network (CNN) Framework: TensorFlow/keras Input: 28x28 grayscale images (shape: 28,28,1) Output: Predicted digit (0-9) Version: v1 Architecture:
Training:
Purpose:
Parameters:
Total params: 503,136
Trainable params: 167,562
Non trainable params: 448
Optimizer params: 335,126
This model is a Convolutional Neural Network trained on the MNIST dataset to recognize handwritten digits (0–9). It was created as a learning project to explore deep learning concepts, convolutional architectures, and TensorFlow/Keras. The Neural network consists of multiple convolutional and pooling layers, followed by dense layers. Predictions on custom handwritten digits may fluctuate due to differences in handwriting styles.
This model is intended for experimentation, learning, and exploration of digit recognition. It reflects the creator’s current understanding of the convolutional neural network.
This model is intended for standalone use as a demonstration of CNN based handwritten digit recognition. It is designed for experimentation, learning, and testing with MNIST pattern images. The model is not intended for integration into production systems or larger applications.
Technical Limitations:
Sociotechnical Considerations:
It is recommended to use images that are preprocessed to match the MNIST format (28×28 pixels, grayscale) to improve prediction accuracy.
Users should not rely on the model for critical applications or decision-making tasks, because it can be wrong.
Download the mnist_cnn_v1.keras model.
Install the following dependencies
pip install tensorflow
pip install numpy
pip install Pillow
Then run the following script. (Make sure the model is in the same folder as the script)
import sys
import os
import tensorflow as tf
import numpy as np
from PIL import Image
model_path = "mnist_cnn_v1.keras"
if not os.path.exists(model_path):
print(f"Error: {model_path} not found. Make sure the model file is in the same folder.")
sys.exit(1)
model = tf.keras.models.load_model(model_path)
_, (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_test = x_test.reshape(-1, 28, 28, 1) / 255.0
def preprocess_image(image_path):
img = Image.open(image_path).convert("L")
img = img.resize((28, 28))
img = np.array(img) / 255.0
if np.mean(img) > 0.5:
img = 1.0 - img
img = img.reshape(1, 28, 28, 1)
return img
if len(sys.argv) != 2:
print("Usage: python predict.py <image_path>")
sys.exit(1)
image_path = sys.argv[1]
img = preprocess_image(image_path)
prediction = model.predict(img)
predicted_digit = np.argmax(prediction)
print("Predicted digit:", predicted_digit)
Dataset: MNIST (handwritten digits 0–9)
Preprocessing: Images converted to grayscale, resized to 28×28, normalized to [0,1], and inverted if mostly white background
Optimizer: Adam
Loss Function: Categorical Crossentropy
Batch Size: 32
Epochs: 15
Training/Validation Split: Standard MNIST split (60,000 training, 10,000 validation)
Regularization: BatchNormalization after each convolutional layer
• Input images are automatically converted to grayscale and resized to 28×28 pixels.
• Pixel values are normalized to the [0, 1] range.
• Images with mostly white background are inverted so that digit strokes are white and background is black.
• The processed image is reshaped to (1, 28, 28, 1) before getting into the model.
• All preprocessing is handled automatically in predict.py.
The model was trained and evaluated on MNIST dataset (handwritten digits 0-9)
Training Details: Epochs: 15 Optimizer: Adam Loss: Categorical Crossentropy
| Epoch | Training Accuracy | Validation Accuracy | Training Loss | Validation Loss |
|---|---|---|---|---|
| 15 | 99.82% | 99.18% | 0.0054 | 0.0319 |
The model generalizes well to standard MNIST digits.
Best results are obtained for digits with medium to thick strokes.
Very thin strokes may lose detail during resizing.
Input preprocessing (predict.py) improves robustness to white-background digits.