Using ImageDataGenerator with images in .npy format

Viewed 1995

I am pretty new to Keras. I am trying to train a model using ImageDataGenerator. I have a very large amount of images for training saved in .npy format. I wanted to use flow_from_directory() so I stored the images as recommended in the documentation (one folder per class). The problem is this only works for png, jpeg, tiff, etc. but won't work with my .npy files.

Is there any way I could use this function or something similar that gives me all the augmentation possibilities that ImageDataGenerator gives?

Thank you very much, any help is appreciated

1 Answers

Yes, it's possible if you are willing to adapt the source code of the ImageDataGenerator (which is actually quite straightforward to read and understand). Looking at the keras-preprocessing github, I think it would suffice to replace the load_img method in the DirectoryIterator class with your own load_array method that reads .npy files from disk instead of images:

...
# build batch of image data
for i, j in enumerate(index_array):
     fname = self.filenames[j]
     ## Replace the code below with your own function
     img = load_img(os.path.join(self.directory, fname),
                    color_mode=self.color_mode,
                    target_size=self.target_size,
                    interpolation=self.interpolation)
     x = img_to_array(img, data_format=self.data_format)
 ...

So minimally, you would make the following change to that line:

...
# build batch of image data
for i, j in enumerate(index_array):
    fname = self.filenames[j]
    img = np.load(os.path.join(self.directory, fname))
...

But probably you will want to implement some of the additional logic that Keras' load_img utility function also has like color mode, target size etc. and wrap everything in your own load_array function.

Related