如何使用keras ImageDataGenerator扩充图像?

时间:2020-05-29 01:47:48

标签: python machine-learning keras deep-learning data-augmentation

我训练了364个图像的训练集,这些图像存储在numpy数组中,其标签也存储在不同的numpy数组中(有8个标签要分类)。数据集很小,我想使用增强,但只能找到能够增强图像的资源它们根据标签存储在特定的文件夹中。因此,如何使用ImageDataGenerator实时增强图像。谢谢!

1 个答案:

答案 0 :(得分:3)

解决方案

您应该使用flow而不是flow_from_directoryflow可以接受您的numpy数组。我正在粘贴来自Coursera课程的代码。


training_images = training_images.reshape((27455,28,28,1))
testing_images = testing_images.reshape((7172,28,28,1))

# Create an ImageDataGenerator and do Image Augmentation
training_datagen = ImageDataGenerator(
      rescale = 1./255,
      rotation_range=40,
      width_shift_range=0.2,
      height_shift_range=0.2,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
      fill_mode='nearest')

validation_datagen = ImageDataGenerator(rescale = 1./255)

training_datagen.fit(training_images)
validation_datagen.fit(testing_images)

testing_labels = tf.one_hot(testing_labels, 25)
training_labels = tf.one_hot(training_labels, 25)

history = model.fit_generator(training_datagen.flow(
    training_images,
    y = training_labels,
    batch_size = 32
), epochs=15, validation_data = validation_datagen.flow(
    testing_images,
    y = testing_labels,
    batch_size = 32
), verbose = 1)

编辑1

Coursera课程名称:Convolutional Neural Networks in TensorFlow by deeplearning.ai

如何知道图像是否增加

修复batch size,并查看训练完成1个历时需要执行的步骤。从中可以推断出图像正在增强。

我相信此解决方案可以解决您的问题。如果是,请接受,否则在下面有什么问题发表评论?

编辑2

# here's a more "manual" example
for e in range(epochs):
    print('Epoch', e)
    batches = 0
    for x_batch, y_batch in training_datagen.flow(x_train, y_train, batch_size=32):
        ##### Check each batch manually####
        batches += 1

增强特定图像

请查看下面的文章,该文章将向您展示如何逐张增加图像。

https://towardsdatascience.com/data-augmentation-techniques-in-python-f216ef5eed69