First, preliminary work

My environment:

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

Recommended Reading:

  • Depth study of 100 cases – convolution neural network (CNN) implementation mnist handwritten numeral recognition | 1 day
  • Depth study of 100 cases (VGG – 19) – convolution neural network to identify the spirit the characters in the cage | 7 days
  • Depth study of 100 cases (VGG – 16) – convolution neural network to identify one piece hat | gang on the sixth day

From the column:100 Examples of Deep Learning

1. Set the GPU

You can 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 more than one GPU, 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.cifar10.load_data()
Copy the code

3. The normalization

# Normalizes the value of the pixel between 0 and 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
((50000, 32, 32, 3), (10000, 32, 32, 3), (50000, 1), (10000, 1))
Copy the code

4. A visual

class_names = ['airplane'.'automobile'.'bird'.'cat'.'deer'.'dog'.'frog'.'horse'.'ship'.'truck']

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][0]])
plt.show()
Copy the code

2. Build CNN network

model = models.Sequential([
    layers.Conv2D(32, (3.3), activation='relu', input_shape=(32.32.3)), # Convolution layer 1, convolution kernel 3*3
    layers.MaxPooling2D((2.2)),                   # pool 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, connects the convolution layer with the full connection layer
    layers.Dense(64, activation='relu'),   Full connection layer, further extraction of features
    layers.Dense(10)                       # Output layer, output expected results
])

model.summary()  Print network structure
Copy the code
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 30, 30, 32) 896 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 15, 15, 32) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 13, 13, 64) 18496 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 6, 6, 64) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 4, 4, 64) 36928 _________________________________________________________________ flatten (Flatten) (None, 1024) 0 _________________________________________________________________ dense (Dense) (None, 64) 65600 _________________________________________________________________ dense_1 (Dense) (None, 10) 650 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Total params: 122570 Trainable params: 122570 Non - trainable params: 0 _________________________________________________________________Copy the code

Three, compile,

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

Iv. Training model

history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))
Copy the code
Epoch 1/10 1563/1563 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 9 s 4 ms/step - loss: 1.7862 accuracy: 0.3390 - val_loss: 1.2697 - val_accuracy: 0.5406 Epoch 2/10 1563/1563 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 5 s 3 ms/step - loss: 1.2270 - accuracy: 0.5595 - val_loss: 1.0731 - val_accuracy: 0.6167 Epoch 3/10 1563/1563 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 5 s 3 ms/step - loss: 1.0355 accuracy: 0.6377-val_accuracy: 0.9678-val_accuracy: 0.6610 Epoch 4/10 1563/1563 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 5 s 3 ms/step - loss: 0.9221 accuracy: 0.6727-val_accuracy: 0.9589-val_accuracy: 0.6648 Epoch 5/10 1563/1563 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 5 s 3 ms/step - loss: 0.8474 accuracy: 0.7022-val_accuracy: 0.8962-val_accuracy: 0.6853 Epoch 6/10 1563/1563 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 5 s 3 ms/step - loss: 0.7814 accuracy: Accuracy: 0.772-val_loss: 0.9124-val_accuracy: 0.6873 Epoch 7/10 1563/1563 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 5 s 3 ms/step - loss: 0.7398 accuracy: 0.7398-val_accuracy: 0.8924 - val_accuracy: 0.6929 Epoch 8/10 1563/1563 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 5 s 3 ms/step - loss: 0.7008 accuracy: 0.7542 -val_accuracy: 0.9809 -val_accuracy: 0.6854 Epoch 9/10 1563/1563 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 5 s 3 ms/step - loss: 0.6474 accuracy: 0.7732-val_accuracy: 0.8559-val_accuracy: 0.7137 Epoch 10/10 1563/1563 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 5 s 3 ms/step - loss: 0.6041 accuracy: 0.7889-val_loss: 0.8909-val_accuracy: 0.7046Copy the code

Five, the prediction

The probability of each category is predicted by the model. The larger the number, the more likely the picture is to belong to this category

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
ship
Copy the code

Vi. Model evaluation

import matplotlib.pyplot as plt

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.8909 - accuracy: 0.7046Copy the code
print(test_acc)
Copy the code
0.7045999765396118
Copy the code

Recommended Reading:

  • Depth study of 100 cases – convolution neural network (CNN) implementation mnist handwritten numeral recognition | 1 day
  • Depth study of 100 cases (VGG – 19) – convolution neural network to identify the spirit the characters in the cage | 7 days
  • Depth study of 100 cases (VGG – 16) – convolution neural network to identify one piece hat | gang on the sixth day

From the column:100 Examples of Deep Learning