1

我正在尝试提供一个目录作为 ImageDataGenerator.flow_from_directory 的输入,但我无法做到。

train_data_dir = "/train"
validation_data_dir = "/test"

train_generator = ImageDataGenerator.flow_from_directory(directory=train_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size, 
class_mode = "categorical")

validation_generator = ImageDataGenerator.flow_from_directory(directory=validation_data_dir,
target_size = (img_height, img_width),
class_mode = "categorical")

上面的代码返回以下错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-126-39ed634afa51> in <module>
      2 target_size = (img_height, img_width),
      3 batch_size = batch_size,
----> 4 class_mode = "categorical")
      5 
      6 validation_generator = ImageDataGenerator.flow_from_directory(validation_data_dir,

TypeError: flow_from_directory() missing 1 required positional argument: 'self'

我该如何解决这个问题?

4

1 回答 1

1

您不能直接从ImageDataGenerator调用flow_from_directory方法。您必须首先创建此类的实例。尝试这个:

train_gen = ImageDataGenerator()
val_gen = ImageDataGenerator()

您可以在此处添加增强参数。参考:https
://keras.io/preprocessing/image/之后可以使用flow_from_directory

train_generator = train_gen.flow_from_directory(directory=train_data_dir,
target_size = (img_height, img_width),
batch_size = batch_size, 
class_mode = "categorical")
于 2019-11-11T08:46:02.670 回答