-3

I have training set of 364 images stored in numpy array also its labels stored in a different numpy array(there are 8 labels to classify).The dataset being small I want to use augmentation but can only find resources which augment images if they are stored in specific folders according to the labels. So how can I augment images in real time using ImageDataGenerator. Thanks!

1 Answers1

3

Solution

You should use flow instead of flow_from_directory. flow can take your numpy arrays. I am pasting code from coursera course.


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)

Edit 1

Name of Coursera Course: Convolutional Neural Networks in TensorFlow by deeplearning.ai

How to know images increased or not

fix batch size and see how many steps training takes to complete 1 epoch. From that you can infer, images are augmenting.

I believe this solution solves your problem. If yes accept it else comment below what's the problem?

Edit 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

Augmenting particular images

check below article which will show you hoe to augment images one by one.

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

Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
PSKP
  • 1,178
  • 14
  • 28
  • How can I know the number of images that are augmented as the accuracy didnt improve a bit. Also can you name the coursera course. Thanks! – Gunjan Patait May 29 '20 at 02:45
  • 1
    I edited answer. added your answer to your question. – PSKP May 29 '20 at 02:55
  • Removed the batch_size parameter and ran it for a epoch. It trained 10 images. I just can't understand it. What does 10/10 meant? Is it the number of augmented images? – Gunjan Patait May 29 '20 at 03:29
  • dont remove batch size. use `batch_size=16` and train then suppose for 1 epoch it takes x steps then examples will be `batch_size * x`. i.e. `16 * x` – PSKP May 29 '20 at 03:32
  • So, it took 19 steps. So that means 16*19=304 i.e 304 are the number of augmented images, right? – Gunjan Patait May 29 '20 at 03:46
  • Also how can I augment specific number of images corresponding to each label? Thanks! – Gunjan Patait May 29 '20 at 03:51
  • No its total number of images. I think. but i found new way to check images after augmentation manually. I am adding to original answer. ;-) – PSKP May 29 '20 at 03:53
  • augmenting specific number of images is not possible in keras. I am adding 1 article which will show how to use `offline` data augmentation. You can use that for images 1 by 1 so you can augment only those images which you want. And also I changed code slightly. – PSKP May 29 '20 at 04:28