zono
zono

Reputation: 8584

'ImageDataGenerator' object has no attribute 'image_data_generator'

I tried transfer learning and then I got "no attribute 'image_data_generator'" error. What does this error mean?

You can reproduce my error in the following way.

*Steps to Reproduce

$ cd /tmp
$ git clone --branch no-attribute-error https://github.com/y-zono/btc-bch-ml.git
$ cd btc-bch-ml
$ docker build -t model-builder .
$ docker run -v /tmp/btc-bch-ml/docker:/docker -d -it model-builder
$ docker exec -it CONTAINER_ID /bin/bash
# cd /docker
# python3 ./train-predict.py

Traceback (most recent call last):
  File "./train-predict.py", line 62, in <module>
    history = model.fit_generator(img_itr_train, steps_per_epoch=steps_per_epoch, epochs=n_epoch)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/_impl/keras/engine/training.py", line 1598, in fit_generator
    initial_epoch=initial_epoch)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/_impl/keras/engine/training_generator.py", line 162, in fit_generator
    generator_output = next(output_generator)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/_impl/keras/utils/data_utils.py", line 613, in get
    six.raise_from(StopIteration(e), e)
  File "<string>", line 3, in raise_from
StopIteration: 'ImageDataGenerator' object has no attribute 'image_data_generator'

*Notes

Tensorflow version is "tensorflow-1.8.0"

I posted all my code and training data into github.

https://github.com/y-zono/btc-bch-ml/blob/no-attribute-error/docker/train-predict.py#L62

Upvotes: 2

Views: 4039

Answers (4)

carsteen
carsteen

Reputation: 1

I had the same issue with tensorflow-1.8.0. The quickest hack is to create an empty class containing your preprocessing function and pass it to the image_data_generator object of ImageDataGenerator instance :

def preprocess_input(im_array):
    return im_array/255-.5

class foo(object):
    pass

imgen = ImageDataGenerator(preprocessing_function='pass anything')

imgen.image_data_generator = foo
imgen.image_data_generator.preprocessing_function = preprocess_input

flowgen = imgen.flow_from_directory('/tmp/training_folder/') 

flowgen.__getitem__(0)[0] #check result

Another solutoin is to fix the source code in tensorflow/python/keras/_impl/keras/preprocessing/image.py by deleting image_data_generator line 672

Upvotes: 0

Aliaksandr Nazarau
Aliaksandr Nazarau

Reputation: 11

Change

idg_train = ImageDataGenerator(rescale=1/255., 
    shear_range=0.1,
    zoom_range=0.1,
    horizontal_flip=True,
    preprocessing_function=preprocess_input)

to

idg_train = ImageDataGenerator(rescale=1/255., 
    shear_range=0.1, 
    zoom_range=0.1, 
    horizontal_flip=True)

Upvotes: 1

Wuyang Li
Wuyang Li

Reputation: 31

When using tensorflow1.8, I got the same error. If you remove the preprocessing function passed to ImageDatagenerator, your code would work, however, you have to preprocess the images in other ways.

Upvotes: 2

11t
11t

Reputation: 643

I think I have a fix for your problem. This issue is occurring because of one of the import statement. I suggest you replacing

from tensorflow.python.keras.applications.resnet50 import preprocess_input

to

from keras.preprocessing.image import ImageDataGenerator

This worked when I tried it. Let me know if you still face issue.

Upvotes: 0

Related Questions