python - 如何在 Keras/Tensorflow 中返回增强数据

标签 python numpy matrix tensorflow keras

我有一个经过增强数据预训练的网络,但我想从最后一层提取特征向量并训练另一个分类器(例如,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/

相关文章:

python - 如何在不制作新的.jpg文件的情况下使用opencv从相机获取图像

python - SWIG Python 包装器在 Mac OSX 上崩溃

python - Discord.py - 每条消息的 message.content 都是空的

python - 是否有一个 numpy 函数来获取子矩阵的总和?

python - 如何在 numpy 中优化此函数的计算?

python - 字典列表中的 Nan 值

c++ - 求解线性方程组的最有效方法

r - R中矩阵的快速子集化

c - OpenMP 性能不佳

python - 来自 csv 的 3D Pandas 数据框