在下列条款中,需要将数据集划分为列车、验证和测试文件夹,在这些文件夹中,测试文件夹不应包含标记的子文件夹。相反,它应该只包含一个文件夹(即Test_folder)。
当我使用下面的代码时,我会得到输出消息,表示没有找到任何图像。
Ver.1:
test_generator = test_datagen.flow_from_directory(
"dataset\\test\\test_folder\\",
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=1,
class_mode=None,
shuffle=False,
seed=10)
Output message: "Found 0 images belonging to 0 classes.".相反,如果我使用与列车和验证文件夹相同的文件夹结构(dataset\test\class_a\test_1.jpg等),一切似乎都正常,我设法评估我的模型。
Ver.2:
test_generator = test_datagen.flow_from_directory(
"dataset\\test\\",
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=32,
class_mode='categorical',
shuffle=False,
seed=10)
Output message: "Found 1500 images belonging to 3 classes.".我还尝试了指定“classes”属性的推荐,但仍然找到了0幅图像。
Ver.3:
test_generator = test_datagen.flow_from_directory(
"dataset2\\test\\test_folder\\",
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=1,
classes=['test'],
class_mode=None,
shuffle=False,
seed=10)
Output message: Found 0 images belonging to 1 classes.因此,调用flow_from_directory()方法的正确方法是什么?为什么我得到没有找到文件的消息?当我使用Ver.2解决方案时,我的模型是否没有正确的评估?
发布于 2020-01-06 21:17:32
请找到一个有效的解决方案这里。
发电机看起来像:
# Data generators
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
# Note that the validation data should not be augmented!
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
# This is the target directory
train_dir,
# All images will be resized to 150x150
target_size=(150, 150),
batch_size=batch_size,
# Since we use categorical_crossentropy loss, we need binary labels
class_mode='categorical')
validation_generator = test_datagen.flow_from_directory(
validation_dir,
target_size=(150, 150),
batch_size=batch_size,
class_mode='categorical')确保您的图像以正确的方式存储。例如。
...images/train/class1/
...images/train/class2/
...images/val/class1/
...images/val/class2/生成器函数确实需要这种结构。因此,请确保火车/测试目录中的每个类都有子文件夹。
您还可以使用data generator函数作为在这篇文章中描述进行预测。
发布于 2020-09-04 13:40:15
上述场景(彼得提供的)假设validation_dir是test_datagen.flow_from_directory()函数的一个参数。因此,逻辑是test_dir只有您在万维网中提供的一个文件夹结构(./test/folder/image)。
test_datagen.flow_from_directory( validation_dir,...)是一个级联的方法,它是语法,它允许在同一个对象上调用多个方法。这样,您就可以使用flow_from_directory()的函数。
发布于 2021-01-12 13:50:38
它找不到任何类,因为测试没有子目录。如果没有类,它就无法加载您的图像,正如您在上面的日志输出中看到的那样。但是,有一个解决办法,因为您可以指定测试目录的父目录,并指定只想加载测试“类”:
datagen = ImageDataGenerator()
test_data = datagen.flow_from_directory('.', classes=['test'])https://datascience.stackexchange.com/questions/65979
复制相似问题