Reference URL: https://www.tensorflow.org/tutorials/keras/classification?hl=ja
Do the following
--Build a neural network to classify images --Training neural networks --Evaluate the performance of the model
#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
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.
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']
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()
Scales image data values in the range 0 to 1
train_images = train_images / 255.0
test_images = test_images / 255.0
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()
tf.keras.layers.Flatten
input_shape = (28, 28) specifies the shape of the input data
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
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')
])
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, specify
cross entropy --metrics: Items quantified during learning and testing --This time, specify ʻaccuracy
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
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>
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
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()
#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()
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()
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)
np.argmax(predictions_single[0])
9
Recommended Posts