Keras图像分类:显示高准确率但在测试图像上低

2024-04-26 08:12:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试对Caltech101数据集进行图像分类。我在Keras中使用了几个经过预训练的模型。我在训练集中使用了一些增强:

train_datagen = keras.preprocessing.image.ImageDataGenerator(
                    rescale=1./255, rotation_range=15,
                                   width_shift_range=0.1,
                                   height_shift_range=0.1,
                                   shear_range=0.01,
                                   zoom_range=[0.9, 1.25],
                                   horizontal_flip=False,
                                   vertical_flip=False,
                                   fill_mode='reflect',
                                   data_format='channels_last',
                                   brightness_range=[0.5, 1.5])
    validation_datagen = keras.preprocessing.image.ImageDataGenerator(rescale=1./255)

    train_generator = train_datagen.flow_from_directory(
                    train1_dir,  # Source directory for the training images
                    target_size=(image_size, image_size),
                    batch_size=batch_size)

    validation_generator = validation_datagen.flow_from_directory(
                    validation_dir, # Source directory for the validation images
                    target_size=(image_size, image_size),
                    batch_size=batch_size)

我还使用了一些早期停止(100个时代之后停止):

^{pr2}$

首先我训练最后一层:

base_model.trainable = False
    model = tf.keras.Sequential([
      base_model,
      keras.layers.GlobalAveragePooling2D(),
      keras.layers.Dense(num_classes, activation='softmax')
    ])
    model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=0.0001),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    epochs = 10000
    steps_per_epoch = train_generator.n // batch_size
    validation_steps = validation_generator.n // batch_size
    history = model.fit_generator(train_generator,
                                  steps_per_epoch = steps_per_epoch,
                                  epochs=epochs,
                                  workers=4,
                                  validation_data=validation_generator,
                                  validation_steps=validation_steps, 
                                  callbacks=callbacks)

然后,我按照Keras教程训练前面的层:

# After top classifier is trained, we finetune the layers of the network
base_model.trainable = True
# Let's take a look to see how many layers are in the base model
print("Number of layers in the base model: ", len(base_model.layers))
# Fine tune from this layer onwards
fine_tune_at = 1
# Freeze all the layers before the `fine_tune_at` layer
for layer in base_model.layers[:fine_tune_at]:
    layer.trainable =  False

model.compile(optimizer = tf.keras.optimizers.RMSprop(lr=2e-5),
              loss='categorical_crossentropy',
              metrics=['accuracy'])
epochs = 10000
history_fine = model.fit_generator(train_generator,
                                   steps_per_epoch = steps_per_epoch,
                                   epochs=epochs,
                                   workers=4,
                                   validation_data=validation_generator,
                                   validation_steps=validation_steps, 
                                   callbacks=callbacks
                                   )

最后,在模型完成训练之后,我在一个单独的测试集中手动测试它

label_list = train_generator.class_indices
    numeric_to_class = {}
    for key, val in label_list.items():
        numeric_to_class[val] = key
    total_num_images = 0
    acc_num_images = 0
    with open("%s_prediction_%s.txt" % (dataset_name, model_name), "wt") as fid:
        fid.write("Label list:\n")
        for label in label_list:
            fid.write("%s," % label)
        fid.write("\n")
        fid.write("true_class,predicted_class\n")
        fid.write("--------------------------\n")
        for label in label_list:
            testing_dir = os.path.join(test_dir, label)
            for img_file in os.listdir(testing_dir):
                img = cv2.imread(os.path.join(testing_dir, img_file))
                img_resized = cv2.resize(img, (image_size, image_size), interpolation = cv2.INTER_AREA)                
                img1 = np.reshape(img_resized, (1, img_resized.shape[0], img_resized.shape[1], img_resized.shape[2]))
                pred_class_num = model.predict_classes(img1)
                pred_class_num = pred_class_num[0]
                true_class_num = label_list[label]                
                predicted_label = numeric_to_class[pred_class_num]               
                fid.write("%s,%s\n" % (label, predicted_label))
                if predicted_label == label:
                    acc_num_images += 1
                total_num_images += 1

    acc = acc_num_images / (total_num_images * 1.0)

我不得不这样做,因为库不输出F1分数。然而,我发现val U acc上升非常高(约0.8),但在训练后的测试阶段,准确度非常低(我想大约是0.1)。我不明白为什么会这样。请帮帮我,非常感谢。在

2019年10月15日更新:我尝试在网络顶部训练一个线性支持向量机,而没有进行任何微调,我使用VGG16(带RMSProp优化器)在Caltech101上获得了70%的准确率。不过,如果这不是最好的选择。在

更新2:我在我的自定义数据集上使用了Daniel Moller建议的预处理部分(大约450个图像,283个类“打开”,203个类“关闭”,当使用“提前停止”时,耐心=100,只需训练最后一个层:

model = tf.keras.Sequential([
  base_model,
  keras.layers.GlobalAveragePooling2D(),
  keras.layers.Dense(num_classes, activation='softmax')
])

Accuracy

Loss

更新3:我也尝试在VGG16中使用最后一个完全连接的层,并在每个层之后添加了dropout层,并使用dropout rate(速率设置为0) 60%,耐心=10(提前停车):

base_model = tf.keras.applications.VGG16(input_shape=IMG_SHAPE, \
                                               include_top=True, \
                                               weights='imagenet')
base_model.layers[-3].Trainable = True
base_model.layers[-2].Trainable = True
fc1 = base_model.layers[-3]
fc2 = base_model.layers[-2]
predictions = keras.layers.Dense(num_classes, activation='softmax')
dropout1 = Dropout(0.6)
dropout2 = Dropout(0.6)
x = dropout1(fc1.output)
x = fc2(x)
x = dropout2(x)
predictors = predictions(x)
model = Model(inputs=base_model.input, outputs=predictors)

我得到了最高的验证精度0.93750,测试精度:0.966216。图: enter image description hereAccuracy


Tags: theimageimgbasesizemodellayerssteps
1条回答
网友
1楼 · 发布于 2024-04-26 08:12:05

主要问题是:

在中打开要预测的图像时,您忘记重新缩放1/255.

.....
img = cv2.imread(os.path.join(testing_dir, img_file))
img_resized = cv2.resize(img, (image_size, image_size), interpolation = cv2.INTER_AREA)
img1 = np.reshape(img_resized, 
                  (1, img_resized.shape[0], img_resized.shape[1], img_resized.shape[2]))

#you will probably need:
img1 = img1/255.

pred_class_num = model.predict_classes(img1)
...........

另外,请注意,cv2将以BGR格式打开图像,而Keras可能以RGB打开它们。在

  • 从Keras生成器获取图像
  • 通过图像打开
  • 绘制这些图像,检查它们是否看起来正常(或者至少相同,如果一切都是BGR,这不是问题,尽管所有的图看起来都很有趣)

示例:

^{pr2}$

matplotlib绘制这些图像。
还要确保它们的范围相同:

plt.imshow(keras_train)
plt.plot()
plt.imshow(keras_val)
plt.plot()
plt.imshow(your_image)
plt.plot()
print(keras_train.max(), keras_val.max(), img1.max())

您可能需要使用np.flip(images, axis=-1)将BGR转换为RGB。在

导入keras模型的提示

如果您从keras导入了基本模型,您应该从同一个模块导入预处理,并使用keras图像打开器。这将消除可能的不匹配:

^{4}$

在两个生成器中,使用预处理函数:

#no rescale, only preprocessing function
train_datagen = keras.preprocessing.image.ImageDataGenerator(
                               rotation_range=15,
                               width_shift_range=0.1,
                               height_shift_range=0.1,
                               shear_range=0.01,
                               zoom_range=[0.9, 1.25],
                               horizontal_flip=False,
                               vertical_flip=False,
                               fill_mode='reflect',
                               data_format='channels_last',
                               brightness_range=[0.5, 1.5],
                               preprocessing_function=preprocess_input)
validation_datagen = keras.preprocessing.image.ImageDataGenerator(
                               preprocessing_function=preprocess_input)

加载并预处理图像以进行预测:

img = image.load_img(img_path, target_size=(image_size,image_size))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

更多信息:https://keras.io/applications/

其他可能性

但可能还有其他一些事情,比如:

  • 非常强烈的过度适应,你的耐心是100!通常这是一个非常高的数字。但要确认这一点,唯一的办法就是检查瓦卢acc是否比最好的那一个上升太多。
    • 建议:在开始预测之前重新加载ModelCheckpoint保存的最佳模型
    • 建议:张贴acc的历史,这样我们就可以看到它是否是坏的
  • 基本模型是否有BatchNormalization层?-当您冻结一个批处理规范化层时,它将保持moving_mean和{}不变,但是这些值是用不同的数据训练的。如果您的数据没有相同的均值和方差,您的模型将得到很好的训练,但验证将是一场灾难
    • 建议:查看验证损失/acc是否从一开始就严重错误
    • 建议:不要冻结基本模型中的BatchNormalization层(迭代这些层,只冻结其他类型)
    • 建议:如果您碰巧知道用于训练基本模型的数据库,请将您的数据平均值和变化量与该数据库的平均值和变量进行比较(分别对待每个通道)

相关问题 更多 >