Jazmin Barrionuevo

Jazmin Barrionuevo

Handwritten recognition

2024

2-minute read

Python

TensorFlow

OpenCV

Numpy

Matplotlib

I decided to build a handwritten digit recognition model. I wanted to create something that could not only recognize digits but also visualize the predictions.

Jazmin Barrionuevo

I used TensorFlow which has been my go-to library for machine learning projects. Its flexibility and robustness make it perfect for building and deploying machine learning models. But what really stands out is Keras, TensorFlow’s API, which simplifies the process of building neural networks.

In this project, I used the MNIST dataset—a dataset of 70000 images of handwritten digits, each labeled from 0 to 9. I loaded it directly from TensorFlow’s dataset module.

Neural Network

The first step is normalizing the dataset by dividing the pixel values by 255.0. This effectively normalizes the data to a range between 0 and 1. And then with Keras we build our neural network:

1 2 3 4 5 6 7 8 model = tf.keras.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10, activation='softmax') ])

Introduce Dropout layers into the model is a good way to avoid overfitting. Dropout randomly disables a fraction of the neurons during training, forcing the model to learn more robust features and reducing the risk of overfitting.

To better monitor the model's performance during training, we can add validation data. This allows us to observe how well the model performs on a separate set of data that it hasn't seen during training, providing a more accurate measure of its real world effectiveness.

1 model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))

In my initial implementation, I trained the model for 10 epochs. It’s enough to allow the model to learn the patterns in the data without overfitting. Training for too few epochs can result in an under-fitted model that hasn’t learned enough from the data. Conversely, training for too many epochs might lead to overfitting.

Jazmin Barrionuevo

Integrating OpenCV for Image Processing and Visualizing with Matplotlib

For the prediction part of the project, I needed a way to process new images of handwritten digits. This is where OpenCV came in. Then Matplotlib allowed me to display the processed images alongside their predicted labels. This step was particularly rewarding because I could see the model in action, recognizing digits just like a human would.

Jazmin BarrionuevoJazmin Barrionuevo

You can find the full project on my github:Handwritten-recognition