I would like to increase the data in my dataset to create a CNN deep learning classification model.

Which is better for the model, using data augmentation by ImageDataGenerator or using openCV to increase the data?

By the way, I am using Keras and floydhub.

  • 1
    $\begingroup$ Since you are using Keras, why not start withe Keras ImageDataGenerator? $\endgroup$
    – user12075
    Sep 15 '18 at 20:47
  • $\begingroup$ @user12075 I edit the question according to what I mean $\endgroup$
    – Noran
    Sep 16 '18 at 2:00
  • $\begingroup$ Here is tutorial on how to make your own custom generator using opencv. Which you can expand as much as you want with your ideas. $\endgroup$
    – photeesh
    Mar 12 '19 at 12:02

Keras' ImageDataGenerator doesn't offer much support by itself for data augmentation. However it has a parameter called preprocessing_function which allows you to use custom augmentors with it.

I personally use imgaug which offers virtually any augmentation you can think of and works well with ImageDataGenerator like I said.

  • $\begingroup$ Can I use imgaug if I read the images from a directory? I am not using numpy images.. $\endgroup$
    – N.IT
    Sep 18 '18 at 7:05
  • 1
    $\begingroup$ @N.IT you use the ImageDataGenerator to load the images just like you currently do it. imgaug just lets you make augmentation on-the-fly. $\endgroup$
    – user50384
    Sep 23 '18 at 11:46

An example with library imgaug, Keras, ImageDataGenerator and flow_from_dataframe:

import imgaug as ia
import imgaug.augmenters as iaa

seq = iaa.Sequential([
        iaa.Crop(px=(0, 16)), 
        # crop images from each side by 0 to 16px (randomly chosen)
        # horizontally flip 50% of the images
        iaa.GaussianBlur(sigma=(0, 3.0)) 
        # blur images with a sigma of 0 to 3.0

def augment(img):
        seq_det = seq.to_deterministic()
        aug_image = seq_det.augment_image(img)

        return applications.inception_resnet_v2.preprocess_input(aug_image)

train_generator = image.ImageDataGenerator(preprocessing_function=augment)

train_flow = train_generator.flow_from_dataframe(
        target_size=(img_height ,img_width),


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.