验证精度高,而火车精度低

时间:2020-03-15 16:06:15

标签: validation keras deep-learning

我正在一个项目中,我想使用从受影响和健康受试者的自然言语片段中获得的频谱图图像对阿尔茨海默氏痴呆症进行分类。最初,我从头开始使用VGG16架构,并使用其权重获得离散结果,然后使用InceptionV3更改了架构,这里出现了问题:查看我的结果,可以看到训练精度如何保持在52%左右,而验证精度却相反从33%上升到98%(峰值也达到100%),我不知道这怎么可能。我知道数据集很小(用于训练的图像约450张,用于验证的图像约50张)结果的可变性可能很高,但并非如此。你们中的任何人都知道为什么会这样吗,以防万一,如何解决问题?代码如下:

from keras.applications.inception_v3 import InceptionV3

train_dir = 'C:/Users/marto/Desktop/Datasets/3. Dem@Care_Resized_V3/Train'
validation_dir = 'C:/Users/marto/Desktop/Datasets/3. Dem@Care_Resized_V3/Val'

### Instantiate convolutional base

img_width, img_height, channels = 299, 299, 3

train_size, validation_size = 436, 48

conv_base = InceptionV3(weights= None, #'imagenet',
                  include_top=False,
                  input_shape=(img_width, img_height, channels))

### Estracting features from InceptionV3 architecture 

from keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(rescale=1./255)
batch_size = 4

def extract_features(directory, sample_count):
    features = np.zeros(shape=(sample_count, 8, 8, 2048))
    labels = np.zeros(shape=(sample_count))

    # Preprocess data
    generator = datagen.flow_from_directory(directory,
                                            target_size=(img_width,img_height),
                                            batch_size = batch_size,
                                            class_mode='binary',
                                            shuffle = True)

    # Pass data through convolutional base
    i = 0
    for inputs_batch, labels_batch in generator:
        features_batch = conv_base.predict(inputs_batch)
        features[i * batch_size: (i + 1) * batch_size] = features_batch
        labels[i * batch_size: (i + 1) * batch_size] = labels_batch
        i += 1
        if i * batch_size >= sample_count:
            break
    return features, labels

train_features, train_labels = extract_features(train_dir, train_size)  
validation_features, validation_labels = extract_features(validation_dir, validation_size)

################################ MODELING #####################################

from keras import models
from keras import layers
from keras import optimizers
import keras_metrics as km
from keras.optimizers import RMSprop
from sklearn.model_selection import StratifiedKFold

seed = 42
epochs = 50

kfold = StratifiedKFold(n_splits=5, shuffle=False, random_state = seed)
cv_scores = []

### Defining the Model

for train, val in kfold.split(train_features, train_labels):

    model = models.Sequential()
    model.add(layers.Flatten(input_shape=(8,8,2048)))
    model.add(layers.Dense(128, activation='tanh', input_dim=(8*8*2048)))
    model.add(layers.Dropout(0.3))
    model.add(layers.Dense(1, activation='sigmoid'))

    ### Compiling the  model

    model.compile(optimizer = RMSprop(lr= 0.001),
                  loss='binary_crossentropy',
                  metrics=['acc', km.binary_precision(), km.binary_recall()])

    ### Training the Model

    history = model.fit(train_features, train_labels,
                        epochs=epochs,
                        batch_size=batch_size, 
                        validation_data=(validation_features, validation_labels))```


These reported are the first 10 epochs of the first CV_Folder:

Train on 436 samples, validate on 48 samples
Epoch 1/50
436/436 [==============================] - 19s 42ms/step - loss: 0.7451 - acc: 0.5046 - precision: 0.5263 - recall: 0.5677 - val_loss: 0.6611 - val_acc: 0.9792 - val_precision: 1.0000 - val_recall: 0.9792
Epoch 2/50
436/436 [==============================] - 17s 39ms/step - loss: 0.7204 - acc: 0.4908 - precision: 0.5142 - recall: 0.5546 - val_loss: 0.5067 - val_acc: 1.0000 - val_precision: 1.0000 - val_recall: 1.0000
Epoch 3/50
436/436 [==============================] - 17s 39ms/step - loss: 0.7107 - acc: 0.5115 - precision: 0.5317 - recall: 0.5852 - val_loss: 0.3703 - val_acc: 1.0000 - val_precision: 1.0000 - val_recall: 1.0000
Epoch 4/50
436/436 [==============================] - 17s 39ms/step - loss: 0.7148 - acc: 0.4977 - precision: 0.5189 - recall: 0.5983 - val_loss: 0.6066 - val_acc: 0.9792 - val_precision: 1.0000 - val_recall: 0.9792
Epoch 5/50
436/436 [==============================] - 17s 39ms/step - loss: 0.7219 - acc: 0.4771 - precision: 0.5019 - recall: 0.5808 - val_loss: 0.7211 - val_acc: 0.3333 - val_precision: 1.0000 - val_recall: 0.3333
Epoch 6/50
436/436 [==============================] - 17s 40ms/step - loss: 0.7028 - acc: 0.5344 - precision: 0.5504 - recall: 0.6201 - val_loss: 0.4808 - val_acc: 1.0000 - val_precision: 1.0000 - val_recall: 1.0000
Epoch 7/50
436/436 [==============================] - 17s 40ms/step - loss: 0.7044 - acc: 0.5596 - precision: 0.5761 - recall: 0.6114 - val_loss: 0.5336 - val_acc: 0.9792 - val_precision: 1.0000 - val_recall: 0.9792
Epoch 8/50
436/436 [==============================] - 17s 40ms/step - loss: 0.7053 - acc: 0.5321 - precision: 0.5490 - recall: 0.6114 - val_loss: 0.5413 - val_acc: 0.9792 - val_precision: 1.0000 - val_recall: 0.9792
Epoch 9/50
436/436 [==============================] - 17s 39ms/step - loss: 0.7008 - acc: 0.5344 - precision: 0.5504 - recall: 0.6201 - val_loss: 0.4413 - val_acc: 1.0000 - val_precision: 1.0000 - val_recall: 1.0000
Epoch 10/50
436/436 [==============================] - 17s 39ms/step - loss: 0.7043 - acc: 0.5413 - precision: 0.5535 - recall: 0.6550 - val_loss: 0.6734 - val_acc: 0.6667 - val_precision: 1.0000 - val_recall: 0.6667

0 个答案:

没有答案