keras-team / keras-preprocessing

Utilities for working with image data, text data, and sequence data.
Other
1.02k stars 444 forks source link

Altered ImageDataGenerator gives error "__init__() got an unexpected keyword argument" #276

Open Nestak2 opened 4 years ago

Nestak2 commented 4 years ago

I am using keras=2.3.1 and I wanted to use my own version of zca_whitening. For that I made changes directly into the ImageDataGenerator class in the keras file /home/user/.local/lib/python3.6/site-packages/keras_preprocessing/image/image_data_generator.py. The file, including my changes is this one. My neural network file nn_script.py that uses this altered image_data_generator.py can be seen here. There are no problems, as long as nn_script.py uses the default keras augmentaions like:

train_datagen = ImageDataGenerator(
    rotation_range=30,
    shear_range=0.2,
    samplewise_center=True, samplewise_std_normalization=True) 

But if I decide to switch on my custom version of zca_whitening called zca_whitening_fast in this way

train_datagen = ImageDataGenerator(
    rotation_range=30,
    shear_range=0.2,
    samplewise_center=True, samplewise_std_normalization=True,
    zca_whitening_fast=True) 

I get the error message

Traceback (most recent call last):
  File "nn_script.py", line 55
TypeError: __init__() got an unexpected keyword argument 'zca_whitening_fast'

You can reproduce my error message by substituting your image_data_generator.py file with the one I post above and trying to create a generator with the option zca_whitening_fast=True.

First I thought the problem is, that I have forgotten to include zca_whitening_fast in def __init__() in the file image_data_generator.py, but it is there, as you can check. I also tried deleting the __pycache__ folder, because I thought, that some older files are getting executed in this folder, but the same error appeared. Now I wonder if the error message comes from an __init__() that is in a different file, not in image_data_generator.py. I think the solution is something obvious, I probably need to make a change in another file in the keras preprocessing folder, but I don't know which. I don't think the problem is because my keras version is old and because of compatibility reasons with my graphic card I can't upgrade keras.

Any ideas why I get this error message? Thanks

Nestak2 commented 4 years ago

A stackexchange user gave me the advice:

You modified the files in the keras_preprocessing package, but you are probably importing keras.preprocessing, which is a different package. This is fine, keras.preprocessing just forwards all calls to keras_preprocessing.

So what you need to do is to also make your modifications to the keras/preprocessing/image.py files inside the keras package, as shown here

So, I also inserted my new variable zca_whitening_fast into keras/preprocessing/image.py, but the same error appears :( Do I need to update something? And is there a way to trace from what file comes this error message TypeError: __init__() got an unexpected keyword argument 'zca_whitening_fast'?

Dref360 commented 4 years ago

I think you should inherit ImageDataGenerator and apply your modifications in the child class.