'Multiple CNN output + data augmentation with Keras error: `x` (images tensor) and `y` (labels) should have the same length

I am working on a CNN model running through a dataset of images, where I have one set of convolution layers, then two sets of dense layers for outputting age and gender of the image dataset.

Data is organised as X = images and Y = [gender, age], then split into training and validation datasets

=====dataset=====

for i in range(len(images)):
    ar = np.asarray(images[i])
    x.append(ar)
    agegen = [int(gender(i),int(age(i)] # gender, age  
    y.append(agegen)
x = np.array(x)
y = np.array(y)

===split train and validation dataset===

x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.25)

Then I perform data augmentation using ImageDataGenerator:

===data augmentation===

train_datagen = ImageDataGenerator(rescale=1./255,
                                   rotation_range = 90,
                                   width_shift_range=0.5,
                                   height_shift_range = 0.5,
                                   brightness_range=[0.2,1.0],
                                   horizontal_flip=True,
                                   shear_range = 0.3,
                                   zoom_range = [0.5,2],
                                   fill_mode = 'nearest'
                                   )
val_datagen = ImageDataGenerator(rescale=1./255)

After convolution layers and dense layers, I compile the model with 2 outputs: age and gender

===Model===

modelA = Model(inputs=inputs, outputs=[agemodel,gendermodel])
modelA.compile(optimizer='adam', 
              loss=['mse','binary_crossentropy'],
              metrics=['mae','accuracy'])

And then i fit the model with x and multiple y

===fit model===

train_dataset = train_datagen.flow(x_train, [y_train_age, y_train_gender])

val_dataset = val_datagen.flow(
        x_val,[y_val_age, y_val_gender])
historyA = modelA.fit(train_dataset,validation_data=val_dataset,epochs = epo, steps_per_epoch = 80, validation_steps = 30, batch_size=batch_size)

And I receive below error.

=====error message=====
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-0d5753674f97> in <module>()
      6 batch_size = 64
      7 
----> 8 train_dataset = train_datagen.flow(x_train, [y_train_age, y_train_gender])
      9 
     10 val_dataset = val_datagen.flow(

2 frames
/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/numpy_array_iterator.py in __init__(self, x, y, image_data_generator, batch_size, shuffle, sample_weight, seed, data_format, save_to_dir, save_prefix, save_format, subset, dtype)
     87                              'should have the same length. '
     88                              'Found: x.shape = %s, y.shape = %s' %
---> 89                              (np.asarray(x).shape, np.asarray(y).shape))
     90         if sample_weight is not None and len(x) != len(sample_weight):
     91             raise ValueError('`x` (images tensor) and `sample_weight` '

ValueError: `x` (images tensor) and `y` (labels) should have the same length. Found: x.shape = (3750, 128, 128, 3), y.shape = (2, 0)

Alternatively I tried to run the model with y_train and y_val only:

train_dataset = train_datagen.flow(x_train, y_train)

val_dataset = val_datagen.flow(
        x_val,y_val)

But the loss of the model result (y=gender) goes to negative infinity exponentially.

Would there be a way to perform data augmentation with multiple y as outputs?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source