第T4周:TensorFlow实现猴痘识别

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

目标
实现猴痘病例图片的准确实别
具体实现

(一)环境

语言环境:Python 3.10
编 译 器: PyCharm
框 架: TensorFlow

(二)具体步骤:

1.使用GPU
# 使用GPU  
gpus = tf.config.list_physical_devices("GPU")  
if gpus:  gpu0 = gpus[0]  tf.config.experimental.set_memory_growth(gpu0, True)  # 设置GPU显存用量按需使用  tf.config.set_visible_devices([gpu0],"GPU")  print(gpus)
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
2.导入猴痘图片数据,查看基本情况
# 查看一下数据的基本情况  
data_dir = "./datasets/mp/"  
data_dir = pathlib.Path(data_dir)  # 转换成Path对象,便于后续访问  
image_count = len(list(data_dir.glob('*/*.jpg')))   # 遍历data_dir下面所有的.jpg图片(包含所有子目录)。  
print("图片总数量为:", image_count)  
MonkeyPox = list(data_dir.glob('MonkeyPox/*.jpg'))  # 遍历data_dir子目录MonkeyPox下所有的.jpg图片  
print("猴痘图片数量为:", len(MonkeyPox))  
# print(MonkeyPox[1])  
img = PIL.Image.open(MonkeyPox[1])    # 查看一张猴痘的图片,看看是什么样子  
img.show()
图片总数量为: 2142
猴痘图片数量为: 980

image.png

3. 数据预处理,加载数据
# 数据预处理,并将数据加载到dataset中  
batch_size = 32  
img_width = 224  
img_height = 224  train_ds = tf.keras.preprocessing.image_dataset_from_directory(  directory=data_dir,     # 数据图片所在的目录,如果下面的labels为inferred则包含子目录  labels="inferred",      # 默认为inferred表示从目录结果中获取labels,如果为None就是没有labels,或者是一个labels的元组/列表。  validation_split=0.2,   # 0-1之间的数,表示为验证保留的数据部分,这里相当于保留20%作为验证数据  subset="training",      # 返回数据的子集,从”training"/"validation"/"both"三个中选择,这里只返回训练数据子集  shuffle=True,           # 打乱数据集,默认是True,就是打乱。如果为False,则按字母数字顺序进行排序。  seed=123,               # 打乱数据的随机种子,不改变这个数字,每次的打乱顺序应该是一样的  image_size=(img_height, img_width),     # 图片重新设置大小,如果不设定,默认是(256, 256)  batch_size=batch_size   # 数据批次大小,默认是32.假如设置为None则不进行批次处理  
)  
Found 2142 files belonging to 2 classes.
Using 1714 files for training.
val_ds = tf.keras.preprocessing.image_dataset_from_directory(  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']
4. 数据可视化
# 数据可视化  
plt.figure(figsize=(20, 5))  
for images, labels in train_ds.take(1):  # 取train_da的1个元素  print(images.shape)     # 因为数据集是按批次的组织的(上面设置了batch_size),因为1个元素也有32张图片  for i in range(20):  ax = plt.subplot(2, 10, i + 1)  # 2行10列,索引号是从1开始的  plt.imshow(images[i].numpy().astype("uint8"))   # 接受一个array  plt.title(class_names[labels[i]])   # 标题  plt.axis("off")     # 关闭中轴线和标签  
plt.show()

image.png

5. 检查数据类型
# 检查数据类型  
for image_batch, labels_batch in train_ds:  print(image_batch.shape)  print(labels_batch.shape)  break
(32, 224, 224, 3)
(32,)
6.配置数据集,加速
# 配置数据库,加速  
AUTOTUNE = tf.data.AUTOTUNE  
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)  
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
7.构建CNN网络模型

image.png

num_classes = len(class_names)
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()  # 打印网络结构
8.编码模型
# 设置优化器
opt = tf.keras.optimizers.Adam(learning_rate=1e-4)model.compile(optimizer=opt,loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])
9.训练模型
# 训练模型  
from tensorflow.keras.callbacks import ModelCheckpoint  
epochs = 50  
checkpointer = ModelCheckpoint('./models/Monkeypox_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]  
)

整个过程如下,第一轮训练都会根据monitor监视的值是否有改进,来判断是否要保存该轮的模型.前面9轮都有改进,都在保存,后面没有改进就没有保存:

