Open christian-rauch opened 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!
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
By setting: backend=keras.backend, layers = keras.layers, models = keras.models, utils = keras.utils as kwargs, problem solved. Thank you.
@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 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.
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.....
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.
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)
When instantiating
MobileNetV2
without specialkwargs
I get the error: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 casekeras.backend
) since there is no reason to mix backends in one instance of keras.Why does one have to set the
backend
,layers
andmodels
explicitly when usingMobileNetV2
?