keras-applications icon indicating copy to clipboard operation
keras-applications copied to clipboard

MobileNetV2 with undefined backend, layers and models

Open christian-rauch opened this issue 6 years ago • 8 comments

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?

christian-rauch avatar Nov 30 '18 19:11 christian-rauch

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!

Luckick avatar Dec 06 '18 21:12 Luckick

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

christian-rauch avatar Dec 06 '18 21:12 christian-rauch

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

Luckick avatar Dec 07 '18 02:12 Luckick

@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')

taehoonlee avatar Dec 07 '18 05:12 taehoonlee

@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.

christian-rauch avatar Dec 07 '18 11:12 christian-rauch

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.....

Light-- avatar Apr 26 '19 11:04 Light--

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.

hfwang0318 avatar Aug 07 '19 14:08 hfwang0318

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)

dagrimm avatar Sep 12 '19 18:09 dagrimm