Keras predict_generator 输出不同数量的样本
Keras predict_generator outputs a different number of samples
我正在尝试通过使用数据扩充来提高使用 Xception 作为预训练模型的迁移学习模型的性能。目标是对犬种进行分类。 train_tensors
和 valid_tensors
分别在一个 numpy 数组中包含训练和测试图像。
from keras.applications.xception import Xception
model = Xception(include_top = False, weights = "imagenet")
datagen = ImageDataGenerator(zoom_range=0.2,
horizontal_flip=True,
width_shift_range = 0.2,
height_shift_range = 0.2,
fill_mode = 'nearest',
rotation_range = 45)
batch_size = 32
bottleneck_train = model.predict_generator(datagen.flow(train_tensors,
train_targets,
batch_size = batch_size),
train_tensors.shape[0]// batch_size)
bottleneck_valid = model.predict_generator(datagen.flow(valid_tensors,
valid_targets,
batch_size = batch_size),
test_tensors.shape[0]//batch_size)
print(train_tensors.shape)
print(bottleneck_train.shape)
print(valid_tensors.shape)
print(bottleneck_valid.shape)
但是,最后 4 行的输出是:
(6680, 224, 224, 3)
(6656, 7, 7, 2048)
(835, 224, 224, 3)
(832, 7, 7, 2048)
predict_generator 函数返回的样本数量与其提供的样本数量不同。样本是否被跳过或遗漏?
是的,有些样本被遗漏了,这是因为 6680 和 835 没有完全除以 32(您的批处理大小),您可以调整批处理大小,使其完全除以两个数字。
或者您可以使用 math.ceil
python 函数调整代码以包含一个额外的批次(其大小会稍小):
import math
bottleneck_train = model.predict_generator(datagen.flow(train_tensors,
train_targets,
batch_size = batch_size),
math.ceil(train_tensors.shape[0] / batch_size))
bottleneck_valid = model.predict_generator(datagen.flow(valid_tensors,
valid_targets,
batch_size = batch_size),
math.ceil(test_tensors.shape[0] /batch_size))
我正在尝试通过使用数据扩充来提高使用 Xception 作为预训练模型的迁移学习模型的性能。目标是对犬种进行分类。 train_tensors
和 valid_tensors
分别在一个 numpy 数组中包含训练和测试图像。
from keras.applications.xception import Xception
model = Xception(include_top = False, weights = "imagenet")
datagen = ImageDataGenerator(zoom_range=0.2,
horizontal_flip=True,
width_shift_range = 0.2,
height_shift_range = 0.2,
fill_mode = 'nearest',
rotation_range = 45)
batch_size = 32
bottleneck_train = model.predict_generator(datagen.flow(train_tensors,
train_targets,
batch_size = batch_size),
train_tensors.shape[0]// batch_size)
bottleneck_valid = model.predict_generator(datagen.flow(valid_tensors,
valid_targets,
batch_size = batch_size),
test_tensors.shape[0]//batch_size)
print(train_tensors.shape)
print(bottleneck_train.shape)
print(valid_tensors.shape)
print(bottleneck_valid.shape)
但是,最后 4 行的输出是:
(6680, 224, 224, 3)
(6656, 7, 7, 2048)
(835, 224, 224, 3)
(832, 7, 7, 2048)
predict_generator 函数返回的样本数量与其提供的样本数量不同。样本是否被跳过或遗漏?
是的,有些样本被遗漏了,这是因为 6680 和 835 没有完全除以 32(您的批处理大小),您可以调整批处理大小,使其完全除以两个数字。
或者您可以使用 math.ceil
python 函数调整代码以包含一个额外的批次(其大小会稍小):
import math
bottleneck_train = model.predict_generator(datagen.flow(train_tensors,
train_targets,
batch_size = batch_size),
math.ceil(train_tensors.shape[0] / batch_size))
bottleneck_valid = model.predict_generator(datagen.flow(valid_tensors,
valid_targets,
batch_size = batch_size),
math.ceil(test_tensors.shape[0] /batch_size))