本文主要是介绍第T4周:猴痘病识别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 本文为🔗365天深度学习训练营 中的学习记录博客
- 原作者:K同学啊
我的环境:
● 语言环境:Python3.6.5
● 编译器:jupyter notebook
● 深度学习框架:TensorFlow 2.6.2
● 数据:猴痘病数据集
一、前期工作
- 设置GPU(如果使用的是CPU可以忽略这步)
from tensorflow import keras
from tensorflow.keras import layers,models
import os, PIL, pathlib
import matplotlib.pyplot as plt
import tensorflow as tfgpus = tf.config.list_physical_devices("GPU")if gpus:gpu0 = gpus[0] #如果有多个GPU,仅使用第0个GPUtf.config.experimental.set_memory_growth(gpu0, True) #设置GPU显存用量按需使用tf.config.set_visible_devices([gpu0],"GPU")gpus
代码输出(因为我的电脑只有CPU,所以是空列表):
[]
- 导入数据
data_dir = "./T4/"data_dir = pathlib.Path(data_dir)
data_dir
代码输出:
WindowsPath('T4')
- 查看数据
image_count = len(list(data_dir.glob('*/*.jpg')))print("图片总数为:",image_count)
代码输出:
图片总数为: 2142
Monkeypox = list(data_dir.glob('Monkeypox/*.jpg'))
PIL.Image.open(str(Monkeypox[0]))
代码输出:
二、数据预处理
- 加载数据
使用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)
代码输出:
Found 2142 files belonging to 2 classes.
Using 1714 files for training.
"""
关于image_dataset_from_directory()的详细介绍可以参考文章: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)
代码输出:
Found 2142 files belonging to 2 classes.
Using 428 files for validation.
我们可以通过class_names输出数据集的标签。标签将按字母顺序对应于目录名称。
class_names = train_ds.class_names
print(class_names)
代码输出:
['Monkeypox', 'Others']
- 可视化数据
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")
代码输出:
- 再次检查数据
for image_batch, labels_batch in train_ds:print(image_batch.shape)print(labels_batch.shape)break
代码输出:
(32, 224, 224, 3)
(32,)
● Image_batch是形状的张量(32,224,224,3)。这是一批形状224x224x3的32张图片(最后一维指的是彩色通道RGB)。
● Label_batch是形状(32,)的张量,这些标签对应32张图片
- 配置数据集
● shuffle() :打乱数据,关于此函数的详细介绍可以参考:https://zhuanlan.zhihu.com/p/42417456
● prefetch() :预取数据,加速运行
prefetch()功能详细介绍:CPU 正在准备数据时,加速器处于空闲状态。相反,当加速器正在训练模型时,CPU 处于空闲状态。因此,训练所用的时间是 CPU 预处理时间和加速器训练时间的总和。prefetch()将训练步骤的预处理和模型执行过程重叠到一起。当加速器正在执行第 N 个训练步时,CPU 正在准备第 N+1 步的数据。这样做不仅可以最大限度地缩短训练的单步用时(而不是总用时),而且可以缩短提取和转换数据所需的时间。如果不使用prefetch(),CPU 和 GPU/TPU 在大部分时间都处于空闲状态:
使用prefetch()可显著减少空闲时间:
● cache() :将数据集缓存到内存当中,加速运行
AUTOTUNE = tf.data.AUTOTUNEtrain_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
三、构建CNN网络
卷积神经网络(CNN)的输入是张量 (Tensor) 形式的 (image_height, image_width, color_channels),包含了图像高度、宽度及颜色信息。不需要输入batch size。color_channels 为 (R,G,B) 分别对应 RGB 的三个颜色通道(color channel)。在此示例中,我们的 CNN 输入的形状是 (224, 224, 3)即彩色图像。我们需要在声明第一层时将形状赋值给参数input_shape。
网络结构图:
num_classes = 2"""
关于卷积核的计算不懂的可以参考文章:https://blog.csdn.net/qq_38251616/article/details/114278995layers.Dropout(0.3) 作用是防止过拟合,提高模型的泛化能力。
在上一篇文章花朵识别中,训练准确率与验证准确率相差巨大就是由于模型过拟合导致的关于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*3layers.AveragePooling2D((2, 2)), # 池化层2,2*2采样layers.Dropout(0.3), layers.Conv2D(64, (3, 3), activation='relu'), # 卷积层3,卷积核3*3layers.Dropout(0.3), layers.Flatten(), # Flatten层,连接卷积层与全连接层layers.Dense(128, activation='relu'), # 全连接层,特征进一步提取layers.Dense(num_classes) # 输出层,输出预期结果
])model.summary() # 打印网络结构
代码输出:
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
rescaling_1 (Rescaling) (None, 224, 224, 3) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 222, 222, 16) 448
_________________________________________________________________
average_pooling2d_2 (Average (None, 111, 111, 16) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 109, 109, 32) 4640
_________________________________________________________________
average_pooling2d_3 (Average (None, 54, 54, 32) 0
_________________________________________________________________
dropout_2 (Dropout) (None, 54, 54, 32) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 52, 52, 64) 18496
_________________________________________________________________
dropout_3 (Dropout) (None, 52, 52, 64) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 173056) 0
_________________________________________________________________
dense_2 (Dense) (None, 128) 22151296
_________________________________________________________________
dense_3 (Dense) (None, 2) 258
=================================================================
Total params: 22,175,138
Trainable params: 22,175,138
Non-trainable params: 0
_________________________________________________________________
四、编译
在准备对模型进行训练之前,还需要再对其进行一些设置。以下内容是在模型的编译步骤中添加的:
●损失函数(loss):用于衡量模型在训练期间的准确率。
●优化器(optimizer):决定模型如何根据其看到的数据和自身的损失函数进行更新。
●指标(metrics):用于监控训练和测试步骤。以下示例使用了准确率,即被正确分类的图像的比率。
# 设置优化器
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 ModelCheckpointepochs = 50checkpointer = ModelCheckpoint('best_model.h5',monitor='val_accuracy',verbose=1,save_best_only=True,save_weights_only=True)history = model.fit(train_ds,validation_data=val_ds,epochs=epochs,callbacks=[checkpointer])
代码输出:
Epoch 1/50
54/54 [==============================] - 68s 1s/step - loss: 0.7046 - accuracy: 0.5449 - val_loss: 0.6762 - val_accuracy: 0.5374Epoch 00001: val_accuracy improved from -inf to 0.53738, saving model to best_model.h5
Epoch 2/50
54/54 [==============================] - 63s 1s/step - loss: 0.6802 - accuracy: 0.5782 - val_loss: 0.6697 - val_accuracy: 0.5794Epoch 00002: val_accuracy improved from 0.53738 to 0.57944, saving model to best_model.h5
Epoch 3/50
54/54 [==============================] - 64s 1s/step - loss: 0.6485 - accuracy: 0.6435 - val_loss: 0.6585 - val_accuracy: 0.6098Epoch 00003: val_accuracy improved from 0.57944 to 0.60981, saving model to best_model.h5
Epoch 4/50
54/54 [==============================] - 63s 1s/step - loss: 0.6045 - accuracy: 0.6809 - val_loss: 0.6459 - val_accuracy: 0.6519Epoch 00004: val_accuracy improved from 0.60981 to 0.65187, saving model to best_model.h5
Epoch 5/50
54/54 [==============================] - 63s 1s/step - loss: 0.5782 - accuracy: 0.7095 - val_loss: 0.5627 - val_accuracy: 0.7079Epoch 00005: val_accuracy improved from 0.65187 to 0.70794, saving model to best_model.h5
Epoch 6/50
54/54 [==============================] - 63s 1s/step - loss: 0.5464 - accuracy: 0.7211 - val_loss: 0.5211 - val_accuracy: 0.7593Epoch 00006: val_accuracy improved from 0.70794 to 0.75935, saving model to best_model.h5
Epoch 7/50
54/54 [==============================] - 63s 1s/step - loss: 0.5134 - accuracy: 0.7585 - val_loss: 0.4807 - val_accuracy: 0.7827Epoch 00007: val_accuracy improved from 0.75935 to 0.78271, saving model to best_model.h5
Epoch 8/50
54/54 [==============================] - 63s 1s/step - loss: 0.4629 - accuracy: 0.7900 - val_loss: 0.4564 - val_accuracy: 0.7874Epoch 00008: val_accuracy improved from 0.78271 to 0.78738, saving model to best_model.h5
Epoch 9/50
54/54 [==============================] - 64s 1s/step - loss: 0.4241 - accuracy: 0.8209 - val_loss: 0.4451 - val_accuracy: 0.7804Epoch 00009: val_accuracy did not improve from 0.78738
Epoch 10/50
54/54 [==============================] - 63s 1s/step - loss: 0.3987 - accuracy: 0.8419 - val_loss: 0.4442 - val_accuracy: 0.7921Epoch 00010: val_accuracy improved from 0.78738 to 0.79206, saving model to best_model.h5
Epoch 11/50
54/54 [==============================] - 63s 1s/step - loss: 0.3913 - accuracy: 0.8291 - val_loss: 0.4493 - val_accuracy: 0.8014Epoch 00011: val_accuracy improved from 0.79206 to 0.80140, saving model to best_model.h5
Epoch 12/50
54/54 [==============================] - 63s 1s/step - loss: 0.3790 - accuracy: 0.8466 - val_loss: 0.4411 - val_accuracy: 0.7780Epoch 00012: val_accuracy did not improve from 0.80140
Epoch 13/50
54/54 [==============================] - 64s 1s/step - loss: 0.3843 - accuracy: 0.8454 - val_loss: 0.4123 - val_accuracy: 0.8037Epoch 00013: val_accuracy improved from 0.80140 to 0.80374, saving model to best_model.h5
Epoch 14/50
54/54 [==============================] - 63s 1s/step - loss: 0.3490 - accuracy: 0.8582 - val_loss: 0.4135 - val_accuracy: 0.8107Epoch 00014: val_accuracy improved from 0.80374 to 0.81075, saving model to best_model.h5
Epoch 15/50
54/54 [==============================] - 63s 1s/step - loss: 0.3222 - accuracy: 0.8781 - val_loss: 0.4358 - val_accuracy: 0.7921Epoch 00015: val_accuracy did not improve from 0.81075
Epoch 16/50
54/54 [==============================] - 63s 1s/step - loss: 0.3016 - accuracy: 0.8821 - val_loss: 0.4079 - val_accuracy: 0.8271Epoch 00016: val_accuracy improved from 0.81075 to 0.82710, saving model to best_model.h5
Epoch 17/50
54/54 [==============================] - 63s 1s/step - loss: 0.3023 - accuracy: 0.8757 - val_loss: 0.3708 - val_accuracy: 0.8388Epoch 00017: val_accuracy improved from 0.82710 to 0.83879, saving model to best_model.h5
Epoch 18/50
54/54 [==============================] - 63s 1s/step - loss: 0.2785 - accuracy: 0.8961 - val_loss: 0.3703 - val_accuracy: 0.8411Epoch 00018: val_accuracy improved from 0.83879 to 0.84112, saving model to best_model.h5
Epoch 19/50
54/54 [==============================] - 63s 1s/step - loss: 0.2691 - accuracy: 0.8932 - val_loss: 0.3782 - val_accuracy: 0.8575Epoch 00019: val_accuracy improved from 0.84112 to 0.85748, saving model to best_model.h5
Epoch 20/50
54/54 [==============================] - 63s 1s/step - loss: 0.2743 - accuracy: 0.8921 - val_loss: 0.3647 - val_accuracy: 0.8481Epoch 00020: val_accuracy did not improve from 0.85748
Epoch 21/50
54/54 [==============================] - 63s 1s/step - loss: 0.2436 - accuracy: 0.9090 - val_loss: 0.3560 - val_accuracy: 0.8528Epoch 00021: val_accuracy did not improve from 0.85748
Epoch 22/50
54/54 [==============================] - 68s 1s/step - loss: 0.2638 - accuracy: 0.8950 - val_loss: 0.3697 - val_accuracy: 0.8435Epoch 00022: val_accuracy did not improve from 0.85748
Epoch 23/50
54/54 [==============================] - 67s 1s/step - loss: 0.2157 - accuracy: 0.9224 - val_loss: 0.3609 - val_accuracy: 0.8458Epoch 00023: val_accuracy did not improve from 0.85748
Epoch 24/50
54/54 [==============================] - 63s 1s/step - loss: 0.2106 - accuracy: 0.9247 - val_loss: 0.3516 - val_accuracy: 0.8621Epoch 00024: val_accuracy improved from 0.85748 to 0.86215, saving model to best_model.h5
Epoch 25/50
54/54 [==============================] - 63s 1s/step - loss: 0.2198 - accuracy: 0.9201 - val_loss: 0.3610 - val_accuracy: 0.8551Epoch 00025: val_accuracy did not improve from 0.86215
Epoch 26/50
54/54 [==============================] - 63s 1s/step - loss: 0.2152 - accuracy: 0.9137 - val_loss: 0.4469 - val_accuracy: 0.8341Epoch 00026: val_accuracy did not improve from 0.86215
Epoch 27/50
54/54 [==============================] - 64s 1s/step - loss: 0.2041 - accuracy: 0.9282 - val_loss: 0.4422 - val_accuracy: 0.8388Epoch 00027: val_accuracy did not improve from 0.86215
Epoch 28/50
54/54 [==============================] - 63s 1s/step - loss: 0.1937 - accuracy: 0.9259 - val_loss: 0.3736 - val_accuracy: 0.8505Epoch 00028: val_accuracy did not improve from 0.86215
Epoch 29/50
54/54 [==============================] - 63s 1s/step - loss: 0.1645 - accuracy: 0.9382 - val_loss: 0.3482 - val_accuracy: 0.8715Epoch 00029: val_accuracy improved from 0.86215 to 0.87150, saving model to best_model.h5
Epoch 30/50
54/54 [==============================] - 63s 1s/step - loss: 0.1580 - accuracy: 0.9463 - val_loss: 0.3899 - val_accuracy: 0.8598Epoch 00030: val_accuracy did not improve from 0.87150
Epoch 31/50
54/54 [==============================] - 63s 1s/step - loss: 0.1537 - accuracy: 0.9469 - val_loss: 0.3556 - val_accuracy: 0.8715Epoch 00031: val_accuracy did not improve from 0.87150
Epoch 32/50
54/54 [==============================] - 63s 1s/step - loss: 0.1418 - accuracy: 0.9539 - val_loss: 0.4686 - val_accuracy: 0.8388Epoch 00032: val_accuracy did not improve from 0.87150
Epoch 33/50
54/54 [==============================] - 63s 1s/step - loss: 0.1386 - accuracy: 0.9568 - val_loss: 0.3477 - val_accuracy: 0.8645Epoch 00033: val_accuracy did not improve from 0.87150
Epoch 34/50
54/54 [==============================] - 63s 1s/step - loss: 0.1232 - accuracy: 0.9597 - val_loss: 0.3638 - val_accuracy: 0.8645Epoch 00034: val_accuracy did not improve from 0.87150
Epoch 35/50
54/54 [==============================] - 64s 1s/step - loss: 0.1066 - accuracy: 0.9632 - val_loss: 0.3803 - val_accuracy: 0.8598Epoch 00035: val_accuracy did not improve from 0.87150
Epoch 36/50
54/54 [==============================] - 64s 1s/step - loss: 0.1106 - accuracy: 0.9656 - val_loss: 0.3922 - val_accuracy: 0.8645Epoch 00036: val_accuracy did not improve from 0.87150
Epoch 37/50
54/54 [==============================] - 63s 1s/step - loss: 0.1129 - accuracy: 0.9632 - val_loss: 0.3628 - val_accuracy: 0.8645Epoch 00037: val_accuracy did not improve from 0.87150
Epoch 38/50
54/54 [==============================] - 63s 1s/step - loss: 0.1123 - accuracy: 0.9621 - val_loss: 0.3786 - val_accuracy: 0.8715Epoch 00038: val_accuracy did not improve from 0.87150
Epoch 39/50
54/54 [==============================] - 63s 1s/step - loss: 0.0975 - accuracy: 0.9685 - val_loss: 0.3694 - val_accuracy: 0.8738Epoch 00039: val_accuracy improved from 0.87150 to 0.87383, saving model to best_model.h5
Epoch 40/50
54/54 [==============================] - 63s 1s/step - loss: 0.0957 - accuracy: 0.9679 - val_loss: 0.3869 - val_accuracy: 0.8668Epoch 00040: val_accuracy did not improve from 0.87383
Epoch 41/50
54/54 [==============================] - 64s 1s/step - loss: 0.0875 - accuracy: 0.9732 - val_loss: 0.3858 - val_accuracy: 0.8762Epoch 00041: val_accuracy improved from 0.87383 to 0.87617, saving model to best_model.h5
Epoch 42/50
54/54 [==============================] - 63s 1s/step - loss: 0.0892 - accuracy: 0.9749 - val_loss: 0.3837 - val_accuracy: 0.8762Epoch 00042: val_accuracy did not improve from 0.87617
Epoch 43/50
54/54 [==============================] - 64s 1s/step - loss: 0.0866 - accuracy: 0.9697 - val_loss: 0.4139 - val_accuracy: 0.8762Epoch 00043: val_accuracy did not improve from 0.87617
Epoch 44/50
54/54 [==============================] - 63s 1s/step - loss: 0.0735 - accuracy: 0.9802 - val_loss: 0.4439 - val_accuracy: 0.8668Epoch 00044: val_accuracy did not improve from 0.87617
Epoch 45/50
54/54 [==============================] - 63s 1s/step - loss: 0.0818 - accuracy: 0.9767 - val_loss: 0.3982 - val_accuracy: 0.8668Epoch 00045: val_accuracy did not improve from 0.87617
Epoch 46/50
54/54 [==============================] - 63s 1s/step - loss: 0.0853 - accuracy: 0.9708 - val_loss: 0.4230 - val_accuracy: 0.8692Epoch 00046: val_accuracy did not improve from 0.87617
Epoch 47/50
54/54 [==============================] - 63s 1s/step - loss: 0.0647 - accuracy: 0.9813 - val_loss: 0.4467 - val_accuracy: 0.8715Epoch 00047: val_accuracy did not improve from 0.87617
Epoch 48/50
54/54 [==============================] - 64s 1s/step - loss: 0.0737 - accuracy: 0.9749 - val_loss: 0.4538 - val_accuracy: 0.8692Epoch 00048: val_accuracy did not improve from 0.87617
Epoch 49/50
54/54 [==============================] - 63s 1s/step - loss: 0.0637 - accuracy: 0.9796 - val_loss: 0.4283 - val_accuracy: 0.8715Epoch 00049: val_accuracy did not improve from 0.87617
Epoch 50/50
54/54 [==============================] - 63s 1s/step - loss: 0.0735 - accuracy: 0.9802 - val_loss: 0.4505 - val_accuracy: 0.8621Epoch 00050: val_accuracy did not improve from 0.87617
六、模型评估
- 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()
代码输出:
- 指定图片进行预测
# 加载效果最好的模型权重
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("./T4/Others/NM15_02_11.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)])
代码输出:
预测结果为: Others
- 随机指定图片进行预测
import randompre_dir=list(data_dir.glob('*/*.jpg'))file_random=random.choice(pre_dir)
print("需要预测的图片:",file_random)
代码输出:
需要预测的图片: T4\Monkeypox\M21_02_05.jpg
from PIL import Image
import numpy as npimg = Image.open(file_random) #这里选择你需要预测的图片
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)])
代码输出:
预测结果为: Monkeypox
#预测图片展示
PIL.Image.open(str(file_random))
代码输出:
这篇关于第T4周:猴痘病识别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!