Keras Forecast_generator输出不同数量的样本

时间:2018-12-29 08:45:56

标签: keras conv-neural-network transfer-learning

我正在尝试通过使用数据增强来提高将Xception用作预训练模型的转移学习模型的性能。目标是对狗的品种进行分类。 train_tensorsvalid_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函数返回的样本数量与其提供的样本数量有所不同。是否跳过或遗漏了样本?

1 个答案:

答案 0 :(得分:1)

是的,一些样本被排除在外了,这是因为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))