I. Preliminary work

My environment:

  • Language: Python3.6.5
  • Compiler: Jupyter Notebook
  • Deep learning environment: TensorFlow2

From Column: 100 Examples of deep Learning

1. Set the GPU

Skip this step if you are using a CPU

import tensorflow as tf
gpus = tf.config.list_physical_devices("GPU")

if gpus:
    gpu0 = gpus[0]                                        # If there are multiple Gpus, use only the 0th GPU
    tf.config.experimental.set_memory_growth(gpu0, True)  Set GPU memory usage as required
    tf.config.set_visible_devices([gpu0],"GPU")
Copy the code

2. Import data

import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt

(train_images, train_labels), (test_images, test_labels) = datasets.fashion_mnist.load_data()
Copy the code

3. The normalization

Normalize the values of pixels to the range from 0 to 1.
train_images, test_images = train_images / 255.0, test_images / 255.0

train_images.shape,test_images.shape,train_labels.shape,test_labels.shape
Copy the code
((60000, 28, 28), (10000, 28, 28), (60000), (10000))Copy the code

Loading the dataset returns four NumPy arrays:

  • Train_images and train_Labels arrays are training sets, data that the model is used to learn.
  • Test_images and test_Labels arrays are test sets that are used to test the model.

The image is a 28×28 NumPy array with pixel values between 0 and 255. The labels are arrays of integers between 0 and 9. These labels correspond to the clothing category represented by the image:

The label class The label class
0 T-shirt/jacket 5 sandals
1 The trousers 6 shirt
2 jumper 7 Sports shoes
3 dress 8 package
4 coat 9 Short boots

4. Reformat the image

# Adjust the data to the format we need
train_images = train_images.reshape((60000.28.28.1))
test_images = test_images.reshape((10000.28.28.1))

train_images.shape,test_images.shape,train_labels.shape,test_labels.shape
Copy the code
((60000, 28, 28, 1), (10000, 28, 28, 1), (60000), (10000))Copy the code

5. A visual

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

plt.figure(figsize=(20.10))
for i in range(20):
    plt.subplot(5.10,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()
Copy the code

Second, build CNN network

The input of CNN is Tensor (image_height, image_width, color_channels), which contains the height, width and color of the image. Batch size is not required. Color_channels are (R,G and B) corresponding to the three color channels of RGB. In this example, our CNN input, the image in the fashion_mnist dataset, is in the shape of (28, 28, 1), i.e., the grayscale image. We need to assign the shape to the parameter input_shape when we declare the first layer.

model = models.Sequential([
    layers.Conv2D(32, (3.3), activation='relu', input_shape=(28.28.1)), # convolution layer 1, convolution kernel 3*3
    layers.MaxPooling2D((2.2)),                   Pooling layer 1,2 *2 sampling
    layers.Conv2D(64, (3.3), activation='relu'),  # convolution layer 2, convolution kernel 3*3
    layers.MaxPooling2D((2.2)),                   # Pool layer 2, 2*2 sampling
    layers.Conv2D(64, (3.3), activation='relu'),  # convolution layer 3, convolution kernel 3*3
    
    layers.Flatten(),                      #Flatten layer, connecting the convolution layer and the full connection layer
    layers.Dense(64, activation='relu'),   # Full connection layer, further feature extraction
    layers.Dense(10)                       # output layer, output expected results
])

model.summary()  Print the network structure
Copy the code
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 26, 26, 32) 320 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 13, 13, 32) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 11, 11, 64) 18496 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 3, 3, 64) 36928 _________________________________________________________________ flatten (Flatten) (None, 576) 0 _________________________________________________________________ dense (Dense) (None, 64) 36928 _________________________________________________________________ dense_1 (Dense) (None, 10) 650 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Total params: 93322 Trainable params: 93322 Non - trainable params: 0 _________________________________________________________________Copy the code

Three, compile,

Before you’re ready to train the model, you need to set it up a little more. The following was added in the build step of the model:

  • Loss function (LOSS) : Used to measure the accuracy of the model during training. You want to minimize this function in order to “steer” the model in the right direction.
  • Optimizer: Determines how the model will update based on the data it sees and its own loss function.
  • Metrics: Used to monitor training and testing steps. The following example uses accuracy, which is the percentage of images that are correctly classified.
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
Copy the code

4. Training model

history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))
Copy the code
Epoch 1/10 1875/1875 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 9 s 4 ms/step - loss: 0.7005 accuracy: 0.7426 - val_loss: 0.3692 - val_accuracy: 0.8697 Epoch 2/10 1875/1875 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 6 s 3 ms/step - loss: 0.3303-accuracy: 0.8789 - val_loss: 0.3106-val_accuracy: 0.8855 Epoch 3/10 1875/1875 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 6 s 3 ms/step - loss: 0.2770 accuracy: 0.8988 - val_loss: 0.3004 - val_accuracy: 0.8902 Epoch 4/10 1875/1875 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 6 s 3 ms/step - loss: 0.2398 accuracy: 0.9097-val_loss: 0.2898-val_accuracy: 0.8968 Epoch 5/10 1875/1875 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 6 s 3 ms/step - loss: 0.2191 accuracy: 0.9195-VAL_loss: 0.2657-val_accuracy: 0.9057 Epoch 6/10 1875/1875 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 6 s 3 ms/step - loss: 0.1952 accuracy: 0.9292-val_loss: 0.2731-val_accuracy: 0.9036 Epoch 7/10 1875/1875 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 6 s 3 ms/step - loss: 0.1791 accuracy: 0.9322-VAL_loss: 0.2747-VAL_accuracy: 0.9056 Epoch 8/10 1875/1875 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 6 s 3 ms/step - loss: 0.1576 accuracy: 0.9416-VAL_loss: 0.2750-val_accuracy: 0.9049 Epoch 9/10 1875/1875 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 6 s 3 ms/step - loss: 0.1421 accuracy: 0.9461-VAL_loss: 0.2876-val_accuracy: 0.9032 Epoch 10/10 1875/1875 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 6 s 3 ms/step - loss: 0.1330 accuracy: 0.9509 - VAL_loss: 0.2769 - VAL_accuracy: 0.9144Copy the code

Five, the prediction

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

plt.imshow(test_images[1])
Copy the code

import numpy as np

pre = model.predict(test_images)
print(class_names[np.argmax(pre[1]])Copy the code
Pullover
Copy the code

6. Model evaluation

plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.5.1])
plt.legend(loc='lower right')
plt.show()

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

313/313-0s-loss: 0.2769-accuracy: 0.9144Copy the code
print("Test accuracy is:",test_acc)
Copy the code
The test accuracy is 0.9143999814987183Copy the code

“100 Examples of Deep Learning” column: [Portal]

  • The data of “100 Cases of In-depth Learning” can be obtained from the background reply of [DL+ days] on ✨ wechat (K students)
  • ✨ wechat communication group: add my wechat (mTYjkh_) pull you into the group, remarks: nuggets + purpose
  • ✨QQ communication group: 807041986