keras-team / keras-applications

Reference implementations of popular deep learning models.
Other
2k stars 910 forks source link

AttributeError: 'NoneType' object has no attribute 'image_data_format' when creating model #69

Closed sun-yitao closed 5 years ago

sun-yitao commented 5 years ago

Steps to replicate: (tried on MacOS Mojave and Windows 10)

$conda install tensorflow-gpu keras 
The following packages will be UPDATED:

    keras:               2.2.2-0                   --> 2.2.4-0
    keras-applications:  1.0.4-py36_1              --> 1.0.6-py36_0
    keras-base:          2.2.2-py36_0              --> 2.2.4-py36_0
    keras-preprocessing: 1.0.2-py36_1              --> 1.0.5-py36_0
    tensorboard:         1.10.0-py36he025d50_0     --> 1.12.2-py36h33f27b4_0
    tensorflow:          1.10.0-gpu_py36h3514669_0 --> 1.12.0-gpu_py36ha5f9131_0
    tensorflow-base:     1.10.0-gpu_py36h6e53903_0 --> 1.12.0-gpu_py36h6e53903_0
    tensorflow-gpu:      1.10.0-hf154084_0         --> 1.12.0-h0d30ee6_0

Proceed ([y]/n)? y

conda create -n testenv tensorflow-gpu keras works too

$python
Python 3.6.7 |Anaconda, Inc.| (default, Oct 28 2018, 19:44:12) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
>>> from keras_applications.xception import Xception
>>> Xception()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\USER\AppData\Local\conda\conda\envs\yitao\lib\site-packages\keras_applications\xception.py", line 107, in Xception
    if backend.image_data_format() != 'channels_last':
AttributeError: 'NoneType' object has no attribute 'image_data_format'

Creating a ResNeXt (keras applications 1.0.7) and Inception v3 model also gives the same error. I also tried installing keras and keras-applications using pip but same error again. Downgrading to keras 2.2.2 and everything will work properly.

Update: I found that this works:

import keras
from keras import applications
model = applications.xception.Xception()

However I'm not sure if this is the intended usage as the docs seem to be using the previous style of from keras.applications.inception_v3 import InceptionV3

taehoonlee commented 5 years ago

@sun-yitao, The official documents say from keras.applications.xception import Xception not from keras_applications.xception import Xception.

sun-yitao commented 5 years ago

Oh dear must have seen wrongly, will close this issue now

Kautenja commented 5 years ago

alternatively, you can use the following to interact with keras_applications directly. I had to do so to get access to ResNet101 that is unavailable through keras.applications for some reason.

import keras
import keras_applications
keras_applications.set_keras_submodules(
    backend=keras.backend,
    layers=keras.layers,
    models=keras.models,
    utils=keras.utils
)
taehoonlee commented 5 years ago

@Kautenja, That can be one of the workarounds. Thank you.

kkanellis commented 5 years ago

FYI this no longer works, as the set_keras_submodules function has been removed from keras_applications/__init__.py

idearibosome commented 5 years ago

@kkanellis Instead of using set_keras_submodules, you can directly pass the keras submodules when you create the model, e.g.,

import keras
import keras_applications

resnet = keras_applications.resnet.ResNet101(
    backend=keras.backend,
    layers=keras.layers,
    models=keras.models,
    utils=keras.utils
)
TahaAnwar commented 5 years ago

@kkanellis Instead of using set_keras_submodules, you can directly pass the keras submodules when you create the model, e.g.,

import keras
import keras_applications

resnet = keras_applications.resnet.ResNet101(
    backend=keras.backend,
    layers=keras.layers,
    models=keras.models,
    utils=keras.utils
)

Model loads with this but later with doing inference with .predict() This gives this error: 'NoneType' object has no attribute 'image_data_format'

astronstar commented 4 years ago

from keras_applications import resnet_common model = resnet_common.ResNet101(include_top=False, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000,backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils)

running as : Downloading data from https://github.com/keras-team/keras-applications/releases/download/resnet/resnet101_weights_tf_dim_ordering_tf_kernels_notop.h5 163840/171446536 [..............................] - ETA: 18:08:28

maybe it work on keras.version==2.2.4,keras_applications.version=1.0.8