为什么我的验证准确率很低(50%)而训练准确率超过 95%?

问题描述 投票:0回答:0

我想使用 EfficientNetB2 训练一个神经网络,虽然训练阶段进展顺利,但验证总是在 50%。 这是我的代码:

import numpy as np
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator 


from google.colab import drive
drive.mount('/content/gdrive')

!unzip /content/gdrive/MyDrive/chest-xray-pneumonia.zip

img = tf.keras.utils.image_dataset_from_directory(directory='/content/chest_xray/train')
img_val = tf.keras.utils.image_dataset_from_directory(directory='/content/chest_xray/val')
img_test = tf.keras.utils.image_dataset_from_directory(directory='/content/chest_xray/test')

# Data augmentation
IMG_SIZE = 128
data_augmentation = tf.keras.Sequential([
  tf.keras.layers.Resizing(IMG_SIZE, IMG_SIZE),
  tf.keras.layers.Rescaling(1./255),
  tf.keras.layers.RandomTranslation(0.2,0.2),
  tf.keras.layers.RandomRotation(0.3),
  tf.keras.layers.RandomFlip("horizontal"),
  tf.keras.layers.RandomZoom(0.2)
])

data_resize = tf.keras.Sequential([
  tf.keras.layers.Resizing(IMG_SIZE, IMG_SIZE),
  tf.keras.layers.Rescaling(1./255)
])

img_aug = img.map(
  lambda x, y: (data_augmentation(x, training=True), y))

img_val_aug = img_val.map(
  lambda x, y: (data_resize(x, training=True), y))
img_test_aug = img_test.map(
  lambda x, y: (data_resize(x, training=True), y))
img_aug.shuffle(100)
# model structure 
base_model = tf.keras.applications.EfficientNetB2(input_shape=(128,128,3),
                                               include_top=False,
                                               weights='imagenet')
base_model.trainable = True

my_model = tf.keras.models.Sequential([
  base_model,
  tf.keras.layers.GlobalAveragePooling2D(),
  tf.keras.layers.Dense(128 , activation='relu'),
  tf.keras.layers.Dropout(0.3),
  tf.keras.layers.Dense(64 , activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(1 , activation='sigmoid')
  ])

my_model.summary()
# Balancing Labels 
normal_count = 1341
pneumonia_count = 3875
total_count = normal_count + pneumonia_count

normal_weight = (1 / normal_count) * (total_count) / 2.0
pneumonia_weight = (1 / pneumonia_count) * (total_count) / 2.0

class_weight = {0: normal_weight,
                1: pneumonia_weight}
# Compile and train
base_learning_rate = 0.001

my_model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=base_learning_rate),
              loss=tf.keras.losses.BinaryCrossentropy(),
              metrics=['accuracy'])

history = my_model.fit(img_aug,
                    epochs=5,
                    batch_size = 128,
                    class_weight=class_weight)

# Results 
print(my_model.evaluate(img_val_aug))

我尝试了不同的方法来导入数据集,但结果总是一样的。根据这篇文章使用 EfficientNet 从胸部 X 光图像分类自动诊断肺炎 我应该获得超过 90% 的验证和测试集准确率。

python deep-learning neural-network conv-neural-network efficientnet
© www.soinside.com 2019 - 2024. All rights reserved.