Deep Learning: Classifying Handwritten Digits with TensorFlow and Keras
Handwritten digit recognition is a classic problem in computer vision and machine learning. The goal is to correctly identify handwritten digits in an image, such as those from the MNIST dataset. Deep learning models, such as Convolutional Neural Networks (CNNs), have proven to be effective in solving this problem.
Introduction to Handwritten Digit Recognition and its real-world applications
Handwritten digit recognition is a machine learning problem where the goal is to correctly identify handwritten digits in an image. This technology has numerous real-world applications, such as handwriting recognition in check processing and signature verification in bank transactions. We will explore how to classify handwritten digits using Tensorflow and Keras, two powerful deep learning libraries.
Tensorflow and Keras are two widely used deep learning libraries that provide a framework for developing sophisticated machine learning models. In this section, we will introduce these libraries and provide a guide for their installation and setup. We will see an overview of their architecture to understand how they work.
The MNIST dataset is a widely used dataset for handwritten digit recognition and is used in this blog post for demonstration purposes. We will cover how to load the MNIST dataset using TensorFlow and Keras and perform preprocessing and normalization on the data to prepare it for training and testing.
Convolutional Neural Networks (CNNs) are a popular deep learning architecture for image classification problems, such as handwritten digit recognition. We will introduce CNNs and show you how to build a basic CNN model using TensorFlow and Keras. We will also add layers to the model to improve its performance.
How to Build Handwritten Digits Recognition with TensorFlow and Keras
- Install and set up TensorFlow and Keras: Before starting the project, you will need to install TensorFlow and Keras on your computer. The easiest way to do this is by using the pip package manager.
- Load the MNIST dataset: The MNIST dataset is a widely used dataset for handwritten digit recognition and will be used in this project. Load the dataset using TensorFlow and Keras and perform preprocessing and normalization on the data to prepare it for training and testing.
- Build a Convolutional Neural Network (CNN) model: CNNs are a popular deep learning architecture for image classification problems, such as handwritten digit recognition. Use TensorFlow and Keras to build a basic CNN model.
- Train and evaluate the model: Train the CNN model on the MNIST dataset and evaluate its performance using metrics such as accuracy.
- Improve the model’s performance: Explore techniques for improving the model’s performance, such as hyperparameter tuning and regularization. Tune the model’s hyperparameters and add regularization techniques to prevent overfitting.
- Conclusion: Summarize the results and provide a conclusion for the project.
Installing TensorFlow and Keras:
pip install tensorflow
pip install keras
Loading the MNIST dataset:
from tensorflow.keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
Preprocessing the data:
x_train = x_train.reshape(-1, 28 * 28) / 255.0
x_test = x_test.reshape(-1, 28 * 28) / 255.0
y_train = tensorflow.keras.utils.to_categorical(y_train, 10)
y_test = tensorflow.keras.utils.to_categorical(y_test, 10)
Building the CNN model:
import tensorflow as tf
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
Compiling the model:
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
Training the model:
history = model.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test))
Evaluating the model:
test_loss, test_acc = model.evaluate(x_test, y_test)
print('Test accuracy:', test_acc)
These are the steps that need to be followed. Complete example code is as follows:
Handwritten Digits Recognition with TensorFlow and Keras
import tensorflow as tf
from tensorflow import keras
# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# Normalize the data
x_train = x_train / 255.0
x_test = x_test / 255.0
# Build the model
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Train the model
model.fit(x_train, y_train, epochs=5)
# Evaluate the model
test_loss, test_acc = model.evaluate(x_test, y_test)
print('Test accuracy:', test_acc)
If you prefer github, you may find the jupyter notebook file from following link.