Machine learning notes (1) Tensorflow / keras to classify clothing images (Fashion MNIST data set)

Development environment

Anaconda 4.9.2 / Python 3.6.12 / Tensorflow 2.4.1(CPU版)

Introduction to the program

This program is designed to take advantage of the high-level API TensorFlow for building and training model - keras training a neural network model , the garment image classification (10 classes). The data set in the program uses the Fashion MNIST data set, which contains 70,000 grayscale images (28x28 pixels) in 10 categories such as T-shirt, Trouser, and Pullover.
This program uses 60,000 images (training set) to train the network, and 10,000 images (test set) to test the accuracy of the network learning to classify images.

Program decomposition

Output Tensorflow version

Code

import tensorflow as tf 
from tensorflow import keras
import numpy as np 
import matplotlib.pyplot as plt 
print(tf.__version__)

Result: Version Tensorflow 2.4.1
Insert picture description here

Download the Fashion MNIST dataset

Code

fashion_mnist = keras.datasets.fashion_mnist
(train_images,train_labels),(test_images,test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt','Trouser','Pullover','Dress','Coat','Sandal','Shirt','Sneaker','Bag','Ankle boot']

load_data() will return four NumPy arrays: the train_images and train_labels arrays are the training set, and the test_images and test_labels arrays are the test set.
The image is a 28x28 NumPy array with pixel values ​​between 0 and 255 .
The label is an integer array, between 0 and 9 . These tags correspond to T-shirt, Trouser, Pullover, Dress, Coat, Sandal, Shirt, Sneaker, Bag, Ankle boot in the class_names array.

Result: Downloaded 4 compressed packages in .gz format as shown in the figure under the fashion_mnist folder with the path> .keras> datasets> fashion_mnist
Insert picture description here
Insert picture description here

Preprocessed data

Code

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()

Divide the values ​​of train_images and test_images by 255 to reduce them to between 0-1 so that they can be sent to the neural network.
In order to verify whether the data format is correct and whether the network is ready to be built and trained, the first 25 images in the training set are displayed here, and the class name is displayed below each image.

result
Insert picture description here

Build a neural network model

Code

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

The first layer of the network keras.layers.Flatten: Convert the image format from a two-dimensional array (28x28 pixels) to a one-dimensional array (28x28=784 pixels). This layer has no parameters to learn, it just reformats the data.
The second layer of the network keras.layers.Dense: fully connected layer, 128 neurons, the activation function uses the linear rectification function ReLU.
The third layer of the network keras.layers.Dense: returns a logits array of length 10 (linear output).

Compile the model

Code

model.compile(optimizer='adam',loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])

Set up three metrics.
Optimizer: Decide how the model is updated based on the data it sees and its own loss function.
Loss function: used to measure the accuracy of the model during training. It is hoped that this function can be minimized in order to "guide" the model in the correct direction.
Accuracy: The ratio of images that are correctly classified.

Training and prediction

1. Send training data to the model

Code

model.fit(train_images,train_labels,epochs=10)

Use all the data in the training set to train the model 10 times. During model training, loss and accuracy indicators are displayed. The accuracy of this model on the training data reached about 0.91 (or 91%).

result
Insert picture description here

2. Evaluate on the test set

Code

test_loss,test_acc = model.evaluate(test_images,test_labels,verbose=2)
print('\nTest accuracy:',test_acc)
print('\nTest loss:',test_loss)

result
Insert picture description here

The results show that the accuracy of the model on the test set is slightly lower than the training set.
The gap between training accuracy and testing accuracy represents overfitting.
Overfitting means that the machine learning model does not perform as well as the training set on new, previously unseen input. An overfitted model will "remember" the noise and details in the training set, which will negatively affect the model's performance on new data.

3. Make predictions

Code

probability_model = tf.keras.Sequential([model,tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)
print(np.argmax(predictions[8]))

After training, use it to make predictions on some images. The model has a linear output, namely logits. A softmax layer is attached here to convert logits into probabilities that are easier to understand.

Here is the prediction result of the 9th image (starting from 0) in the prediction test set, predictions[8] is an array containing 10 numbers, representing the model's "confidence" for each of the 10 different garments, taking the confidence The label with the largest degree (argmax) is the prediction result.

result
Insert picture description here

Complete code

import tensorflow as tf 
from tensorflow import keras
import numpy as np 
import matplotlib.pyplot as plt 
print(tf.__version__)
fashion_mnist = keras.datasets.fashion_mnist
(train_images,train_labels),(test_images,test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt','Trouser','Pullover','Dress','Coat','Sandal','Shirt','Sneaker','Bag','Ankle boot']
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()
model = keras.Sequential([keras.layers.Flatten(input_shape=(28,28)),keras.layers.Dense(128,activation='relu'),keras.layers.Dense(10)])
model.compile(optimizer='adam',loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])
model.fit(train_images,train_labels,epochs=10)
test_loss,test_acc = model.evaluate(test_images,test_labels,verbose=2)
print('\nTest accuracy:',test_acc)
print('\nTest loss:',test_loss)
probability_model = tf.keras.Sequential([model,tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)
print(np.argmax(predictions[8]))

Guess you like

Origin blog.csdn.net/Echoshit8/article/details/114025500