Python ValueError:生成器的输出应为元组`(x,y,sample\u weight)`或`(x,y)`
我是Keras的新手,我正在尝试用Python训练一台人脸检测机器。正如您所看到的,生成器返回了值,但输出的格式似乎不合适。非常感谢您的建议 完整值错误如下所示: ValueError:生成器的输出应为元组Python ValueError:生成器的输出应为元组`(x,y,sample\u weight)`或`(x,y)`,python,tensorflow,keras,Python,Tensorflow,Keras,我是Keras的新手,我正在尝试用Python训练一台人脸检测机器。正如您所看到的,生成器返回了值,但输出的格式似乎不合适。非常感谢您的建议 完整值错误如下所示: ValueError:生成器的输出应为元组(x,y,样本\u权重) 或者(x,y)。找到:[[0.10196079 0.08235294 0.07058824] [0.10196079 0.08235294 0.07058824] [0.10196079 0.08235294 0.07058824] ... [0.10196079 0
(x,y,样本\u权重)
或者(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行,在callbacks=[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\u生成器中 初始纪元=初始纪元)
文件“C:\Users\user\Anaconda3\lib\site->packages\keras\engine\training\u generator.py”,第198行,在fit\u generator中 str(发电机输出) 完整代码如下
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\u col
,并且没有class\u mode=None
(仅用于预测),至少对于train\u生成器
(我不知道您打算如何使用测试\u生成器
)
您还可以看到,使用该错误时,它告诉您它没有获得所有必需的元素(
x
数据,y
标签)。问题在于,这里没有提供目标列。
如果您查看,您可以看到您需要(因为您正在训练您的模型)指定一个y\u col
,并且没有class\u mode=None
(仅用于预测),至少对于train\u生成器
(我不知道您打算如何使用测试\u生成器
)
您还可以看到,使用该错误时,它告诉您它没有获得所有必要的元素(
x
数据,y
标签)。但问题是我的csv只包含两列,文件名和人头数(我正在训练一台人脸检测机)。如果我将其更改为class_mode='categorical'。我会得到另一个错误,那就是TypeError:如果class_mode=“categorical”,y_col=“HeadCount”列值必须是string、list或tuple类型。或者我应该使用另一个class_模式吗?对不起,我对MLWell完全是初学者。如果您的目标值是图像中的人头数,那么您应该使用“sparse”
最有可能(但你可以做“其他”
)。你可以在文档中阅读你拥有的不同类型的class\u模式
。但问题是我的csv只包含两列,文件名和人头数(我正在训练一台面部检测机)。如果我将其更改为class_mode='categorical'。我会得到另一个错误,那就是TypeError:如果class_mode=“categorical”,y_col=“HeadCount”列值必须是string、list或tuple类型。或者我应该使用另一个class_模式吗?对不起,我对MLWell完全是初学者。如果您的目标值是图像中的人头数,那么您应该使用“sparse”
最有可能(但你可以做“其他”
)。你可以在文档中阅读你拥有的不同类型的class\u模式。
。嘿@zhenhao,别忘了它是否解决了你的问题;)嘿@zhenhao,别忘了它是否解决了你的问题;)