【问题标题】:How to fix 'Expected to see 2 array(s), but instead got the following list of 1 arrays'如何修复“预计会看到 2 个数组,但得到以下 1 个数组列表”
【发布时间】:2019-06-28 07:13:10
【问题描述】:

我正在训练一个多类 cnn 模型。 model.fit 方法可以正常工作,但是当我使用 fit_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),(8708, 100, 100, 3),(15000, 119),(8708, 119)

型号:

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

【问题讨论】:

    标签: python numpy keras


    【解决方案1】:

    您定义了一个具有 2 个输出的模型:[y1, y2]
    因此,它需要配备两个不同的标签数组,一个大小为 (, 119),另一个大小为 (,2),对应于您的 2 个 Dense 输出层。

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

    编辑

    试试这个帖子的小改编:
    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)
    

    【讨论】:

    • 感谢您的回复。我可以用 model.fit 方法训练模型,而不是 fit_generator 方法。我按照你说的做了:model.fit_generator(ImageDataGenerator(shear_range=0.3, zoom_range=0.1, horizontal_flip=True).flow(x_train, [y_train_age, y_train_gender], 32), steps_per_epoch=len(x_train) / 32, epochs=5, verbose=1, callbacks=callbacks, validation_data=(x_test, [y_test_age, y_test_gender])) 但是出现错误:ValueError: could not broadcast input array from shape (15000,117) into shape (15000)
    • 此方法出错:ValueError: ('NumpyArrayIterator 中的输入数据应具有排名 4。您传递了一个带形状的数组', (15000, 117))
    【解决方案2】:

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

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

    只需在您的.fit().fit_generator() 方法中将y_train 替换为y_train_age。这样我们就可以使用y1 作为Age 的输出和y2 作为Gender 的输出。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2020-09-13
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 2020-05-20
      相关资源
      最近更新 更多