Epoch 1/50
2024-10-07 13:20:42.878241: I tensorflow/stream_executor/cuda/cuda_dnn.cc:384] Loaded cuDNN version 8101
2024-10-07 13:20:44.358266: W tensorflow/stream_executor/gpu/redzone_allocator.cc:314] INTERNAL: ptxas exited with non-zero error code -1, output: 
Relying on driver to perform ptx compilation. 
Modify $PATH to customize ptxas location.
This message will be only logged once.
2024-10-07 13:20:45.626487: I tensorflow/stream_executor/cuda/cuda_blas.cc:1614] TensorFloat-32 will be used for the matrix multiplication. This will only be logged once.
52/54 [===========================>..] - ETA: 0s - loss: 0.7343 - accuracy: 0.5200
Epoch 1: val_accuracy improved from -inf to 0.64720, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 8s 36ms/step - loss: 0.7324 - accuracy: 0.5216 - val_loss: 0.6843 - val_accuracy: 0.6472
Epoch 2/50
52/54 [===========================>..] - ETA: 0s - loss: 0.6673 - accuracy: 0.5812
Epoch 2: val_accuracy did not improve from 0.64720
54/54 [==============================] - 1s 26ms/step - loss: 0.6667 - accuracy: 0.5846 - val_loss: 0.6560 - val_accuracy: 0.5911
Epoch 3/50
52/54 [===========================>..] - ETA: 0s - loss: 0.6356 - accuracy: 0.6466
Epoch 3: val_accuracy improved from 0.64720 to 0.67290, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 2s 28ms/step - loss: 0.6373 - accuracy: 0.6435 - val_loss: 0.6179 - val_accuracy: 0.6729
Epoch 4/50
54/54 [==============================] - ETA: 0s - loss: 0.6140 - accuracy: 0.6744
Epoch 4: val_accuracy improved from 0.67290 to 0.69159, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 2s 28ms/step - loss: 0.6140 - accuracy: 0.6744 - val_loss: 0.5860 - val_accuracy: 0.6916
Epoch 5/50
52/54 [===========================>..] - ETA: 0s - loss: 0.5892 - accuracy: 0.6921
Epoch 5: val_accuracy improved from 0.69159 to 0.71729, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 2s 28ms/step - loss: 0.5913 - accuracy: 0.6902 - val_loss: 0.5633 - val_accuracy: 0.7173
Epoch 6/50
52/54 [===========================>..] - ETA: 0s - loss: 0.5391 - accuracy: 0.7345
Epoch 6: val_accuracy improved from 0.71729 to 0.74065, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 2s 28ms/step - loss: 0.5373 - accuracy: 0.7375 - val_loss: 0.5284 - val_accuracy: 0.7407
Epoch 7/50
52/54 [===========================>..] - ETA: 0s - loss: 0.4945 - accuracy: 0.7539
Epoch 7: val_accuracy improved from 0.74065 to 0.76636, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 2s 28ms/step - loss: 0.4927 - accuracy: 0.7567 - val_loss: 0.4725 - val_accuracy: 0.7664
Epoch 8/50
54/54 [==============================] - ETA: 0s - loss: 0.4634 - accuracy: 0.7812
Epoch 8: val_accuracy improved from 0.76636 to 0.77336, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 2s 29ms/step - loss: 0.4634 - accuracy: 0.7812 - val_loss: 0.4631 - val_accuracy: 0.7734
Epoch 9/50
52/54 [===========================>..] - ETA: 0s - loss: 0.4212 - accuracy: 0.8012
Epoch 9: val_accuracy improved from 0.77336 to 0.80841, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 2s 28ms/step - loss: 0.4221 - accuracy: 0.8016 - val_loss: 0.4328 - val_accuracy: 0.8084
Epoch 10/50
54/54 [==============================] - ETA: 0s - loss: 0.3982 - accuracy: 0.8320
Epoch 10: val_accuracy did not improve from 0.80841
54/54 [==============================] - 1s 26ms/step - loss: 0.3982 - accuracy: 0.8320 - val_loss: 0.4337 - val_accuracy: 0.8037
Epoch 11/50
53/54 [============================>.] - ETA: 0s - loss: 0.3518 - accuracy: 0.8543
Epoch 11: val_accuracy did not improve from 0.80841
54/54 [==============================] - 2s 28ms/step - loss: 0.3512 - accuracy: 0.8547 - val_loss: 0.4281 - val_accuracy: 0.7967
Epoch 12/50
54/54 [==============================] - ETA: 0s - loss: 0.3193 - accuracy: 0.8804
Epoch 12: val_accuracy improved from 0.80841 to 0.82710, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 2s 33ms/step - loss: 0.3193 - accuracy: 0.8804 - val_loss: 0.4022 - val_accuracy: 0.8271
Epoch 13/50
54/54 [==============================] - ETA: 0s - loss: 0.2932 - accuracy: 0.8915
Epoch 13: val_accuracy did not improve from 0.82710
54/54 [==============================] - 2s 30ms/step - loss: 0.2932 - accuracy: 0.8915 - val_loss: 0.3839 - val_accuracy: 0.8178
Epoch 14/50
54/54 [==============================] - ETA: 0s - loss: 0.2712 - accuracy: 0.8979
Epoch 14: val_accuracy improved from 0.82710 to 0.84346, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 2s 33ms/step - loss: 0.2712 - accuracy: 0.8979 - val_loss: 0.3866 - val_accuracy: 0.8435
Epoch 15/50
54/54 [==============================] - ETA: 0s - loss: 0.2556 - accuracy: 0.8985
Epoch 15: val_accuracy improved from 0.84346 to 0.85514, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 2s 32ms/step - loss: 0.2556 - accuracy: 0.8985 - val_loss: 0.3688 - val_accuracy: 0.8551
Epoch 16/50
54/54 [==============================] - ETA: 0s - loss: 0.2380 - accuracy: 0.9014
Epoch 16: val_accuracy did not improve from 0.85514
54/54 [==============================] - 2s 29ms/step - loss: 0.2380 - accuracy: 0.9014 - val_loss: 0.3657 - val_accuracy: 0.8505
Epoch 17/50
54/54 [==============================] - ETA: 0s - loss: 0.2155 - accuracy: 0.9189
Epoch 17: val_accuracy did not improve from 0.85514
54/54 [==============================] - 2s 31ms/step - loss: 0.2155 - accuracy: 0.9189 - val_loss: 0.3662 - val_accuracy: 0.8435
Epoch 18/50
54/54 [==============================] - ETA: 0s - loss: 0.2019 - accuracy: 0.9230
Epoch 18: val_accuracy did not improve from 0.85514
54/54 [==============================] - 2s 29ms/step - loss: 0.2019 - accuracy: 0.9230 - val_loss: 0.4061 - val_accuracy: 0.8388
Epoch 19/50
54/54 [==============================] - ETA: 0s - loss: 0.1832 - accuracy: 0.9294
Epoch 19: val_accuracy did not improve from 0.85514
54/54 [==============================] - 2s 29ms/step - loss: 0.1832 - accuracy: 0.9294 - val_loss: 0.4042 - val_accuracy: 0.8341
Epoch 20/50
53/54 [============================>.] - ETA: 0s - loss: 0.1685 - accuracy: 0.9370
Epoch 20: val_accuracy improved from 0.85514 to 0.86916, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 2s 31ms/step - loss: 0.1700 - accuracy: 0.9347 - val_loss: 0.3639 - val_accuracy: 0.8692
Epoch 21/50
54/54 [==============================] - ETA: 0s - loss: 0.1757 - accuracy: 0.9364
Epoch 21: val_accuracy did not improve from 0.86916
54/54 [==============================] - 2s 29ms/step - loss: 0.1757 - accuracy: 0.9364 - val_loss: 0.3550 - val_accuracy: 0.8621
Epoch 22/50
54/54 [==============================] - ETA: 0s - loss: 0.1433 - accuracy: 0.9469
Epoch 22: val_accuracy did not improve from 0.86916
54/54 [==============================] - 2s 29ms/step - loss: 0.1433 - accuracy: 0.9469 - val_loss: 0.3699 - val_accuracy: 0.8668
Epoch 23/50
53/54 [============================>.] - ETA: 0s - loss: 0.1362 - accuracy: 0.9501
Epoch 23: val_accuracy did not improve from 0.86916
54/54 [==============================] - 2s 29ms/step - loss: 0.1373 - accuracy: 0.9504 - val_loss: 0.3753 - val_accuracy: 0.8505
Epoch 24/50
53/54 [============================>.] - ETA: 0s - loss: 0.1232 - accuracy: 0.9578
Epoch 24: val_accuracy improved from 0.86916 to 0.87383, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 2s 31ms/step - loss: 0.1224 - accuracy: 0.9586 - val_loss: 0.3653 - val_accuracy: 0.8738
Epoch 25/50
53/54 [============================>.] - ETA: 0s - loss: 0.1109 - accuracy: 0.9670
Epoch 25: val_accuracy did not improve from 0.87383
54/54 [==============================] - 2s 29ms/step - loss: 0.1105 - accuracy: 0.9673 - val_loss: 0.3940 - val_accuracy: 0.8668
Epoch 26/50
54/54 [==============================] - ETA: 0s - loss: 0.1088 - accuracy: 0.9667
Epoch 26: val_accuracy did not improve from 0.87383
54/54 [==============================] - 2s 29ms/step - loss: 0.1088 - accuracy: 0.9667 - val_loss: 0.3898 - val_accuracy: 0.8645
Epoch 27/50
54/54 [==============================] - ETA: 0s - loss: 0.1062 - accuracy: 0.9650
Epoch 27: val_accuracy improved from 0.87383 to 0.88084, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 2s 32ms/step - loss: 0.1062 - accuracy: 0.9650 - val_loss: 0.3874 - val_accuracy: 0.8808
Epoch 28/50
54/54 [==============================] - ETA: 0s - loss: 0.0984 - accuracy: 0.9697
Epoch 28: val_accuracy did not improve from 0.88084
54/54 [==============================] - 2s 29ms/step - loss: 0.0984 - accuracy: 0.9697 - val_loss: 0.3873 - val_accuracy: 0.8785
Epoch 29/50
54/54 [==============================] - ETA: 0s - loss: 0.0879 - accuracy: 0.9726
Epoch 29: val_accuracy did not improve from 0.88084
54/54 [==============================] - 2s 29ms/step - loss: 0.0879 - accuracy: 0.9726 - val_loss: 0.4120 - val_accuracy: 0.8738
Epoch 30/50
54/54 [==============================] - ETA: 0s - loss: 0.1058 - accuracy: 0.9650
Epoch 30: val_accuracy did not improve from 0.88084
54/54 [==============================] - 2s 30ms/step - loss: 0.1058 - accuracy: 0.9650 - val_loss: 0.3867 - val_accuracy: 0.8715
Epoch 31/50
54/54 [==============================] - ETA: 0s - loss: 0.0714 - accuracy: 0.9790
Epoch 31: val_accuracy improved from 0.88084 to 0.88318, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 2s 32ms/step - loss: 0.0714 - accuracy: 0.9790 - val_loss: 0.4207 - val_accuracy: 0.8832
Epoch 32/50
54/54 [==============================] - ETA: 0s - loss: 0.0741 - accuracy: 0.9813
Epoch 32: val_accuracy did not improve from 0.88318
54/54 [==============================] - 2s 30ms/step - loss: 0.0741 - accuracy: 0.9813 - val_loss: 0.4050 - val_accuracy: 0.8762
Epoch 33/50
54/54 [==============================] - ETA: 0s - loss: 0.0589 - accuracy: 0.9837
Epoch 33: val_accuracy did not improve from 0.88318
54/54 [==============================] - 2s 29ms/step - loss: 0.0589 - accuracy: 0.9837 - val_loss: 0.4326 - val_accuracy: 0.8668
Epoch 34/50
54/54 [==============================] - ETA: 0s - loss: 0.0508 - accuracy: 0.9895
Epoch 34: val_accuracy improved from 0.88318 to 0.88785, saving model to ./models\Monkeypox_best_model.h5
54/54 [==============================] - 2s 32ms/step - loss: 0.0508 - accuracy: 0.9895 - val_loss: 0.4585 - val_accuracy: 0.8879
Epoch 35/50
54/54 [==============================] - ETA: 0s - loss: 0.0496 - accuracy: 0.9907
Epoch 35: val_accuracy did not improve from 0.88785
54/54 [==============================] - 2s 30ms/step - loss: 0.0496 - accuracy: 0.9907 - val_loss: 0.4816 - val_accuracy: 0.8692
Epoch 36/50
54/54 [==============================] - ETA: 0s - loss: 0.0807 - accuracy: 0.9737
Epoch 36: val_accuracy did not improve from 0.88785
54/54 [==============================] - 2s 29ms/step - loss: 0.0807 - accuracy: 0.9737 - val_loss: 0.4706 - val_accuracy: 0.8621
Epoch 37/50
54/54 [==============================] - ETA: 0s - loss: 0.0688 - accuracy: 0.9755
Epoch 37: val_accuracy did not improve from 0.88785
54/54 [==============================] - 2s 30ms/step - loss: 0.0688 - accuracy: 0.9755 - val_loss: 0.4468 - val_accuracy: 0.8715
Epoch 38/50
54/54 [==============================] - ETA: 0s - loss: 0.0541 - accuracy: 0.9825
Epoch 38: val_accuracy did not improve from 0.88785
54/54 [==============================] - 2s 29ms/step - loss: 0.0541 - accuracy: 0.9825 - val_loss: 0.4552 - val_accuracy: 0.8621
Epoch 39/50
53/54 [============================>.] - ETA: 0s - loss: 0.0497 - accuracy: 0.9881
Epoch 39: val_accuracy did not improve from 0.88785
54/54 [==============================] - 2s 29ms/step - loss: 0.0491 - accuracy: 0.9883 - val_loss: 0.4660 - val_accuracy: 0.8832
Epoch 40/50
53/54 [============================>.] - ETA: 0s - loss: 0.0422 - accuracy: 0.9863
Epoch 40: val_accuracy did not improve from 0.88785
54/54 [==============================] - 2s 28ms/step - loss: 0.0448 - accuracy: 0.9854 - val_loss: 0.4934 - val_accuracy: 0.8808
Epoch 41/50
52/54 [===========================>..] - ETA: 0s - loss: 0.0496 - accuracy: 0.9861
Epoch 41: val_accuracy did not improve from 0.88785
54/54 [==============================] - 1s 26ms/step - loss: 0.0499 - accuracy: 0.9854 - val_loss: 0.4625 - val_accuracy: 0.8715
Epoch 42/50
52/54 [===========================>..] - ETA: 0s - loss: 0.0373 - accuracy: 0.9927
Epoch 42: val_accuracy did not improve from 0.88785
54/54 [==============================] - 1s 27ms/step - loss: 0.0370 - accuracy: 0.9930 - val_loss: 0.4864 - val_accuracy: 0.8621
Epoch 43/50
53/54 [============================>.] - ETA: 0s - loss: 0.0352 - accuracy: 0.9911
Epoch 43: val_accuracy did not improve from 0.88785
54/54 [==============================] - 1s 27ms/step - loss: 0.0354 - accuracy: 0.9907 - val_loss: 0.5226 - val_accuracy: 0.8762
Epoch 44/50
54/54 [==============================] - ETA: 0s - loss: 0.0314 - accuracy: 0.9924
Epoch 44: val_accuracy did not improve from 0.88785
54/54 [==============================] - 1s 26ms/step - loss: 0.0314 - accuracy: 0.9924 - val_loss: 0.5197 - val_accuracy: 0.8668
Epoch 45/50
53/54 [============================>.] - ETA: 0s - loss: 0.0361 - accuracy: 0.9911
Epoch 45: val_accuracy did not improve from 0.88785
54/54 [==============================] - 1s 27ms/step - loss: 0.0356 - accuracy: 0.9912 - val_loss: 0.5102 - val_accuracy: 0.8692
Epoch 46/50
53/54 [============================>.] - ETA: 0s - loss: 0.0221 - accuracy: 0.9970
Epoch 46: val_accuracy did not improve from 0.88785
54/54 [==============================] - 1s 27ms/step - loss: 0.0222 - accuracy: 0.9971 - val_loss: 0.5320 - val_accuracy: 0.8692
Epoch 47/50
54/54 [==============================] - ETA: 0s - loss: 0.0310 - accuracy: 0.9942
Epoch 47: val_accuracy did not improve from 0.88785
54/54 [==============================] - 1s 26ms/step - loss: 0.0310 - accuracy: 0.9942 - val_loss: 0.5445 - val_accuracy: 0.8645
Epoch 48/50
54/54 [==============================] - ETA: 0s - loss: 0.0263 - accuracy: 0.9930
Epoch 48: val_accuracy did not improve from 0.88785
54/54 [==============================] - 1s 26ms/step - loss: 0.0263 - accuracy: 0.9930 - val_loss: 0.5158 - val_accuracy: 0.8785
Epoch 49/50
53/54 [============================>.] - ETA: 0s - loss: 0.0262 - accuracy: 0.9929
Epoch 49: val_accuracy did not improve from 0.88785
54/54 [==============================] - 1s 26ms/step - loss: 0.0260 - accuracy: 0.9930 - val_loss: 0.5357 - val_accuracy: 0.8785
Epoch 50/50
54/54 [==============================] - ETA: 0s - loss: 0.0209 - accuracy: 0.9947
Epoch 50: val_accuracy did not improve from 0.88785
54/54 [==============================] - 1s 26ms/step - loss: 0.0209 - accuracy: 0.9947 - val_loss: 0.5442 - val_accuracy: 0.8645
10.模型评估
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()

