0

我正在尝试通过使用数据增强来提高使用 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 函数返回的样本数量与其提供给它的样本不同。样品是否被跳过或遗漏?

4

1 回答 1

2

是的,一些样本被遗漏了,这是因为 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))
于 2018-12-29T11:23:19.187 回答