简体   繁体   English

将Keras增强数据保存为numpy数组

[英]saving Keras augmented data as a numpy array

using keras ImageDataGenerator , we can save augmented images as png or jpg : 使用keras ImageDataGenerator ,我们可以将增强图像另存为png或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'):

I have a dataset of the shape (1600, 4, 100,100), which means 1600 images with 4 channels of 100x100 pixels. 我有一个形状为(1600,4,100,100)的数据集,这意味着1600个图像具有4个100x100像素的通道。 How can I save the augmented data as numpy array of shape (N,4,100,100) instead of individual images? 如何将增强数据另存为形状为(N,4,100,100)的numpy数组而不是单个图像?

Since you know the number of samples = 1600, you can stop datagen.flow() as long as this number is reached. 由于您知道样本数= 1600,因此只要达到此数目,就可以停止datagen.flow()

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(...)

Note that you should set batch_size properly (eg batch_size=10 ) so that no extra augmented images are generated. 请注意,您应该正确设置batch_size (例如, batch_size=10 ),以便不生成额外的增强图像。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM