簡體   English   中英

相同輸入 Keras 的不同訓練和驗證結果

[英]Different training and validation results with same input Keras

我正在嘗試為不同的多分類目的重新訓練 MobileNet:

train_datagen = ImageDataGenerator(
     preprocessing_function = preprocess_input

training_generator = train_datagen.flow_from_directory(
    directory = train_data_dir,
    target_size=(parameters["img_width"], parameters["img_height"]),
    batch_size = parameters["batch_size"],
    class_mode= "categorical",
    subset = "training",
    color_mode = "rgb",
    seed = 42)

# Define the Model
base_model = MobileNet(weights='imagenet', 
                       include_top=False, input_shape = (128, 128, 3)) #imports the mobilenet model and discards the last 1000 neuron layer.

# Let only the last n layers as trainable
for layer in base_model.layers:
    layer.trainable = False


x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(800,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results.
x = Dense(600,activation='relu')(x) #dense layer 2
x = Dropout(0.8)(x)
x = Dense(256,activation='relu')(x) #dense layer 3
x = Dropout(0.2)(x)
preds = Dense(N_classes, activation='softmax')(x) #final layer with softmax activation
model= Model(inputs = base_model.input, outputs = preds)

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

並將訓練設置作為驗證數據集,訓練集為

history = model.fit_generator(
        training_generator,
        steps_per_epoch= training_generator.n // parameters["batch_size"],

        epochs = parameters["epochs"]
        ,
        ##### VALIDATION SET = TRAINING 
        validation_data = training_generator,
        validation_steps = training_generator.n // parameters["batch_size"],

        callbacks=[
                EarlyStopping(monitor = "acc", patience = 8, restore_best_weights=False),
                ReduceLROnPlateau(patience = 3)]
        )

但是,即使在訓練時它們是相同的數據集,我在 TRAINING AND VALIDATION ACCURACY 之間的准確性確實存在顯着差異 可能是因為什么?

訓練截圖

訓練神經網絡涉及訓練數據庫中數據的隨機分布。 因此,結果不可重現。 如果您在准確性上有顯着差異,您可以嘗試:

  1. 獲得更大的培訓數據庫;
  2. 重新訓練網絡;
  3. 獲得具有更一致結果的數據庫。

LE:如果你在訓練時的准確性有顯着差異並不重要。 訓練是一個迭代優化過程,它最小化均方誤差目標函數。 實現這個目標需要一段時間。

我不知道確切原因,但我重復了你的問題。 出現問題是因為您使用的是 SAME 生成器,該生成器運行用於訓練,然后再次用於驗證。 如果您創建一個用於驗證的單獨生成器,該生成器將相同的訓練數據作為輸入,那么一旦您運行足夠多的時期以使訓練准確度進入 90% 的范圍,您將看到驗證准確度穩定下來並向訓練准確度收斂Train-Valid Acc與紀元

暫無
暫無

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

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