第T6周:使用TensorFlow实现好莱坞明星识别

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

    文章目录

    • 一、前期工作
      • 1.设置GPU(如果使用的是CPU可以忽略这步)
      • 2. 导入数据
      • 3. 查看数据
    • 二、数据预处理
      • 1、加载数据
      • 2、数据可视化
      • 3、再次检查数据
      • 4、配置数据集
    • 三、构建CNN网络
    • 四、训练模型
      • 1、设置动态学习率
      • 2、早停与保存最佳模型参数
      • 3、模型训练
    • 五、模型评估
    • 六、模型优化
      • 1、`冻结卷积基,只训练全连接层的参数`
      • 2、`设置lr、ModelCheckpoint、EarlyStopping`
      • 3、`再次训练`
      • 4、`Loss与Accuracy图`
      • 5、` 指定图片进行预测`
    • 七、总结

电脑环境:
语言环境: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 = "./48-data/"
data_dir = pathlib.Path(data_dir)

3. 查看数据

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

输出:图片总数为: 1800

打开一张图片:

roses = list(data_dir.glob('Nicole Kidman/*.jpg'))
PIL.Image.open(str(roses[0]))

在这里插入图片描述

二、数据预处理

1、加载数据

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

batch_size = 32
img_height = 224
img_width = 224

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=0.1,
    subset="training",
    label_mode = "categorical",
    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.1,
    subset="validation",
    label_mode = "categorical",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size)

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

class_names = train_ds.class_names
print(class_names)

输出:

[‘Angelina Jolie’, ‘Brad Pitt’, ‘Denzel Washington’, ‘Hugh Jackman’, ‘Jennifer Lawrence’, ‘Johnny Depp’, ‘Kate Winslet’, ‘Leonardo DiCaprio’, ‘Megan Fox’, ‘Natalie Portman’, ‘Nicole Kidman’, ‘Robert Downey Jr’, ‘Sandra Bullock’, ‘Scarlett Johansson’, ‘Tom Cruise’, ‘Tom Hanks’, ‘Will Smith’]

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[np.argmax(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, 17)

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网络

调用官方的VGG-16网络框架:

from keras.applications import VGG16

conv_base = VGG16(weights='imagenet',
                  include_top=False,
                  input_shape=(224, 224, 3))

加上全连接层:

model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dropout(0.4))
model.add(layers.Dense(len(class_names)))
model.summary()

网络详情:

_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 vgg16 (Functional)          (None, 7, 7, 512)         14714688  
                                                                 
 flatten (Flatten)           (None, 25088)             0         
                                                                 
 dense (Dense)               (None, 256)               6422784   
                                                                 
 dropout (Dropout)           (None, 256)               0         
                                                                 
 dense_1 (Dense)             (None, 17)                4369      
                                                                 
