The website address

Classify clothing images

import tensorflow as tf 
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
Copy the code

1. Import fashion MNIST data set

fashion_nmist = keras.datasets.fashion_mnist
(train_image,train_labels),(test_images,test_labels) = fashion_nmist.load_data()
Copy the code

Fashion MNIST is a temporary replacement data set for MNIST, which has classic images such as handwritten numbers.

Four numpy arrays are returned when the data is loaded:

  • Train_image,train_labels is the training set, used for learning data
- TEST_images,test_labels indicates the test set, which is used to predict the modelCopy the code

Where, each image is a 28*28 NUMPY array with pixels between 0 and 25 and labels between 0 and 9. Each label corresponds to a different clothing class:

Class_names = ['T-shirt/ Top ', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'shirt ', 'Sneaker', 'Bag', 'Ankle boot']Copy the code

2. Browse the data

The training set

train_image.shape
train_labels.shape #len(train.labels)
Copy the code

The test set

test_images.shape
len(test_labels)
Copy the code

3. Preprocess data

View one of the images

plt.figure()
plt.imshow(train_image[2])
plt.colorbar()
plt.grid(False)
plt.show()
Copy the code

The results are as follows

Compress all the values between 0 and 1, feed them to the neural model network, divide the training set and test set by 255, and then verify.

train_image = train_image/255 test_images = test_images/255 ! [image.png](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7bc5d19e4eec497197ac6fc0138b92ca~tplv-k3u1fbpfcp-watermark Figure (figsize = (10,10)) for I in range(25): Subplot (5,5, I +1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(train_image[I],cmap= plt.cm.binary) plt.grid(False) plt.imshow(train_image[I],cmap= plt.cm.binary) plt.xlabel(class_names[train_labels[I]]) plt.show()Copy the code

4. Build a model

To build a neural network, you need to configure the layers of the model and then compile the model.

4.1 set the layer

Model = keras.sequential ([keras.layers.flatten (input_shape=(28,28))), keras.layers.dense (128,activation = 'relu'), Sequential([keras.layers.flatten (input_shape=(28,28)), keras.layers.Dense(10) ])Copy the code
  1. The basic structure of a neural network is layers,
  2. The first layer of the network is to remove images from a two-dimensional array (2828 pixels) into a one-dimensional array (2828 =784 pixels) (flattened pixels)
  3. When you flatten out the pixels, the network will consist of two keras.layers.DenseThe sequence of layers. They are densely connected or fully connected neural layers. The first layer has 128 nodes (neurons) and the second layer returns a logits array of length 1-.
  4. Each node contains a score that identifies which of the 10 classes the current image belongs to

4.2 Compiling Model

model.compile(optimizer = 'Adam',
             loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
             metrics=['accuracy'])
Copy the code
  1. Optimizer: How does the model update based on the data it sees and its own loss function
  2. Loss function: Used to measure the accuracy of the model during the virtual orchid period
  3. Metrics: Used to monitor training and testing steps,accuracy It stands for accuracy.

5. Training model

Steps to train the model

  1. Feed the training data to the model. In this case, the training data is in the TRAIN_images and train_Labels arrays.
  2. Model learning associates images with labels.
  3. The model is required to predict the test set (in this case, the test_images array).
  4. Verify that the predictions match the labels in the test_Labels array.

5.1 Feed data to the model

model.fit(train_image,train_labels,epochs = 10)
Copy the code

Indicators of loss and accuracy are displayed during the training of the model.

5.2 Assessment Accuracy rate

test_loss,test_acc = model.evaluate(test_images,test_labels,verbose=2)
print('\n Test accuract:',test_acc)
Copy the code

The results show that the accuracy of the model on the test data set is slightly lower than the training data set. The gap between training accuracy and test accuracy represents overfitting. Overfitting is when machine learning models do not perform as well on new, previously unseen inputs as they do on training data. Overfitting models “remember” noise and details in the training data set, which negatively affects the model’s performance on new data. For more information, see the following:

  • We demonstrated fitting
  • Avoid over-fitting strategies

5.3 Making Predictions

After the model is trained, it can also be used to predict the image. The model has a linear output called logits, which we can convert to a more understandable one by adding a Softmax.

predictions = probability_model.predict(test_images)
predictions[0]

np.argmax(predictions[0])
Copy the code

The predicted result is an array of 10 numbers. They represent the model’s “confidence” in each of 10 different garments. You can see which tag has the highest confidence value

View the model’s predictions for all 10 classes by graphing the diagram.

def plot_image(i, predictions_array, true_label, img): predictions_array, true_label, img = predictions_array, 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): predictions_array, true_label = predictions_array, true_label[I] plt.grid(False) plt.xticks(range(10)) 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')Copy the code

5.4 Verify the prediction results

After the model has been trained, you can use it to make predictions for some images.

Let’s look at the 0th image, the prediction result, and the prediction array. Correct predictions are labeled blue, and wrong predictions are labeled red. The numbers represent the percentage of prediction tags (100 in total).

I = 0 plt.figure(figsize=(6,3)) plt.subplot(1,2,1) plot_image(I, predictions[I], test_labels, Plt.subplot (1,2,2) plot_value_array(I, predictions[I], test_labels) plt.show()Copy the code

Let’s draw a few images using the model’s predictions. Note that even with high confidence, the model can be wrong.

# Plot the first X test images, their predicted labels, and the true labels.
# Color correct predictions in blue and incorrect predictions in red.
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[i], test_labels, test_images)
  plt.subplot(num_rows, 2*num_cols, 2*I+2)
  plot_value_array(i, predictions[i], test_labels)
plt.tight_layout()
plt.show()
Copy the code

Use trained models

Make a prediction on a sample

Img = test_images[1] img. Shape #(28,28)Copy the code

When batch forecasting, even if there is only one sample, it must be added to the list:

Img =(np.expand_dims(img,0)) img. Shape # (1,28,28)Copy the code

Then label prediction is made for this image:

Predictions_single = reality_model (img) print(predictions_single)Copy the code

drawing

plot_value_array(1, predictions_single[0], test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)
Copy the code

Keras.model. predict returns a set of lists, each for each image in a batch of data. Get a prediction of our (unique) image in the batch

np.argmax(predictions_single[0])
Copy the code