本文介绍了将Keras增强数据保存为numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用keras ImageDataGenerator ,我们可以将增强图像另存为png或jpg:

using keras ImageDataGenerator,we can save augmented images as png or jpg :

    for X_batch, y_batch in datagen.flow(train_data, train_labels, batch_size=batch_size,\
                save_to_dir='images', save_prefix='aug', save_format='png'):

我有一个形状为(1600,4,100,100)的数据集,这意味着1600个具有4个100x100像素通道的图像.如何将扩充后的数据另存为形状为(N,4,100,100)的numpy数组,而不是单个图像?

I have a dataset of the shape (1600, 4, 100,100), which means 1600 images with 4 channels of 100x100 pixels. How can I save the augmented data as numpy array of shape (N,4,100,100) instead of individual images?

推荐答案

由于您知道样本数量= 1600,因此只要达到该数量,就可以停止datagen.flow().

Since you know the number of samples = 1600, you can stop datagen.flow() as long as this number is reached.

augmented_data = []
num_augmented = 0
for X_batch, y_batch in datagen.flow(train_data, train_labels, batch_size=batch_size, shuffle=False):
    augmented_data.append(X_batch)
    num_augmented += batch_size
    if num_augmented == train_data.shape[0]:
        break
augmented_data = np.concatenate(augmented_data)
np.save(...)

请注意,您应正确设置batch_size(例如batch_size=10),以免生成额外的增强图像.

Note that you should set batch_size properly (e.g. batch_size=10) so that no extra augmented images are generated.

这篇关于将Keras增强数据保存为numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:54