简体   繁体   中英

Keras ImageDataGenerator.flow_from_directory returns TypeError

I'm trying to give a directory as input to ImageDataGenerator.flow_from_directory but I'm unable to do it.

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")

The above code returns the following error

---------------------------------------------------------------------------
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'

How do I solve this?

You can not directly call flow_from_directory method from ImageDataGenerator . You'll have to create instance of this class first. Try this:

train_gen = ImageDataGenerator()
val_gen = ImageDataGenerator()

you can add parameters for augmentation here. Refer: https://keras.io/preprocessing/image/
After that you can use 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM