值:发电机的输出应为元组`(x,y,sample_weight)`or`(x,y)`



我是Keras的新手,我正在尝试训练Python的面部检测机。如您所见,发电机返回的值,但似乎输出不适合格式。任何建议都非常感谢

完整的Valueerror如下:

valueerror:发电机的输出应为元组(x, y, sample_weight)(x, y)。发现:[[[[[0.10196079 0.08235294 0.07058824]] [0.10196079 0.08235294 0.07058824] [0.10196079 0.08235294 0.07058824] ... [0.10196079 0.08235294 0.07058824] [0.10196079 0.08235294 0.07058824] [0.10196079 0.08235294 0.07058824]

这是追溯

file" c:/users/user/pycharmprojects/untitled4/transferlearning.py",line> 103,在callbacks = [checkpoint,artir](
文件" c:c:c: user user anaconda3 lib stite-> packages keras keras legacy interfaces interfaces.py",第91行, 返回func(*args,** kwargs(
文件" c: user user user anaconda3 lib lib site-packages keras keras Engine Engine triagn.py",第1418行,在fit_generator中 onirome_epoch = initial_epoch(
文件" C: USER USER ANACONDA3 lib site-> packages keras egine Engine triending_generator.py",第198行,in fit_generator str(generator_output((

下面的完整代码

image_dir = path.join(root_dir, 'train_countinghead', 'image_data')
img_width, img_height = 256, 256
train_csv = pandas.read_csv(path.join(root_dir, 'train_countinghead', 'train.csv'))
test_csv = pandas.read_csv(path.join(root_dir, 'test_headcount.csv'))
train_samples = len(train_csv)
test_samples = len(test_csv)
batch_size = 16
epochs = 50
model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(img_width, img_height, 3))
# Freeze the layers which you don't want to train. Here I am freezing the first 5 layers.
for layer in model.layers[:5]:
    layer.trainable = False
# Adding custom Layers
x = model.output
x = Flatten()(x)
x = Dense(1024, activation="relu")(x)
x = Dropout(0.5)(x)
x = Dense(1024, activation="relu")(x)
predictions = Dense(16, activation="softmax")(x)
# creating the final model
model_final = Model(inputs=model.input, outputs=predictions)
# compile the model
model_final.compile(loss="categorical_crossentropy", optimizer=optimizers.SGD(lr=0.0001, momentum=0.9),
                    metrics=["accuracy"])
# Initiate the train and test generators with data Augumentation
train_datagen = ImageDataGenerator(
    rescale=1./255,
    horizontal_flip=True,
    fill_mode="nearest",
    zoom_range=0.3,
    width_shift_range=0.3,
    height_shift_range=0.3,
    rotation_range=30
)
test_datagen = ImageDataGenerator(
    rescale=1. / 255,
    horizontal_flip=True,
    fill_mode="nearest",
    zoom_range=0.3,
    width_shift_range=0.3,
    height_shift_range=0.3,
    rotation_range=30
)
# if `class_mode` is `"categorical"` (default value) it must include the `y_col` column with the class/es of each image.
# Check the comments in method definition for more
train_generator = train_datagen.flow_from_dataframe(
    dataframe=train_csv,
    directory=image_dir,
    x_col='Name',
    target_size=(img_height, img_width),
    batch_size=batch_size,
    class_mode=None
)
test_generator = test_datagen.flow_from_dataframe(
    dataframe=test_csv,
    directory=image_dir,
    x_col='Name',
    target_size=(img_height, img_width),
    batch_size=batch_size,
    class_mode=None
)
# Save the model according to the conditions
checkpoint = ModelCheckpoint(path.join(root_dir, "vgg16_1.h5"), monitor='val_acc', verbose=1, save_best_only=True,
                             save_weights_only=False,
                             mode='auto', period=1)
early = EarlyStopping(monitor='val_acc', min_delta=0, patience=10, verbose=1, mode='auto')
# Train the model
model_final.fit_generator(
    train_generator,
    # samples_per_epoch=train_samples,
    steps_per_epoch=train_samples / batch_size,
    epochs=epochs,
    validation_data=test_generator,
    validation_steps=test_samples / batch_size,
    callbacks=[checkpoint, early])

问题是您在这里没有提供目标列。如果您查看文档,则可以看到您需要(因为您正在训练模型(来指定y_col,并且没有class_mode=None(仅用于预测(,至少对于train_generator(我不知道(您打算使用test_generator(。

您还可以看到使用该错误,这告诉您它没有得到所有必要的元素(x数据,y标签(。

最新更新