2017-07-30 76 views
2

背景

在微調Keras中的分類模型時,它印刷了val_acc: 0.8456This code被用於微調。微調模型中的Keras精度差異

經過微調,手動加載訓練好的模型並預測評估集合,收到的準確度更低,爲0.28

下面的代碼被用於評估:

model = load_model(MODEL_PATH) 
... 
img = kimage.load_img(img_path, target_size=target_size) 
x = kimage.img_to_array(img) 
x = np.expand_dims(x, axis=0) 
x = vgg19.preprocess_input(x) 
pred = model.predict(x) 

問題

什麼可能是在精度0.85 != 0.28大差異的原因是什麼?

回答

2

您正在使用不同的預處理進行培訓和測試。 具體地說,

rescale = 1./255 

用於訓練,但

x = vgg19.preprocess_input(x) 

用於測試。

什麼imagenet_utils.preprocess_input()確實被扣除平均(計算機上ImageNet,由名字的建議):

# Zero-center by mean pixel 
    x[:, :, :, 0] -= 103.939 
    x[:, :, :, 1] -= 116.779 
    x[:, :, :, 2] -= 123.68 

所以這是相當不同的應用在你的訓練數據預處理。

1

使用相同的ImageDataGenerator

ImageDataGenerator是:

train_datagen = ImageDataGenerator(
    rescale=1./255, ...) 

之所以能夠重現其預處理如下:

img = load_img(image_path, target_size=target_size) 
x = img_to_array(img) 
x = np.expand_dims(x, axis=0) 
x *= rescale_factor 

score = model.predict(x)