Handwritten Detection using Tensor Flow
Project Guide
- Here's an example of building a simple neural network using Python and Pandas to classify images of handwritten digits from the MNIST dataset.
- Visualize how this neural network works.
- Resources
Here's an example of building a simple neural network using Python and Pandas to classify images of handwritten digits from the MNIST dataset.
This example solves the problem of classifying images of handwritten digits from the MNIST dataset. The goal is to build a neural network that can accurately predict the digit shown in the input image. The problem is a classic machine learning problem and is often used as a benchmark for evaluating different algorithms and models. The example uses a simple neural network with two fully connected layers to achieve high accuracy in classifying the digits.
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
# Load the MNIST dataset
mnist = tf.keras.datasets.mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# Flatten the input images
X_train = X_train.reshape(X_train.shape[0], -1)
X_test = X_test.reshape(X_test.shape[0], -1)
# Normalize the data
X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255
# Convert the target variable to one-hot encoded vectors
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)
y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)
# Define the model architecture
model = Sequential()
model.add(Dense(128, activation='relu', input_shape=(784,)))
model.add(Dense(10, activation='softmax'))
# Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train the model
model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_test, y_test))
# Evaluate the model
test_loss, test_acc = model.evaluate(X_test, y_test)
print('Test accuracy:', test_acc)
Visualize how this neural network works.
Use the matplotlib library to display some of the input images and the corresponding predicted labels. This code will create a 5x5 grid of subplots, each containing an input image from the test set and its predicted label. The np.argmax function is used to find the index of the predicted label with the highest probability.
import matplotlib.pyplot as plt
# Make predictions on the test set
y_pred = model.predict(X_test)
# Plot some of the input images and their predicted labels
fig, axs = plt.subplots(5, 5, figsize=(10, 10))
axs = axs.ravel()
for i in range(25):
axs[i].imshow(X_test[i].reshape(28, 28), cmap=plt.cm.gray_r)
axs[i].set_title('Predicted label = {}'.format(np.argmax(y_pred[i])))
axs[i].axis('off')
plt.show()
Resources
Lesson to expand on MNIST.
TensorFlow's official MNIST tutorial:TensorFlow is a popular open-source machine learning framework, and their MNIST tutorial provides a great introduction to building neural networks with TensorFlow. You can find the tutorial here: https://www.tensorflow.org/tutorials/keras/classification. Coursera's Deep Learning Specialization: This is a series of courses taught by Andrew Ng that provides a comprehensive introduction to deep learning. The second course in the series, "Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization," covers the MNIST example in detail. You can find the course here: https://www.coursera.org/specializations/deep-learning.
Neural Networks and Deep Learning book: This is a free online book by Michael Nielsen that provides an introduction to neural networks and deep learning. The book includes a chapter on the MNIST example, which provides a detailed explanation of how the neural network works. You can find the book here: http://neuralnetworksanddeeplearning.com/chap1.html.
Fast.ai: This is a set of online courses and resources that provide an introduction to deep learning. The courses include hands-on coding assignments, including an assignment on the MNIST example. You can find the courses here: https://www.fast.ai/.