简体   繁体   English

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

[英]How to return augmented data in Keras/Tensorflow

I have a pre-trained network in augmented data, but I want to extract feature vectors from the last but one layer and train another classifier (eg, svm). 我在增强数据中有一个预先训练的网络,但是我想从最后一层提取特征向量,然后训练另一个分类器(例如svm)。 To do that I need to extract the output on the augmented training data and from the test data. 为此,我需要从增强的训练数据和测试数据中提取输出。

However, I am quite noob in Keras/tensorflow and I just need to have the augmented training data in a numpy array to use it in my feature extractor code. 但是,我对Keras / tensorflow相当陌生,我只需要将增强的训练数据存储在numpy数组中即可在我的特征提取器代码中使用它。 I can do this if I dont use augmented training data with no problem. 如果我不使用扩展训练数据就没有问题,我可以这样做。

Here is what I tried so far: 这是我到目前为止尝试过的:

#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)

According to Keras Documentation function flow(X, y): Takes numpy data & label arrays, and generates batches of augmented/normalized data. 根据Keras文档的功能流(X,y):获取numpy数据和标签数组,并生成一批增强/规范化数据。 Yields batches indefinitely, in an infinite loop. 无限循环无限期地批量生产批次。

So, how can I loop and return the augmented data in a matrix of shape (num_images, width, height, channels)? 因此,如何循环并以形状矩阵(num_images,宽度,高度,通道)返回增强数据?

Suppose you have N number of grayscale images of size 28x28, then you can use 假设您有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)

Here, the shape of the x_train_data will be [N, 28, 28, 1]. 在此,x_train_data的形状为[N,28,28,1]。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在keras或tensorflow中使用ImageDataGenerator获得相同的增强图像? - How to obtain same augmented images using ImageDataGenerator in keras or tensorflow? 如何知道在 tensorflow.keras.layers.experimental.preprocessing 中增加了多少图像? - how to know how many images are augmented in tensorflow.keras.layers.experimental.preprocessing? 将Keras增强数据保存为numpy数组 - saving Keras augmented data as a numpy array 在不同级别的增强数据上评估 tensorflow model - Evaluating a tensorflow model on different levels of augmented data 在 Python 中使用来自 Keras 的图像数据生成器时,每批增加了多少数据? - When using Image Data Generator from Keras in Python, how much of the data per batch is augmented? 如何在 Tensorflow Keras 中标准化我的图像数据 - how to normalize my image data in Tensorflow Keras 如何为 Keras/tf.Keras 构建自定义数据生成器,其中 X 图像被增强并且相应的 Y 标签也是图像 - How to build a Custom Data Generator for Keras/tf.Keras where X images are being augmented and corresponding Y labels are also images 如何使用 TensorFlow 将增强图像添加到原始数据集? - How to add augmented images to original dataset with TensorFlow? 第一个时代中 keras 模型数据增强维度的错误 - Error with keras model data augmented dimensions in First epoch keras 数据增强 cnn 总是期望分类器 output 中的单个神经元? - keras data augmented cnn always expect a single neuron in the classifier output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM