【问题标题】:How to load validation data with flow_from_directory function in tensorflow如何在 tensorflow 中使用 flow_from_directory 函数加载验证数据
【发布时间】:2021-11-24 17:21:02
【问题描述】:

[已解决]

在 tensorflow 模块 tensorflow.keras.preprocessing.image 中使用 flow_from_directory 函数时遇到问题

我可以加载我的所有数据来训练我的模型,但我无法加载我的数据来验证训练...

我的代码(编辑)[工作!]

# Import
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, models
from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_data_dir = '../data/train'
validation_data_dir = '../data/validation'

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    #validation_split=0.20
    )

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1./255)

def create_dataset(train_data_dir, validation_data_dir, img_height, img_width, batch_size):

    train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        shuffle=True,
        class_mode='categorical',
        subset = 'training',
        )

    validation_generator = train_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        shuffle=True,
        class_mode='categorical',
        subset = 'validation',
        )

    return train_generator, validation_generator

我的数据集位于我在 CNN 模型上调用的函数中,当我运行它时,它总是返回:

Found 0 images belonging to 6 classes.
Epoch 1/5Found 5732 images belonging to 6 classes.
Found 0 images belonging to 6 classes.
Epoch 1/5

编辑: 这是我在 model.py 文件中调用函数 create_dataset 的部分:

image_height = 100
image_width = 100
batch_size = 32

ds_train, ds_validation = create_dataset("../data/train", "../data/validation", image_height, image_width, batch_size)

C ñ ñ ...

model.fit(ds_train, epochs=5, verbose=2)
model.evaluate(ds_validation, verbose=2)
model.save('../complete_saved_model/')

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 你能否也包括你如何拟合模型的代码?
  • 是的,我刚刚编辑了我的问题
  • 我发现我的错误...我对我的代码不是很小心。

标签: python tensorflow keras dataset image-preprocessing


【解决方案1】:

在您的 train_generator 中删除代码子集 = 'training'。同样在你的 validation_generator 移除代码子集 = 'validation'。

【讨论】:

  • 您好 Gerry 感谢您的回答,但子集不会改变任何东西!两天前我找到了解决方案,我的错误是在验证步骤中使用了 test_datagen 而不是 train_datagen ...
猜你喜欢
  • 2020-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多