简体   繁体   中英

AttributeError: 'DirectoryIterator' object has no attribute 'shape' Tensorflow CNN

I am trying to use the fit_generator to fit a model. Everything works up to the fit, but it is throwing the error above. I am using Keras 2.2.4, and TF 2.0.0. All other previous CNNs have worked, but it does not like the image generator.

# import relevant functions
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D

# rotate, and shift data
image_gen = ImageDataGenerator(rotation_range=20,
                              width_shift_range=0.1,
                              height_shift_range=0.1,
                              shear_range=0.1,
                              zoom_range=0.1,
                              horizontal_flip=True,
                              fill_mode='nearest')
# build the model
model = Sequential()

model.add(Conv2D(filters=32, kernel_size=(3,3),input_shape=image_shape, activation='relu',))
model.add(MaxPool2D(pool_size=(2, 2)))

model.add(Conv2D(filters=64, kernel_size=(3,3),input_shape=image_shape, activation='relu',))
model.add(MaxPool2D(pool_size=(2, 2)))

model.add(Conv2D(filters=64, kernel_size=(3,3),input_shape=image_shape, activation='relu',))
model.add(MaxPool2D(pool_size=(2, 2)))

model.add(Flatten())

model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# set up early stopping
early_stopping = EarlyStopping(monitor='val_loss', patience=2)

batch_size=16

# build train and test generators
train_image_gen = image_gen.flow_from_directory(train_path,
                                               target_size = image_shape[:2],
                                               color_mode='rgb',
                                               batch_size=batch_size,
                                               class_mode='binary')

# don't shuffle test data

test_image_gen = image_gen.flow_from_directory(test_path,
                                               target_size = image_shape[:2],
                                               color_mode='rgb',
                                               batch_size=batch_size,
                                               class_mode='binary',
                                               shuffle=False)

# fit on the generators
results = model.fit_generator(train_image_gen, epochs=20,
                             validation_data=test_image_gen,
                             callbacks=[early_stopping])

From the snapshots of my notebook, everything works except for the `model.fit_generator' step.

在此处输入图像描述

When I run the last step, I get the error.

在此处输入图像描述

The train_image_gen and test_image_gen object appear to be correct with proper references to the directories for a binary image classification problem.

The directories are set up properly too:

在此处输入图像描述

I have rerun it all, it appears to be in issue with the PlaidML library and the generators.

Before doing any other work, my first import is the following:

 import plaidml.keras
 plaidml.keras.install_backend()
 from keras import backend as K

This is a library that allows for the use of the GPU and greatly speeds up all the other CNNs that I have worked with. For some reason, it does not work with the generators as an input. That's the difference. Otherwise, the plaidML library allows people to use their AMD or Nvidia GPU quite easily.

This is the first time the that I have had an issue with the library.

For those who are interested, here is the link:

https://rustyonrampage.github.io/deep-learning/2018/10/18/tensorfow-amd.html

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