为了账号安全,请及时绑定邮箱和手机立即绑定

Keras 推理损失和前向传播不匹配

Keras 推理损失和前向传播不匹配

喵喔喔 2022-06-22 17:39:17
我正在使用 Keras 预训练模型 ResNet50 来训练我自己的数据集,该数据集仅包含一张用于测试目的的图像。首先,我用我的图像评估模型,得到 0.5 的损失和 1 的准确度。然后,我拟合模型,得到 6 的损失和 0 的准确度。我不明白为什么推理的损失和前向传播不匹配。Keras 中的推理和前向传播的行为似乎不同。我附上了我的代码片段和它的屏幕截图。model = ResNet50(weights='imagenet')img_path = 'elephant.jpg'img = image.load_img(img_path, target_size=(224, 224))x = image.img_to_array(img)x = np.expand_dims(x, axis=0)x = preprocess_input(x)y = np.zeros((1, 1000))y[0, 386] = 1model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['categorical_accuracy'])model.evaluate(x, y)1/1 [===============================] - 1s 547ms/步 [0.5232877135276794, 1.0]model.fit(x, y, validation_data=(x, y))训练 1 个样本,验证 1 个样本 Epoch 1/1 1/1 [=============================] - 3s 3 秒/步 - 损失:6.1883 - categorical_accuracy:0.0000e+00 - val_loss:9.8371e-04 - val_categorical_accuracy:1.0000model.evaluate(x, y)1/1 [===============================] - 0s 74ms/步 [0.0009837078396230936, 1.0]
查看完整描述

1 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

很抱歉一开始误解了这个问题。这个问题非常棘手。并且问题很可能是由评论中提到的@Natthaphon 的 BatchNorm 层引起的,因为我在 VGG16 上尝试过,损失是匹配的。


然后我在 ResNet50 中进行了测试,即使我“冻结”了所有层,eval loss 和 fit loss 仍然不匹配。实际上,我手动检查了 BN 权重,它们确实没有改变。


from keras.applications import ResNet50, VGG16

from keras.applications.resnet50 import preprocess_input

from keras_preprocessing import image

import keras

from keras import backend as K

import numpy as np


img_path = '/home/zhihao/Downloads/elephant.jpeg'

img = image.load_img(img_path, target_size=(224, 224))


model = ResNet50(weights='imagenet')


for layer in model.layers:

    layer.trainable = False


x = image.img_to_array(img)

x = np.expand_dims(x, axis=0)

x = preprocess_input(x)


y = np.zeros((1, 1000))

y[0, 386] = 1


model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['categorical_accuracy'])


model.evaluate(x, y)

# 1/1 [==============================] - 2s 2s/step

# [0.2981376349925995, 1.0]


model.fit(x, y, validation_data=(x, y))

# Train on 1 samples, validate on 1 samples

# Epoch 1/1

# 1/1 [==============================] - 1s 549ms/step - loss: 5.3056 - categorical_accuracy: 0.0000e+00 - val_loss: 0.2981 - val_categorical_accuracy: 1.0000

我们可以注意到评估损失为 0.2981,拟合损失为 5.3056。我猜 Batch Norm 层在评估模式和训练模式之间有不同的行为。如我错了请纠正我。


真正冻结我发现的模型的一种方法是使用K.set_learning_phase(0)以下方法,


model = ResNet50(weights='imagenet')


K.set_learning_phase(0)  # all new operations will be in test mode from now on


model.fit(x, y, validation_data=(x, y))


# Train on 1 samples, validate on 1 samples

# Epoch 1/1

# 1/1 [==============================] - 4s 4s/step - loss: 0.2981 - categorical_accuracy: 1.0000 - val_loss: 16.1181 - val_categorical_accuracy: 0.0000e+00

现在这两个损失是匹配的。


查看完整回答
反对 回复 2022-06-22
  • 1 回答
  • 0 关注
  • 103 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号