Image recognition process learning summary

Through the learning of image recognition, the process and classification of image recognition are preliminarily summarized, hoping to help the small partners who are learning.
insert image description here
1. Preparatory work
1. Acquisition of data sets
Before data analysis, data is required for identification. The so-called data here refers to images. We need to classify the images that need to be identified in order to call them better. The following takes the weather dataset as an example, which is divided into four categories. The division of the dataset is shown in the figure below:

['cloudy', 'rain', 'shine', 'sunrise']

insert image description here
2. Obtain the path of the dataset
There are many ways to obtain the dataset. The pathlib function library is used here, and the os function library can also be used to obtain data

import pathlib
data_dir = "G:\BaiduNetdiskDownload\climate\weather_photos/"
data_dir = pathlib.Path(data_dir)
#查看数据数量
image_count = len(list(data_dir.glob('*/*.jpg')))

3. Data set division
Here, the function validation_split divides the data set into 0.8:0.2 or 4:1

#训练集
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)
#验证集
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)

4. Data preprocessing

shuffle(): shuffle data, for detailed introduction of this function, please refer to: https:lzhuanlan.zhihu.com/p/42417456
prefetch(): prefetch data, speed up operation
insert image description here

AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
num_classes = 4
batch_size = 32
img_height = 180
img_width = 180

2. Network model construction
1. Model
construction The basic components of a convolutional neural network include an input layer, a convolutional layer, an activation function, a pooling layer, and a fully connected layer (put at the end as an output layer). The main functions of each layer will not be introduced in detail here, you can refer to the link to
directly upload the code:

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)),  # 卷积层1,卷积核3*3
    layers.AveragePooling2D((2, 2)),  # 池化层1,2*2采样
    layers.Conv2D(32, (3, 3), activation='relu'),  # 卷积层2,卷积核3*3
    layers.AveragePooling2D((2, 2)),  # 池化层2,2*2采样
    layers.Conv2D(64, (3, 3), activation='relu'),  # 卷积层3,卷积核3*3
    layers.Dropout(0.3),

    layers.Flatten(),  # Flatten层,连接卷积层与全连接层
    layers.Dense(128, activation='relu'),  # 全连接层,特征进一步提取
    layers.Dense(num_classes)  # 输出层,输出预期结果
])

model.summary()  # 打印网络结构

The detailed parameters of the network can be printed out through **model.summary()**
insert image description here
2. Network configuration
includes the selection of optimizer, the selection of calculation function, and the design of learning rate

# 编译 设置优化器
#learning_rate=0.001学习率
opt = tf.keras.optimizers.Adam(learning_rate=0.001)

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

3. Model evaluation
1. Training + verification

epochs = 50#训练次数

history = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=epochs
)

Training process visualization code

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()

The training process is as shown in Figure
insert image description here
2. Evaluation
Save the trained model and call it when evaluating the prediction results

model.save('./checkpoint/model.h5')
#评价结果
score = model.evaluate_generator(Generator(testpath,batch_size),steps=int(m) // batch_size)

The evaluation formula is as follows

def Precision(y_true, y_pred):
    """精确率"""
    tp = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))  # true positives
    pp = K.sum(K.round(K.clip(y_pred, 0, 1)))  # predicted positives
    precision = tp / (pp + K.epsilon())
    return precision

def Recall(y_true, y_pred):
    """召回率"""
    tp = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))  # true positives
    pp = K.sum(K.round(K.clip(y_true, 0, 1)))  # possible positives
    recall = tp / (pp + K.epsilon())
    return recall


def F1(y_true, y_pred):
    """F1-score"""
    precision = Precision(y_true, y_pred)
    recall = Recall(y_true, y_pred)
    f1 = 2 * ((precision * recall) / (precision + recall + K.epsilon()))
    return f1

How to use, please refer to the link

Guess you like

Origin blog.csdn.net/self_Name_/article/details/126216845