我正在尝试实现我的VGG16模型,下面是我的代码:
model_vgg = applications.VGG16(include_top=False, weights='imagenet')
train_generator_bottleneck = datagen.flow_from_directory(
train_data_dir,
target_size=(64,64),
batch_size=batch_size,
class_mode=None,
shuffle=False)
Found 9741 images belonging to 15 classes.现在,我正在提取要素并保存它们
bottleneck_features_train = model_vgg.predict_generator(train_generator_bottleneck, 19)
np.save(open(bottleneck_features_train.npy', 'wb'), bottleneck_features_train)现在加载我的数据
train_data = np.load(open('bottleneck_features_train.npy', 'rb'))
train_labels = train_generator_bottleneck.classes
train_labels = to_categorical(train_labels, num_classes=15)在这里我得到了错误的标签和数据形状
print train_data.shape
print train_labels.shape输出:
(9728L, 2L, 2L, 512L)
(9741L, 15L)为什么我得到了错误的形状输出,请帮助?
发布于 2018-04-22 20:07:28
我不是100%确定你这里说的“错误”是什么意思,但我会假设你希望train_data.shape有9741个样本,而不是9728个。
原因来自于predict_generator的工作方式。您可以在此处指定一个参数,即steps=19。这意味着您的模型将对19批输入数据进行预测。现在我假设您的train_generator_bottleneck的batch_size是512,因此您会得到19*512 = 9728的输出。
如果你想要9741个输出,我会尝试这样做:
bottleneck_features_train = model_vgg.predict_generator(train_generator_bottleneck, 20)
bottleneck_features_train = bottleneck_features_train[:9741]
np.save(open(bottleneck_features_train.npy', 'wb'), bottleneck_features_train)这解决了你的形状问题吗?
https://stackoverflow.com/questions/49965331
复制相似问题