image.png

11.指定图片进行预测

新建立一个python文件试试

#!/usr/bin/python  
# @Project  :Tensorflow  
# @Name     : T4-verify.py  
# @Time     :2024/10/7 下午1:39  
# @Email    : changdefeng06@gmail.com  
# @Author   : idefeng  
import tensorflow as tf  
from PIL import Image  
import numpy as np  # 指定图片  
img = Image.open('./datasets/mp/Monkeypox/M01_01_04.jpg')  
img.show()  
image = tf.keras.utils.load_img('./datasets/mp/Monkeypox/M01_01_04.jpg', target_size=(224, 224))  
image_array = tf.keras.utils.img_to_array(image)  # 将PIL对象转换成numpy数组  image_array = tf.expand_dims(image_array, 0)  class_name = ['猴痘', '其他']  
models = tf.keras.models.load_model('./models/Monkeypox_best_model.h5', )  
predictions = models.predict(image_array)  
print("预测结果为:", class_name[np.argmax(predictions)])

运行结果出错了:

ValueError: No model config found in the file at <tensorflow.python.platform.gfile.GFile object at 0x000001E8FB5B6D30>.

难道我们上面保存的“Monkeypox_best_model.h5”不是模型文件,记得我们ModelCheckpoint中其中一个参数设置的是save_weights_only=True.只保存了权重。这是什么意思呢?根据保存和恢复模型  |  TensorFlow Core中所描述:
image.png
完整的模型应该包含架构,权重和训练配置三部分,而上面我们仅仅保存了weights(权重)显然这个文件不是模型本身,因此推断是这里出现了问题。解决思路:

  1. 新文件中重新构建CNN网络,而且这个网络模型要和上面保存权重的模型结构一致。然后在这个重新构建的CNN网络模型中load_weights来获得之前训练的权重。
  2. 在上面的文件中保存一个完整的模型。
    试试第一种模式:
