keras-team / keras-applications

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

MobileNetV2 with undefined backend, layers and models #54

Open christian-rauch opened 5 years ago

christian-rauch commented 5 years ago

When instantiating MobileNetV2 without special kwargs I get the error:

Using TensorFlow backend.
Traceback (most recent call last):
[...]
  File "/usr/local/lib/python2.7/dist-packages/keras_applications/mobilenet_v2.py", line 267, in MobileNetV2
    if backend.image_data_format() == 'channels_first':
AttributeError: 'NoneType' object has no attribute 'image_data_format'

because backend is undefined (None).

Explicitly defining the backend via kwargs MobileNetV2(..., backend=keras.backend) solves the issue.

It is not obvious to me, why get_submodules_from_kwargs is not using the keras defaults (in this case keras.backend) since there is no reason to mix backends in one instance of keras.

Why does one have to set the backend, layers and models explicitly when using MobileNetV2?

Luckick commented 5 years ago

Thank you for your sharing! Do you have any idea what would be appropriate to set up keras_utils in kwargs? Thank you for your help!

christian-rauch commented 5 years ago

Are there any other options than Keras?

Because get_submodules_from_kwargs uses the global variables _KERAS_BACKEND, _KERAS_LAYERS, _KERAS_MODELS and _KERAS_UTILS they should simply be set to keras.backend etc.

Currently they are define to None: https://github.com/keras-team/keras-applications/blob/df0e26c73951e107a84969944305f492a9abe6d7/keras_applications/__init__.py#L7-L10

Luckick commented 5 years ago

By setting: backend=keras.backend, layers = keras.layers, models = keras.models, utils = keras.utils as kwargs, problem solved. Thank you.

taehoonlee commented 5 years ago

@christian-rauch, The keras-applications is designed to work with two backends: keras and tf.keras. Thus, if you want to use the keras-based applications, you should import modules from keras NOT keras-applications directly. The official example is:

from keras.applications.resnet50 import ResNet50
model = ResNet50(weights='imagenet')
christian-rauch commented 5 years ago

@taehoonlee If the official keras applications use keras.applications as namespace, I don't see the reason why keras cannot be the default in keras_applications. Similar to how keras manages its backends (tensorflow, theano, cntk), keras_applications should have a user-configurable backend with a sensible default setting.

Using keras.applications for importing models that are defined in a different python package (that needs to be installed manually) also entails that the namespaces between both packages always need to be in sync. E.g. a new model in keras_applications will need its counterpart boilerplate definition in keras.applications before it can be used.

Light-- commented 5 years ago

By setting: backend=keras.backend, layers = keras.layers, models = keras.models, utils = keras.utils as kwargs, problem solved. Thank you.

modify the init file called by your self-defined model file (for me, that is " init .py" called by "new_vgg16.py"), change the following lines in init .py :

def get_submodules_from_kwargs(kwargs):
    backend = kwargs.get('backend', _KERAS_BACKEND)
    layers = kwargs.get('layers', _KERAS_LAYERS)
    models = kwargs.get('models', _KERAS_MODELS)
    utils = kwargs.get('utils', _KERAS_UTILS)

to

def get_submodules_from_kwargs(kwargs):
    # backend = kwargs.get('backend', _KERAS_BACKEND)
    backend = keras.backend
    layers = keras.layers
    models = keras.models
    utils = keras.utils

that is what i think Luckick means.....

hfwang0318 commented 5 years ago

I just by setting kwargs manually, for examples: model = ResNeXt(..., backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) thus you can run correctly.

dagrimm commented 5 years ago

Answer from @hfwang0318 works for TF 2.0, too. Just set kwargs to model = ResNeXt(..., backend=tf.keras.backend, layers=tf.keras.layers, models=tf.keras.models, utils=tf.keras.utils)