第T4周:使用TensorFlow实现猴痘病识别

  • 本文为365天深度学习训练营 中的学习记录博客
  • 原作者:K同学啊

    文章目录

    • 一、前期工作
      • 1.设置GPU(如果使用的是CPU可以忽略这步)
      • 2. 导入数据
      • 3. 查看数据
    • 二、数据预处理
      • 1、加载数据
      • 2、数据可视化
      • 3、再次检查数据
      • 4、配置数据集
    • 三、构建CNN网络
    • 四、编译
    • 五、训练模型
    • 六、模型评估
      • 1. Loss与Accuracy图
      • 2.指定图片进行预测
    • 七、优化
      • 1、使用`model.evaluate`使用测试集评估模型
      • 2、网络结构优化
      • 3、 Loss与Accuracy图
      • 4、使用`model.evaluate`评估优化后的模型
    • 八、总结

电脑环境:
语言环境:Python 3.8.0
编译器:Jupyter Notebook
深度学习环境:tensorflow 2.15.0

一、前期工作

1.设置GPU(如果使用的是CPU可以忽略这步)

from tensorflow import keras
from keras import layers, models
import os, PIL, pathlib
import matplotlib.pyplot as plt
import tensorflow as tf

gpus = tf.config.list_physical_devices("GPU")

if gpus:
    gpu0 = gpus[0] #如果有多个GPU,仅使用第0个GPU
    tf.config.experimental.set_memory_growth(gpu0, True) #设置GPU显存用量按需使用
    tf.config.set_visible_devices([gpu0],"GPU")

2. 导入数据

data_dir = "./data/"
data_dir = pathlib.Path(data_dir)

3. 查看数据

image_count = len(list(data_dir.glob('*/*.jpg')))
print("图片总数为:",image_count)

输出:图片总数为: 2142

打开一张图片:

Monkeypox = list(data_dir.glob('Monkeypox/*.jpg'))
PIL.Image.open(str(Monkeypox[1]))

第T4周:使用TensorFlow实现猴痘病识别_第1张图片

二、数据预处理

1、加载数据

使用image_dataset_from_directory方法将磁盘中的数据加载到tf.data.Dataset中。

测试集与验证集的关系:

  1. 验证集并没有参与训练过程梯度下降过程的,狭义上来讲是没有参与模型的参数训练更新的。
  2. 但是广义上来讲,验证集存在的意义确实参与了一个“人工调参”的过程,我们根据每一个epoch训练之后模型在valid data上的表现来决定是否需要训练进行early stop,或者根据这个过程模型的性能变化来调整模型的超参数,如学习率,batch_size等等。
  3. 因此,我们也可以认为,验证集也参与了训练,但是并没有使得模型去overfit验证集。
batch_size = 32
img_height = 224
img_width = 224

"""
关于image_dataset_from_directory()的详细介绍可以参考文章: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)

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)

我们可以通过class_names输出数据集的标签。标签将按字母顺序对应于目录名称。

class_names = train_ds.class_names
print(class_names)

输出:

[‘Monkeypox’, ‘Others’]

2、数据可视化

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

3、再次检查数据

for image_batch, labels_batch in train_ds:
    print(image_batch.shape)
    print(labels_batch.shape)
    break

输出:

(32, 224, 224, 3)
(32,)

4、配置数据集

AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)

三、构建CNN网络

num_classes = 2

"""
关于卷积核的计算不懂的可以参考文章:https://blog.csdn.net/qq_38251616/article/details/114278995

layers.Dropout(0.4) 作用是防止过拟合,提高模型的泛化能力。
在上一篇文章花朵识别中,训练准确率与验证准确率相差巨大就是由于模型过拟合导致的

关于Dropout层的更多介绍可以参考文章: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)), # 卷积层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.Dropout(0.3),  
    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()  # 打印网络结构

四、编译