#!/usr/bin/python  
# @Project  :Tensorflow  
# @Name     : T4-verify.py  
# @Time     :2024/10/7 下午1:39  
# @Email    : changdefeng06@gmail.com  
# @Author   : idefeng  
import tensorflow as tf  
from tensorflow.keras import models, layers  
from PIL import Image  
import numpy as np  # 指定图片  
img = Image.open('./datasets/mp/Monkeypox/M01_01_04.jpg')  
img.show()  
image = tf.keras.utils.load_img('./datasets/mp/Monkeypox/M01_01_04.jpg', target_size=(224, 224))  
image_array = tf.keras.utils.img_to_array(image)  # 将PIL对象转换成numpy数组  image_array = tf.expand_dims(image_array, 0)  class_names = ['猴痘', '其他']  model = models.Sequential([  layers.experimental.preprocessing.Rescaling(1. / 255, input_shape=(224, 224, 3)),  layers.Conv2D(16, (3, 3), activation='relu', input_shape=(224, 224, 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(len(class_names))  # 输出层,输出预期结果  
])  
model.load_weights('./models/Monkeypox_best_model.h5')    # 加载权重  predictions = model.predict(image_array)  
print("预测结果为:", class_names[np.argmax(predictions)])

image.png
换一张其他图片预测:
image.png