=================================================================
Total params: 21141841 (80.65 MB)
Trainable params: 21141841 (80.65 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

四、训练模型

1、设置动态学习率

# 设置初始学习率
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=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

2、早停与保存最佳模型参数

from keras.callbacks import ModelCheckpoint, EarlyStopping

epochs = 100

# 保存最佳模型参数
checkpointer = ModelCheckpoint('best_model.h5',
                                monitor='val_accuracy',
                                verbose=1,
                                save_best_only=True,
                                save_weights_only=True)

# 设置早停
earlystopper = EarlyStopping(monitor='val_accuracy',
                             min_delta=0.001,
                             patience=20,
                             verbose=1)

3、模型训练

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

输出:

Epoch 1/100
45/45 [==============================] - ETA: 0s - loss: 3.8728 - accuracy: 0.0639
Epoch 1: val_accuracy improved from -inf to 0.05833, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 392s 2s/step - loss: 3.8728 - accuracy: 0.0639 - val_loss: 2.8329 - val_accuracy: 0.0583
Epoch 2/100
45/45 [==============================] - ETA: 0s - loss: 2.8281 - accuracy: 0.0750
Epoch 2: val_accuracy improved from 0.05833 to 0.08611, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 20s 435ms/step - loss: 2.8281 - accuracy: 0.0750 - val_loss: 2.8312 - val_accuracy: 0.0861
Epoch 3/100
45/45 [==============================] - ETA: 0s - loss: 2.8119 - accuracy: 0.0986
Epoch 3: val_accuracy improved from 0.08611 to 0.12500, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 20s 441ms/step - loss: 2.8119 - accuracy: 0.0986 - val_loss: 2.7734 - val_accuracy: 0.1250
Epoch 4/100
45/45 [==============================] - ETA: 0s - loss: 2.7812 - accuracy: 0.1153
Epoch 4: val_accuracy improved from 0.12500 to 0.13333, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 20s 444ms/step - loss: 2.7812 - accuracy: 0.1153 - val_loss: 2.7716 - val_accuracy: 0.1333
Epoch 5/100
45/45 [==============================] - ETA: 0s - loss: 2.7444 - accuracy: 0.1319
Epoch 5: val_accuracy improved from 0.13333 to 0.13889, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 20s 455ms/step - loss: 2.7444 - accuracy: 0.1319 - val_loss: 2.7138 - val_accuracy: 0.1389
Epoch 6/100
45/45 [==============================] - ETA: 0s - loss: 2.6869 - accuracy: 0.1458
Epoch 6: val_accuracy improved from 0.13889 to 0.15556, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 22s 493ms/step - loss: 2.6869 - accuracy: 0.1458 - val_loss: 2.6313 - val_accuracy: 0.1556
Epoch 7/100
45/45 [==============================] - ETA: 0s - loss: 2.6230 - accuracy: 0.1507
Epoch 7: val_accuracy did not improve from 0.15556
45/45 [==============================] - 19s 426ms/step - loss: 2.6230 - accuracy: 0.1507 - val_loss: 2.6150 - val_accuracy: 0.1500
Epoch 8/100
45/45 [==============================] - ETA: 0s - loss: 2.5483 - accuracy: 0.1826
Epoch 8: val_accuracy did not improve from 0.15556
45/45 [==============================] - 19s 431ms/step - loss: 2.5483 - accuracy: 0.1826 - val_loss: 2.6042 - val_accuracy: 0.1361
Epoch 9/100
45/45 [==============================] - ETA: 0s - loss: 2.5030 - accuracy: 0.1840
Epoch 9: val_accuracy improved from 0.15556 to 0.16389, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 21s 474ms/step - loss: 2.5030 - accuracy: 0.1840 - val_loss: 2.5547 - val_accuracy: 0.1639
Epoch 10/100
45/45 [==============================] - ETA: 0s - loss: 2.4198 - accuracy: 0.2208
Epoch 10: val_accuracy improved from 0.16389 to 0.18056, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 21s 471ms/step - loss: 2.4198 - accuracy: 0.2208 - val_loss: 2.5333 - val_accuracy: 0.1806
Epoch 11/100
45/45 [==============================] - ETA: 0s - loss: 2.3769 - accuracy: 0.2222
Epoch 11: val_accuracy improved from 0.18056 to 0.21944, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 21s 466ms/step - loss: 2.3769 - accuracy: 0.2222 - val_loss: 2.4761 - val_accuracy: 0.2194
Epoch 12/100
45/45 [==============================] - ETA: 0s - loss: 2.2730 - accuracy: 0.2583
Epoch 12: val_accuracy did not improve from 0.21944
45/45 [==============================] - 20s 442ms/step - loss: 2.2730 - accuracy: 0.2583 - val_loss: 2.4136 - val_accuracy: 0.2194
Epoch 13/100
45/45 [==============================] - ETA: 0s - loss: 2.1822 - accuracy: 0.2944
Epoch 13: val_accuracy did not improve from 0.21944
45/45 [==============================] - 20s 444ms/step - loss: 2.1822 - accuracy: 0.2944 - val_loss: 2.3941 - val_accuracy: 0.2056
Epoch 14/100
45/45 [==============================] - ETA: 0s - loss: 2.0759 - accuracy: 0.3229
Epoch 14: val_accuracy improved from 0.21944 to 0.26389, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 22s 481ms/step - loss: 2.0759 - accuracy: 0.3229 - val_loss: 2.3304 - val_accuracy: 0.2639
Epoch 15/100
45/45 [==============================] - ETA: 0s - loss: 1.9345 - accuracy: 0.3542
Epoch 15: val_accuracy did not improve from 0.26389
45/45 [==============================] - 20s 444ms/step - loss: 1.9345 - accuracy: 0.3542 - val_loss: 2.2658 - val_accuracy: 0.2556
Epoch 16/100
45/45 [==============================] - ETA: 0s - loss: 1.8489 - accuracy: 0.3743
Epoch 16: val_accuracy improved from 0.26389 to 0.29444, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 22s 482ms/step - loss: 1.8489 - accuracy: 0.3743 - val_loss: 2.2204 - val_accuracy: 0.2944
Epoch 17/100
45/45 [==============================] - ETA: 0s - loss: 1.7346 - accuracy: 0.4229
Epoch 17: val_accuracy did not improve from 0.29444
45/45 [==============================] - 20s 444ms/step - loss: 1.7346 - accuracy: 0.4229 - val_loss: 2.2433 - val_accuracy: 0.2778
Epoch 18/100
45/45 [==============================] - ETA: 0s - loss: 1.5925 - accuracy: 0.4847
Epoch 18: val_accuracy improved from 0.29444 to 0.30278, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 22s 488ms/step - loss: 1.5925 - accuracy: 0.4847 - val_loss: 2.2539 - val_accuracy: 0.3028
Epoch 19/100
45/45 [==============================] - ETA: 0s - loss: 1.5517 - accuracy: 0.4938
Epoch 19: val_accuracy did not improve from 0.30278
45/45 [==============================] - 20s 443ms/step - loss: 1.5517 - accuracy: 0.4938 - val_loss: 2.2492 - val_accuracy: 0.2972
Epoch 20/100
45/45 [==============================] - ETA: 0s - loss: 1.3983 - accuracy: 0.5285
Epoch 20: val_accuracy did not improve from 0.30278
45/45 [==============================] - 20s 451ms/step - loss: 1.3983 - accuracy: 0.5285 - val_loss: 2.2970 - val_accuracy: 0.3028
Epoch 21/100
45/45 [==============================] - ETA: 0s - loss: 1.2226 - accuracy: 0.5840
Epoch 21: val_accuracy improved from 0.30278 to 0.31667, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 23s 507ms/step - loss: 1.2226 - accuracy: 0.5840 - val_loss: 2.2511 - val_accuracy: 0.3167
Epoch 22/100
45/45 [==============================] - ETA: 0s - loss: 1.1661 - accuracy: 0.6201
Epoch 22: val_accuracy did not improve from 0.31667
45/45 [==============================] - 21s 464ms/step - loss: 1.1661 - accuracy: 0.6201 - val_loss: 2.3456 - val_accuracy: 0.3000
Epoch 23/100
45/45 [==============================] - ETA: 0s - loss: 1.0238 - accuracy: 0.6590
Epoch 23: val_accuracy did not improve from 0.31667
45/45 [==============================] - 20s 436ms/step - loss: 1.0238 - accuracy: 0.6590 - val_loss: 2.4425 - val_accuracy: 0.3083
Epoch 24/100
45/45 [==============================] - ETA: 0s - loss: 0.9449 - accuracy: 0.6868
Epoch 24: val_accuracy improved from 0.31667 to 0.32778, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 22s 486ms/step - loss: 0.9449 - accuracy: 0.6868 - val_loss: 2.3126 - val_accuracy: 0.3278
Epoch 25/100
45/45 [==============================] - ETA: 0s - loss: 0.8259 - accuracy: 0.7229
Epoch 25: val_accuracy did not improve from 0.32778
45/45 [==============================] - 20s 434ms/step - loss: 0.8259 - accuracy: 0.7229 - val_loss: 2.3506 - val_accuracy: 0.3000
Epoch 26/100
45/45 [==============================] - ETA: 0s - loss: 0.7882 - accuracy: 0.7333
Epoch 26: val_accuracy did not improve from 0.32778
45/45 [==============================] - 20s 438ms/step - loss: 0.7882 - accuracy: 0.7333 - val_loss: 2.3976 - val_accuracy: 0.3083
Epoch 27/100
45/45 [==============================] - ETA: 0s - loss: 0.6816 - accuracy: 0.7806
Epoch 27: val_accuracy did not improve from 0.32778
45/45 [==============================] - 20s 441ms/step - loss: 0.6816 - accuracy: 0.7806 - val_loss: 2.5215 - val_accuracy: 0.3167
Epoch 28/100
45/45 [==============================] - ETA: 0s - loss: 0.6466 - accuracy: 0.7931
Epoch 28: val_accuracy did not improve from 0.32778
45/45 [==============================] - 20s 444ms/step - loss: 0.6466 - accuracy: 0.7931 - val_loss: 2.4860 - val_accuracy: 0.3194
Epoch 29/100
45/45 [==============================] - ETA: 0s - loss: 0.5820 - accuracy: 0.8062
Epoch 29: val_accuracy did not improve from 0.32778
45/45 [==============================] - 20s 446ms/step - loss: 0.5820 - accuracy: 0.8062 - val_loss: 2.4623 - val_accuracy: 0.3194
Epoch 30/100
45/45 [==============================] - ETA: 0s - loss: 0.5293 - accuracy: 0.8292
Epoch 30: val_accuracy did not improve from 0.32778
45/45 [==============================] - 20s 445ms/step - loss: 0.5293 - accuracy: 0.8292 - val_loss: 2.5641 - val_accuracy: 0.3222
Epoch 31/100
45/45 [==============================] - ETA: 0s - loss: 0.4870 - accuracy: 0.8403
Epoch 31: val_accuracy did not improve from 0.32778
45/45 [==============================] - 20s 443ms/step - loss: 0.4870 - accuracy: 0.8403 - val_loss: 2.6284 - val_accuracy: 0.3250
Epoch 32/100
45/45 [==============================] - ETA: 0s - loss: 0.4601 - accuracy: 0.8507
Epoch 32: val_accuracy improved from 0.32778 to 0.33889, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 22s 482ms/step - loss: 0.4601 - accuracy: 0.8507 - val_loss: 2.6226 - val_accuracy: 0.3389
Epoch 33/100
45/45 [==============================] - ETA: 0s - loss: 0.4220 - accuracy: 0.8708
Epoch 33: val_accuracy did not improve from 0.33889
45/45 [==============================] - 20s 446ms/step - loss: 0.4220 - accuracy: 0.8708 - val_loss: 2.6235 - val_accuracy: 0.3278
Epoch 34/100
45/45 [==============================] - ETA: 0s - loss: 0.4048 - accuracy: 0.8708
Epoch 34: val_accuracy improved from 0.33889 to 0.34444, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 22s 492ms/step - loss: 0.4048 - accuracy: 0.8708 - val_loss: 2.5722 - val_accuracy: 0.3444
Epoch 35/100
45/45 [==============================] - ETA: 0s - loss: 0.3656 - accuracy: 0.8889
Epoch 35: val_accuracy did not improve from 0.34444
45/45 [==============================] - 21s 469ms/step - loss: 0.3656 - accuracy: 0.8889 - val_loss: 2.7488 - val_accuracy: 0.3389
Epoch 36/100
45/45 [==============================] - ETA: 0s - loss: 0.3666 - accuracy: 0.8889
Epoch 36: val_accuracy did not improve from 0.34444
45/45 [==============================] - 21s 463ms/step - loss: 0.3666 - accuracy: 0.8889 - val_loss: 2.7453 - val_accuracy: 0.3278
Epoch 37/100
45/45 [==============================] - ETA: 0s - loss: 0.3088 - accuracy: 0.9076
Epoch 37: val_accuracy improved from 0.34444 to 0.35000, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 21s 469ms/step - loss: 0.3088 - accuracy: 0.9076 - val_loss: 2.7392 - val_accuracy: 0.3500
Epoch 38/100
45/45 [==============================] - ETA: 0s - loss: 0.3152 - accuracy: 0.8986
Epoch 38: val_accuracy improved from 0.35000 to 0.35278, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 21s 467ms/step - loss: 0.3152 - accuracy: 0.8986 - val_loss: 2.7681 - val_accuracy: 0.3528
Epoch 39/100
45/45 [==============================] - ETA: 0s - loss: 0.2952 - accuracy: 0.9111
Epoch 39: val_accuracy did not improve from 0.35278
45/45 [==============================] - 20s 442ms/step - loss: 0.2952 - accuracy: 0.9111 - val_loss: 2.7665 - val_accuracy: 0.3417
Epoch 40/100
45/45 [==============================] - ETA: 0s - loss: 0.2937 - accuracy: 0.9056
Epoch 40: val_accuracy did not improve from 0.35278
45/45 [==============================] - 20s 444ms/step - loss: 0.2937 - accuracy: 0.9056 - val_loss: 2.8206 - val_accuracy: 0.3417
Epoch 41/100
45/45 [==============================] - ETA: 0s - loss: 0.2608 - accuracy: 0.9160
Epoch 41: val_accuracy improved from 0.35278 to 0.35556, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 22s 489ms/step - loss: 0.2608 - accuracy: 0.9160 - val_loss: 2.8521 - val_accuracy: 0.3556
Epoch 42/100
45/45 [==============================] - ETA: 0s - loss: 0.2588 - accuracy: 0.9167
Epoch 42: val_accuracy did not improve from 0.35556
45/45 [==============================] - 20s 445ms/step - loss: 0.2588 - accuracy: 0.9167 - val_loss: 2.8687 - val_accuracy: 0.3361
Epoch 43/100
45/45 [==============================] - ETA: 0s - loss: 0.2643 - accuracy: 0.9153
Epoch 43: val_accuracy did not improve from 0.35556
45/45 [==============================] - 20s 446ms/step - loss: 0.2643 - accuracy: 0.9153 - val_loss: 2.8563 - val_accuracy: 0.3306
Epoch 44/100
45/45 [==============================] - ETA: 0s - loss: 0.2337 - accuracy: 0.9326
Epoch 44: val_accuracy did not improve from 0.35556
45/45 [==============================] - 21s 471ms/step - loss: 0.2337 - accuracy: 0.9326 - val_loss: 2.8820 - val_accuracy: 0.3417
Epoch 45/100
45/45 [==============================] - ETA: 0s - loss: 0.2270 - accuracy: 0.9264
Epoch 45: val_accuracy improved from 0.35556 to 0.35833, saving model to /content/drive/MyDrive/app/T6/best_model.h5
45/45 [==============================] - 22s 485ms/step - loss: 0.2270 - accuracy: 0.9264 - val_loss: 2.9108 - val_accuracy: 0.3583
Epoch 46/100
45/45 [==============================] - ETA: 0s - loss: 0.2310 - accuracy: 0.9382
Epoch 46: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 447ms/step - loss: 0.2310 - accuracy: 0.9382 - val_loss: 2.8827 - val_accuracy: 0.3556
Epoch 47/100
45/45 [==============================] - ETA: 0s - loss: 0.2290 - accuracy: 0.9347
Epoch 47: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 447ms/step - loss: 0.2290 - accuracy: 0.9347 - val_loss: 2.8759 - val_accuracy: 0.3528
Epoch 48/100
45/45 [==============================] - ETA: 0s - loss: 0.2132 - accuracy: 0.9361
Epoch 48: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 447ms/step - loss: 0.2132 - accuracy: 0.9361 - val_loss: 2.8647 - val_accuracy: 0.3472
Epoch 49/100
45/45 [==============================] - ETA: 0s - loss: 0.2449 - accuracy: 0.9243
Epoch 49: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 445ms/step - loss: 0.2449 - accuracy: 0.9243 - val_loss: 2.8989 - val_accuracy: 0.3333
Epoch 50/100
45/45 [==============================] - ETA: 0s - loss: 0.2371 - accuracy: 0.9229
Epoch 50: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 445ms/step - loss: 0.2371 - accuracy: 0.9229 - val_loss: 2.8993 - val_accuracy: 0.3361
Epoch 51/100
45/45 [==============================] - ETA: 0s - loss: 0.2011 - accuracy: 0.9458
Epoch 51: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 445ms/step - loss: 0.2011 - accuracy: 0.9458 - val_loss: 2.8976 - val_accuracy: 0.3389
Epoch 52/100
45/45 [==============================] - ETA: 0s - loss: 0.2190 - accuracy: 0.9347
Epoch 52: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 445ms/step - loss: 0.2190 - accuracy: 0.9347 - val_loss: 2.9062 - val_accuracy: 0.3417
Epoch 53/100
45/45 [==============================] - ETA: 0s - loss: 0.2196 - accuracy: 0.9312
Epoch 53: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 445ms/step - loss: 0.2196 - accuracy: 0.9312 - val_loss: 2.9152 - val_accuracy: 0.3389
Epoch 54/100
45/45 [==============================] - ETA: 0s - loss: 0.2086 - accuracy: 0.9417
Epoch 54: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 445ms/step - loss: 0.2086 - accuracy: 0.9417 - val_loss: 2.8989 - val_accuracy: 0.3472
Epoch 55/100
45/45 [==============================] - ETA: 0s - loss: 0.2074 - accuracy: 0.9417
Epoch 55: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 446ms/step - loss: 0.2074 - accuracy: 0.9417 - val_loss: 2.9394 - val_accuracy: 0.3444
Epoch 56/100
45/45 [==============================] - ETA: 0s - loss: 0.2061 - accuracy: 0.9410
Epoch 56: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 446ms/step - loss: 0.2061 - accuracy: 0.9410 - val_loss: 2.9220 - val_accuracy: 0.3389
Epoch 57/100
45/45 [==============================] - ETA: 0s - loss: 0.1886 - accuracy: 0.9514
Epoch 57: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 446ms/step - loss: 0.1886 - accuracy: 0.9514 - val_loss: 2.9286 - val_accuracy: 0.3333
Epoch 58/100
45/45 [==============================] - ETA: 0s - loss: 0.1918 - accuracy: 0.9472
Epoch 58: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 446ms/step - loss: 0.1918 - accuracy: 0.9472 - val_loss: 2.9408 - val_accuracy: 0.3333
Epoch 59/100
45/45 [==============================] - ETA: 0s - loss: 0.1918 - accuracy: 0.9424
Epoch 59: val_accuracy did not improve from 0.35833
45/45 [==============================] - 21s 472ms/step - loss: 0.1918 - accuracy: 0.9424 - val_loss: 2.9616 - val_accuracy: 0.3361
Epoch 60/100
45/45 [==============================] - ETA: 0s - loss: 0.1911 - accuracy: 0.9424
Epoch 60: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 444ms/step - loss: 0.1911 - accuracy: 0.9424 - val_loss: 2.9689 - val_accuracy: 0.3333
Epoch 61/100
45/45 [==============================] - ETA: 0s - loss: 0.1828 - accuracy: 0.9458
Epoch 61: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 447ms/step - loss: 0.1828 - accuracy: 0.9458 - val_loss: 2.9638 - val_accuracy: 0.3333
Epoch 62/100
45/45 [==============================] - ETA: 0s - loss: 0.1862 - accuracy: 0.9396
Epoch 62: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 446ms/step - loss: 0.1862 - accuracy: 0.9396 - val_loss: 2.9723 - val_accuracy: 0.3333
Epoch 63/100
45/45 [==============================] - ETA: 0s - loss: 0.1807 - accuracy: 0.9424
Epoch 63: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 446ms/step - loss: 0.1807 - accuracy: 0.9424 - val_loss: 2.9732 - val_accuracy: 0.3361
Epoch 64/100
45/45 [==============================] - ETA: 0s - loss: 0.1731 - accuracy: 0.9542
Epoch 64: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 445ms/step - loss: 0.1731 - accuracy: 0.9542 - val_loss: 2.9762 - val_accuracy: 0.3389
Epoch 65/100
45/45 [==============================] - ETA: 0s - loss: 0.1733 - accuracy: 0.9576
Epoch 65: val_accuracy did not improve from 0.35833
45/45 [==============================] - 20s 445ms/step - loss: 0.1733 - accuracy: 0.9576 - val_loss: 2.9801 - val_accuracy: 0.3333
Epoch 65: early stopping

五、模型评估

acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(len(loss))

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、冻结卷积基,只训练全连接层的参数

from keras.applications import VGG16
from keras.models import Sequential
from keras.layers import Dense, Flatten, Dropout, Conv2D, MaxPool2D

vgg_model = VGG16(weights='imagenet', include_top=False, input_shape=(img_height, img_width, 3))


model = models.Sequential()
model.add(vgg_model)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(len(class_names), activation='softmax'))

