5
$\begingroup$

I'm new to CNNs, starting off with keras. I'm currently using ImageDataGenerator to import my train/validation folders (which each have 2 class subfolders for my binary classification task). Was wondering how can I import my train/validation files without using ImageDataGenerator? I'm aware that ImageDataGenerator is good for accuracy as it does some augmentation, but I want to compare the accuracy to a training set without any augmentations. Essentially I think I need to put all the images into an array, but not sure how to.

Basically I want to know what is the normal way to import training/validation data for images, so I can compare what is the accuracy difference with/without imagedatagen. I know with normal NN tasks it's easy as you can just do pd.read_csv().

I'm currently importing like so:

train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        'data/train',
        target_size=(150, 150),
        batch_size=32,
        class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
        'data/validation',
        target_size=(150, 150),
        batch_size=32,
        class_mode='binary')
$\endgroup$

1 Answer 1

6
$\begingroup$

The docs for ImageDataGenerator suggest that no augmentation is done by default. So you could instantiate it without any augmentation parameters and keep the rest of your code for handling your directory structure:

train_datagen = ImageDataGenerator(rescale=1./255)

You are also allowed to write your own custom data generator and pass it to model.fit_generator(). Here is a nice tutorial.

Or if your data fits in memory you could write some simpler code possibly using keras.preprocessing.image.load_img to load all the images into an array and pass them to model.fit instead.

$\endgroup$
2
  • $\begingroup$ Ah, I see. Thanks Imran! However, for the purpose of developing my python skills, is there a way to achieve the task manually? $\endgroup$
    – Programmer
    Feb 2, 2018 at 9:39
  • $\begingroup$ OK, I have updated my answer with some ideas. $\endgroup$
    – Imran
    Feb 2, 2018 at 9:55

Your Answer

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

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