Keras: AttributeError: 'NoneType' object has no attribute 'image_data_format'

Created on 26 Apr 2019  路  10Comments  路  Source: keras-team/keras

I have a problem this any models in keras.applications

/home/ds/anaconda3/envs/placer/bin/python /home/ds/projects/placer/tests.py
Using TensorFlow backend.
Traceback (most recent call last):
  File "/home/ds/projects/placer/tests.py", line 12, in <module>
    validation_test()
  File "/home/ds/projects/placer/tests.py", line 5, in validation_test
    my_flat_placer = Placer('places/1')
  File "/home/ds/projects/placer/core/Placer.py", line 9, in __init__
    self.place_model = PlaceModel(self)
  File "/home/ds/projects/placer/core/PlaceModel.py", line 10, in __init__
    self.resnet_model = mobilenet_v2.MobileNetV2(include_top=False, weights='imagenet', input_shape=(224, 224, 3))
  File "/home/ds/anaconda3/envs/placer/lib/python3.6/site-packages/keras_applications/mobilenet_v2.py", line 260, in MobileNetV2
    if backend.image_data_format() == 'channels_first':
AttributeError: 'NoneType' object has no attribute 'image_data_format'``
tensorflow awaiting response support

Most helpful comment

There is something wrong with the two approaches:

keras.applications.resnet gives the error ImportError: No module named resnetwith all resnet versions except resnet 50.

whereas it imports well with keras_applications.resnet import ResNet101 but, throws the following attribute error

~\Anaconda3\envs\Suvidha\lib\site-packages\keras_applications\resnet_common.py in ResNet(stack_fn, preact, use_bias, model_name, include_top, weights, input_tensor, input_shape, pooling, classes, **kwargs)
    346                                       default_size=224,
    347                                       min_size=32,
--> 348                                       data_format=backend.image_data_format(),
    349                                       require_flatten=include_top,
    350                                       weights=weights)

AttributeError: 'NoneType' object has no attribute 'image_data_format'

All 10 comments

In order to expedite the trouble-shooting process, please provide a code snippet to reproduce the issue reported here. Thanks!

Please try this. It works very well:
change keras_applications to keras.applications
if you are using ResNET:
from keras.applications.resnet50 import ResNet50

Please check this link
https://github.com/keras-team/keras-applications/issues/54

Automatically closing due to lack of recent activity. Please update the issue when new information becomes available, and we will reopen the issue. Thanks!

I have the latest Keras version (2.2.4) and when I do

from keras.applications.resnet import ResNet152

I am getting error:

ImportError: No module named resnet

Am I missing something?

There is something wrong with the two approaches:

keras.applications.resnet gives the error ImportError: No module named resnetwith all resnet versions except resnet 50.

whereas it imports well with keras_applications.resnet import ResNet101 but, throws the following attribute error

~\Anaconda3\envs\Suvidha\lib\site-packages\keras_applications\resnet_common.py in ResNet(stack_fn, preact, use_bias, model_name, include_top, weights, input_tensor, input_shape, pooling, classes, **kwargs)
    346                                       default_size=224,
    347                                       min_size=32,
--> 348                                       data_format=backend.image_data_format(),
    349                                       require_flatten=include_top,
    350                                       weights=weights)

AttributeError: 'NoneType' object has no attribute 'image_data_format'

There is something wrong with the two approaches:

keras.applications.resnet gives the error ImportError: No module named resnetwith all resnet versions except resnet 50.

whereas it imports well with keras_applications.resnet import ResNet101 but, throws the following attribute error

~\Anaconda3\envs\Suvidha\lib\site-packages\keras_applications\resnet_common.py in ResNet(stack_fn, preact, use_bias, model_name, include_top, weights, input_tensor, input_shape, pooling, classes, **kwargs)
    346                                       default_size=224,
    347                                       min_size=32,
--> 348                                       data_format=backend.image_data_format(),
    349                                       require_flatten=include_top,
    350                                       weights=weights)

AttributeError: 'NoneType' object has no attribute 'image_data_format'

Having the same problem ...any solution yet

you can simply refer:
http://donghao.org/2019/02/22/using-resnext-in-keras-2-2-4/
or
https://github.com/keras-team/keras-applications/issues/54

Very easily you will be able to solve your problem

I had the very same problem. Fixed it by using:

from keras_applications.imagenet_utils import _obtain_input_shape
from keras.applications.inception_v3 import preprocess_input
from keras_applications.imagenet_utils import decode_predictions

Note the . in the second line. Apparently, Keras doesn't mind the "deprecated" (v.2.2.2) syntax here, whereas for _obtain_input_shape and probably decode_predictions too (not tested), you need the underscore syntax: keras_applications. Tested w/ Tensorflow 1.12, Cudatoolkit 10.0 and Keras 2.2.4.

I face haved the same problem:

AttributeError: 'NoneType' object has no attribute 'image_data_format'

when trying to use the preprocess_input from Resnext50 on Keras 2.2.5. I have fixed as:

import keras
from keras_applications.resnext import preprocess_input as resnext50_preprocess_input

Then use the preprocess_input function as:

resnext50_preprocess_input(
                image,
                backend=keras.backend,
                layers=keras.layers,
                models=keras.models,
                utils=keras.utils
)

I'm building a model from pre-trained model as the following:

import keras
from keras_applications.resnext import preprocess_input as resnext50_preprocess_input

Then use the preprocess_input function as:

resnext50_preprocess_input(
                image,
                backend=keras.backend,
                layers=keras.layers,
                models=keras.models,
                utils=keras.utils
)

How do you ( @edumucelli ) apply the hyperparameters of resnext50_preprocess_input(...) in ImageDataGenerator class?

UPDATE :

ResNeXt is known as next-generation of ResNet. The neural network architecture changes but the input preprocessing remains.
Hence, with that justification, training DirectoryIterator based on from tensorflow.keras.preprocessing.image import ImageDataGenerator over ResNeXt base model may apply the preprocessing_input as the following:

from tensorflow.keras.applications.resnet_v2 import preprocess_input 

or

from keras.applications.resnet import preprocess_input 

then

datagen = preprocessing.image.ImageDataGenerator(
    ..., 
    preprocessing_function = preprocess_input, 
    ...
)
Was this page helpful?
0 / 5 - 0 ratings