Search code examples
pythonnumpykeras

How to fix 'Expected to see 2 array(s), but instead got the following list of 1 arrays'


I'm training a multi class cnn model. model.fit method works fine but when I use fit_generator method the error in the title occurs.

y_train_age = utils.to_categorical(y_train_age, 117)
y_test_age = utils.to_categorical(y_test_age, 117)

y_train_gender = utils.to_categorical(y_train_gender, 2)
y_test_gender = utils.to_categorical(y_test_gender, 2)

y_train = np.concatenate((y_train_age, y_train_gender), axis=1)
y_test = np.concatenate((y_test_age, y_test_gender), axis=1)

print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)

(15000, 100, 100, 3),(8708, 100, 100, 3),(15000, 119),(8708, 119)

Model:

from keras import layers
from keras.models import Model
from keras.layers import Input, Dense, Activation
from keras.layers import AveragePooling2D, MaxPooling2D, Flatten, Conv2D, ZeroPadding2D

x_input = Input((100,100,3))

x = Conv2D(64, (3,3))(x_input)
x = Activation('relu')(x)
x = MaxPooling2D((3, 3), strides=(2, 2))(x)

x = Conv2D(64, (3,3))(x)
x = Activation('relu')(x)
x = MaxPooling2D((3, 3), strides=(2, 2))(x)

x = Conv2D(128, (3,3))(x)
x = Activation('relu')(x)
x = MaxPooling2D((3, 3), strides=(2, 2))(x)

x = Conv2D(256, (3,3))(x)
x = Activation('relu')(x)
x = MaxPooling2D((3, 3), strides=(2, 2))(x)

x = Flatten()(x)
x = Dense(64, activation='relu')(x)
x = Dense(128, activation='relu')(x)
x = Dense(128, activation='relu')(x)

y1 = Dense(117, activation='softmax', name="Age")(x)
y2 = Dense(2, activation='softmax', name="Gender")(x)

model = Model(inputs=x_input, outputs=[y1, y2])
model.compile(loss=['categorical_crossentropy', 'categorical_crossentropy'], optimizer='adam', metrics=['accuracy'])
model.summary()

And the problem:

from keras.preprocessing.image import ImageDataGenerator

model.fit_generator(ImageDataGenerator(shear_range=0.3, zoom_range=0.1, 
                    horizontal_flip=True).flow(x_train, y_train, 32),
                    steps_per_epoch=len(x_train) / 32,
                    epochs=5, verbose=1,
                    validation_data=(x_test, y_test))

The error:

ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[0., 0., 0., ..., 0., 1., 0.],
       [0., 0., 0., ..., 0., 0., 1.],
       [0., 0., 0., ..., 0., 0., 1.],
       ...,
       [0., 0., 0., ..., 0., 0., 1.],
       [0., 0., 0., ..., 0., 0., 1....

Please help me, thanks.

THE ANSWER

generator = ImageDataGenerator(...)

def generate_data_generator(generator, X, Y1, Y2):
    genX1 = generator.flow(X, Y1, seed=7)
    genX2 = generator.flow(X, Y2, seed=7)
    while True:
        X1i = genX1.next()
        X2i = genX2.next()
        yield X1i[0], [X1i[1], X2i[1]]

history = model.fit_generator(generate_data_generator(generator, x_train, y_train_age, y_train_gender),
                    steps_per_epoch=len(x_train) / 32,
                    epochs=5, 
                    verbose=1, 
                    callbacks = callbacks,
                    validation_data=(x_test, [y_test_age, y_test_gender]))

Solution

  • You defined a model with 2 outputs : [y1, y2]
    So it expected to be fitted with two differents array of labels, one of size (, 119) and the other one of size (,2) which correspond to your 2 Dense outputs layers.

    With the fit function it would look like this :

    model.fit( x = X_train,
               y = [y_train, y_train_gender],
               validation_data=(X_test, [y_test, y_test_gender]),
               batch_size = batch_size,
               epochs = num_epochs,
               verbose = 1)
    

    I'am not very used to ImageDataGenerator, but try something like this :

    from keras.preprocessing.image import ImageDataGenerator
    
    model.fit_generator(ImageDataGenerator(shear_range=0.3, zoom_range=0.1, 
                        horizontal_flip=True).flow(x_train, [y_train, y_train_gender], 32),
                        steps_per_epoch=len(x_train) / 32,
                        epochs=5, verbose=1,
                        validation_data=(x_test, [y_test, y_test_gender]))
    

    EDIT

    Try this little adaptation of this post :
    Keras: How to use fit_generator with multiple outputs of different type

    generator = ImageDataGenerator(shear_range=0.3,
                                    zoom_range=0.1, 
                                    horizontal_flip=True) 
    
    
    def generate_data_generator(generator, X, Y1, Y2):
        genX1 = generator.flow(X, Y1, seed=7)
        genX2 = generator.flow(X, Y2, seed=7)
        while True:
            X1i = genX1.next()
            X2i = genX2 .next()
            yield X1i[0], [X1i[1], X2i[1]]
    
    
    model.fit_generator(generate_data_generator(generator, x_train, y_train, y_train_gender),
                        steps_per_epoch=len(x_train) / 32,
                        epochs=5, 
                        verbose=1)