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
現在這兩個損失是匹配的。
添加回答
舉報