在 Keras 中,冻结网络的方法是将其 trainable 属性设为 False。
首先查看有多少个权重张量:

print('This is the number of trainable weights before freezing the conv base:', len(model.trainable_weights))

输出:

This is the number of trainable weights before freezing the conv base: 30
一共13个conv层和2个Dense层,每层两个权重张量(主权重矩阵和偏置向量)

将vgg_model的trainable 属性设为 False:

# 冻结卷基层
vgg_model.trainable = False

再次查看:

print('This is the number of trainable weights after freezing the conv base:', len(model.trainable_weights))

输出:

This is the number of trainable weights after freezing the conv base: 4

如此设置之后,只有添加的两个 Dense 层的权重才会被训练。

2、设置lr、ModelCheckpoint、EarlyStopping

和之前一样。

# 设置初始学习率
initial_learning_rate = 1e-4

lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
    initial_learning_rate,
    decay_steps=60,
    decay_rate=0.96,
    staircase=True
)

optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)

model.compile(optimizer=optimizer,
              loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
from keras.callbacks import ModelCheckpoint, EarlyStopping

epochs = 100

# 保存最佳模型参数
checkpointer = ModelCheckpoint('best_model.h5',
                                monitor='val_accuracy',
                                verbose=1,
                                save_best_only=True,
                                save_weights_only=True)

# 设置早停
earlystopper = EarlyStopping(monitor='val_accuracy',
                             min_delta=0.001,
                             patience=20,
                             verbose=1)

3、再次训练

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

输出:

Epoch 1/100
51/51 [==============================] - ETA: 0s - loss: 15.5740 - accuracy: 0.1074
Epoch 1: val_accuracy improved from -inf to 0.35000, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 293s 845ms/step - loss: 15.5740 - accuracy: 0.1074 - val_loss: 2.6472 - val_accuracy: 0.3500
Epoch 2/100
51/51 [==============================] - ETA: 0s - loss: 2.6200 - accuracy: 0.2994
Epoch 2: val_accuracy improved from 0.35000 to 0.43611, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 9s 173ms/step - loss: 2.6200 - accuracy: 0.2994 - val_loss: 1.9987 - val_accuracy: 0.4361
Epoch 3/100
51/51 [==============================] - ETA: 0s - loss: 1.8993 - accuracy: 0.4179
Epoch 3: val_accuracy improved from 0.43611 to 0.52778, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 8s 159ms/step - loss: 1.8993 - accuracy: 0.4179 - val_loss: 1.7028 - val_accuracy: 0.5278
Epoch 4/100
51/51 [==============================] - ETA: 0s - loss: 1.6321 - accuracy: 0.4772
Epoch 4: val_accuracy improved from 0.52778 to 0.56944, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 10s 197ms/step - loss: 1.6321 - accuracy: 0.4772 - val_loss: 1.6052 - val_accuracy: 0.5694
Epoch 5/100
51/51 [==============================] - ETA: 0s - loss: 1.4345 - accuracy: 0.5136
Epoch 5: val_accuracy improved from 0.56944 to 0.59722, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 9s 186ms/step - loss: 1.4345 - accuracy: 0.5136 - val_loss: 1.5235 - val_accuracy: 0.5972
Epoch 6/100
51/51 [==============================] - ETA: 0s - loss: 1.2427 - accuracy: 0.5537
Epoch 6: val_accuracy improved from 0.59722 to 0.62222, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 9s 180ms/step - loss: 1.2427 - accuracy: 0.5537 - val_loss: 1.5024 - val_accuracy: 0.6222
Epoch 7/100
51/51 [==============================] - ETA: 0s - loss: 1.0806 - accuracy: 0.6130
Epoch 7: val_accuracy improved from 0.62222 to 0.62778, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 10s 190ms/step - loss: 1.0806 - accuracy: 0.6130 - val_loss: 1.4337 - val_accuracy: 0.6278
Epoch 8/100
51/51 [==============================] - ETA: 0s - loss: 0.9529 - accuracy: 0.6611
Epoch 8: val_accuracy improved from 0.62778 to 0.65833, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 9s 186ms/step - loss: 0.9529 - accuracy: 0.6611 - val_loss: 1.3828 - val_accuracy: 0.6583
Epoch 9/100
51/51 [==============================] - ETA: 0s - loss: 0.8889 - accuracy: 0.6716
Epoch 9: val_accuracy did not improve from 0.65833
51/51 [==============================] - 9s 186ms/step - loss: 0.8889 - accuracy: 0.6716 - val_loss: 1.3703 - val_accuracy: 0.6500
Epoch 10/100
51/51 [==============================] - ETA: 0s - loss: 0.7911 - accuracy: 0.7123
Epoch 10: val_accuracy improved from 0.65833 to 0.67222, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 9s 181ms/step - loss: 0.7911 - accuracy: 0.7123 - val_loss: 1.3866 - val_accuracy: 0.6722
Epoch 11/100
51/51 [==============================] - ETA: 0s - loss: 0.7064 - accuracy: 0.7543
Epoch 11: val_accuracy improved from 0.67222 to 0.68333, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 8s 167ms/step - loss: 0.7064 - accuracy: 0.7543 - val_loss: 1.3614 - val_accuracy: 0.6833
Epoch 12/100
51/51 [==============================] - ETA: 0s - loss: 0.6766 - accuracy: 0.7593
Epoch 12: val_accuracy did not improve from 0.68333
51/51 [==============================] - 8s 161ms/step - loss: 0.6766 - accuracy: 0.7593 - val_loss: 1.3141 - val_accuracy: 0.6806
Epoch 13/100
51/51 [==============================] - ETA: 0s - loss: 0.6331 - accuracy: 0.7728
Epoch 13: val_accuracy did not improve from 0.68333
51/51 [==============================] - 8s 160ms/step - loss: 0.6331 - accuracy: 0.7728 - val_loss: 1.3756 - val_accuracy: 0.6778
Epoch 14/100
51/51 [==============================] - ETA: 0s - loss: 0.5838 - accuracy: 0.7877
Epoch 14: val_accuracy improved from 0.68333 to 0.69167, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 9s 179ms/step - loss: 0.5838 - accuracy: 0.7877 - val_loss: 1.3946 - val_accuracy: 0.6917
Epoch 15/100
51/51 [==============================] - ETA: 0s - loss: 0.5148 - accuracy: 0.8123
Epoch 15: val_accuracy did not improve from 0.69167
51/51 [==============================] - 8s 160ms/step - loss: 0.5148 - accuracy: 0.8123 - val_loss: 1.4108 - val_accuracy: 0.6778
Epoch 16/100
51/51 [==============================] - ETA: 0s - loss: 0.4900 - accuracy: 0.8062
Epoch 16: val_accuracy did not improve from 0.69167
51/51 [==============================] - 8s 161ms/step - loss: 0.4900 - accuracy: 0.8062 - val_loss: 1.3512 - val_accuracy: 0.6889
Epoch 17/100
51/51 [==============================] - ETA: 0s - loss: 0.4834 - accuracy: 0.8173
Epoch 17: val_accuracy did not improve from 0.69167
51/51 [==============================] - 8s 161ms/step - loss: 0.4834 - accuracy: 0.8173 - val_loss: 1.3588 - val_accuracy: 0.6889
Epoch 18/100
51/51 [==============================] - ETA: 0s - loss: 0.4556 - accuracy: 0.8278
Epoch 18: val_accuracy improved from 0.69167 to 0.70000, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 10s 202ms/step - loss: 0.4556 - accuracy: 0.8278 - val_loss: 1.3768 - val_accuracy: 0.7000
Epoch 19/100
51/51 [==============================] - ETA: 0s - loss: 0.4350 - accuracy: 0.8457
Epoch 19: val_accuracy improved from 0.70000 to 0.70556, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 9s 167ms/step - loss: 0.4350 - accuracy: 0.8457 - val_loss: 1.3625 - val_accuracy: 0.7056
Epoch 20/100
51/51 [==============================] - ETA: 0s - loss: 0.3907 - accuracy: 0.8667
Epoch 20: val_accuracy improved from 0.70556 to 0.70833, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 9s 181ms/step - loss: 0.3907 - accuracy: 0.8667 - val_loss: 1.3188 - val_accuracy: 0.7083
Epoch 21/100
51/51 [==============================] - ETA: 0s - loss: 0.4094 - accuracy: 0.8414
Epoch 21: val_accuracy did not improve from 0.70833
51/51 [==============================] - 8s 162ms/step - loss: 0.4094 - accuracy: 0.8414 - val_loss: 1.3284 - val_accuracy: 0.7056
Epoch 22/100
51/51 [==============================] - ETA: 0s - loss: 0.3572 - accuracy: 0.8642
Epoch 22: val_accuracy did not improve from 0.70833
51/51 [==============================] - 9s 182ms/step - loss: 0.3572 - accuracy: 0.8642 - val_loss: 1.3724 - val_accuracy: 0.7083
Epoch 23/100
51/51 [==============================] - ETA: 0s - loss: 0.3208 - accuracy: 0.8827
Epoch 23: val_accuracy improved from 0.70833 to 0.72500, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 9s 185ms/step - loss: 0.3208 - accuracy: 0.8827 - val_loss: 1.3418 - val_accuracy: 0.7250
Epoch 24/100
51/51 [==============================] - ETA: 0s - loss: 0.3316 - accuracy: 0.8673
Epoch 24: val_accuracy did not improve from 0.72500
51/51 [==============================] - 8s 162ms/step - loss: 0.3316 - accuracy: 0.8673 - val_loss: 1.3702 - val_accuracy: 0.7222
Epoch 25/100
51/51 [==============================] - ETA: 0s - loss: 0.3231 - accuracy: 0.8796
Epoch 25: val_accuracy did not improve from 0.72500
51/51 [==============================] - 8s 162ms/step - loss: 0.3231 - accuracy: 0.8796 - val_loss: 1.3799 - val_accuracy: 0.7222
Epoch 26/100
51/51 [==============================] - ETA: 0s - loss: 0.2842 - accuracy: 0.8920
Epoch 26: val_accuracy did not improve from 0.72500
51/51 [==============================] - 8s 162ms/step - loss: 0.2842 - accuracy: 0.8920 - val_loss: 1.3978 - val_accuracy: 0.7250
Epoch 27/100
51/51 [==============================] - ETA: 0s - loss: 0.3163 - accuracy: 0.8784
Epoch 27: val_accuracy did not improve from 0.72500
51/51 [==============================] - 8s 163ms/step - loss: 0.3163 - accuracy: 0.8784 - val_loss: 1.4428 - val_accuracy: 0.7250
Epoch 28/100
51/51 [==============================] - ETA: 0s - loss: 0.2910 - accuracy: 0.8883
Epoch 28: val_accuracy did not improve from 0.72500
51/51 [==============================] - 9s 182ms/step - loss: 0.2910 - accuracy: 0.8883 - val_loss: 1.4257 - val_accuracy: 0.7111
Epoch 29/100
51/51 [==============================] - ETA: 0s - loss: 0.2721 - accuracy: 0.9025
Epoch 29: val_accuracy did not improve from 0.72500
51/51 [==============================] - 8s 161ms/step - loss: 0.2721 - accuracy: 0.9025 - val_loss: 1.4045 - val_accuracy: 0.7139
Epoch 30/100
51/51 [==============================] - ETA: 0s - loss: 0.2741 - accuracy: 0.8957
Epoch 30: val_accuracy improved from 0.72500 to 0.74167, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 9s 181ms/step - loss: 0.2741 - accuracy: 0.8957 - val_loss: 1.4440 - val_accuracy: 0.7417
Epoch 31/100
51/51 [==============================] - ETA: 0s - loss: 0.2596 - accuracy: 0.9123
Epoch 31: val_accuracy did not improve from 0.74167
51/51 [==============================] - 8s 160ms/step - loss: 0.2596 - accuracy: 0.9123 - val_loss: 1.4244 - val_accuracy: 0.7306
Epoch 32/100
51/51 [==============================] - ETA: 0s - loss: 0.2355 - accuracy: 0.9228
Epoch 32: val_accuracy did not improve from 0.74167
51/51 [==============================] - 8s 161ms/step - loss: 0.2355 - accuracy: 0.9228 - val_loss: 1.3860 - val_accuracy: 0.7417
Epoch 33/100
51/51 [==============================] - ETA: 0s - loss: 0.2562 - accuracy: 0.8963
Epoch 33: val_accuracy did not improve from 0.74167
51/51 [==============================] - 8s 162ms/step - loss: 0.2562 - accuracy: 0.8963 - val_loss: 1.4298 - val_accuracy: 0.7417
Epoch 34/100
51/51 [==============================] - ETA: 0s - loss: 0.2079 - accuracy: 0.9210
Epoch 34: val_accuracy did not improve from 0.74167
51/51 [==============================] - 8s 162ms/step - loss: 0.2079 - accuracy: 0.9210 - val_loss: 1.4304 - val_accuracy: 0.7417
Epoch 35/100
51/51 [==============================] - ETA: 0s - loss: 0.1996 - accuracy: 0.9191
Epoch 35: val_accuracy did not improve from 0.74167
51/51 [==============================] - 8s 165ms/step - loss: 0.1996 - accuracy: 0.9191 - val_loss: 1.4654 - val_accuracy: 0.7389
Epoch 36/100
51/51 [==============================] - ETA: 0s - loss: 0.2363 - accuracy: 0.9099
Epoch 36: val_accuracy improved from 0.74167 to 0.74722, saving model to /content/drive/MyDrive/app/T6/best_model2.h5
51/51 [==============================] - 10s 202ms/step - loss: 0.2363 - accuracy: 0.9099 - val_loss: 1.4391 - val_accuracy: 0.7472
Epoch 37/100
51/51 [==============================] - ETA: 0s - loss: 0.2303 - accuracy: 0.9160
Epoch 37: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 162ms/step - loss: 0.2303 - accuracy: 0.9160 - val_loss: 1.4713 - val_accuracy: 0.7444
Epoch 38/100
51/51 [==============================] - ETA: 0s - loss: 0.2382 - accuracy: 0.9068
Epoch 38: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 162ms/step - loss: 0.2382 - accuracy: 0.9068 - val_loss: 1.4830 - val_accuracy: 0.7417
Epoch 39/100
51/51 [==============================] - ETA: 0s - loss: 0.1997 - accuracy: 0.9235
Epoch 39: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 162ms/step - loss: 0.1997 - accuracy: 0.9235 - val_loss: 1.4800 - val_accuracy: 0.7278
Epoch 40/100
51/51 [==============================] - ETA: 0s - loss: 0.1872 - accuracy: 0.9278
Epoch 40: val_accuracy did not improve from 0.74722
51/51 [==============================] - 9s 182ms/step - loss: 0.1872 - accuracy: 0.9278 - val_loss: 1.4292 - val_accuracy: 0.7361
Epoch 41/100
51/51 [==============================] - ETA: 0s - loss: 0.1961 - accuracy: 0.9235
Epoch 41: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 161ms/step - loss: 0.1961 - accuracy: 0.9235 - val_loss: 1.4463 - val_accuracy: 0.7444
Epoch 42/100
51/51 [==============================] - ETA: 0s - loss: 0.2036 - accuracy: 0.9284
Epoch 42: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 162ms/step - loss: 0.2036 - accuracy: 0.9284 - val_loss: 1.4586 - val_accuracy: 0.7361
Epoch 43/100
51/51 [==============================] - ETA: 0s - loss: 0.1565 - accuracy: 0.9377
Epoch 43: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 162ms/step - loss: 0.1565 - accuracy: 0.9377 - val_loss: 1.4763 - val_accuracy: 0.7417
Epoch 44/100
51/51 [==============================] - ETA: 0s - loss: 0.1738 - accuracy: 0.9364
Epoch 44: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 162ms/step - loss: 0.1738 - accuracy: 0.9364 - val_loss: 1.4917 - val_accuracy: 0.7417
Epoch 45/100
51/51 [==============================] - ETA: 0s - loss: 0.1853 - accuracy: 0.9290
Epoch 45: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 162ms/step - loss: 0.1853 - accuracy: 0.9290 - val_loss: 1.4507 - val_accuracy: 0.7333
Epoch 46/100
51/51 [==============================] - ETA: 0s - loss: 0.1699 - accuracy: 0.9383
Epoch 46: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 162ms/step - loss: 0.1699 - accuracy: 0.9383 - val_loss: 1.4672 - val_accuracy: 0.7361
Epoch 47/100
51/51 [==============================] - ETA: 0s - loss: 0.1705 - accuracy: 0.9327
Epoch 47: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 161ms/step - loss: 0.1705 - accuracy: 0.9327 - val_loss: 1.4683 - val_accuracy: 0.7333
Epoch 48/100
51/51 [==============================] - ETA: 0s - loss: 0.1315 - accuracy: 0.9531
Epoch 48: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 162ms/step - loss: 0.1315 - accuracy: 0.9531 - val_loss: 1.4708 - val_accuracy: 0.7333
Epoch 49/100
51/51 [==============================] - ETA: 0s - loss: 0.1561 - accuracy: 0.9352
Epoch 49: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 161ms/step - loss: 0.1561 - accuracy: 0.9352 - val_loss: 1.4800 - val_accuracy: 0.7306
Epoch 50/100
51/51 [==============================] - ETA: 0s - loss: 0.1485 - accuracy: 0.9420
Epoch 50: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 161ms/step - loss: 0.1485 - accuracy: 0.9420 - val_loss: 1.5146 - val_accuracy: 0.7194
Epoch 51/100
51/51 [==============================] - ETA: 0s - loss: 0.1666 - accuracy: 0.9358
Epoch 51: val_accuracy did not improve from 0.74722
51/51 [==============================] - 9s 183ms/step - loss: 0.1666 - accuracy: 0.9358 - val_loss: 1.4996 - val_accuracy: 0.7389
Epoch 52/100
51/51 [==============================] - ETA: 0s - loss: 0.1649 - accuracy: 0.9333
Epoch 52: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 161ms/step - loss: 0.1649 - accuracy: 0.9333 - val_loss: 1.4963 - val_accuracy: 0.7361
Epoch 53/100
51/51 [==============================] - ETA: 0s - loss: 0.1534 - accuracy: 0.9432
Epoch 53: val_accuracy did not improve from 0.74722
51/51 [==============================] - 9s 182ms/step - loss: 0.1534 - accuracy: 0.9432 - val_loss: 1.4637 - val_accuracy: 0.7472
Epoch 54/100
51/51 [==============================] - ETA: 0s - loss: 0.1498 - accuracy: 0.9457
Epoch 54: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 161ms/step - loss: 0.1498 - accuracy: 0.9457 - val_loss: 1.4639 - val_accuracy: 0.7472
Epoch 55/100
51/51 [==============================] - ETA: 0s - loss: 0.1524 - accuracy: 0.9438
Epoch 55: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 162ms/step - loss: 0.1524 - accuracy: 0.9438 - val_loss: 1.4952 - val_accuracy: 0.7361
Epoch 56/100
51/51 [==============================] - ETA: 0s - loss: 0.1234 - accuracy: 0.9568
Epoch 56: val_accuracy did not improve from 0.74722
51/51 [==============================] - 8s 162ms/step - loss: 0.1234 - accuracy: 0.9568 - val_loss: 1.5060 - val_accuracy: 0.7389
Epoch 56: early stopping

4、Loss与Accuracy图

在这里插入图片描述
模型性能肉眼可见的提升!

model.evaluate(val_ds)

输出:

12/12 [==============================] - 1s 116ms/step - loss: 1.5060 - accuracy: 0.7389
[1.5059789419174194, 0.7388888597488403]

5、 指定图片进行预测

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

img = Image.open("/content/drive/MyDrive/app/T6/48-data/Tom Cruise/001_08212dcd.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 30ms/step
预测结果为: Tom Cruise

预测正确。

七、总结

直接使用官网的VGG16模型,效果很差。冻结VGG卷积基,只训练Dense层,增加Dropout,效果提升很大。

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【论文速读】| LLMCloudHunter:利用大语言模型(LLMs)从基于云的网络威胁情报(CTI)中自动提取检测规则

本次分享论文:LLMCloudHunter: Harnessing LLMs for Automated Extraction of Detection Rules from Cloud-Based CTI 基本信息 原文作者:Yuval Schwartz, Lavi Benshimol, Dudu Mimran, Yuval Elovici, Asaf Shabtai 作者单位:Ben-Gurion…

mfc100u.dll 文件缺失?两种方法快速修复丢失mfc100u.dll 文件难题

您的电脑是否遭遇了 mfc100u.dll 文件缺失的问题?这种情况通常由多种原因引起。在本文中,我们将介绍两种修复 mfc100u.dll 文件丢失问题的策略——一种是手动方法,另一种是自动修复的使用。我们将探讨如何有效地解决 mfc100u.dll 文件缺失的几…

Linux下git入门操作

0.创建仓库 可以按这个配置来,.gitignore中存放了上传时忽略的文件类型后缀。 1.clone仓库 在gitee上创建好仓库,点击克隆/下载, 复制地址fyehong/Linux_notes 。 在所需的文件夹中放置仓库。比如我在文件夹lesson9下存储仓库。就在less…

Python爬虫技术 第18节 数据存储

Python 爬虫技术常用于从网页上抓取数据,并将这些数据存储起来以供进一步分析或使用。数据的存储方式多种多样,常见的包括文件存储和数据库存储。下面我将通过一个简单的示例来介绍如何使用 Python 爬取数据,并将其存储为 CSV 和 JSON 文件格…

【数据结构】二叉树链式结构——感受递归的暴力美学

前言: 在上篇文章【数据结构】二叉树——顺序结构——堆及其实现中,实现了二叉树的顺序结构,使用堆来实现了二叉树这样一个数据结构;现在就来实现而二叉树的链式结构。 一、链式结构 链式结构,使用链表来表示一颗二叉树…

【机器学习】解开反向传播算法的奥秘

🌈个人主页: 鑫宝Code 🔥热门专栏: 闲话杂谈| 炫酷HTML | JavaScript基础 ​💫个人格言: "如无必要,勿增实体" 文章目录 解开反向传播算法的奥秘反向传播算法的概述反向传播算法的数学推导1. 前向传播2…

3.k8s:服务发布:service,ingress;配置管理:configMap,secret,热更新;持久化存储:volumes,nfs,pv,pvc

目录​​​​​​​ 一、服务发布 1.service (1)service和pod之间的关系 (2) service内部服务创建访问 (3)service访问外部服务 (4)基于域名访问外部 (5&#xff…

Prometheus各类监控及监控指标和告警规则

目录 linux docker监控 linux 系统进程监控 linux 系统os监控 windows 系统os监控 配置文件&告警规则 Prometheus配置文件 node_alert.rules docker_container.rules mysql_alert.rules vmware.rules Alertmanager告警规则 consoul注册服务 Dashboard JSON…

并发编程--volatile

1.什么是volatile volatile是 轻 量 级 的 synchronized,它在多 处 理器开 发 中保 证 了共享 变 量的 “ 可 见 性 ” 。可 见 性的意思是当一个 线 程 修改一个共享变 量 时 ,另外一个 线 程能 读 到 这 个修改的 值 。如果 volatile 变 量修 饰 符使用…

车载录像机:移动安全领域的科技新星

随着科技的飞速发展,人类社会的各个领域都在不断经历技术革新。其中,车载录像机作为安防行业与汽车技术结合的产物,日益受到人们的关注。它不仅体现了人类科技发展的成果,更在安防领域发挥了重要作用。本文将详细介绍车载录像机的…

Spring Boot集成canal快速入门demo

1.什么是canal? canal 是阿里开源的一款 MySQL 数据库增量日志解析工具,提供增量数据订阅和消费。 工作原理 MySQL主备复制原理 MySQL master 将数据变更写入二进制日志(binary log), 日志中的记录叫做二进制日志事件&#xff…

【QT】UDP

目录 核心API 示例:回显服务器 服务器端编写: 第一步:创建出socket对象 第二步: 连接信号槽 第三步:绑定端口号 第四步:编写信号槽所绑定方法 第五步:编写第四步中处理请求的方法 客户端…

Simulink代码生成: 基本模块的使用

文章目录 1 引言2 模块使用实例2.1 In/Out模块2.2 Constant模块2.3 Scope/Display模块2.4 Ground/Terminator模块 3 总结 1 引言 本文中博主介绍Simulink中最简单最基础的模块,包括In/Out模块(输入输出),Constant模块&#xff08…

Postman测试工具详细解读

目录 一、Postman的基本概念二、Postman的主要功能1. 请求构建2. 响应查看3. 断言与自动化测试4. 环境与变量5. 集合与文档化6. 与团队实时协作 三、Postman在API测试中的重要性1. 提高测试效率2. 保障API的稳定性3. 促进团队协作4. 生成文档与交流工具 四、Postman的使用技巧1…

CAS算法

CAS算法 1. CAS简介 CAS叫做CompareAndSwap,比较并交换,主要是通过处理器的指令来保证操作的原子性。 CAS基本概念 内存位置 (V):需要进行CAS操作的内存地址。预期原值 (A):期望该内存位置上的旧值。新值 (B):如果旧…

VSCode python autopep8 格式化 长度设置

ctrl, 打开设置 > 搜索autopep8 > 找到Autopep8:Args > 添加项--max-line-length150

Java泛型的介绍和基本使用

什么是泛型 ​ 泛型就是将类型参数化,比如定义了一个栈,你必须在定义之前声明这个栈中存放的数据的类型,是int也好是double或者其他的引用数据类型也好,定义好了之后这个栈就无法用来存放其他类型的数据。如果这时候我们想要使用这…

谷粒商城实战笔记-71-商品服务-API-属性分组-前端组件抽取父子组件交互

文章目录 一,一次性创建所有的菜单二,开发属性分组界面1,左侧三级分类树形组件2,右侧分组列表3,左右两部分通信3.1 子组件发送数据3.2,父组件接收数据 Vue的父子组件通信父组件向子组件传递数据子组件向父组…

SpringBoot添加密码安全配置以及Jwt配置

Maven仓库(依赖查找) 1、SpringBoot安全访问配置 首先添加依赖 spring-boot-starter-security 然后之后每次启动项目之后,访问任何的请求都会要求输入密码才能请求。(如下) 在没有配置的情况下,默认用户…

LLM agentic模式之工具使用: Gorilla

Gorilla Gorilla出自2023年5月的论文《Gorilla: Large Language Model Connected with Massive APIs》,针对LLM无法准确地生成API调用时的参数,构建API使用数据集后基于Llama微调了一个模型。 数据集构建 API数据集APIBench的构建过程如下&#xff1…