Python 如何修复';期望看到2个数组,但得到了以下1个数组的列表';

Python 如何修复';期望看到2个数组,但得到了以下1个数组的列表';,python,numpy,keras,Python,Numpy,Keras,我正在训练一个多级别的cnn模型model.fit方法工作正常,但当我使用fit\u generator方法时,标题中会出现错误 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.t

我正在训练一个多级别的cnn模型
model.fit
方法工作正常,但当我使用
fit\u generator
方法时,标题中会出现错误

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)、(87081000,100,3)、(15000,119)、(8708119)

型号:

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()
问题是:

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))
错误:

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....
请帮帮我,谢谢

答案

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

您定义了一个具有两个输出的模型:[y1,y2]
因此,它预计将配备两个不同的标签阵列,一个是大小
(,119)
,另一个是大小
(,2)
,对应于您的两个密集输出层

使用fit功能,它将如下所示:

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)
我不太习惯ImageDataGenerator,但请尝试以下方法:

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]))
编辑 试试这篇文章的小改编:


对现有答案稍作修改,您使用的是
y_train
,它是
Age
Gender
的串联向量,但它应该只包含
Age
,因为您已经有了
y_train\u Gender
,它对
Gender
进行编码,为了适应这种情况,我对代码的几个部分进行了更改

y1 = Dense(117, activation='softmax', name="Age")(x)    # and not 119
y2 = Dense(2, activation='softmax', name="Gender")(x)
只需在
.fit()
.fit\u generator()
方法中将
y\u train
替换为
y\u train\u age
。这样,我们使用
y1
作为
Age
的输出,
y2
作为
Gender
的输出


希望这有帮助

谢谢你的回复。我可以用model.fit方法训练模型,而不是用fit\u生成器方法。我照你说的做了:
model.fit\u生成器(ImageDataGenerator(剪切范围=0.3,缩放范围=0.1,水平翻转=True),flow(x\u train,[y\u train\u age,y\u train\u gender],32),每个历元的步骤=len(x\u train)/32,历元=5,verbose=1,回调=回调,验证数据=(x_test,[y_test_age,y_test_gender])
但出现错误:ValueError:无法将输入数组从形状(15000117)广播到形状(15000)使用此方法出现错误:ValueError:('input data in
numpyrayderator
应具有排名4。您传递了一个带有形状的数组',(15000,117))
y1 = Dense(117, activation='softmax', name="Age")(x)    # and not 119
y2 = Dense(2, activation='softmax', name="Gender")(x)