Keras: Keras ValueError: 'Error when checking model target: expected no data, but got: ', array

Created on 4 Mar 2019  Â·  8Comments  Â·  Source: keras-team/keras

Hi. I am training a variational autoencoder model using my own data generator. The model compiles fine but when I call the fit_generator method following error appears:

    Traceback (most recent call last):


     File "C:/Users/Vlad/Desktop/bachelorarbeit/autoencoder/vae_script.py", line 77, in <module>
        vae.fit_generator(generator=xtrain, use_multiprocessing= True, workers= 6)
      File "C:\Users\Vlad\Desktop\bachelorarbeit\autoencoder\venv\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
        return func(*args, **kwargs)
      File "C:\Users\Vlad\Desktop\bachelorarbeit\autoencoder\venv\lib\site-packages\keras\engine\training.py", line 1418, in fit_generator
        initial_epoch=initial_epoch)
      File "C:\Users\Vlad\Desktop\bachelorarbeit\autoencoder\venv\lib\site-packages\keras\engine\training_generator.py", line 217, in fit_generator
        class_weight=class_weight)
      File "C:\Users\Vlad\Desktop\bachelorarbeit\autoencoder\venv\lib\site-packages\keras\engine\training.py", line 1211, in train_on_batch
        class_weight=class_weight)
      File "C:\Users\Vlad\Desktop\bachelorarbeit\autoencoder\venv\lib\site-packages\keras\engine\training.py", line 789, in _standardize_user_data
        exception_prefix='target')
      File "C:\Users\Vlad\Desktop\bachelorarbeit\autoencoder\venv\lib\site-packages\keras\engine\training_utils.py", line 63, in standardize_input_data
        'expected no data, but got:', data)
    ValueError: ('Error when checking model target: expected no data, but got:', array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
           1, 1, 1, 1, 1, 1, 1, 1, 1, 1]))

    Process finished with exit code 1

As far as I can see the ValueError is raised by following lines in the trainings_util.py in the standardize_input_data method:

        if not names:
        if data is not None and hasattr(data, '__len__') and len(data):
            raise ValueError('Error when checking model ' +
                             exception_prefix + ': '
                             'expected no data, but got:', data)
        return []

where 'names' is according to the docstring a list of expected array names. I tried solving this problem by feeding the generator with a tuple of dicitionaries of the type ({'input': X}, {'output': y}) where X is the data and y the targets, but the same error is raised.

My question is what names should I pass to the generator or should I pass any names at all? Also another question would is how to solve this problem? Can you give me a hint on what I am missing?

I am using the lastest Keras version (2.2.4) with Tensorflow backend (version 1.12.0) on a Windows 10 64-Bit machine. The code for the generator class and the model script can be found here:

(1) Model: https://pastebin.com/YR9y9WAN
(2) Data generator: https://pastebin.com/UK7VyQe4

Thank you!

Please make sure that the boxes below are checked before you submit your issue.
If your issue is an implementation question, please ask your question on StackOverflow or on the Keras Slack channel instead of opening a GitHub issue.

Thank you!

  • [ x] Check that you are up-to-date with the master branch of Keras. You can update with:
    pip install git+git://github.com/keras-team/keras.git --upgrade --no-deps

  • [ x] Check that your version of TensorFlow is up-to-date. The installation instructions can be found here.

  • [x] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).

tensorflow awaiting response support

Most helpful comment

So what I have figured in my case is that when you have multi-output models, passing loss functions as a list does not work reliably. However, using a dictionary to define loss for each output works fine. That fixed my issue.

All 8 comments

In order to expedite the trouble-shooting process, please provide a minimal code snippet to reproduce the issue reported here. Thanks!

Automatically closing due to lack of recent activity. Please update the issue when new information becomes available, and we will reopen the issue. Thanks!

I had the same problem. Here's my code:

width = 229
height = 229
input_tensor = Input((height, width, 3))
x = input_tensor

x = keras.applications.inception_resnet_v2.preprocess_input(x)
base_model = keras.applications.inception_resnet_v2.InceptionResNetV2(input_tensor=x, weights='imagenet', include_top=False)

model = Model(base_model.input, GlobalAveragePooling2D()(base_model.output))

gen = ImageDataGenerator()
train_generator = gen.flow_from_directory("train2", (229,229), shuffle=False, 
                                              batch_size=16)
test_generator = gen.flow_from_directory("test2", (229,229), shuffle=False, 
                                             batch_size=16, class_mode=None)

train = model.predict_generator(train_generator, train_generator.nb_sample)
test = model.predict_generator(test_generator, test_generator.nb_sample)
with h5py.File("data_%s.h5"%MODEL) as h:
    h.create_dataset("train", data=train)
    h.create_dataset("test", data=test)
    h.create_dataset("label", data=train_generator.classes)

here's the error:

Found 4500 images belonging to 3 classes.
Found 1500 images belonging to 1 classes.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-32-bbdd7e707b04> in <module>()
     15                                              batch_size=16, class_mode=None)
     16 
---> 17 train = model.predict_generator(train_generator, train_generator.samples)
     18 test = model.predict_generator(test_generator, test_generator.samples)
     19 with h5py.File("data_%s.h5"%MODEL) as h:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name + '` call to the ' +
     90                               'Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py in predict_generator(self, generator, steps, max_queue_size, workers, use_multiprocessing, verbose)
   1520             workers=workers,
   1521             use_multiprocessing=use_multiprocessing,
-> 1522             verbose=verbose)

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training_generator.py in predict_generator(model, generator, steps, max_queue_size, workers, use_multiprocessing, verbose)
    451                 x = generator_output
    452 
--> 453             outs = model.predict_on_batch(x)
    454             outs = to_list(outs)
    455 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py in predict_on_batch(self, x)
   1266             Numpy array(s) of predictions.
   1267         """
-> 1268         x, _, _ = self._standardize_user_data(x)
   1269         if self._uses_dynamic_learning_phase():
   1270             ins = x + [0.]

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
    749             feed_input_shapes,
    750             check_batch_axis=False,  # Don't enforce the batch size.
--> 751             exception_prefix='input')
    752 
    753         if y is not None:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
     61             raise ValueError('Error when checking model ' +
     62                              exception_prefix + ': '
---> 63                              'expected no data, but got:', data)
     64         return []
     65     if data is None:

ValueError: ('Error when checking model input: expected no data, but got:', array([[[[255., 255., 255.],
         [255., 255., 255.],

Thanks!

may be the weights was trained by the higher version, i have the same issue, but i figure it out by install the version from 2.1.6 to 2.2.4

I have the same issue but my version is 2.3.0

Any update here? Were you able to solve the problem? I am stuck in the same. The model works fine using standard loss function such as 'mse'. However, when I use a custom loss function it displays this error. I am using a multi-output model. The custom loss functions follow appropriate signature as it works in other cases.

So what I have figured in my case is that when you have multi-output models, passing loss functions as a list does not work reliably. However, using a dictionary to define loss for each output works fine. That fixed my issue.

Try 'compiling' before do 'predict', worked for me.

model = .....

optimizer = tf.keras.optimizers.Adam(0.001, epsilon=1e-08)
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])

model_predictions = model.predict(test_generator, steps=math.ceil(test_generator.samples/test_generator.batch_size))

Was this page helpful?
0 / 5 - 0 ratings

Related issues

snakeztc picture snakeztc  Â·  3Comments

braingineer picture braingineer  Â·  3Comments

nryant picture nryant  Â·  3Comments

somewacko picture somewacko  Â·  3Comments

zygmuntz picture zygmuntz  Â·  3Comments