1 回答
TA贡献1818条经验 获得超7个赞
您可以使用 ImageDataGenerator 的验证 split 关键字自动拆分训练数据和测试数据。
train_datagen = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
validation_split=0.2) # set validation split
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary',
subset='training') # set as training data
validation_generator = train_datagen.flow_from_directory(
train_data_dir, # same directory as training data
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary',
subset='validation') # set as validation data
model.fit_generator(
train_generator,
steps_per_epoch = train_generator.samples // batch_size,
validation_data = validation_generator,
validation_steps = validation_generator.samples // batch_size,
epochs = nb_epochs)
当ImageDataGenerator自动打乱您的输入数据时,您使用ImageDataGenerator的数据会被打乱和分割。
在你的情况下,你需要flow而不是flow_from_directory
添加回答
举报