First, preliminary work

In this paper, CNN will be used to realize the recognition of four weather states: cloudy, rainy, sunny and sunrise. Compared to the previous article, we added a Dropout layer and adjusted the maximum pooling layer to the average pooling layer to increase the model’s generalization capability.

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
  • Deep learning 100 cases – convolution neural network (CNN) color image classification | 2 days
  • 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 matplotlib.pyplot as plt
import os,PIL

# Set random seeds to reproduce the results as much as possible
import numpy as np
np.random.seed(1)

# Set random seeds to reproduce the results as much as possible
import tensorflow as tf
tf.random.set_seed(1)

from tensorflow import keras
from tensorflow.keras import layers,models

import pathlib
Copy the code
data_dir = "D:/jupyter notebook/DL-100-days/datasets/weather_photos/"

data_dir = pathlib.Path(data_dir)
Copy the code

3. View data

The data set is divided into four categories: Cloudy, Rain, Shine and Sunrise, which are respectively stored in subfolders named with their respective names in the weather_photos folder.

image_count = len(list(data_dir.glob('*/*.jpg')))

print("The total number of pictures is:",image_count)
Copy the code
Total number of pictures: 1125Copy the code
roses = list(data_dir.glob('sunrise/*.jpg'))
PIL.Image.open(str(roses[0]))
Copy the code

2. Data preprocessing

1. Load data

Use the image_DATASet_from_directory method to load the data from the disk into tf.data.dataset

batch_size = 32
img_height = 180
img_width = 180
Copy the code
"" "about image_dataset_from_directory () articles detailing can refer to: https://mtyjkh.blog.csdn.net/article/details/117018789, "" "
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=0.2,
    subset="training",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size)
Copy the code
Found 1125 files belonging to 4 classes.
Using 900 files for training.
Copy the code
"" "about image_dataset_from_directory () articles detailing can refer to: https://mtyjkh.blog.csdn.net/article/details/117018789, "" "
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=0.2,
    subset="validation",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size)
Copy the code
Found 1125 files belonging to 4 classes.
Using 225 files for validation.
Copy the code

We can output the labels of the dataset through class_names. The labels will correspond alphabetically to the directory name.

class_names = train_ds.class_names
print(class_names)
Copy the code
['cloudy', 'rain', 'shine', 'sunrise']
Copy the code

2. Visualize data

plt.figure(figsize=(20.10))

for images, labels in train_ds.take(1) :for i in range(20):
        ax = plt.subplot(5.10, i + 1)

        plt.imshow(images[i].numpy().astype("uint8"))
        plt.title(class_names[labels[i]])
        
        plt.axis("off")
Copy the code

3. Check the data again

for image_batch, labels_batch in train_ds:
    print(image_batch.shape)
    print(labels_batch.shape)
    break
Copy the code
(32, 180, 180, 3)
(32,)
Copy the code
  • Image_batchIs the tensor of the shape (32,180,180,3). This is a batch of 32 images with the shape 180x180x3 (the last dimension refers to the color channel RGB).
  • Label_batchIs the tensor of the shape (32,), and these labels correspond to 32 pictures

4. Configure the data set

  • Shuffle () : disturb data, detailed introduction about this function can be reference: zhuanlan.zhihu.com/p/42417456
  • Prefetch () : prefetch data to speed up the running

The accelerator is idle while the CPU is preparing data. In contrast, the CPU is idle while the accelerator is training the model. Therefore, the training time is the sum of the CPU preprocessing time and the accelerator training time. Prefetch () overlaps the preprocessing of the training steps with the model execution. While the accelerator is performing the NTH training step, the CPU is preparing the data for the NTH +1 step. Doing so not only minimizes the single-step time of the training (as opposed to the total time), but also reduces the time required to extract and transform the data. If you don’t use prefetch(), the CPU and GPU/TPU are idle most of the time:

useprefetch()Can significantly reduce idle time:

  • Cache () : The data set is cached in memory to speed up operation
AUTOTUNE = tf.data.AUTOTUNE

train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
Copy the code

Third, build CNN network

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

num_classes = 4

