簡體   English   中英

減少 CNN 模型中的驗證損失

[英]reducing validation loss in CNN Model

import tensorflow as tf
import tensorflow.keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
import pickle
import numpy as np
from keras.models import model_from_json
from keras.models import load_model
import matplotlib.pyplot as plt

# Opening the files about data
X = pickle.load(open("X.pickle", "rb"))
y = pickle.load(open("y.pickle", "rb"))

# normalizing data (a pixel goes from 0 to 255)
X = X/255.0

# Building the model
model = Sequential()
# 3 convolutional layers
model.add(Conv2D(32, (3, 3), input_shape = X.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))


model.add(Conv2D(64, (3, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.9))

# 5 hidden layers
model.add(Flatten())

model.add(Dense(128))
model.add(Activation("relu"))

model.add(Dense(128))
model.add(Activation("relu"))

model.add(Dense(128))
model.add(Activation("relu"))

model.add(Dense(128))
model.add(Activation("relu"))

model.add(Dense(128))
model.add(Activation("relu"))

# The output layer with 7 neurons, for 7 classes
model.add(Dense(13))
model.add(Activation("softmax"))

# Compiling the model using some basic parameters
model.compile(loss="sparse_categorical_crossentropy",
                optimizer="adam",
                metrics=["accuracy"])

# Training the model, with 40 iterations
# validation_split corresponds to the percentage of images used for the validation phase compared to all the images

print("X = " + str(len(X)))
print("y = " + str(len(y)))

history = model.fit(X, y, batch_size=32, epochs=1000, validation_split=0.1)

# Saving the model
model_json = model.to_json()
with open("model.json", "w") as json_file :
    json_file.write(model_json)

model.save_weights("model.h5")

print("Saved model to disk")

model.save('CNN.model')

# Printing a graph showing the accuracy changes during the training phase
print(history.history.keys())

plt.show()

plt.plot(history.history['accuracy'])

plt.plot(history.history['loss'])

plt.title('model accuracy')

plt.ylabel('accuracy')

plt.xlabel('epoch')

plt.legend(['train', 'validation'], loc='upper left')

plt.show()

問題是,我的訓練損失越來越小,但驗證准確度非常高。 並且驗證的准確性也極低。 我該如何解決這個問題? 我試圖將下降值增加到 0.9,但損失仍然高得多。 我也嘗試使用線性函數進行激活,但沒有用。

請幫忙。

模型丟失看起來像這樣

正如已經提到的,在沒有看到數據的情況下很難給出好的建議。

我會嘗試如下: - 在 maxpooling 層之后刪除 Dropout - 刪除一些密集層 - 在密集層之間添加 dropout

如果它仍然過度擬合,請在密集層之間添加 dropout

編輯:在我看到損失和准確性圖后,我建議如下:

  1. 最高優先級是獲取更多數據。
  2. 然后使用數據增強甚至增加你的數據集
  3. 如果額外的數據沒有幫助,則進一步降低神經網絡的復雜性(但我認為訓練會隨着數據的增加而減慢,並且驗證損失也會在更長的時期內減少)

數據增強是減少過度擬合的最佳技術。 嘗試使用數據生成器進行訓練和驗證集,以減少損失並提高准確性。

要了解有關增強和可用轉換的更多信息,請查看https://github.com/keras-team/keras-preprocessing

# Add our data-augmentation parameters to ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255.,
                               rotation_range = 40,
                               width_shift_range = 0.2,
                               height_shift_range = 0.2,
                               shear_range = 0.2,
                               zoom_range = 0.2,
                               horizontal_flip = True)

# Note that the validation data should not be augmented!
test_datagen = ImageDataGenerator(rescale = 1./255.)

# Flow training images in batches of 20 using train_datagen generator
train_generator = train_datagen.flow_from_directory(train_dir,
                                                batch_size = 20,
                                                class_mode = 'binary', 
                                                target_size = (150, 150))     

# Flow validation images in batches of 20 using test_datagen generator
validation_generator =  test_datagen.flow_from_directory(validation_dir,
                                                      batch_size  = 20,
                                                      class_mode  = 'binary', 
                                                      target_size = (150, 150)) 

# Now fit the training, validation generators to the CNN model
history = model.fit_generator(train_generator,
        validation_data = validation_generator,
        steps_per_epoch = 100,
        epochs = 3,
        validation_steps = 50,
        verbose = 2,callbacks=[callbacks])

暫無
暫無

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

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