100 Examples of Deep Learning-Convolutional Neural Network (CNN) Weather Recognition | Day 5

100 Examples of Deep Learning-Convolutional Neural Network (CNN) Weather Recognition | Day 5

1. Preliminary work

This article will use CNN to recognize four weather states: cloudy, rainy, sunny, and sunrise. Compared with the previous article, in order to increase the generalization ability of the model, this article added a dropout layer and adjusted the maximum pooling layer to an average pooling layer.

My environment:

  • Language environment: Python3.6.5
  • Compiler: jupyter notebook
  • Deep learning environment: TensorFlow 2.4.1

Recommended reading:

From the column: "100 Examples of Deep Learning"

1. Setting up the GPU

If you are using a CPU, you can ignore this step

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

2. Import data

import matplotlib.pyplot as plt import os,PIL # Set the random seed as much as possible to make the result reproducible import numpyas np np.random.seed( 1 ) # Set the random seed as much as possible to make the result reproducible import tensorflowas 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 code

3. View the data

The data set is divided into

cloudy
,
rain
,
shine
,
sunrise
4.categories, respectively stored in
weather_photos
In the subfolders named after their respective names in the folder.

image_count = len ( list (data_dir.glob( '*/*.jpg' ))) Print ( "Total pictures as:" , IMAGE_COUNT) Copy the code
The total number of pictures is: 1125Copy code
roses = list (data_dir.glob( 'sunrise/*.jpg' )) PIL.Image. Open ( STR (Roses [ 0 ])) Copy the code

2. Data preprocessing

1. Load data

use

image_dataset_from_directory
Method to load the data from the disk to
tf.data.Dataset
in

