Search code examples
tensorflowkerasdeep-learningregressionconv-neural-network

ValueError: Data cardinality is ambiguous. Make sure all arrays contain the same number of samples


This is a regression problem, where I want to generate 5 float values from each image of size 224 x 224. So I use fully connected networks with 5 nodes in the last layer. But doing so in keras gives me the following error:

import keras, os
import numpy as np
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.applications.inception_v3 import InceptionV3

## data_list = list of four 224x224 numpy arrays

inception = InceptionV3(weights='imagenet', include_top=False)
x = inception.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(5, activation='relu')(x)

y = [np.random.random(5),np.random.random(5),np.random.random(5),np.random.random(5)]

model = Model(inputs=inception.input, outputs=predictions)
opt = Adam(lr=0.001)
model.compile(optimizer=opt, loss="mae")
model.fit(data_list, y, verbose=0, epochs=100)

Error:

ValueError: Data cardinality is ambiguous:
     x sizes: 224, 224, 224, 224
     y sizes: 5, 5, 5, 5
Make sure all arrays contain the same number of samples.

What could be going wrong?


Solution

  • Convert data_list and y to numpy arrays or tensors.

    In your code the list is treated as four inputs while your model has one input - https://keras.io/api/models/model_training_apis/

    Add these lines:

    import tensorflow as tf
    
    data_list = tf.stack(data_list)
    y = tf.stack(y)