Home Technology peripherals It Industry A Beginner's Guide to Keras: Digit Recognition in 30 Minutes

A Beginner's Guide to Keras: Digit Recognition in 30 Minutes

Feb 10, 2025 pm 03:27 PM

This article details building a Keras model for handwritten digit recognition using a Convolutional Neural Network (CNN) and the MNIST dataset. Let's rephrase it for clarity and improved flow.

A Beginner's Guide to Keras: Digit Recognition in 30 Minutes

Building a Handwritten Digit Recognizer with Keras

This tutorial demonstrates creating a model to recognize handwritten digits using Python's Keras library, a high-level API that simplifies building and training neural networks. We'll leverage the power of Convolutional Neural Networks (CNNs) and the widely used MNIST dataset.

Understanding the Approach

Our model employs a CNN, a particularly efficient architecture for image classification. Unlike traditional neural networks, CNNs process data in a 3D array (x, y coordinates and color), making them ideal for image data. The MNIST dataset, containing 60,000 training and 10,000 testing examples of handwritten digits, provides the necessary labeled data for training.

Artificial Neural Networks (ANNs) and CNNs

An ANN is a mathematical model transforming input data into output through hidden layers, each layer representing a probability. Training involves adjusting weights and biases based on errors, allowing the network to learn patterns.

CNNs offer a significant advantage for image processing. Their 3D array structure means each hidden layer node connects to only a small input region, dramatically increasing efficiency compared to traditional ANNs. Key CNN layers include convolutional layers (feature extraction), pooling layers (feature reduction), flattening layers (dimensionality reduction), and a final classification layer.

A Beginner's Guide to Keras: Digit Recognition in 30 Minutes

A Beginner's Guide to Keras: Digit Recognition in 30 Minutes

Working with the MNIST Dataset

The MNIST dataset is readily available within Keras. We load the training and testing data using mnist.load_data(). Visualizing sample digits helps understand the data structure:

1

2

3

4

5

6

7

from keras.datasets import mnist

import matplotlib.pyplot as plt

 

(x_train, y_train), (x_test, y_test) = mnist.load_data()

image_index = 35

plt.imshow(x_train[image_index], cmap='Greys')

plt.show()

Copy after login
Copy after login

A Beginner's Guide to Keras: Digit Recognition in 30 Minutes

The training and testing sets have dimensions (60000, 28, 28) and (10000, 28, 28) respectively, indicating 28x28 pixel images.

Data Preprocessing

Before model creation, data needs preprocessing:

  1. Reshaping: Images are reshaped to (M x N x 1) format using .reshape().
  2. Normalization: Pixel values (0-255) are normalized to 0-1 by dividing by 255.
  3. One-Hot Encoding: The dependent variable (y_train, y_test) is converted to a binary class matrix using to_categorical() for compatibility with the model's output.

1

2

3

4

5

6

7

from keras.datasets import mnist

import matplotlib.pyplot as plt

 

(x_train, y_train), (x_test, y_test) = mnist.load_data()

image_index = 35

plt.imshow(x_train[image_index], cmap='Greys')

plt.show()

Copy after login
Copy after login

Model Design and Training

Our CNN model is built sequentially:

  1. Convolutional Layers: Extract features from the input images.
  2. Pooling Layer: Reduces dimensionality and computational cost.
  3. Dropout Layer: Prevents overfitting.
  4. Flatten Layer: Converts the multi-dimensional output to a 1D array.
  5. Dense Layers: Perform final classification.

1

2

3

4

5

img_rows, img_cols = 28, 28

x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) / 255

x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) / 255

y_train = to_categorical(y_train, num_classes=10)

y_test = to_categorical(y_test, num_classes=10)

Copy after login

The model is compiled using sparse_categorical_crossentropy loss (for integer labels), the Adam optimizer, and accuracy as the metric. Training is performed using .fit(), specifying epochs and batch size. The trained model is saved for later use.

1

2

3

4

5

6

7

8

9

10

11

12

13

from keras.models import Sequential

from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D

 

model = Sequential([

    Conv2D(32, (3, 3), activation='relu', input_shape=(img_rows, img_cols, 1)),

    Conv2D(64, (3, 3), activation='relu'),

    MaxPooling2D((2, 2)),

    Dropout(0.25),

    Flatten(),

    Dense(128, activation='relu'),

    Dropout(0.5),

    Dense(10, activation='softmax') # 10 output classes (digits 0-9)

])

Copy after login

Testing with a Custom Image

To test the model, we load a custom handwritten digit image, preprocess it (convert to grayscale, reshape, normalize), load the saved model, and use .predict() to get the classification.

1

2

3

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

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

model.save("test_model.h5")

Copy after login

A Beginner's Guide to Keras: Digit Recognition in 30 Minutes

Conclusion

This tutorial provides a foundational understanding of building a handwritten digit recognition model using Keras and CNNs. While achieving high accuracy (e.g., >99%), further improvements are possible through model parameter tuning, data augmentation, and exploring more advanced CNN architectures. The provided FAQs offer further insights into the concepts involved.

The above is the detailed content of A Beginner's Guide to Keras: Digit Recognition in 30 Minutes. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Building a Network Vulnerability Scanner with Go Building a Network Vulnerability Scanner with Go Apr 01, 2025 am 08:27 AM

This Go-based network vulnerability scanner efficiently identifies potential security weaknesses. It leverages Go's concurrency features for speed and includes service detection and vulnerability matching. Let's explore its capabilities and ethical

CNCF Arm64 Pilot: Impact and Insights CNCF Arm64 Pilot: Impact and Insights Apr 15, 2025 am 08:27 AM

This pilot program, a collaboration between the CNCF (Cloud Native Computing Foundation), Ampere Computing, Equinix Metal, and Actuated, streamlines arm64 CI/CD for CNCF GitHub projects. The initiative addresses security concerns and performance lim

Serverless Image Processing Pipeline with AWS ECS and Lambda Serverless Image Processing Pipeline with AWS ECS and Lambda Apr 18, 2025 am 08:28 AM

This tutorial guides you through building a serverless image processing pipeline using AWS services. We'll create a Next.js frontend deployed on an ECS Fargate cluster, interacting with an API Gateway, Lambda functions, S3 buckets, and DynamoDB. Th

Top 21 Developer Newsletters to Subscribe To in 2025 Top 21 Developer Newsletters to Subscribe To in 2025 Apr 24, 2025 am 08:28 AM

Stay informed about the latest tech trends with these top developer newsletters! This curated list offers something for everyone, from AI enthusiasts to seasoned backend and frontend developers. Choose your favorites and save time searching for rel

See all articles