2
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ Since you are using Keras, why not start withe Keras ImageDataGenerator? $\endgroup$
    – user12075
    Commented Sep 15, 2018 at 20:47
  • $\begingroup$ @user12075 I edit the question according to what I mean $\endgroup$
    – Noran
    Commented Sep 16, 2018 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
    Commented Mar 12, 2019 at 12:02

2 Answers 2

2
$\begingroup$

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)
        iaa.Fliplr(0.5), 
        # 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(
        dataframe=train_df,
        directory=train_data_dir,
        x_col="path",
        y_col=columns,
        batch_size=batch_size,
        class_mode="other",
        target_size=(img_height ,img_width),
        shuffle=True
    )


$\endgroup$
1
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Can I use imgaug if I read the images from a directory? I am not using numpy images.. $\endgroup$
    – N.IT
    Commented Sep 18, 2018 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
    Commented Sep 23, 2018 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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