首页 文章

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

提问于
浏览
1

我在增强数据中有一个预先训练过的网络,但我想从最后一层提取特征向量并训练另一个分类器(例如,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函数流程(X,y):获取numpy数据和标签数组,并生成批量的扩充/规范化数据 . 在无限循环中无限期地产生批次 .

那么,我如何循环并以形状矩阵(num_images,width,height,channels)返回增强数据?

1 回答

  • 2

    假设您有 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] .

相关问题