iTranslated by AI
MNIST Image Classification with Keras: An Illustrated Guide

Introduction

We will build an image classification AI using the deep learning framework "Keras."
In this tutorial, we will use the "MNIST" sample dataset.
We will classify handwritten digit images (28×28px) into one of 10 possible classes.
01 Preparation
Required libraries
numpy
tensorflow
02 Full Code
Full code
import numpy as np
from tensorflow import keras
from tensorflow.keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.summary()
print("\n◆Train:")
model.fit(train_images, train_labels, epochs=10)
print("Done")
print("\n◆Predict:")
predictions = model.predict(test_images, verbose=1)
num = 2
result = np.argmax(predictions[num])
print(f"The result of image {num} is [{result}].")
print("\n◆Evaluate:")
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=1)
print(f"Test accuracy:{100 * test_acc:5.2f}%")
03 Output Results
Output results
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) (None, 784) 0
dense (Dense) (None, 128) 100480
dense_1 (Dense) (None, 10) 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________
◆Train:
Epoch 1/10
1875/1875 [==============================] - 2s 1ms/step - loss: 2.9254 - accuracy: 0.8558
Epoch 2/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4251 - accuracy: 0.9022
Epoch 3/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3081 - accuracy: 0.9235
Epoch 4/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.2524 - accuracy: 0.9345
Epoch 5/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.2366 - accuracy: 0.9409
Epoch 6/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.2128 - accuracy: 0.9462
Epoch 7/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.2112 - accuracy: 0.9476
Epoch 8/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.1913 - accuracy: 0.9516
Epoch 9/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.1903 - accuracy: 0.9522
Epoch 10/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.1896 - accuracy: 0.9528
Done
◆Predict:
313/313 [==============================] - 0s 800us/step
The result of image 2 is [1].
◆Evaluate:
313/313 [==============================] - 0s 915us/step - loss: 0.3369 - accuracy: 0.9410
Test accuracy:94.10%
If you want to try it out quickly, please run the code based on the content above.
Below is a detailed explanation for those who want to understand it more deeply.
Explanation
step01 Loading Libraries
import numpy as np
from tensorflow import keras
from tensorflow.keras.datasets import mnist
Load the necessary libraries. This time, we need two.
- numpy : A library for numerical calculations with arrays.
- tensorflow : A library (framework) for deep learning.
We are loading the Keras framework and the MNIST dataset from TensorFlow. These will be used later.
step02 Loading Sample Data
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
This is the code for loading the data. Keras provides sample datasets, and one of them is "mnist".
By executing the above code, you can use the MNIST dataset in your program. The data is loaded into four parts, and the contents are as follows:
- train_images : Training data
- train_labels : Training labels (ground truth)
- test_images : Test data
- test_labels : Test labels (ground truth)
Dividing the data into four parts makes the subsequent work easier.

[Supplement] Code to check the size and type of data
You can check the size and type of this data using code like the following.
print("\n◆Training Data:")
print(f"train_images : {train_images.shape}, {type(train_images)}")
print(f"train_labels : {train_labels.shape}, {type(train_labels)}")
print("\n◆Test Data:")
print(f"test_images : {test_images.shape}, {type(test_images)}")
print(f"test_labels : {test_labels.shape}, {type(test_labels)}")
◆Training Data:
train_images : (60000, 28, 28), <class 'numpy.ndarray'>
train_labels : (60000,), <class 'numpy.ndarray'>
◆Test Data:
test_images : (10000, 28, 28), <class 'numpy.ndarray'>
test_labels : (10000,), <class 'numpy.ndarray'>
It contains 70,000 images of 28x28 pixels, of which 60,000 are used as training data and 10,000 as test data. Each is accompanied by a corresponding correct label.
step03 Creating the Model
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
With the code above, you can create a model. The final output is an instance named model. This is the AI itself. At this point, it hasn't been trained yet, so it can't be called an AI just yet.
The model created above has the following structure:

- 1st Layer : 784 neurons
- 2nd Layer : 128 neurons (Activation function: relu)
- 3rd Layer : 10 neurons (Activation function: softmax)
In the input layer, keras.layers.Flatten is a layer that rearranges the 28x28 pixel images into a single row. This changes depending on the size of the input dataset. Since we are using the MNIST dataset this time, it becomes 28x28=784 neurons.
In the next layer, since we are using keras.layers.Dense, it is fully connected to the input layer. The number of neurons is 128, as specified by the argument. Note that the relu function is used as the activation function.
In the last layer, since we are using keras.layers.Dense, it is fully connected to the second layer. The number of neurons is 10, as specified by the argument. The reason for choosing 10 neurons is that the number of classes we ultimately want to classify is 10. Note that the softmax function is used as the activation function. This will output the final result as a probability display.
step04 Model Settings
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
.
.
.
The remaining content is summarized in a book. I would be very happy if you could pick it up ↓
Learn more about Artificial Intelligence/AI/Machine Learning
NekoAllergy's AI
I am posting about machine learning on YouTube. I would be happy if you could take a look when you have time.
Created by NekoAllergy

Discussion