I. Preliminary work

CNN will be used to realize flower identification in this paper

My environment:

  • Language: Python3.6.5
  • Compiler: Jupyter Notebook
  • Deep learning environment: TensorFlow2
  • Data address: [Portal]

🚀 from column: [100 Cases 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. Download data

import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow as tf

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

import pathlib
Copy the code
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"

data_dir = tf.keras.utils.get_file(fname    = 'flower_photos'.The name of the downloaded file
                                   origin   = dataset_url,     The URL path of Dataset;
                                   untar    = True.# Whether to decompress the file
                                   cache_dir= 'D:/jupyter notebook/DL-100-days')

data_dir = pathlib.Path(data_dir)
data_dir
Copy the code
WindowsPath('D:/jupyter notebook/DL-100-days/datasets/flower_photos')
Copy the code

Finally, the data is saved to the D:\ Jupyter Notebook \DL-100-days\datasets\flower_photos directory

3. Check the data

Data sets are divided into Daisy, Dandelion, Roses, Sunflowers, and Tulips, which are stored in five subfolders of flower_photo folder respectively

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

print("Total number of pictures is:",image_count)
Copy the code
Total number of images: 3,670Copy the code
roses = list(data_dir.glob('roses/*'))
PIL.Image.open(str(roses[0]))
Copy the code

Second, 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 3670 files belonging to 5 classes.
Using 2936 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 3670 files belonging to 5 classes.
Using 734 files for validation.
Copy the code

We can output the label of the dataset with class_names. Labels will correspond alphabetically to directory names.

class_names = train_ds.class_names
print(class_names)
Copy the code
['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
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 images

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 operation

prefetch()The accelerator is idle when 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 CPU preprocessing time and accelerator training time.prefetch()The preprocessing of training steps and model execution process are overlapped together. While the accelerator is performing the NTH training step, the CPU is preparing the data for the N+1 step. Doing so not only minimizes the single step time of training (as opposed to the total time), but also reduces the time required to extract and transform the data. If not usedprefetch(), the CPU and GPU/TPU are idle most of the time:useprefetch()Can significantly reduce idle time:

  • Cache () : Cache data sets into memory to speed up operations
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 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.

num_classes = 5

"" "about the calculation of convolution kernels do not understand can refer to the article: https://blog.csdn.net/qq_38251616/article/details/114278995 "" "

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.MaxPooling2D((2.2)),                   Pooling layer 1,2 *2 sampling
    layers.Conv2D(32, (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(128, activation='relu'),   # Full connection layer, further feature extraction
    layers.Dense(num_classes)               # output layer, output expected results
])

model.summary()  Print the network structure
Copy the code
Model: "sequential_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= rescaling_1 (Rescaling) (None, 180, 180, 3) 0 _________________________________________________________________ conv2d_3 (Conv2D) (None, 178, 178, 16) 448 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 89, 89, 16) 0 _________________________________________________________________ conv2d_4 (Conv2D) (None, 87, 87, 32) 4640 _________________________________________________________________ max_pooling2d_3 (MaxPooling2 (None, 43, 43, 32) 0 _________________________________________________________________ conv2d_5 (Conv2D) (None, 41, 41, 64) 18496 _________________________________________________________________ flatten_1 (Flatten) (None, 107584) 0 _________________________________________________________________ dense_2 (Dense) (None, 128) 13770880 _________________________________________________________________ dense_3 (Dense) (None, 5) 645 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Total params: 13795109 Trainable params: 13795109 Non - trainable params: 0 _________________________________________________________________Copy the code

Four, 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.
  • 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

5. Training model

history = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=10
)
Copy the code
Epoch 1/10 92/92 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 9 s 29 ms/step - loss: 1.7851 accuracy: 0.3435 - val_loss: 1.0564 - val_accuracy: 0.5640 Epoch 2/10 92/92 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 11-1 s/ms step - loss: 1.0037-accuracy: 0.5867 - val_loss: 1.0490-val_accuracy: 0.5708 Epoch 3/10 92/92 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 11-1 s/ms step - loss: 0.8206 accuracy: 0.6746 - val_loss: 0.9763 - val_accuracy: 0.6158 Epoch 4/10 92/92 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 1 s 12 ms/step - loss: 0.6061-accuracy: 0.7864-val_loss: 0.9745-val_accuracy: 0.6158 Epoch 5/10 92/92 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 1 s 12 ms/step - loss: 0.3319 accuracy: 0.8929 - val_loss: 1.2550 - val_accuracy: 0.6076 Epoch 6/10 92/92 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 11-1 s/ms step - loss: 0.1607-accuracy: 0.9473-val_loss: 1.4897-val_accuracy: 0.6172 Epoch 7/10 92/92 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 11-1 s/ms step - loss: 0.0864 accuracy: 0.9757 - val_loss: 1.5388 - val_accuracy: 0.6226 Epoch 8/10 92/92 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 1 s 12 ms/step - loss: 0.0621-accuracy: 0.9818-val_loss: 2.0122-val_accuracy: 0.6008 Epoch 9/10 92/92 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 11-1 s/ms step - loss: 0.0390 accuracy: 0.9893 - val_loss: 1.9353 - val_accuracy: 0.6267 Epoch 10/10 92/92 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 11-1 s/ms step - loss: 0.0061-accuracy: 0.9995-val_loss: 2.1597-val_accuracy: 0.6335Copy 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(val_ds, verbose=2)
Copy the code

23/23-0s-loss: 2.1597-accuracy: 0.6335Copy the code

It can be seen from the above that as the number of iterations increases, the gap between training accuracy rate and verification accuracy rate gradually increases, which is caused by over-fitting. Please refer to my next article for solutions

print("Verification accuracy is:",test_acc)
Copy the code
The verification accuracy is 0.6335150003433228Copy the code

🚀 from column: [100 Cases of deep learning]