Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hacky way to augment multichannel images in Keras

Tags:

python

keras

I need to augment multichannel images and would like to use ImageDataGenerator Unfortunately, it supports 1,3 and 4 channel images only and I need much more. Would it be OK to directly edit site-packages/Keras/preprocessing/image.py adding necessary number of channels?

    if x.shape[self.channel_axis] not in {1, 3, 4, XXX}:
        raise ValueError(
            'Expected input to be images (as Numpy array) '
            'following the dimension ordering convention "' + self.dim_ordering + '" '
            '(channels on axis ' + str(self.channel_axis) + '), i.e. expected '
            'either 1, 3 or 4 channels on axis ' + str(self.channel_axis) + '. '
            'However, it was passed an array with shape ' + str(x.shape) +
            ' (' + str(x.shape[self.channel_axis]) + ' channels).')

where XXX - is a number of channels that I need. Would this break anything? Thanks!

like image 532
Dennis Sakva Avatar asked Jan 20 '17 16:01

Dennis Sakva


People also ask

Why we use ImageDataGenerator?

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. It does not add the data.

What is Datagen flow?

In datagen. flow() , It takes data in the form of datasets like np. arrays directly where in case of datage. flow_from_directory it takes data from folders containing labelled images. But both return batches of augmented images in batches.


1 Answers

I tried that anyway and it seems to work without too many side effects. I didn't explore every possible problem, though. So if you have multichannel data (like satellite imagery, etc) you can try this hack. There are couple of places where you need to augment the augmentation code :)

like image 200
Dennis Sakva Avatar answered Sep 22 '22 08:09

Dennis Sakva