# 设置优化器
opt = tf.keras.optimizers.Adam(learning_rate=1e-4)
model.compile(optimizer=opt,
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

五、训练模型

关于ModelCheckpoint的详细介绍可参考文章ModelCheckpoint 讲解【TensorFlow2入门手册】

from tensorflow.keras.callbacks import ModelCheckpoint

epochs = 50

checkpointer = ModelCheckpoint('best_model.h5',  # 保存最好模型的路径
                                monitor='val_accuracy',  # 需要监视的指标
                                verbose=1,  # 信息展示模式,0/1
                                save_best_only=True,  # 当设置为True时,监测指标有改进时才会保存当前的模型
                                save_weights_only=True)  # 当设置为True时,则只保存模型权重,否则将保存整个模型(模型结构,配置信息等)

history = model.fit(train_ds,
                    validation_data=val_ds,
                    epochs=epochs,
                    callbacks=[checkpointer])

输出:

Epoch 1/50
53/54 [============================>.] - ETA: 0s - loss: 0.7034 - accuracy: 0.5612
Epoch 1: val_accuracy improved from -inf to 0.58879, saving model to /content/drive/MyDrive/app/T4/best_model.h5
54/54 [==============================] - 280s 976ms/step - loss: 0.7029 - accuracy: 0.5607 - val_loss: 0.6588 - val_accuracy: 0.5888
.................................
Epoch 47/50
53/54 [============================>.] - ETA: 0s - loss: 0.0545 - accuracy: 0.9875
Epoch 47: val_accuracy did not improve from 0.88318
54/54 [==============================] - 2s 45ms/step - loss: 0.0549 - accuracy: 0.9872 - val_loss: 0.4760 - val_accuracy: 0.8762
Epoch 48/50
53/54 [============================>.] - ETA: 0s - loss: 0.0525 - accuracy: 0.9857
Epoch 48: val_accuracy did not improve from 0.88318
54/54 [==============================] - 3s 48ms/step - loss: 0.0526 - accuracy: 0.9860 - val_loss: 0.4829 - val_accuracy: 0.8808
Epoch 49/50
54/54 [==============================] - ETA: 0s - loss: 0.0606 - accuracy: 0.9802
Epoch 49: val_accuracy improved from 0.88318 to 0.88551, saving model to /content/drive/MyDrive/app/T4/best_model.h5
54/54 [==============================] - 4s 76ms/step - loss: 0.0606 - accuracy: 0.9802 - val_loss: 0.5093 - val_accuracy: 0.8855
Epoch 50/50
53/54 [============================>.] - ETA: 0s - loss: 0.0614 - accuracy: 0.9786
Epoch 50: val_accuracy did not improve from 0.88551
54/54 [==============================] - 3s 51ms/step - loss: 0.0615 - accuracy: 0.9784 - val_loss: 0.4773 - val_accuracy: 0.8762

六、模型评估

1. Loss与Accuracy图

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

第T4周:使用TensorFlow实现猴痘病识别_第2张图片

2.指定图片进行预测

# 加载效果最好的模型权重
model.load_weights('best_model.h5')
from PIL import Image
import numpy as np

# img = Image.open("./45-data/Monkeypox/M06_01_04.jpg")  #这里选择你需要预测的图片
img = Image.open("./45-data/Others/NM01_01_00.jpg")  #这里选择你需要预测的图片
image = tf.image.resize(img, [img_height, img_width])

img_array = tf.expand_dims(image, 0) 

predictions = model.predict(img_array) # 这里选用你已经训练好的模型
print("预测结果为:",class_names[np.argmax(predictions)])

输出:

1/1 [==============================] - 0s 19ms/step
预测结果为: Others

预测正确。

七、优化

1、使用model.evaluate使用测试集评估模型

test_loss, test_acc = model.evaluate(val_ds, verbose=1)  # verbose=0不显示进度条,1显示进度条
print('Test loss:', test_loss)
print('Test accuracy:', test_acc)

输出:

14/14 [==============================] - 0s 16ms/step - loss: 0.5093 - accuracy: 0.8855
Test loss: 0.5093046426773071
Test accuracy: 0.8855140209197998

在上边Loss与Accuracy图中可以看出,模型存在过拟合的问题,出现过拟合的问题解决办法有减小网络的大小、添加正则化项、添加dropout层等。

2、网络结构优化

经过多次训练和优化,最终效果最好的网络结构如下:包括5个卷积层,5个池化层,1个dropout层。

num_classes = 2

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
    layers.AveragePooling2D((2, 2)),  # 池化层1
    layers.Conv2D(32, (3, 3), activation='relu'),  # 卷积层2
    layers.AveragePooling2D((2, 2)),  # 池化层2
    layers.Conv2D(32, (3, 3), activation='relu'),  # 卷积层3
    layers.AveragePooling2D((2, 2)),  # 池化层3
    layers.Conv2D(32, (3, 3), activation='relu'),  # 卷积层4
    layers.AveragePooling2D((2, 2)),  # 池化层4
    layers.Conv2D(64, (3, 3), activation='relu'),  # 卷积层5
    layers.AveragePooling2D((2, 2)),  # 池化层5
    layers.Dropout(0.4),

    layers.Flatten(),
    layers.Dense(128, activation='relu'),
    layers.Dense(num_classes)
])

