0

我们的 val_loss 和 val_acc 有一些问题。在几个 epoch(大约 30 个)之后,val_acc 下降了大约 50-60%,而 val_loss 增加到了 0.98 - 1.4 之间(见下图)。帖子的最后是第 45 个时代的结束。

在此处输入图像描述 [损失和 Val_Loss]

    import pickle
    from datetime import time
    import matplotlib.pyplot as plt
    import numpy as np
    import tf as tf
    from keras import optimizers
    from keras.models import Sequential
    from keras.layers import *
    from keras.callbacks import TensorBoard
    from keras.utils import np_utils

    pickle_in = open("X.pickle", "rb")
    X = pickle.load(pickle_in)

    pickle_in = open("y.pickle", "rb")
    y = pickle.load(pickle_in)

    pickle_in = open("PredictionData\\X_Test.pickle", "rb")
    X_Test = pickle.load(pickle_in)

    X = X/255.0
    X_Test = X_Test/255.0

    y = np_utils.to_categorical(y, 5)

    NAME = "Emotion Detection"

    model = Sequential()

    model.add(Conv2D(32, (1, 1), activation="relu", use_bias=True,
                     bias_initializer="Ones",
                     input_shape=(145, 65, 1),
                     dim_ordering="th"))

    model.add(Conv2D(64, (3, 3),
                     activation="relu"))

    model.add(Conv2D(128, (3, 3),
                     activation="relu"))
    model.add(Dropout(0.2))

    model.add(Conv2D(64, (3, 3),
                     activation="relu"))

    model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors

    model.add(Dense(128,
                    activation="relu"))
    model.add(Dropout(0.2))

    model.add(Dense(32,
                    activation="relu"))

    model.add(Dense(5,
                    activation='sigmoid'))

    tensorboard = TensorBoard(log_dir="Tensorboard\\".format(time))

    sgd = optimizers.SGD(lr=0.001, decay=1e-6,
                         momentum=0.9, nesterov=True)

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

    history = model.fit(X, y, batch_size=16,
                        epochs=45, validation_split=0.12,
                        callbacks=[tensorboard])


    plt.plot(history.history['accuracy'])
    plt.plot(history.history['val_accuracy'])
    plt.title('Model accuracy')
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['Accuracy', 'Val_Accuracy'], loc='upper left')
    plt.show()

    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('Model loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['Loss', 'Val_Loss'], loc='upper left')
    plt.show()

    classes = model.predict(X_Test)
    plt.bar(range(5), classes[0])
    plt.show()
    print("prediction: class", np.argmax(classes[0]))


    model.summary()

    model.save("TrainedModel\\emotionDetector.h5") 

2493/2493 [==============================] - 35s 14ms/步 - 损失:0.2324 - 准确度:0.9202 - val_loss :1.3789 - val_accuracy:0.6353

_________________________________________________________________
Layer (type)                 Output Shape              Param    
=================================================================
conv2d_1 (Conv2D)            (None, 32, 65, 1)         4672      
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 30, 63, 64)        640       
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 28, 61, 128)       73856     
_________________________________________________________________
dropout_1 (Dropout)          (None, 28, 61, 128)       0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 26, 59, 64)        73792     
_________________________________________________________________
flatten_1 (Flatten)          (None, 98176)             0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               12566656  
_________________________________________________________________
dropout_2 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 32)                4128      
_________________________________________________________________
dense_3 (Dense)              (None, 5)                 165       
_________________________________________________________________
Total params: 12,723,909
Trainable params: 12,723,909
Non-trainable params: 0
_________________________________________________________________

希望你能帮助我们。提前致谢。

4

1 回答 1

0

这些图是过拟合的典型例子。我建议观看它https://en.wikipedia.org/wiki/Overfitting

于 2020-04-19T16:05:48.380 回答