其他分享
首页 > 其他分享> > keras调用flow_from_directory()出现“Found 0 images belonging to 2 classes”问题

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

作者:互联网

图像分类时,keras调用flow_from_directory()出现“Found 0 images belonging to 5 classes”问题

代码如下:

from tensorflow import keras
from keras_preprocessing import image


train_datagen = image.ImageDataGenerator(
    #..... 
    fill_mode = 'nearest',   
    validation_split=0.3
   
) 
valid_datagen = image.ImageDataGenerator(
    rescale=1./255,
)
test_datagen = image.ImageDataGenerator(
    rescale = 1./255,
)


train_generator = train_datagen.flow_from_directory(
    'zuizhong/train',
    target_size=(224,224),#(height,width)
    batch_size=batch_size,
    #subset='training',
    )
valid_generator = valid_datagen.flow_from_directory(
    'zuizhong/val',
    target_size=(224,224),
    batch_size=batch_size,
    subset='validation',
    

    )
#0: 'jinyu118', 1: 'kenuo58', 2: 'liyuan296', 3: 'tieyan630', 4: 'yunyu18'
test_generator = test_datagen.flow_from_directory(
    'zuizhong/test',
    target_size=(224,224),
    batch_size=batch_size,
    )

目录结构如下,没有问题,每个子目录存放的全都为.png图像文件,

但是抛出了"Found 0 images belonging to 2 classes"问题。

经过查看flow_from_directory()的参数说明,终于找到原因:

train_datagen中有validation_split=0.3 要从训练集中分出0.3作为训练集,但后面的验证集确没有从train_datagen中生成,另从valid_datagen中生成了,且为验证集目录val,况且后面还有一句

subset=‘validation’,前后造成了矛盾;

解决方法一:验证集从训练集中分出时,前面有'validation_split=0.3'

valid_generator = train_datagen.flow_from_directory(
    'zuizhong/train',
    target_size=(224,224),
    batch_size=batch_size,
    subset='validation',

解决方法二:单独建立验证集目录val时,不要用'validation_split=0.3'

train_datagen = image.ImageDataGenerator(
    #..... 
    fill_mode = 'nearest',   
    #validation_split=0.3
   
) 
valid_datagen = image.ImageDataGenerator(
    rescale=1./255,
)
....
valid_generator = valid_datagen.flow_from_directory(
    'zuizhong/val',
    target_size=(224,224),
    batch_size=batch_size,
    #subset='validation',

标签:belonging,validation,keras,flow,batch,datagen,train,224,size
来源: https://blog.csdn.net/m0_64748541/article/details/123607643