[PYTHON] I ran the TensorFlow tutorial with comments (first neural network: the beginning of the classification problem)

Reference URL: https://www.tensorflow.org/tutorials/keras/classification?hl=ja

Target

Do the following

--Build a neural network to classify images --Training neural networks --Evaluate the performance of the model

Preparation

Preparation of package

#TensorFlow and tf.import keras
import tensorflow as tf
from tensorflow import keras

#Import helper library
import numpy as np
import matplotlib.pyplot as plt
#Ver confirmation of tensorflow
print(tf.__version__)
2.3.0

Prepare the dataset

This time I will use Fashion-MNIST

Contains 70,000 black and white images in 10 categories Each is a low-resolution (28 x 28 pixels) image showing one type of clothing per sheet as shown in the figure below.

Fashion MNIST sprite
Figure 1. Fashion-MNIST samples (by Zalando, MIT License).
 

The image consists of a 28x28 NumPy array The value of each pixel is an integer between 0 and 255 The label is an array of integers from 0 to 9. Each number corresponds to the clothing class as shown in the table below.

Label Class
0 T-shirt/top
1 Trouser
2 Pullover
3 Dress
4 Coat
5 Sandal
6 Shirt
7 Sneaker
8 Bag
9 Ankle boot
fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

Each image is classified into a single label Because the dataset does not contain the above class name Save the class name for later output of the image

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

Observing the data

print(train_images.shape)
print(test_images.shape)
(60000, 28, 28)
(10000, 28, 28)
plt.figure()
plt.imshow(train_images[0], cmap=plt.cm.binary)
plt.colorbar()
plt.grid(False)
plt.show()

output_12_0.png

Data preprocessing

Scales image data values in the range 0 to 1

train_images = train_images / 255.0

test_images = test_images / 255.0

Data confirmation

plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[train_labels[i]])
plt.show()

output_16_0.png

Model building / learning

Model building

  1. 28 ✖️ 28 2D data smoothed to 1D

    tf.keras.layers.Flatten

input_shape = (28, 28) specifies the shape of the input data

  1. Definition of hidden layer

    tf.keras.layers.Dense

128 is the number of units (number of neurons) activation ='relu' specifies the activation function ReLU Other activation functions: https://www.tensorflow.org/api_docs/python/tf/keras/activations?hl=ja

  1. Definition of fully connected layer

Specify 10 because it will be finally classified into 10 classes. Since we are using softmax as the activation function, 10 nodes Output the probability that the image you are looking at belongs to each of the 10 classes

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10, activation='softmax')
])

Compiling the model

Defining a model for learning

--optimer: Optimizer algorithm --This time, specify ʻAdam --Other optimization algorithms: https://www.tensorflow.org/api_docs/python/tf/keras/optimizers --loss: loss function --This time, specifycross entropy --metrics: Items quantified during learning and testing --This time, specify ʻaccuracy

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

Model training

model.fit(train_images, train_labels, epochs=5)
Epoch 1/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.4964 - accuracy: 0.8259
Epoch 2/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3725 - accuracy: 0.8656
Epoch 3/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3336 - accuracy: 0.8787
Epoch 4/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.3113 - accuracy: 0.8853
Epoch 5/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2925 - accuracy: 0.8922





<tensorflow.python.keras.callbacks.History at 0x7f74fb8965f8>

Evaluation

Evaluate the model

Evaluate the model using test data

test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)

print('\nTest accuracy:', test_acc)
313/313 - 0s - loss: 0.3479 - accuracy: 0.8780

Test accuracy: 0.878000020980835

Forecast

Predict test data with a trained model

predictions = model.predict(test_images)

Classification result of the first image Output as probability

predictions[0]
array([2.1071783e-06, 2.4513878e-07, 3.5516130e-09, 2.4936966e-07,
       6.1619041e-08, 6.4291209e-03, 3.7025956e-08, 2.2654539e-02,
       3.6237492e-07, 9.7091323e-01], dtype=float32)