(三)总结

  1. 数据集的格式可以有多种,可以是numpy数组,文本数据,CSV数据,文件数据等;
  2. 数据集加速配置,如何更好的利用CPU时间
  3. 保存模型的要素,结构、权重、配置。仅保存权重并不是模型本身。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/448639.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql innodb 引擎如何直接复制数据库文件?

mysql innodb 引擎如何直接复制数据库文件&#xff1f;介绍如下&#xff1a; 1、首先找到数据库文件所在位置 一般可以看my.conf/my.ini配置的文件的“datadir” 看示例&#xff1a; “MAMP”在Macos下的数据库文件位置&#xff1a; /Library/Application Support/appsolu…

DAY35||452.用最少数量的箭引爆气球 |435.无重叠区间 |763.划分字母区间

重叠区间专场。 452.用最少数量的箭引爆气球 题目:452. 用最少数量的箭引爆气球 - 力扣&#xff08;LeetCode&#xff09; 有一些球形气球贴在一堵用 XY 平面表示的墙面上。墙面上的气球记录在整数数组 points &#xff0c;其中points[i] [xstart, xend] 表示水平直径在 xst…

恢复已删除文件的 10 种安卓数据恢复工具

由于我们现在在智能手机上存储了大量重要文件&#xff0c;因此了解数据恢复工具变得很重要。您永远不会知道什么时候需要使用 安卓 数据恢复工具。 由于不乏 Windows 数据恢复工具&#xff0c;因此从崩溃的计算机中恢复文件很容易。但是&#xff0c;当涉及到从 安卓恢复数据时…

