关于model.fit参数问题,运行时报错,找不到原因了

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input,Flatten,Dense,Dropout,GlobalAveragePooling2D
from tensorflow.keras.applications.mobilenet import MobileNet,preprocess_input
import math

TRAIN_DATA_DIR = '/kaggle/working/train'
VALIDATION_DATA_DIR = '/kaggle/working/test'
TRAIN_SAMPLES = 25000
VALIDATION_SAMPLES = 12500
NUM_CLASSES = 2
IMG_WIGTH,IMG_HEIGHT=224,224
BATCH_SIZE = 500

train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
                                   rotation_range=20,
                                   width_shift_range=0.2,
                                   height_shift_range=0.2,
                                   zoom_range=0.2)
val_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)

train_generator = train_datagen.flow_from_directory(TRAIN_DATA_DIR,
                                                    target_size=(IMG_WIGTH,
                                                                 IMG_HEIGHT),
                                                    batch_size=BATCH_SIZE,
                                                    shuffle=True,
                                                    seed=12345,
                                                    class_mode='categorical')
validation_generator = val_datagen.flow_from_directory(
    VALIDATION_DATA_DIR,
    target_size=(IMG_WIGTH, IMG_HEIGHT),
    batch_size=BATCH_SIZE,
    shuffle=False,
    class_mode='categorical')

def model_maker():
    base_model = MobileNet(include_top=False, input_shape=(IMG_WIGTH, IMG_HEIGHT, 3))
    for layer in base_model.layers[:]:
        layer.trainable = False
    input = Input(shape=(IMG_WIGTH, IMG_HEIGHT, 3))
    custom_model = base_model(input)
    custom_model = GlobalAveragePooling2D()(custom_model)
    custom_model = Dense(64, activation='relu')(custom_model)
    custom_model = Dropout(0.5)(custom_model)
    predictions = Dense(NUM_CLASSES, activation='softmax')(custom_model)
    return Model(inputs=input, outputs=predictions)

model = model_maker()
model.compile(loss='categorical_crossentropy',
              optimizer=tf.keras.optimizers.Adam(0.001),
              metrics=['acc'])
model.fit(
    train_generator,
    steps_per_epoch=math.ceil(float(TRAIN_SAMPLES) / BATCH_SIZE),
    validation_data=validation_generator,
    validation_steps=math.ceil(float(VALIDATION_SAMPLES) / BATCH_SIZE))

运行时提示以下错误,求解答

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_27/783854733.py in 
     18     steps_per_epoch=math.ceil(float(TRAIN_SAMPLES) / BATCH_SIZE),
     19     validation_data=validation_generator,
---> 20     validation_steps=math.ceil(float(VALIDATION_SAMPLES) / BATCH_SIZE))
     21 
     22 

/opt/conda/lib/python3.7/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     68             # To get the full stack trace, call:
     69             # `tf.debugging.disable_traceback_filtering()`
---> 70             raise e.with_traceback(filtered_tb) from None
     71         finally:
     72             del filtered_tb

/opt/conda/lib/python3.7/site-packages/keras/preprocessing/image.py in __getitem__(self, idx)
    104                 "Asked to retrieve element {idx}, "
    105                 "but the Sequence "
--> 106                 "has length {length}".format(idx=idx, length=len(self))
    107             )
    108         if self.seed is not None:

ValueError: Asked to retrieve element 0, but the Sequence has length 0


该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:

根据报错信息,可以看到 validation_generator 的长度为0,这意味着数据生成器没有生成任何数据。这可能是因为在指定 validation_data_dir 时出现了错误。

您可以尝试检查以下问题:

1、 VALIDATION_DATA_DIR 是否指向正确的路径。
2、 检查 VALIDATION_DATA_DIR 目录中是否包含带有 .jpg 或 .png 扩展名的图像文件。
3、 确保 validation_generator 生成器使用正确的 class_mode 参数,以匹配 VALIDATION_DATA_DIR 中的数据集格式。

请注意,此处只是修改了 VALIDATION_DATA_DIR 的路径,以及在 validation_generator 中设置了正确的 class_mode 参数。如果还有其他错误,请继续调试并查找其他问题。修改后的代码如下:

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input,Flatten,Dense,Dropout,GlobalAveragePooling2D
from tensorflow.keras.applications.mobilenet import MobileNet,preprocess_input
import math
 
TRAIN_DATA_DIR = '/kaggle/working/train'
VALIDATION_DATA_DIR = '/kaggle/working/validation'  # 修改为正确的验证数据集路径
TRAIN_SAMPLES = 25000
VALIDATION_SAMPLES = 12500
NUM_CLASSES = 2
IMG_WIGTH,IMG_HEIGHT=224,224
BATCH_SIZE = 500
 
train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input,
                                   rotation_range=20,
                                   width_shift_range=0.2,
                                   height_shift_range=0.2,
                                   zoom_range=0.2)
val_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
 
train_generator = train_datagen.flow_from_directory(TRAIN_DATA_DIR,
                                                    target_size=(IMG_WIGTH,
                                                                 IMG_HEIGHT),
                                                    batch_size=BATCH_SIZE,
                                                    shuffle=True,
                                                    seed=12345,
                                                    class_mode='categorical')
validation_generator = val_datagen.flow_from_directory(
    VALIDATION_DATA_DIR,
    target_size=(IMG_WIGTH, IMG_HEIGHT),
    batch_size=BATCH_SIZE,
    shuffle=False,
    class_mode='categorical')
 
def model_maker():
    base_model = MobileNet(include_top=False, input_shape=(IMG_WIGTH, IMG_HEIGHT, 3))
    for layer in base_model.layers[:]:
        layer.trainable = False
    input = Input(shape=(IMG_WIGTH, IMG_HEIGHT, 3))
    custom_model = base_model(input)
    custom_model = GlobalAveragePooling2D()(custom_model)
    custom_model = Dense(64, activation='relu')(custom_model)
    custom_model = Dropout(0.5)(custom_model)
    predictions = Dense(NUM_CLASSES, activation='softmax')(custom_model)
    return Model(inputs=input, outputs=predictions)
 
model = model_maker()
model.compile(loss='categorical_crossentropy',
              optimizer=tf.keras.optimizers.Adam(0.001),
              metrics=['acc'])
model.fit(
    train_generator,
    steps_per_epoch=math.ceil(float(TRAIN_SAMPLES) / BATCH_SIZE),
    validation_data=validation_generator,
    validation_steps=math.ceil(float(VALIDATION_SAMPLES) / BATCH_SIZE))

如果以上回答对您有所帮助,点击一下采纳该答案~谢谢