简体   繁体   中英

Error - Multi-Classification Neural Network using Keras

I am receiving a rather annoying error from my NN code, and was hoping that someone with a better knowledge of how Keras works may explain to me why I am getting the error. I appreciate any help! Error:

AttributeError: 'DirectoryIterator' object has no attribute 'ndim'

The error is coming from:

    Traceback (most recent call last):
    File "C:\Users\Cameron\Desktop\AI\CubeFieldNN_Train -fix.py", line 80, in <module>
    validation_steps = (validation_samples / batch_size))

Code:

NN.fit(
train_set, train_labels,
batch_size = batch_size,
epochs = epochs,
validation_data = (validation_set, validation_labels),
validation_steps = (validation_samples / batch_size))

Full Code: https://pastebin.com/V1YwJW3X

Full Error:

    Traceback (most recent call last):
  File "C:\Users\Cameron\Desktop\AI\CubeFieldNN_Train -fix.py", line 80, in <module>
    validation_steps = (validation_samples / batch_size))
  File "C:\Python\lib\site-packages\keras\models.py", line 1002, in fit
    validation_steps=validation_steps)
  File "C:\Python\lib\site-packages\keras\engine\training.py", line 1630, in fit
    batch_size=batch_size)
  File "C:\Python\lib\site-packages\keras\engine\training.py", line 1476, in _standardize_user_data
    exception_prefix='input')
  File "C:\Python\lib\site-packages\keras\engine\training.py", line 76, in _standardize_input_data
    data = [np.expand_dims(x, 1) if x is not None and x.ndim == 1 else x for x in data]
  File "C:\Python\lib\site-packages\keras\engine\training.py", line 76, in <listcomp>
    data = [np.expand_dims(x, 1) if x is not None and x.ndim == 1 else x for x in data]
AttributeError: 'DirectoryIterator' object has no attribute 'ndim'

The transition to fit from fit_generator from your previous question was not really necessary. The flow_from_directory returns a generator type object which returns tuples of both the data and the labels. Similarly for validation_set . Note also, that if you specify validation_steps you have to also specify steps_per_epoch . Therefore, you can use:

NN.fit_generator(train_set,
                 steps_per_epoch=steps_per_epoch,
                 epochs=epochs,
                 validation_data=validation_set,
                 validation_steps=validation_steps)

Alternatively, you can load all images at once and pass it to the NN.fit() function along with the labels as you did.

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