论文笔记:Ontology-enhanced Prompt-tuning for Few-shot Learning

论文来源&#xff1a;WWW 2022 论文地址&#xff1a;https://arxiv.org/pdf/2201.11332.pdfhttps://arxiv.org/pdf/2201.11332.pdf 论文代码&#xff1a;暂未公开 笔记仅供参考&#xff0c;撰写不易&#xff0c;请勿恶意转载抄袭&#xff01; Abstract 小样本学习旨在基于…

深入浅出:MySQL索引优化指南

一、引言 在数据库应用开发中&#xff0c;性能优化是一个永恒的话题。而索引作为提高数据库查询效率的关键手段&#xff0c;其设计和使用直接影响着系统的性能表现。本文将深入探讨MySQL索引的原理、类型以及优化策略&#xff0c;帮助读者更好地理解和应用索引&#xff0c;从而…

FreeRTOS:事件标志组

目录 一、简介 二、 事件控制块 三、相关API 四、 应用场景 一、简介 在FreeRTOS中&#xff0c;使用信号量可以实现同步&#xff0c;但是使用信号量来同步的话任务只能与单个的任务进行同步。有时候某个任务可能会需要与多个任务进行同步&#xff0c;此时信号量就无能为力。…

mysql 09 独立表空间结构

表空间中的页实在是太多了&#xff0c;为了更好的管理这些页面&#xff0c;设计 InnoDB 的大叔们提出了 区 &#xff08;英文名&#xff1a; extent &#xff09;的概念。对于16KB的页来说&#xff0c;连续的64个页就是一个 区 &#xff0c;也就是说一个区默认占用1MB空间大小。…

QT--单选按钮(QRadioButton)和复选按钮(QCheckBox)

在Qt中&#xff0c;单选按钮&#xff08;QRadioButton&#xff09;和复选按钮&#xff08;QCheckBox&#xff09;是两种常用的用户界面控件&#xff0c;它们的主要区别在于选择行为和用途&#xff1a; QRadioButton&#xff08;单选按钮&#xff09; 选择行为&#xff1a;单选…

jvm虚拟机调优实战

使用命令 jps查看进程使用jstat gc -1 5000查看内存占用和回收情况 正式测试 是否跑job区别。大量的job,部分用户点击的热数据 &#xff0c;不同时刻在跑 600-700对比 200 多了400-500m,代码原数据&#xff08;不占用堆区&#xff09;占了300m,所以 堆空间老年代&#xff08;90…

VsCode环境配置C++环境

目录 第一步下载应用 第二步应用文字汉化 第三步安装编译器MinGW 第四步 环境变量的配置 第五步 打开VsCode 第六步 配置环境设施 几个其他的好用的插件 会了吧 MarsCode: AI Coding Assistant 第一步下载应用 VSCode下载官方指定网址&#xff1a; Visual Studio Cod…

使用豆包MarsCode 来处理 Excel 的数据吧!

作者可乐三分糖 背景 Excel 是大部分没有信息化的公司通用的数据处理手段。但并不是所有的人对 excel 都是非常熟悉的。这些同学主要会遇到三类问题&#xff1a; Excel 的一些操作问题&#xff0c;如公式怎么写跨表处理太复杂&#xff0c;即使是写公式也很繁琐。一些数据批处…

