Я тренирую мультиклассовую модель 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]))