调用flow_from_directory()出现“Found 0 images belonging to 2 classes”问题

本文解决使用Keras的ImageDataGenerator时遇到的Found0imagesbelongingto2classes错误。通过调整目录结构及引入正确的keras_preprocessing模块,确保图像数据正确加载。

问题描述: 使用keras的图像数据生成器(keras.preprocessing.image.ImageDataGenerator)抛出图像时出现"Found 0 images belonging to 2 classes"问题。

原因:识别出两个类别,证明目标文件夹满足要求,在stackoverflow上查找原因,发现:

从对应网址下载keras_preprocessing文件夹,并将其置于本地keras对应位置中,从keras_preprocessing.image导入生成器,解决问题。

``` import tensorflow as tf from tensorflow.keras.preprocessing.image import ImageDataGenerator # 定义路径 train_dir = r'C:\Users\29930\Desktop\结构参数图' # 数据增强配置 train_datagen = ImageDataGenerator( rescale=1./255, validation_split=0.2, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True ) # 生成训练集和验证集 train_generator = train_datagen.flow_from_directory( train_dir, target_size=(224, 224), batch_size=32, class_mode='binary', subset='training' ) val_generator = train_datagen.flow_from_directory( train_dir, target_size=(224, 224), batch_size=32, class_mode='binary', subset='validation' ) model = tf.keras.Sequential([ tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(224,224,3)), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Conv2D(64, (3,3), activation='relu'), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Conv2D(128, (3,3), activation='relu'), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Flatten(), tf.keras.layers.Dense(512, activation='relu'), tf.keras.layers.Dropout(0.5), tf.keras.layers.Dense(1, activation='sigmoid') ]) model.compile( optimizer='adam', loss='binary_crossentropy', metrics=['accuracy', tf.keras.metrics.AUC(name='auc')] ) # 添加早停法 early_stop = tf.keras.callbacks.EarlyStopping( monitor='val_loss', patience=5, restore_best_weights=True ) # 训练模型 history = model.fit( train_generator, validation_data=val_generator, epochs=30, callbacks=[early_stop] ) # 保存模型 model.save('copd_cnn_model.h5') # 评估指标可视化 import matplotlib.pyplot as plt plt.plot(history.history['auc'], label='Training AUC') plt.plot(history.history['val_auc'], label='Validation AUC') plt.title('模型AUC曲线') plt.ylabel('AUC值') plt.xlabel('Epoch') plt.legend() plt.show()```运行结果是Found 213 images belonging to 2 classes. Found 52 images belonging to 2 classes. Warning (from warnings module): File "C:\Users\29930\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\layers\convolutional\base_conv.py", line 107 super().__init__(activity_regularizer=activity_regularizer, **kwargs) UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead. Warning (from warnings module): File "C:\Users\29930\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\trainers\data_adapters\py_dataset_adapter.py", line 121 self._warn_if_super_not_called() UserWarning: Your `PyDataset` class should call `super().__init__(**kwargs)` in its constructor. `**kwargs` can include `workers`, `use_multiprocessing`, `max_queue_size`. Do not pass these arguments to `fit()`, as they will be ignored. Traceback (most recent call last): File "D:/建模/cnn.py", line 61, in <module> history = model.fit( File "C:\Users\29930\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\utils\traceback_utils.py", line 122, in error_handler raise e.with_traceback(filtered_tb) from None File "C:\Users\29930\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\utils\image_utils.py", line 227, in load_img raise ImportError( ImportError: Could not import PIL.Image. The use of `load_img` requires PIL. 请根据结果修改代码使其能正常运行
03-20
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

space_dandy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值