Fashion MNIST classification

The Fashion MNist dataset is now called the Hello World of deep learning. Replace the previous handwriting recognition.

The reason should be the development of deep learning, handwriting recognition has become too easy.

The official example

The official code

Try using a convolutional neural network to identify

Official Convolutional Neural Network Reference

Obtain Fashion MNist data

import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)

fashion_mnist = tf.keras.datasets.fashion_mnist

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

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images, test_images = train_images / 255.0, test_images / 255.0
print(train_images.shape)

Define the convolutional neural network model

model = tf.keras.models.Sequential() model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1))) model. The add (tf) keras. The layers. MaxPooling2D ((2, 2))) model. The add (tf) keras. The layers. Conv2D (64, (3, 3), activation='relu')) model.add(tf.keras.layers.MaxPooling2D((2, 2))) model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu')) model.add(tf.keras.layers.Flatten()) model.add(tf.keras.layers.Dense(64, activation='relu')) model.add(tf.keras.layers.Dense(10,activation='softmax')) # model = tf.keras.models.Sequential([ # Tf. Keras. The layers. Conv2D (32, kernel_size = (3, 3), the activation = 'relu', input_shape = (28, 28, 1)), # tf. Keras. The layers. MaxPooling2D (), # tf. Keras. The layers. The Conv2D (64, kernel_size = (5, 5), the activation = 'relu'), # tf.keras.layers.MaxPooling2D(), # tf.keras.layers.Flatten(), # tf.keras.layers.Dense(64,activation='relu'), # tf.keras.layers.Dense(10,activation='softmax')] # ) model.summary()

Training model

model.compile(optimizer='adam', loss=tf.keras.losses.sparse_categorical_crossentropy, Boards =['accuracy']) history = model.fit(train_images. Reshape (-1,28,28,1), train_labels, epochs=10, ,28,28,1 validation_data = (test_images. Reshape (- 1), test_labels), batch_size = 1000)

The training results

We can see the results of the same 10 iterations of training using convolutional neural network

Val_Loss: 0.3499 - Val_Accuracy: 0.8765

It is more accurate than the official data trained by ordinary neural network

Train on 60000 samples, validate on 10000 samples Epoch 1/10 60000/60000 [==============================] - 105s 2ms/sample - loss: 1.1311-Accuracy: 0.6256-Val_Loss: 0.6830-Val_Accuracy: 0.7482 Epoch 2/10 60000/60000 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 2 ms - 105 - s/sample - loss: 0.5803 accuracy: 0.7813-Val_Accuracy: 0.5385-Val_Accuracy: 0.8018 Epoch 3/10 60000/60000 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 2 ms - 105 - s/sample - loss: 0.4933 accuracy: 0.4858-val_accuracy: Accuracy 0.8226 Epoch 4/10 60000/60000 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 2 ms - 106 - s/sample - loss: 0.4413 accuracy: 0.8405-val_accuracy: 0.4407-val_accuracy: 0.8419 Epoch 5/10 60000/60000 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 2 ms - 106 - s/sample - loss: 0.4057 accuracy: 0.8553-VAL_ACCURACY: 0.4226-VAL_ACCURACY: 0.8514 Epoch 6/10 60000/60000 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 2 ms - 105 - s/sample - loss: 0.3795 accuracy: 0.8643 -Val_Accuracy: 0.3933 -Val_Accuracy: 0.8591 Epoch 7/10 60000/60000 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 2 ms - 108 - s/sample - loss: 0.3583 accuracy: 0.8727-Val_Accuracy: 0.3835-Val_Accuracy: 0.8633 Epoch 8/10 60000/60000 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 2 ms - 106 - s/sample - loss: 0.3437 accuracy: 0.8780-Val_Accuracy: 0.3694-Val_Accuracy: 0.8641 Epoch 9/10 60000/60000 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 2 ms - 106 - s/sample - loss: 0.3322 accuracy: 0.8810-Val_Accuracy: 0.3570-Val_Accuracy: 0.8701 Epoch 10/10 60000/60000 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 2 ms - 116 - s/sample - loss: 0.3258 accuracy: 0.8831-Val_Accuracy: 0.3499 - Val_Accuracy: 0.8765

Make a simple prediction

It’s really a jacket

Print (model predict (test_images [10]. Reshape,28,28,1 (- 1))) print (test_labels [10]) PLT. The figure () plt.imshow(test_images[10]) plt.colorbar() plt.grid(False) plt.show() class_names[4]