Keras: "featurewise_center = True" is not working for ImageDataGenerator::flow_from_directory()

Created on 2 Sep 2016  路  9Comments  路  Source: keras-team/keras

Hello,

I have been using fit_generator for classifying a large number of images which could not fit into memory. I used the keras ImageDataGenerator and followed this simple example. The only changes I made to the code are:

# line 84 changed to
train_datagen = ImageDataGenerator(featurewise_center = True)
# line 92 changed to
test_datagen = ImageDataGenerator(featurewise_center = True)

As I just would like to remove from each image the mean pixel values across samples. However, I encountered an error below:

File "/home/ubuntu/anaconda/lib/python2.7/site-packages/keras/models.py", line 683, in fit_generator
    pickle_safe=pickle_safe)
  File "/home/ubuntu/anaconda/lib/python2.7/site-packages/keras/engine/training.py", line 1419, in fit_generator
    'or (x, y). Found: ' + str(generator_output))
Exception: output of generator should be a tuple (x, y, sample_weight) or (x, y). Found: None

After checking the Source code, I found out that the member variable self.mean = None in class ImageDataGenerator (line 180) was used before being assigned a value in class DirectoryIterator (line 502).

To be specific, here is the "pathway" in which self.mean is used:
DirectoryIterator -> next() -> line 595:

x = self.image_data_generator.standardize(x)

and in ImageDataGenerator -> standardize() -> line 294:

if self.featurewise_center:
    x -= self.mean

But self.mean was not computed anywhere before being used. So I think the traceback above should be due to subtracting None from x.

Please let me know if the issue can be fixed or if there are some ideas to get around this issue.

Thank you !
---

stale

Most helpful comment

Check out documentation: https://keras.io/preprocessing/image/

For "featurewise_center" to work, you must call "fit" first to compute the statistics. Unfortunately, it requires a Numpy array as input and does not work with directories. It is meant to be used when you use "flow", not "flow_from_directory".

Imho, the way to go would be to implement a "fit_from_directory" that computes the statistics. WDYT?

All 9 comments

Well, I encountered the same problem.
So before training, I load my images one by one and I compute the mean. Then I added this mean to the ImageDataGenerator mean. So self.mean won't be None anymore

Check out documentation: https://keras.io/preprocessing/image/

For "featurewise_center" to work, you must call "fit" first to compute the statistics. Unfortunately, it requires a Numpy array as input and does not work with directories. It is meant to be used when you use "flow", not "flow_from_directory".

Imho, the way to go would be to implement a "fit_from_directory" that computes the statistics. WDYT?

@slegall56 can you please share your code, I want to implement mean subtraction as well but I'm using .flow_from_directory

I add to image.py

def fit_from_directory(self, mean=None, std=None):
      '''Required for featurewise_center, featurewise_std_normalization 
      when using image from directory.

    # Arguments
        mean: Mean Pixels of training database
        std: Standard deviation of training database
    '''
    if self.featurewise_center:
        self.mean = mean

    if self.featurewise_std_normalization:
        self.std = std

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, but feel free to re-open it if needed.

@slegall56 I don't get it. How could this code calculate the variation?

Hi,
I am facing the same problem. How did you guys implement it finally?

Thanks,
Mario

I am facing it too...

Coming from 2020 and I cant find an answer to the problem as well

Was this page helpful?
0 / 5 - 0 ratings