""" For those who do not understand the calculation of convolution kernel, please refer to the following article: https://blog.csdn.net/qq_38251616/article/details/114278995 the layers. The Dropout (0.4) role is to prevent the fitting, improve the generalization ability of the models. In an article in flower recognition, the training accuracy and validation accuracy is difficult due to model a fitting more introduction about Dropout layer can refer to the article: https://mtyjkh.blog.csdn.net/article/details/115826689 "" "

model = models.Sequential([
    layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
    
    layers.Conv2D(16, (3.3), activation='relu', input_shape=(img_height, img_width, 3)), # Convolution layer 1, convolution kernel 3*3
    layers.AveragePooling2D((2.2)),               # pool layer 1,2 *2 sampling
    layers.Conv2D(32, (3.3), activation='relu'),  # Convolution layer 2, convolution kernel 3*3
    layers.AveragePooling2D((2.2)),               # Pool layer 2, 2*2 sampling
    layers.Conv2D(64, (3.3), activation='relu'),  # Convolution layer 3, convolution kernel 3*3
    layers.Dropout(0.3),  
    
    layers.Flatten(),                       # Flatten layer, connects the convolution layer with the full connection layer
    layers.Dense(128, activation='relu'),   Full connection layer, further extraction of features
    layers.Dense(num_classes)               # Output layer, output expected results
])

model.summary()  Print network structure
Copy the code
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= rescaling (Rescaling) (None, 180, 180, 3) 0 _________________________________________________________________ conv2d (Conv2D) (None, 178, 178, 16) 448 _________________________________________________________________ average_pooling2d (AveragePo (None, 89, 89, 16) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 87, 87, 32) 4640 _________________________________________________________________ average_pooling2d_1 (Average (None, 43, 43, 32) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 41, 41, 64) 18496 _________________________________________________________________ dropout (Dropout) (None, 41, 41, 64) 0 _________________________________________________________________ flatten (Flatten) (None, 107584) 0 _________________________________________________________________ dense (Dense) (None, 128) 13770880 _________________________________________________________________ dense_1 (Dense) (None, 5) 645 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Total params: 13795109 Trainable params: 13795109 Non - trainable params: 0 _________________________________________________________________Copy the code

Four, compile,

Before you are 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: Used to measure the accuracy of the model during training.
  • Optimizer: Determines how the model is updated 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 ratio of images that are correctly classified.
# Set optimizer
opt = tf.keras.optimizers.Adam(learning_rate=0.001)

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

5. Training model

epochs = 10

history = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=epochs
)
Copy the code
Epoch 1/10 29/29 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 6 s 58 ms/step - loss: 1.5865 accuracy: 0.4463 - val_loss: 0.5837 - val_accuracy: 0.7689 Epoch 2/10 29/29 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 0 s 12 ms/step - loss: Accuracy: 0.5289-accuracy: 0.8295-val_loss: 0.5405-val_accuracy: 0.8133 Epoch 3/10 29/29 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 0 s 12 ms/step - loss: 0.2930 accuracy: 0.8967 - val_loss: 0.5364 - val_accuracy: 0.8000 Epoch 4/10 29/29 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 0 s 12 ms/step - loss: 0.2742 - accuracy: 0.9074 - val_loss: 0.4034 - val_accuracy: 0.8267 Epoch 5/10 29/29 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 0 s 11 ms/step - loss: 0.1952 accuracy: 0.9383 - val_loss: 0.3874 - val_accuracy: 0.8844 Epoch 6/10 29/29 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 0 s 11 ms/step - loss: 0.1592 - accuracy: 0.9468-val_loss: 0.3680 - val_accuracy: 0.8756 Epoch 7/10 29/29 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 0 s 12 ms/step - loss: 0.0836 accuracy: 0.9755 - val_loss: 0.3429 - val_accuracy: 0.8756 Epoch 8/10 29/29 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 0 s 12 ms/step - loss: 0.0943 - accuracy: 0.9692-val_loss: 0.3836 - val_accuracy: 0.9067 Epoch 9/10 29/29 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 0 s 12 ms/step - loss: 0.0344 accuracy: 0.9909 - val_loss: 0.3578 - val_accuracy: 0.9067 Epoch 10/10 29/29 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 0 s 11 ms/step - loss: 0.0950-accuracy: 0.9708-val_loss: 0.4710-val_accuracy: 0.8356Copy the code

Vi. Model evaluation

acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(epochs)

plt.figure(figsize=(12.4))
plt.subplot(1.2.1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1.2.2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
Copy the code

Consideration: 1. What is the difference between maximum pooling and average pooling? 2. Is the larger the learning rate, the better? How to set the optimizer?


Recommended Reading:

  • Depth study of 100 cases – convolution neural network (CNN) implementation mnist handwritten numeral recognition | 1 day
  • Deep learning 100 cases – convolution neural network (CNN) color image classification | 2 days
  • 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

Data can be obtained from the reply [DL+5] in the wechat official account [K Classmate ah].