训练输出结果:

Epoch 1/50
54/54 [==============================] - ETA: 0s - loss: 0.6823 - accuracy: 0.5443
Epoch 1: val_accuracy improved from -inf to 0.56308, saving model to /content/drive/MyDrive/app/T4/best_model2.h5
54/54 [==============================] - 5s 52ms/step - loss: 0.6823 - accuracy: 0.5443 - val_loss: 0.6771 - val_accuracy: 0.5631
。。。。。。。。。。。。。。。。。。。。。。。。。。。。
Epoch 47/50
53/54 [============================>.] - ETA: 0s - loss: 0.3012 - accuracy: 0.8757
Epoch 47: val_accuracy improved from 0.85981 to 0.87383, saving model to /content/drive/MyDrive/app/T4/best_model2.h5
54/54 [==============================] - 3s 52ms/step - loss: 0.3042 - accuracy: 0.8734 - val_loss: 0.3395 - val_accuracy: 0.8738
Epoch 48/50
53/54 [============================>.] - ETA: 0s - loss: 0.3027 - accuracy: 0.8829
Epoch 48: val_accuracy did not improve from 0.87383
54/54 [==============================] - 2s 38ms/step - loss: 0.3056 - accuracy: 0.8810 - val_loss: 0.3355 - val_accuracy: 0.8738
Epoch 49/50
53/54 [============================>.] - ETA: 0s - loss: 0.3036 - accuracy: 0.8805
Epoch 49: val_accuracy did not improve from 0.87383
54/54 [==============================] - 2s 38ms/step - loss: 0.3021 - accuracy: 0.8816 - val_loss: 0.3387 - val_accuracy: 0.8621
Epoch 50/50
53/54 [============================>.] - ETA: 0s - loss: 0.2950 - accuracy: 0.8912
Epoch 50: val_accuracy improved from 0.87383 to 0.89019, saving model to /content/drive/MyDrive/app/T4/best_model2.h5
54/54 [==============================] - 2s 39ms/step - loss: 0.2945 - accuracy: 0.8909 - val_loss: 0.3172 - val_accuracy: 0.8902

3、 Loss与Accuracy图

第T4周:使用TensorFlow实现猴痘病识别_第3张图片
从图中可以看出模型过拟合的影响大大减小,我没有在网络中增加正则化项,模型还有提升的空间。

4、使用model.evaluate评估优化后的模型

test_loss, test_acc = model.evaluate(val_ds, verbose=1)  # verbose=0不显示进度条,1显示进度条
print('Test loss:', test_loss)
print('Test accuracy:', test_acc)

输出:

14/14 [==============================] - 0s 13ms/step - loss: 0.3172 - accuracy: 0.8902
Test loss: 0.31724825501441956
Test accuracy: 0.8901869058609009

测试集的loss大大减小,且acc提高了。

八、总结

对于神经网络学习图片特征过程中,过拟合现象很容易发生,耐心调参即可对模型进行优化。

你可能感兴趣的:(tensorflow,人工智能,python)