[英]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.