Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save resized images using ImageDataGenerator and flow_from_directory in keras

Tags:

keras

keras-2

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?

like image 929
Hitesh Avatar asked Dec 15 '17 06:12

Hitesh


People also ask

Which parameter of ImageDataGenerator flow_from_directory is for resizing image?

flow_from_directory(directory) generates augmented images from directory with arbitrary collection of images. So there is need of parameter target_size to make all images of same shape.

What is TF keras preprocessing image ImageDataGenerator?

Keras ImageDataGenerator is a gem! It lets you augment your images in real-time while your model is still training! You can apply any random transformations on each training image as it is passed to the model. This will not only make your model robust but will also save up on the overhead memory!

What is ImageDataGenerator in keras used for?

Introduction to Keras ImageDataGenerator. Keras ImageDataGenerator is used for getting the input of the original data and further, it makes the transformation of this data on a random basis and gives the output resultant containing only the data that is newly transformed.

How many images does ImageDataGenerator generate?

Then the "ImageDataGenerator" will produce 10 images in each iteration of the training. An iteration is defined as steps per epoch i.e. the total number of samples / batch_size. In above case, in each epoch of training there will be 100 iterations.


1 Answers

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/

datagen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1, 
height_shift_range=0.1,shear_range=0.15, 
zoom_range=0.1,channel_shift_range = 10, horizontal_flip=True)

Step 2: Here we pick the original image to perform the augmentation on

read in the image

image_path = 'C:/Users/Darshil/gitly/Deep-Learning/My 
Projects/CNN_Keras/test_augment/caty.jpg'

image = np.expand_dims(ndimage.imread(image_path), 0)

step 3: pick where you want to save the augmented images

save_here = 'C:/Users/Darshil/gitly/Deep-Learning/My 
Projects/CNN_Keras/test_augment'

Step 4. we fit the original image

datagen.fit(image)

step 5: iterate over images and save using the "save_to_dir" parameter

for x, val in zip(datagen.flow(image,                    #image we chose
        save_to_dir=save_here,     #this is where we figure out where to save
         save_prefix='aug',        # it will save the images as 'aug_0912' some number for every new augmented image
        save_format='png'),range(10)) :     # here we define a range because we want 10 augmented images otherwise it will keep looping forever I think
pass
like image 112
DrDEE Avatar answered Sep 22 '22 15:09

DrDEE