# `np.argmax`Get the maximum value (the number of the classified label of the image) from the array with
print(f'predicted label : {np.argmax(predictions[0])}')
#Check the correct answer data
print(f'true label : {test_labels[0]}')
predicted label : 9
true label : 9
def plot_image(i, predictions_array, true_label, img):
    """
Display image with predicted probability
    """
    predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])

    plt.imshow(img, cmap=plt.cm.binary)

    predicted_label = np.argmax(predictions_array)
    if predicted_label == true_label:
        color = 'blue'
    else:
        color = 'red'

    plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                    100*np.max(predictions_array),
                                    class_names[true_label]),
                                    color=color)

def plot_value_array(i, predictions_array, true_label):
    """
Create a bar graph of forecast results
    """
    predictions_array, true_label = predictions_array[i], true_label[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    thisplot = plt.bar(range(10), predictions_array, color="#777777")
    plt.ylim([0, 1]) 
    predicted_label = np.argmax(predictions_array)

    thisplot[predicted_label].set_color('red')
    thisplot[true_label].set_color('blue')
#Displayed as the first image of test data
i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions,  test_labels)
plt.show()

output_33_0.png

#Displayed as the 13th image of test data
i = 12
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions,  test_labels)
plt.show()

output_34_0.png

Draw with 15 pieces of test data

num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
  plt.subplot(num_rows, 2*num_cols, 2*i+1)
  plot_image(i, predictions, test_labels, test_images)
  plt.subplot(num_rows, 2*num_cols, 2*i+2)
  plot_value_array(i, predictions, test_labels)
plt.show()

output_36_0.png

Take out one image from the test data and make a prediction

img = test_images[0]

print(img.shape)
(28, 28)

The tf.keras model is designed to make predictions about batches or "gatherings" in a sample. Therefore, even if you use one image, you need to list it.

#Make an image a member of only one batch
img = (np.expand_dims(img,0))

print(img.shape)
(1, 28, 28)
predictions_single = model.predict(img)

print(predictions_single)
[[2.1071742e-06 2.4513832e-07 3.5515995e-09 2.4936892e-07 6.1618806e-08
  6.4291116e-03 3.7025885e-08 2.2654528e-02 3.6237492e-07 9.7091323e-01]]
plot_value_array(0, predictions_single, test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)

output_42_0.png

np.argmax(predictions_single[0])
9

Recommended Posts

I ran the TensorFlow tutorial with comments (first neural network: the beginning of the classification problem)
I tried running the TensorFlow tutorial with comments (text classification of movie reviews)
I tried a convolutional neural network (CNN) with a tutorial on TensorFlow on Cloud9-Classification of handwritten images-
I tried running the TensorFlow tutorial with comments (_TensorFlow_2_0_Introduction for beginners)
I ran the neural network on the actual FPGA
I tried the MNIST tutorial for beginners of tensorflow.
I tried to find the average of the sequence with TensorFlow
I tried to move ROS (Melodic) with the first Raspberry Pi (Stretch) at the beginning of 2021
Since I touched Tensorflow for 2 months, I explained the convolutional neural network in an easy-to-understand manner with 95.04% of "handwritten hiragana" identification.
First Python 3 ~ The beginning of repetition ~
Simple classification model with neural network
I want to output the beginning of the next month with Python
I tried the TensorFlow tutorial 2nd
[TensorFlow] [Keras] Neural network construction with Keras
Touch the object of the neural network
Build a classifier with a handwriting recognition rate of 99.2% with a TensorFlow convolutional neural network
I tried how to improve the accuracy of my own Neural Network
I tried the TensorFlow tutorial MNIST 3rd
I tried tensorflow for the first time
Persist the neural network built with PyBrain
I tried object detection with YOLO v3 (TensorFlow 2.1) on the GPU of windows!
I will explain about the neural network "Differentiable Neural Computing (DNC)" with external memory.
I solved the deepest problem of Hiroshi Yuki.
Visualize the inner layer of a neural network
Verification of Batch Normalization with multi-layer neural network
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
The 15th offline real-time I tried to solve the problem of how to write with python
Learn Nim with Python (from the beginning of the year).
The first algorithm to learn with Python: FizzBuzz problem
I followed the implementation of the du command (first half)
The story of making a music generation neural network
I measured the performance of 1 million documents with mongoDB
Record of the first machine learning challenge with Keras
I tried to solve the problem with Python Vol.1
I tried the simplest method of multi-label document classification
How to write offline real time I tried to solve the problem of F02 with Python