Keras predict_生成器输出不同数量的样本

Keras predict_生成器输出不同数量的样本,keras,conv-neural-network,transfer-learning,Keras,Conv Neural Network,Transfer Learning,我正试图通过使用数据扩充来改进迁移学习模型的性能,该模型使用异常作为预训练模型。目标是对狗的品种进行分类train_张量和valid_张量分别在numpy数组中包含训练图像和测试图像 from keras.applications.xception import Xception model = Xception(include_top = False, weights = "imagenet") datagen = ImageDataGenerator(zoom_range=0.2,

我正试图通过使用数据扩充来改进迁移学习模型的性能,该模型使用异常作为预训练模型。目标是对狗的品种进行分类
train_张量
valid_张量
分别在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_生成器函数返回的样本数与其提供的样本数不同。是否跳过或遗漏了样本

是的,有些样本被遗漏了,这是因为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))

是的,有些样本被遗漏了,这是因为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))

它应该有多少个样本?我希望这些特性的样本数与训练数据的样本数相同。但是在这里,
train\u tensors
有6680个样本,但是
瓶颈\u train
有6656个样本,它应该有多少个样本?我希望瓶颈\u特性具有与训练数据相同的样本数。但是在这里,
train\u tensors
有6680个样本,而
瓶颈\u train
有6656个样本