= the batch_size 32 IMG_HEIGHT = 180 [ IMG_WIDTH = 180 [ duplicated code
""" For a detailed introduction to image_dataset_from_directory(), please refer to the article: 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 code
Found 1125 files belonging to 4 classes. Using 900 files for training. Copy code
""" For a detailed introduction to image_dataset_from_directory(), please refer to the article: 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 code
Found 1125 files belonging to 4 classes. Using 225 files for validation. Copy code

We can output the label of the data set through class_names. The labels will correspond to the directory names in alphabetical order.

= train_ds.class_names class_names Print (class_names) copying the code
[ 'cloudy', 'rain' , 'shine', 'sunrise'] duplicated code

2. Visualize the 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 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 code
  • Image_batch
    Is the tensor of shape (32,180,180,3). This is a batch of 32 pictures with a shape of 180x180x3 (the last dimension refers to the color channel RGB).
  • Label_batch
    Is a tensor of shape (32,), these labels correspond to 32 images

4. Configure the data set

  • shuffle() : Shuffle the data. For a detailed introduction to this function, please refer to: zhuanlan.zhihu.com/p/42417456
  • prefetch() : prefetch data to speed up operation

prefetch()
Function details: When the CPU is preparing data, the accelerator is in idle state. Conversely, when the accelerator is training the model, the CPU is in an idle state. Therefore, the training time is the sum of the CPU preprocessing time and the accelerator training time.
prefetch()
The preprocessing of the training step and the model execution process are overlapped together. When the accelerator is executing the Nth training step, the CPU is preparing the data for the N+1th step. Doing so can not only minimize the single-step training time (rather than the total time), but also shorten the time required to extract and transform data. If not using
prefetch()
, CPU and GPU/TPU are idle most of the time:

use

prefetch()
Can significantly reduce idle time:

  • cache() : Cache the data set in the memory to speed up the 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 code

3. build a CNN network

The input of convolutional neural network (CNN) is in the form of tensor (Tensor)

(image_height, image_width, color_channels)
, Contains image height, width and color information. No input required
batch size
. color_channels is (R, G, B) corresponding to the three color channels of RGB (color channel). In this example, our CNN input, the image in the fashion_mnist dataset, has the shape
(28, 28, 1)
That is, grayscale images. We need to assign the shape to the parameter when declaring the first layer
input_shape
.

num_classes = 4 """ For those who do not understand the calculation of the convolution kernel, please refer to the article: https://blog.csdn.net/qq_38251616/article/details/114278995 The function of layers.Dropout(0.4) is to prevent over-fitting and improve the generalization ability of the model. In the previous article flower recognition, the huge difference between the training accuracy and the verification accuracy is due to the overfitting of the model. For more information about the Dropout layer, please 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 ) ), # Pooling layer 1, 2*2 sampling layers.Conv2D( 32 , ( 3 , 3 ), activation= 'relu' ), # Convolutional layer 2, Convolution kernel 3*3 layers.AveragePooling2D(( 2 , 2 )), # Pooling layer 2, 2*2 sampling layers.Conv2D( 64 , ( 3 , 3), activation= 'relu' ), # Convolutional layer 3, convolution kernel 3*3 layers.Dropout( 0.3 ), layers.Flatten(), # Flatten layer, connect convolutional layer and fully connected layer layers.Dense( 128 , activation= 'relu' ), # fully connected layer, feature further extraction layers.Dense(num_classes) # output layer, output expected outcome ]) model.summary () # print network structure duplicated 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: 13,795,109 Trainable params: 13,795,109 Non-trainable params: 0 _________________________________________________________________ Copy code

4. compile

Before preparing to train the model, some more settings need to be made. The following are added in the compilation step of the model:

  • Loss function (loss): used to measure the accuracy of the model during training.
  • Optimizer (optimizer): Determine how the model is updated based on the data it sees and its own loss function.
  • Metrics: used to monitor training and testing procedures. The following example uses accuracy, which is the ratio of images that are correctly classified.
# Set the 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 code
Epoch 1/10 29/29 [==============================]-6s 58ms/step-loss: 1.5865-accuracy: 0.4463-val_loss : 0.5837-val_accuracy: 0.7689 Epoch 2/10 29/29 [==============================]-0s 12ms/step-loss: 0.5289-accuracy: 0.8295-val_loss : 0.5405-val_accuracy: 0.8133 Epoch 3/10 29/29 [==============================]-0s 12ms/step-loss: 0.2930-accuracy: 0.8967-val_loss : 0.5364-val_accuracy: 0.8000 Epoch 4/10 29/29 [==============================]-0s 12ms/step-loss: 0.2742-accuracy: 0.9074-val_loss : 0.4034-val_accuracy: 0.8267 Epoch 5/10 29/29 [==============================]-0s 11ms/step-loss: 0.1952-accuracy: 0.9383-val_loss : 0.3874-val_accuracy: 0.8844 Epoch 6/10 29/29 [==============================]-0s 11ms/step-loss: 0.1592-accuracy: 0.9468-val_loss : 0.3680-val_accuracy: 0.8756 Epoch 7/10 29/29 [==============================]-0s 12ms/step-loss: 0.0836-accuracy: 0.9755-val_loss : 0.3429-val_accuracy: 0.8756 Epoch 8/10 29/29 [==============================]-0s 12ms/step-loss: 0.0943-accuracy: 0.9692-val_loss : 0.3836-val_accuracy: 0.9067 Epoch 9/10 29/29 [==============================]-0s 12ms/step-loss: 0.0344-accuracy: 0.9909-val_loss : 0.3578-val_accuracy: 0.9067 Epoch 10/10 29/29 [==============================]-0s 11ms/step-loss: 0.0950-accuracy: 0.9708-val_loss : 0.4710-val_accuracy: 0.8356 Copy code

6. 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 code

Thinking: 1. What is the difference between maximum pooling and average pooling? 2. Is the learning rate the bigger the better? How should the optimizer be set?


Recommended reading:

From the column: "100 Examples of Deep Learning"

Reply [DL+5] in the WeChat public account [K classmates] to get the data.