0

我正在研究一个通过图像数据集运行的 CNN 模型,其中我有一组卷积层,然后是两组密集层,用于输出图像数据集的年龄和性别。

数据组织为 X = 图像和 Y = [性别,年龄],然后分成训练和验证数据集

=====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)

然后我使用 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)

在卷积层和密集层之后,我用 2 个输出编译模型:年龄和性别

===Model===

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

然后我用 x 和多个 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)

我收到以下错误。

=====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)

或者,我尝试仅使用 y_train 和 y_val 运行模型:

train_dataset = train_datagen.flow(x_train, y_train)

val_dataset = val_datagen.flow(
        x_val,y_val)

但是模型结果(y=gender)的损失呈指数级下降到负无穷大。

有没有办法以多个 y 作为输出来执行数据增强?

4

0 回答 0