我有一个经过增强数据预训练的网络,但我想从最后一层提取特征向量并训练另一个分类器(例如,svm)。为此,我需要从增强训练数据和测试数据中提取输出。
但是,我对 Keras/tensorflow 很菜鸟,我只需要在 numpy 数组中添加增强训练数据即可在我的特征提取器代码中使用它。如果我不使用增强训练数据,我可以毫无问题地做到这一点。
这是我迄今为止尝试过的:
#train on augmented data
model.fit_generator(datagen.flow(x_train, y_train,
batch_size=batch_size),
steps_per_epoch=x_train.shape[0] // batch_size,
epochs=epochs,
validation_data=(x_test, y_test))
#extract augmented data. Is this correct?
x_train_augmented, y_train_augmented=datagen.flow(x_train, y_train, batch_size=batch_size)
根据Keras Documentation函数 flow(X, y):获取 numpy 数据和标签数组,并生成批量的增强/标准化数据。在无限循环中无限期地生成批处理。
那么,如何循环并返回形状矩阵(num_images、宽度、高度、 channel )中的增强数据?
最佳答案
假设您有 N
个大小为 28x28 的灰度图像,那么您可以使用
for x_train_augmented, y_train_augmented in datagen.flow(x_train, y_train, batch_size=batch_size):
x_train_data = x_train_augmented.reshape(-1, 28, 28, 1)
这里,x_train_data 的形状将为 [N, 28, 28, 1]。
关于python - 如何在 Keras/Tensorflow 中返回增强数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44297268/