【发布时间】: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]))
【问题讨论】: