【问题标题】:ValueError: Output of generator should be a tuple `(x, y, sample_weight)` or `(x, y)`ValueError: 生成器的输出应该是一个元组 `(x, y, sample_weight)` 或 `(x, y)`
【发布时间】:2019-09-22 19:52:46
【问题描述】:

我是 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]]

这是回溯

文件“C:/Users/user/PycharmProjects/untitled4/transferLearning.py”,第 103 行,在回调中=[checkpoint, early])
文件“C:\Users\user\Anaconda3\lib\site->packages\keras\legacy\interfaces.py”,第 91 行,在包装器中 返回函数(*args, **kwargs)
文件“C:\Users\user\Anaconda3\lib\site-packages\keras\engine\training.py”,第 1418 行,在 fit_generator initial_epoch=initial_epoch)
文件“C:\Users\user\Anaconda3\lib\site->packages\keras\engine\training_generator.py”,第 198 行,在 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])

【问题讨论】:

标签: python tensorflow keras


【解决方案1】:

问题是您在这里没有提供目标列。 如果您查看the documentation,您会发现您需要(因为您正在训练您的模型)指定y_col 并且也没有class_mode=None(仅用于预测),至少对于@987654324 @(我不知道你打算用test_generator做什么)。

您也可以看到使用错误,它告诉您它没有获取所有必要的元素(x 数据,y 标签)。

【讨论】:

  • 但问题是我的 csv 仅包含 2 列、文件名和人数(我正在训练人脸检测机)。如果我将其更改为 class_mode = 'categorical'。我收到另一个错误是 TypeError: If class_mode="categorical", y_col="HeadCount" column values must be type string, list or tuple.
  • 或者我应该使用另一个class_mode?抱歉,我是 ML 的初学者
  • 好吧,如果您的目标值是图像中的人数,那么您最有可能使用"sparse"(但您可以使用"other")。您可以在文档中阅读您拥有的不同类型的class_mode
猜你喜欢
  • 1970-01-01
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 2017-02-26
  • 2017-03-14
  • 1970-01-01
  • 1970-01-01
  • 2015-04-09
相关资源
最近更新 更多