DEEP LEARNING • LESSON 15

Choose a Real Dataset

Before building a deep learning model, we need data. In this project, we will use the MNIST handwritten digit dataset to build a complete image classification system.

SIMPLE IDEA

A neural network is only useful when it learns from useful data.

Our goal is to give the model handwritten digit images and teach it to recognize which digit each image represents.

01

What Is MNIST?

MNIST is a dataset containing handwritten digits from 0 through 9.

Each example contains an image and the correct digit label.

Image              Label

[ handwritten 0 ]    → 0

[ handwritten 3 ]    → 3

[ handwritten 7 ]    → 7

[ handwritten 9 ]    → 9

The image is the input and the digit is the target that our model needs to predict.

02

Why Use MNIST?

MNIST is a good learning dataset because the problem is simple enough to understand while still representing a real image classification task.

Input
  ↓
Handwritten Digit Image
  ↓
Neural Network
  ↓
Prediction
  ↓
0 - 9

It lets us focus on the complete deep learning workflow instead of spending most of the lesson dealing with complicated data collection and cleaning.

03

What Does One Image Look Like?

MNIST images are grayscale images with a size of 28 × 28 pixels.

Image Size

28 pixels
↓
┌────────────────────────────┐
│                            │
│       handwritten          │
│           7                │
│                            │
└────────────────────────────┘
             ↑
          28 pixels

Each pixel contains a numerical value representing the brightness of that pixel.

04

Image as Numbers

A computer does not see the digit like a human does. It sees numbers.

Example pixels:

0    0    0    0
0   255  255   0
0   255    0   0
0   255  255   0

0   = dark
255 = bright

So the handwritten digit is converted into a matrix of numbers that the neural network can process.

05

Load MNIST With Python

Keras provides MNIST directly, so we do not need to download and manually organize the dataset.

import tensorflow as tf

(x_train, y_train), (x_test, y_test) = \
    tf.keras.datasets.mnist.load_data()

The dataset is automatically divided into training data and test data.

06

Training Data vs Test Data

Training Data
      ↓
Used to teach the model

Test Data
      ↓
Used to check the model

This separation is extremely important. If we evaluate the model using the same data it learned from, we do not get a reliable measurement of how well it handles unseen examples.

07

Check the Data Shape

print(x_train.shape)
print(y_train.shape)

print(x_test.shape)
print(y_test.shape)

These values tell us how many images and labels we have and the size of each image.

x_train → training images
y_train → training labels

x_test  → test images
y_test  → test labels
08

What Is the Target?

Suppose the image contains a handwritten 5.

Input:

Image of handwritten 5


Target:

5

During training, the neural network makes a prediction and compares it with the correct target.

Image
  ↓
Model
  ↓
Prediction: 8

Correct Label: 5
  ↓
Calculate Error
  ↓
Update Model
09

The Complete Project Pipeline

This is the workflow we will follow throughout Lesson 15.

Choose Dataset
      ↓
Prepare Data
      ↓
Build Neural Network
      ↓
Train Model
      ↓
Evaluate Model
      ↓
Improve Model
      ↓
Make Predictions
      ↓
Complete Project

Each upcoming topic represents one stage of this pipeline.

10

Why We Are Not Starting With a Complicated Dataset

A common beginner mistake is choosing a dataset that is too complicated and then spending more time fighting the data than learning deep learning.

For example, a dataset containing thousands of different real-world objects introduces many additional problems: image sizes, noisy labels, class imbalance, data augmentation, and much larger computational requirements.

MNIST removes much of that complexity so we can clearly understand the complete model-building process.

KEY TAKEAWAY

Every deep learning project starts with data.

In this project, our input is a 28 × 28 grayscale image of a handwritten digit, and our target is the correct digit from 0 to 9. We split the data into training and test sets so we can train the model and later measure how well it performs on unseen examples.

Quick Check

What is the input?

A 28 × 28 grayscale image of a handwritten digit.

What is the target?

The correct digit from 0 through 9.

Why do we need test data?

To measure how well the trained model performs on data it did not use during training.