簡體   English   中英

機器學習 model 在測試數據上的表現比驗證數據差

[英]Machine Learning model performs worse on test data than validation data

我對機器學習很陌生。

首先,我想訓練一個 model 來對貓和狗的圖片進行分類。

我遇到的問題是,當我訓練我的 model 時,它在訓練數據和驗證數據上給了我(大約)80-85% 的准確度。 損失非常低,驗證數據和訓練數據的損失都在 0.4 - 0.5 左右。 因為這些數字非常相似,我懷疑我沒有過度擬合的問題,對吧?

但是,當我用數據集中的圖片(以前從未見過)測試我的 model 時,准確率大約是 70%-73%。 所以顯着降低。 我找不到任何關於為什么會這樣的信息。 而且,正如我所說,我懷疑過度擬合不是問題,但由於我是初學者,我不太確定。

我的 model 看起來像這樣(我在 python 中使用 tensorflow):

model = Sequential([
    Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding = 'same', input_shape=(224,224,3), kernel_initializer="he_uniform"),
    MaxPool2D(pool_size=(2, 2)),
    Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding = 'same', kernel_initializer="he_uniform"),
    MaxPool2D(pool_size=(2, 2)),
    Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding = 'same', kernel_initializer="he_uniform", kernel_regularizer=l2(.001)),
    MaxPool2D(pool_size=(2, 2)),
    Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding = 'same', kernel_initializer="he_uniform", kernel_regularizer=l2(.001)),
    MaxPool2D(pool_size=(2, 2)),
    Flatten(),
    Dense(units = 128, activation='relu'),
    Dropout(.5),
    Dense(units=2, activation='softmax')
])

Trainable params: 3,471,810
Non-trainable params: 0

優化器,損失:

model.compile(optimizer=Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy'])

這是我使用的數據集: https://www.kaggle.com/tongpython/cat-and-dog我使用 3000 張圖像進行訓練(1500 只狗和 1500 只貓),1000 張用於驗證,1000 張用於測試。 沒有重復(因此驗證集中沒有圖像,它們也在訓練集中等等)。

我像這樣預處理圖像(並且我還使用數據增強):

train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input, rescale=1/255, horizontal_flip=True, vertical_flip=True, width_shift_range=.2, height_shift_range=.2) \
    .flow_from_directory(directory=training_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=64)

valid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input, rescale=1/255, horizontal_flip=True, vertical_flip=True, width_shift_range=.2, height_shift_range=.2) \
    .flow_from_directory(directory=validation_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=64)

test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=test_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=64, shuffle=False)

編輯:解決了這個問題。 我的錯誤是我沒有以完全相同的方式預處理訓練、驗證和測試數據,因為我誤解了一個參數。 感謝所有幫助過我的人。

我相信您的問題是由於您擁有的驗證數據和訓練數據

train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input, rescale=1/255, .....

vgg16.preprocess_input function 在 +1 和 -1 之間重新縮放像素值,因此無需包含 rescale=1/255。 在您的測試生成器中,您不會重新調整像素值。 所以刪除訓練和驗證生成器中的 rescale=1/255

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM