• 【深度学习21天学习挑战赛】6、使用VGG16模型实现卷积神经网——识别眼睛状态


    活动地址:CSDN21天学习挑战赛

    今天继续跟着k同学啊老师继续学习,之前在自己看书的时候,看到过VGG模型的介绍,VGG网络明确指出并实践证明了,浅而大的卷积核不如深而小的卷积核。今天有机会可以实际上手了,老规矩,先跟着案例做,然后再逐步消化。完整源码附后

    1、VGG16模型简介

    VGG16模型可以很好的适用于分类定位任务,其名称来自牛津大学几何组(Visual Geometry Group)的缩写。

    根据卷积核的大小核卷积层数,VGG共有6种配置,分别为A、A-LRN、B、C、D、E,其中D和E两种是最为常用的VGG16和VGG19。
    在这里插入图片描述
    VGG-16:

    • 输入层224×224×3,经过两层相同的卷积
    • 卷积filter为3*3,stride为1,filter数为64
    • 一层pooling
    • 按照相同的方式,让宽和高越来越小,而通道数逐倍增加,直到512
    • 最后用两层相同全连接加一个softmax。
      在这里插入图片描述

    VGG-16的性能与VGG-19差不多,所以基本用VGG-16

    2、加载、配置数据

    数据集是老师提供的:

    • 4308张图片
    • 四类:close_look(闭眼)、forward_look(向前看)、left_look(向左看)、right_look(向右看)

    在这里插入图片描述

    import numpy as np
    np.random.seed(1)
    
    # 设置随机种子尽可能使结果可以重现
    import tensorflow as tf
    tf.random.set_seed(1)
    
    import pathlib
    
    
    data_dir = "./017_Eye_dataset"
    
    data_dir = pathlib.Path(data_dir)
    image_count = len(list(data_dir.glob('*/*')))
    
    print("图片总数为:",image_count)
    
    
    # 加载
    batch_size = 64
    img_height = 224
    img_width = 224 
    train_ds = tf.keras.preprocessing.image_dataset_from_directory(
        data_dir,
        validation_split=0.2,
        subset="training",
        seed=12,
        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=12,
        image_size=(img_height, img_width),
        batch_size=batch_size)
    class_names = train_ds.class_names
    print(class_names)
    
    # 可视化
    plt.figure(figsize=(10, 5))  # 图形的宽为10高为5
    plt.suptitle("数据展示")
    
    for images, labels in train_ds.take(1):
        for i in range(8):
            
            ax = plt.subplot(2, 4, i + 1)  
            
            ax.patch.set_facecolor('yellow')
            
            plt.imshow(images[i].numpy().astype("uint8"))
            plt.title(class_names[labels[i]])
            
            plt.axis("off")
            
    for image_batch, labels_batch in train_ds:
        print(image_batch.shape)
        print(labels_batch.shape)
        break
    # 配置数据集
    # 这里如果是 tf2.6 或者报错,使用 AUTOTUNE = tf.data.experimental.AUTOTUNE
    
    AUTOTUNE = tf.data.AUTOTUNE
    train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
    val_ds   = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    3、构建模型

    这里加载官方的VGG16模型

    model = tf.keras.applications.VGG16()
    # 打印模型信息
    model.summary()
    
    • 1
    • 2
    • 3

    打印如下

    Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels.h5
    553467904/553467096 [==============================] - 5531s 10us/step
    Model: "vgg16"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    input_1 (InputLayer)         [(None, 224, 224, 3)]     0         
    _________________________________________________________________
    block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
    _________________________________________________________________
    block1_conv2 (Conv2D)        (None, 224, 224, 64)      36928     
    _________________________________________________________________
    block1_pool (MaxPooling2D)   (None, 112, 112, 64)      0         
    _________________________________________________________________
    block2_conv1 (Conv2D)        (None, 112, 112, 128)     73856     
    _________________________________________________________________
    block2_conv2 (Conv2D)        (None, 112, 112, 128)     147584    
    _________________________________________________________________
    block2_pool (MaxPooling2D)   (None, 56, 56, 128)       0         
    _________________________________________________________________
    block3_conv1 (Conv2D)        (None, 56, 56, 256)       295168    
    _________________________________________________________________
    block3_conv2 (Conv2D)        (None, 56, 56, 256)       590080    
    _________________________________________________________________
    block3_conv3 (Conv2D)        (None, 56, 56, 256)       590080    
    _________________________________________________________________
    block3_pool (MaxPooling2D)   (None, 28, 28, 256)       0         
    _________________________________________________________________
    block4_conv1 (Conv2D)        (None, 28, 28, 512)       1180160   
    _________________________________________________________________
    block4_conv2 (Conv2D)        (None, 28, 28, 512)       2359808   
    _________________________________________________________________
    block4_conv3 (Conv2D)        (None, 28, 28, 512)       2359808   
    _________________________________________________________________
    block4_pool (MaxPooling2D)   (None, 14, 14, 512)       0         
    _________________________________________________________________
    block5_conv1 (Conv2D)        (None, 14, 14, 512)       2359808   
    _________________________________________________________________
    block5_conv2 (Conv2D)        (None, 14, 14, 512)       2359808   
    _________________________________________________________________
    block5_conv3 (Conv2D)        (None, 14, 14, 512)       2359808   
    _________________________________________________________________
    block5_pool (MaxPooling2D)   (None, 7, 7, 512)         0         
    _________________________________________________________________
    flatten (Flatten)            (None, 25088)             0         
    _________________________________________________________________
    fc1 (Dense)                  (None, 4096)              102764544 
    _________________________________________________________________
    fc2 (Dense)                  (None, 4096)              16781312  
    _________________________________________________________________
    predictions (Dense)          (None, 1000)              4097000   
    =================================================================
    Total params: 138,357,544
    Trainable params: 138,357,544
    Non-trainable params: 0
    _________________________________________________________________
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    4、设置动态学习率

    动态学习率有很多种,这里设置的动态学习率为:指数衰减型(ExponentialDecay)

    假设1个epoch有100个batch(相当于100step),20个epoch过后,step==2000,即step会随着epoch累加

    指数衰减学习率方法说明

    tf.keras.optimizers.schedules.ExponentialDecay(
        initial_learning_rate=初始学习率, decay_steps=衰减步数, decay_rate=衰减率, staircase= 如果等于true则学习率阶梯型下降
    )#  返回值为处理后的学习率 
    
    • 1
    • 2
    • 3
    # 设置初始学习率
    initial_learning_rate = 1e-4
    # 【注意】这里的1e-4,是科学计数法,即1乘以10的-4次方。
    # 所以,1e-4等同于0.0001
    
    lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
            initial_learning_rate, 
            decay_steps=20,      
            decay_rate=0.96,  
            staircase=True)
    
    # 将指数衰减学习率送入优化器
    optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    学习率大小的优缺点
    学习率大
    优点:1、加快学习速率。 2、有助于跳出局部最优值。
    缺点:1、导致模型训练不收敛。2、单单使用大学习率容易导致模型不精确。
    学习率小
    优点:1、有助于模型收敛、模型细化。2、提高模型精度。
    缺点: 1、很难跳出局部最优值。2、收敛缓慢。

    5、配置模型

    model.compile(optimizer=optimizer,
                  loss ='sparse_categorical_crossentropy',
                  metrics =['accuracy'])
    
    • 1
    • 2
    • 3

    6、训练模型

    epochs = 10
    
    history = model.fit(
        train_ds,
        validation_data=val_ds,
        epochs=epochs
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    7、模型评估

    7.1 Accuracy与Loss图

    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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在这里插入图片描述

    7.2 混淆矩阵

    Seaborn 是一个画图库,它基于 Matplotlib 核心库进行了更高阶的 API封装

    from sklearn.metrics import confusion_matrix
    import seaborn as sns
    import pandas as pd
    
    # 定义一个绘制混淆矩阵图的函数
    def plot_cm(labels, predictions):
        
        # 生成混淆矩阵
        conf_numpy = confusion_matrix(labels, predictions)
        # 将矩阵转化为 DataFrame
        conf_df = pd.DataFrame(conf_numpy, index=class_names ,columns=class_names)  
        
        plt.figure(figsize=(8,7))
        
        sns.heatmap(conf_df, annot=True, fmt="d", cmap="BuPu")
        
        plt.title('混淆矩阵',fontsize=15)
        plt.ylabel('真实值',fontsize=14)
        plt.xlabel('预测值',fontsize=14)
    val_pre   = []
    val_label = []
    
    for images, labels in val_ds:#这里可以取部分验证数据(.take(1))生成混淆矩阵
        for image, label in zip(images, labels):
            # 需要给图片增加一个维度
            img_array = tf.expand_dims(image, 0) 
            # 使用模型预测图片中的人物
            prediction = model.predict(img_array)
    
            val_pre.append(class_names[np.argmax(prediction)])
            val_label.append(class_names[label])
    plot_cm(val_label, val_pre)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    在这里插入图片描述

    8、保存和加载模型

    # 保存模型
    model.save('17.h5')
    # 加载模型
    new_model = tf.keras.models.load_model('17.h5')
    
    • 1
    • 2
    • 3
    • 4

    9、预测

    **使用刚刚保存的模型,加载后的的模型(new_model)预测结果

    plt.figure(figsize=(10, 5))  # 图形的宽为10高为5
    plt.suptitle("预测结果展示")
    
    for images, labels in val_ds.take(1):
        for i in range(8):
            ax = plt.subplot(2, 4, i + 1)  
            
            # 显示图片
            plt.imshow(images[i].numpy().astype("uint8"))
            
            # 需要给图片增加一个维度
            img_array = tf.expand_dims(images[i], 0) 
            
            # 使用模型预测图片中的人物
            predictions = new_model.predict(img_array)
            plt.title(class_names[np.argmax(predictions)])
    
            plt.axis("off")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    10、完整源码

    import matplotlib.pyplot as plt
    # 支持中文
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
    plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
    
    import os,PIL
    
    # 设置随机种子尽可能使结果可以重现
    import numpy as np
    np.random.seed(1)
    
    # 设置随机种子尽可能使结果可以重现
    import tensorflow as tf
    tf.random.set_seed(1)
    
    import pathlib
    
    
    data_dir = "./017_Eye_dataset"
    
    data_dir = pathlib.Path(data_dir)
    image_count = len(list(data_dir.glob('*/*')))
    
    print("图片总数为:",image_count)
    
    
    # 加载
    batch_size = 64
    img_height = 224
    img_width = 224 
    train_ds = tf.keras.preprocessing.image_dataset_from_directory(
        data_dir,
        validation_split=0.2,
        subset="training",
        seed=12,
        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=12,
        image_size=(img_height, img_width),
        batch_size=batch_size)
    class_names = train_ds.class_names
    print(class_names)
    
    # 可视化
    plt.figure(figsize=(10, 5))  # 图形的宽为10高为5
    plt.suptitle("数据展示")
    
    for images, labels in train_ds.take(1):
        for i in range(8):
            
            ax = plt.subplot(2, 4, i + 1)  
            
            ax.patch.set_facecolor('yellow')
            
            plt.imshow(images[i].numpy().astype("uint8"))
            plt.title(class_names[labels[i]])
            
            plt.axis("off")
            
    for image_batch, labels_batch in train_ds:
        print(image_batch.shape)
        print(labels_batch.shape)
        break
    
    # 配置数据集
    # 这里如果是 tf2.6 或者报错,使用 AUTOTUNE = tf.data.experimental.AUTOTUNE
    
    AUTOTUNE = tf.data.AUTOTUNE
    train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
    val_ds   = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
    
    model = tf.keras.applications.VGG16()
    model.summary()
    # 设置初始学习率
    initial_learning_rate = 1e-4
    
    lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
            initial_learning_rate, 
            decay_steps=20,   
            decay_rate=0.96,    
            staircase=True)
    
    # 将指数衰减学习率送入优化器
    optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
    
    model.compile(optimizer=optimizer,
                  loss     ='sparse_categorical_crossentropy',
                  metrics  =['accuracy'])
    
    epochs = 10
    
    history = model.fit(
        train_ds,
        validation_data=val_ds,
        epochs=epochs
    )
    
    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()
    
    from sklearn.metrics import confusion_matrix
    import seaborn as sns
    import pandas as pd
    
    # 定义一个绘制混淆矩阵图的函数
    def plot_cm(labels, predictions):
        
        # 生成混淆矩阵
        conf_numpy = confusion_matrix(labels, predictions)
        # 将矩阵转化为 DataFrame
        conf_df = pd.DataFrame(conf_numpy, index=class_names ,columns=class_names)  
        
        plt.figure(figsize=(8,7))
        
        sns.heatmap(conf_df, annot=True, fmt="d", cmap="BuPu")
        
        plt.title('混淆矩阵',fontsize=15)
        plt.ylabel('真实值',fontsize=14)
        plt.xlabel('预测值',fontsize=14)
    val_pre   = []
    val_label = []
    
    for images, labels in val_ds:#这里可以取部分验证数据(.take(1))生成混淆矩阵
        for image, label in zip(images, labels):
            # 需要给图片增加一个维度
            img_array = tf.expand_dims(image, 0) 
            # 使用模型预测图片中的人物
            prediction = model.predict(img_array)
    
            val_pre.append(class_names[np.argmax(prediction)])
            val_label.append(class_names[label])
    plot_cm(val_label, val_pre)
    
    # 保存模型
    model.save('17.h5')
    # 加载模型
    new_model = tf.keras.models.load_model('17.h5')
    
    # 采用加载的模型(new_model)来看预测结果
    
    plt.figure(figsize=(10, 5))  # 图形的宽为10高为5
    plt.suptitle("预测结果展示")
    
    for images, labels in val_ds.take(1):
        for i in range(8):
            ax = plt.subplot(2, 4, i + 1)  
            
            # 显示图片
            plt.imshow(images[i].numpy().astype("uint8"))
            
            # 需要给图片增加一个维度
            img_array = tf.expand_dims(images[i], 0) 
            
            # 使用模型预测图片中的人物
            predictions = new_model.predict(img_array)
            plt.title(class_names[np.argmax(predictions)])
    
            plt.axis("off")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183

    **

    学习日记

    **
    1,学习知识点

    a、VGG16模型的实践
    b、学习了混淆矩阵的用法
    c、尝试了设置动态学习率的使用方法

    2,学习遇到的问题

    还需要仔细消化各知识点,补齐基础

  • 相关阅读:
    软件公司之间合作的保密协议
    Java第三方登录封装工具类
    Car Window Control Reset
    JAVA 18 新特性详解
    机器学习——第五章 神经网络
    禁忌搜索算法TS求解TSP问题
    【计算机网络】数据链路层:使用点对点信道的数据链路层
    leetcode每天5题-Day37
    兼职管理系统
    gitlab+docker+harbor+k8s+jenkins部署简单应用
  • 原文地址:https://blog.csdn.net/m0_48300767/article/details/126312676