所用环境
● 语言环境:Python3.11
● 编译器:jupyter notebook
● 深度学习框架:TensorFlow2.16.1
● 显卡(GPU):NVIDIA GeForce RTX 2070
设置GPU
from tensorflow import keras
from tensorflow.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")
gpus
[]
导入数据
data_dir = r"C:\Users\11054\Desktop\kLearning\p4_learning\data"
data_dir = pathlib.Path(data_dir)
查看数据
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]))
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 = 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,)
配置数据集
# def mean_std_normalize(image):
# return image / 255
#
# train_ds = train_ds.map(lambda x, y: (mean_std_normalize(x), y))
# val_ds = val_ds.map(lambda x, y: (mean_std_normalize(x), y))
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.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.4),
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() # 打印网络结构
Model: "sequential_13"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩ │ rescaling_2 (Rescaling) │ (None, 224, 224, 3) │ 0 │ ├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤ │ conv2d_39 (Conv2D) │ (None, 222, 222, 16) │ 448 │ ├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤ │ average_pooling2d_26 │ (None, 111, 111, 16) │ 0 │ │ (AveragePooling2D) │ │ │ ├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤ │ conv2d_40 (Conv2D) │ (None, 109, 109, 32) │ 4,640 │ ├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤ │ average_pooling2d_27 │ (None, 54, 54, 32) │ 0 │ │ (AveragePooling2D) │ │ │ ├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤ │ dropout_26 (Dropout) │ (None, 54, 54, 32) │ 0 │ ├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤ │ conv2d_41 (Conv2D) │ (None, 52, 52, 64) │ 18,496 │ ├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤ │ dropout_27 (Dropout) │ (None, 52, 52, 64) │ 0 │ ├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤ │ flatten_13 (Flatten) │ (None, 173056) │ 0 │ ├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤ │ dense_26 (Dense) │ (None, 128) │ 22,151,296 │ ├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤ │ dense_27 (Dense) │ (None, 2) │ 258 │ └──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
Total params: 22,175,138 (84.59 MB)
Trainable params: 22,175,138 (84.59 MB)
Non-trainable params: 0 (0.00 B)
编译
# 设置优化器
opt = tf.keras.optimizers.Adam(learning_rate=1e-4)
model.compile(optimizer=opt,
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
训练模型
from tensorflow.keras.callbacks import ModelCheckpoint
epochs = 50
checkpoint = ModelCheckpoint(
filepath='best_model.weights.h5', # Change to .weights.h5
save_weights_only=True,
monitor='val_loss',
mode='min',
save_best_only=True
)
history = model.fit(train_ds,
validation_data=val_ds,
epochs=epochs,
callbacks=[checkpoint])
Epoch 1/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m16s[0m 257ms/step - accuracy: 0.5094 - loss: 0.7558 - val_accuracy: 0.5350 - val_loss: 0.6751
Epoch 2/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 262ms/step - accuracy: 0.5925 - loss: 0.6632 - val_accuracy: 0.6005 - val_loss: 0.6564
Epoch 3/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 255ms/step - accuracy: 0.6289 - loss: 0.6556 - val_accuracy: 0.6308 - val_loss: 0.6436
Epoch 4/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m15s[0m 279ms/step - accuracy: 0.6565 - loss: 0.6333 - val_accuracy: 0.6402 - val_loss: 0.6487
Epoch 5/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 251ms/step - accuracy: 0.6738 - loss: 0.6020 - val_accuracy: 0.6963 - val_loss: 0.5978
Epoch 6/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 253ms/step - accuracy: 0.6961 - loss: 0.5812 - val_accuracy: 0.6659 - val_loss: 0.6477
Epoch 7/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m13s[0m 243ms/step - accuracy: 0.7291 - loss: 0.5505 - val_accuracy: 0.6752 - val_loss: 0.6096
Epoch 8/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m13s[0m 248ms/step - accuracy: 0.7211 - loss: 0.5350 - val_accuracy: 0.7196 - val_loss: 0.5285
Epoch 9/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m13s[0m 247ms/step - accuracy: 0.7731 - loss: 0.4832 - val_accuracy: 0.7243 - val_loss: 0.5279
Epoch 10/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m13s[0m 250ms/step - accuracy: 0.7680 - loss: 0.4829 - val_accuracy: 0.7383 - val_loss: 0.4957
Epoch 11/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m13s[0m 240ms/step - accuracy: 0.7907 - loss: 0.4464 - val_accuracy: 0.7336 - val_loss: 0.4979
Epoch 12/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m13s[0m 246ms/step - accuracy: 0.8025 - loss: 0.4156 - val_accuracy: 0.7500 - val_loss: 0.4833
Epoch 13/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m10s[0m 175ms/step - accuracy: 0.8184 - loss: 0.4268 - val_accuracy: 0.7944 - val_loss: 0.4716
Epoch 14/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m7s[0m 128ms/step - accuracy: 0.8452 - loss: 0.3810 - val_accuracy: 0.7991 - val_loss: 0.4530
Epoch 15/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m7s[0m 127ms/step - accuracy: 0.8464 - loss: 0.3660 - val_accuracy: 0.7827 - val_loss: 0.4764
Epoch 16/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m10s[0m 181ms/step - accuracy: 0.8320 - loss: 0.3806 - val_accuracy: 0.7967 - val_loss: 0.4451
Epoch 17/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 255ms/step - accuracy: 0.8550 - loss: 0.3492 - val_accuracy: 0.7897 - val_loss: 0.4656
Epoch 18/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 250ms/step - accuracy: 0.8770 - loss: 0.3161 - val_accuracy: 0.7477 - val_loss: 0.4867
Epoch 19/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 268ms/step - accuracy: 0.8535 - loss: 0.3309 - val_accuracy: 0.8154 - val_loss: 0.4552
Epoch 20/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 266ms/step - accuracy: 0.8941 - loss: 0.2848 - val_accuracy: 0.7967 - val_loss: 0.4495
Epoch 21/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 256ms/step - accuracy: 0.8743 - loss: 0.2957 - val_accuracy: 0.8131 - val_loss: 0.4250
Epoch 22/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m13s[0m 245ms/step - accuracy: 0.8794 - loss: 0.2941 - val_accuracy: 0.8201 - val_loss: 0.4460
Epoch 23/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 252ms/step - accuracy: 0.8551 - loss: 0.3300 - val_accuracy: 0.8294 - val_loss: 0.4210
Epoch 24/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 251ms/step - accuracy: 0.8998 - loss: 0.2713 - val_accuracy: 0.8131 - val_loss: 0.4808
Epoch 25/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m13s[0m 246ms/step - accuracy: 0.8802 - loss: 0.2752 - val_accuracy: 0.7897 - val_loss: 0.5133
Epoch 26/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 253ms/step - accuracy: 0.8714 - loss: 0.2991 - val_accuracy: 0.8481 - val_loss: 0.4189
Epoch 27/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m13s[0m 248ms/step - accuracy: 0.9051 - loss: 0.2461 - val_accuracy: 0.8435 - val_loss: 0.4028
Epoch 28/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m13s[0m 242ms/step - accuracy: 0.8978 - loss: 0.2519 - val_accuracy: 0.8411 - val_loss: 0.4060
Epoch 29/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m13s[0m 242ms/step - accuracy: 0.9127 - loss: 0.2319 - val_accuracy: 0.8294 - val_loss: 0.4254
Epoch 30/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m13s[0m 246ms/step - accuracy: 0.9162 - loss: 0.2175 - val_accuracy: 0.8575 - val_loss: 0.4212
Epoch 31/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 255ms/step - accuracy: 0.9306 - loss: 0.1994 - val_accuracy: 0.8435 - val_loss: 0.4504
Epoch 32/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m13s[0m 246ms/step - accuracy: 0.9094 - loss: 0.2175 - val_accuracy: 0.8294 - val_loss: 0.4103
Epoch 33/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 252ms/step - accuracy: 0.9161 - loss: 0.1994 - val_accuracy: 0.8481 - val_loss: 0.3999
Epoch 34/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m13s[0m 249ms/step - accuracy: 0.9201 - loss: 0.1888 - val_accuracy: 0.8341 - val_loss: 0.4599
Epoch 35/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 250ms/step - accuracy: 0.9113 - loss: 0.2096 - val_accuracy: 0.8178 - val_loss: 0.4632
Epoch 36/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 251ms/step - accuracy: 0.9378 - loss: 0.1745 - val_accuracy: 0.8551 - val_loss: 0.4268
Epoch 37/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 255ms/step - accuracy: 0.9438 - loss: 0.1538 - val_accuracy: 0.8575 - val_loss: 0.4274
Epoch 38/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 253ms/step - accuracy: 0.9433 - loss: 0.1420 - val_accuracy: 0.8364 - val_loss: 0.4363
Epoch 39/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 252ms/step - accuracy: 0.9325 - loss: 0.1676 - val_accuracy: 0.8458 - val_loss: 0.4268
Epoch 40/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 251ms/step - accuracy: 0.9487 - loss: 0.1396 - val_accuracy: 0.8458 - val_loss: 0.4373
Epoch 41/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 252ms/step - accuracy: 0.9435 - loss: 0.1709 - val_accuracy: 0.8481 - val_loss: 0.4572
Epoch 42/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m13s[0m 249ms/step - accuracy: 0.9519 - loss: 0.1419 - val_accuracy: 0.8435 - val_loss: 0.4637
Epoch 43/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 256ms/step - accuracy: 0.9304 - loss: 0.1656 - val_accuracy: 0.8248 - val_loss: 0.5690
Epoch 44/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 252ms/step - accuracy: 0.9233 - loss: 0.2013 - val_accuracy: 0.8551 - val_loss: 0.4235
Epoch 45/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 252ms/step - accuracy: 0.9634 - loss: 0.1338 - val_accuracy: 0.8481 - val_loss: 0.4394
Epoch 46/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 251ms/step - accuracy: 0.9442 - loss: 0.1380 - val_accuracy: 0.8458 - val_loss: 0.4698
Epoch 47/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 254ms/step - accuracy: 0.9368 - loss: 0.1555 - val_accuracy: 0.8458 - val_loss: 0.4358
Epoch 48/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 253ms/step - accuracy: 0.9529 - loss: 0.1199 - val_accuracy: 0.8505 - val_loss: 0.4860
Epoch 49/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 251ms/step - accuracy: 0.9416 - loss: 0.1373 - val_accuracy: 0.8528 - val_loss: 0.4813
Epoch 50/50
[1m54/54[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m14s[0m 255ms/step - accuracy: 0.9595 - loss: 0.1228 - val_accuracy: 0.8621 - val_loss: 0.4528
模型评估
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.weights.h5')
from PIL import Image
import numpy as np
img = Image.open(r"C:\Users\11054\Desktop\kLearning\p4_learning\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)])
[1m1/1[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 86ms/step
预测结果为: Others
个人总结
使用了新版本的tensorflow,layers.Rescaling(1./255, input_shape=(img_height, img_width, 3))方法与旧版本调用有所不同,尝试了将归一化注释,结果显示收敛精度显著降低