我是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,在callbacks = [checkpoint,early]中)
包装中的文件“ C:\ Users \ user \ Anaconda3 \ lib \ site-> packages \ keras \ legacy \ interfaces.py”,行91 return func(* args,** kwargs)
fit_generator中的文件“ C:\ Users \ user \ Anaconda3 \ lib \ site-packages \ keras \ engine \ training.py”,行1418 initial_epoch = initial_epoch)
在fit_generator中的文件198行中的文件“ C:\ Users \ user \ Anaconda3 \ lib \ site-> packages \ keras \ engine \ training_generator.py” 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])
答案 0 :(得分:0)
问题在于,您在此处未提供目标列。
如果您查看the documentation,会发现您需要(因为正在训练模型)指定y_col
,并且没有class_mode=None
(仅用于预测),至少对于train_generator
(我不知道您打算如何使用test_generator
)。
您还可能已经看到使用该错误,它告诉您并没有获取所有必需的元素(x
数据,y
标签)。