关于k8s集群高可用性的探究

1. k8s的高可用的核心是什么&#xff1f; 说到核心、本质 意味着要从物理层来考虑技术 k8s是一个容器编排管理工具&#xff0c;k8s受欢迎的时机 是docker容器受欢迎时&#xff0c;因为太多的docker容器&#xff0c;管理起来是一个大工程 那么刚好k8s是google自己用了十来年…

如何设计开发RTSP直播播放器?

技术背景 我们在对接RTSP直播播放器相关技术诉求的时候&#xff0c;好多开发者&#xff0c;除了选用成熟的RTSP播放器外&#xff0c;还想知其然知其所以然&#xff0c;对RTSP播放器的整体开发有个基础的了解&#xff0c;方便方案之作和技术延伸。本文抛砖引玉&#xff0c;做个…

【环境搭建】更换电脑后的开发环境怎么重建

目录 &#x1f378;前言 &#x1f37b;一、系统配置检查 &#x1f37a;二、开发环境搭建 &#x1f379;三、章末 &#x1f378;前言 小伙伴们大家好&#xff0c;这次文章跟技术没有关联&#xff0c;因为最近刚更换了装备&#xff0c;开发环境啥的残缺不全&#xff0c;也不能…

Java基础:面向对象编程5

1 Java内部类 1.1 概念 在 Java 中&#xff0c;内部类是指定义在另一个类内部或方法内部的类。内部类可以分为以下几种类型&#xff1a; 成员内部类局部内部类匿名内部类静态内部类 1.2 成员内部类 定义&#xff1a;成员内部类是最常见的内部类&#xff0c;它定义在外部类…

深度解析 Redis 存储结构及其高效性背后的机制

目录 1. Redis 存储结构存储结构存储转换 2. 字典实现数据结构冲突处理负载因子 3. 扩容扩容步骤影响与优化 4. 缩容缩容步骤优化策略 5. 渐进式 Rehash**渐进式 Rehash 的工作原理**Rehash 规则优势 6. SCAN 命令SCAN 的实现原理遍历顺序避免重复和遗漏使用场景 7. 过期&#…

电子商务网站维护技巧:保持WordPress、主题和插件的更新

在这个快节奏的数字时代&#xff0c;维护一个电子商务网站的首要任务之一是保持WordPress、主题和插件的最新状态。过时的软件不仅可能导致功能故障&#xff0c;还可能带来安全风险。本文将深入探讨如何有效地更新和维护您的WordPress网站&#xff0c;以确保其安全性和性能。 …

工业物联网关-ModbusTCP

Modbus-TCP模式把网关视作Modbus从端设备&#xff0c;主端设备可以通过Modbus-TCP协议访问网关上所有终端设备。用户可以自定义多条通道&#xff0c;每条通道可以配置为TCP Server或者TCP Slave。注意&#xff0c;该模式需要指定采集通道&#xff0c;采集通道可以是串口和网口通…

简述微服务高可用之Sentinel、Seate

简述微服务高可用之Sentinel、Seate使用 下文主要讲述使用sentinel,如何降级限流熔断及如何使用seata管理分布式事务 sentinel服务端安装与使用 1、下载 进入https://github.com/alibaba/Sentinel/releases 根据你的需求进行下载对应版本 我这里是JDK17 下载的1.8.8版本&am…

【数据结构与算法】链表(上)

记录自己所学&#xff0c;无详细讲解 无头单链表实现 1.项目目录文件 2.头文件 Slist.h #include <stdio.h> #include <assert.h> #include <stdlib.h> struct Slist {int data;struct Slist* next; }; typedef struct Slist Slist; //初始化 void SlistI…