I am resizing my RGB images stored in a folder(two classes) using following code:
from keras.preprocessing.image import ImageDataGenerator
dataset=ImageDataGenerator()
dataset.flow_from_directory('/home/1',target_size=(50,50),save_to_dir='/home/resized',class_mode='binary',save_prefix='N',save_format='jpeg',batch_size=10)
My data tree is like following:
1/
1_1/
img1.jpg
img2.jpg
........
1_2/
IMG1.jpg
IMG2.jpg
........
resized/
1_1/ (here i want to save resized images of 1_1)
2_2/ (here i want to save resized images of 1_2)
After running the code i am getting following output but not images:
Found 271 images belonging to 2 classes.
Out[12]: <keras.preprocessing.image.DirectoryIterator at 0x7f22a3569400>
How to save images?
Heres a very simple version of saving augmented images of one image wherever you want:
Step 1. Initialize image data generator
Here we figure out what changes we want to make to the original image and generate the augmented images
You can read up about the diff effects here- https://keras.io/preprocessing/image/
Step 2: Here we pick the original image to perform the augmentation on
read in the image
step 3: pick where you want to save the augmented images
Step 4. we fit the original image
step 5: iterate over images and save using the "save_to_dir" parameter
The
flow_from_directory
method gives you an "iterator", as described in your output. An iterator doesn't really do anything on its own. It's waiting to be iterated over, and only then the actual data will be read and generated.An iterator in Keras for fitting is to be used like this:
Normally, instead of doing the loop above, you just pass the generator to a
fit_generator
method. There is no real need to do a for loop:Keras will only save images after they're loaded and augmented by iterating over the generator.
Its only a declaration, you must use that generator, for example,
.next()
then you will see images in
/home/resized