Search code examples
pythontensorflowkeraspython-module

TensorFlow/Keras: How to get missing models (ResNet101, ResNeXt, etc.) from Keras' applications module?


A number of documented Keras applications are missing from my (up-to-date) Keras installation and TensorFlow 1.10 Keras API installation. I import Keras' applications module as suggested and use it as follows:

from keras import applications
resnet = applications.ResNeXt101(include_top=False, weights='imagenet', input_shape=(SCALED_HEIGHT, SCALED_WIDTH, 3), pooling=None)

I also tried

resnet = applications.resnext.ResNeXt101(include_top=False, weights='imagenet', input_shape=(SCALED_HEIGHT, SCALED_WIDTH, 3), pooling=None)

But in both cases I get the same type of error:

AttributeError: module 'keras.applications' has no attribute 'ResNeXt101'

Printing help(applications) yields:

Help on package keras.applications in keras:

NAME
    keras.applications

PACKAGE CONTENTS
    densenet
    imagenet_utils
    inception_resnet_v2
    inception_v3
    mobilenet
    mobilenet_v2
    mobilenetv2
    nasnet
    resnet50
    vgg16
    vgg19
    xception

FUNCTIONS
    DenseNet121 = wrapper(*args, **kwargs)
    DenseNet169 = wrapper(*args, **kwargs)
    DenseNet201 = wrapper(*args, **kwargs)
    InceptionResNetV2 = wrapper(*args, **kwargs)
    InceptionV3 = wrapper(*args, **kwargs)
    MobileNet = wrapper(*args, **kwargs)
    MobileNetV2 = wrapper(*args, **kwargs)
    NASNetLarge = wrapper(*args, **kwargs)
    NASNetMobile = wrapper(*args, **kwargs)
    ResNet50 = wrapper(*args, **kwargs)
    VGG16 = wrapper(*args, **kwargs)
    VGG19 = wrapper(*args, **kwargs)
    Xception = wrapper(*args, **kwargs)
    keras_modules_injection(base_fun)

which shows that the models initially aren't present in my installation. Why not? They're also not packaged in the TensorFlow's Keras API.

I tried copying the files from the Keras applications GitHub repository and pasting them in site-packages/keras/applications/, but this results in the following stacktrace:

File "myscript.py", line 517, in get_fpn
    resnet = applications.resnext.ResNeXt101(include_top=False, weights='imagenet', input_shape=(SCALED_HEIGHT, SCALED_WIDTH, 3), pooling=None)
  File "site-packages/keras/applications/resnet_common.py", line 575, in ResNeXt101
    **kwargs)
  File "site-packages/keras/applications/resnet_common.py", line 348, in ResNet
    data_format=backend.image_data_format(),
AttributeError: 'NoneType' object has no attribute 'image_data_format'

Any ideas on how to fix this? Why aren't these included and working in default installations of either Keras or TensorFlow? Why does the documentation not explain this?


Solution

    • Reason for the problem:

    The backend object is None at line 348. My guess is you tried something like this:

    >>> from keras_applications import resnext
    >>> resnext.ResNeXt101(weights=None)
    

    The backend information is injected from keras.applications to keras_applications via the keras_modules_injection decorator.

    https://github.com/keras-team/keras/blob/c658993cf596fbd39cf800873bc457e69cfb0cdb/keras/applications/resnext.py#L17

    • Procedure to fix the problem:

    Make sure the keras & keras applications versions are as follows:

    >>pip list |grep Keras
    Keras                  2.2.4
    Keras-Applications     1.0.8
    

    If they're not, upgrade using

    >>pip install --upgrade keras keras-applications
    

    Update the changes from this pull request, https://github.com/keras-team/keras/pull/11203/files into site-packages/keras/applications

    from keras import applications
    resnext = applications.resnext.ResNeXt101(include_top=False, weights=None, input_shape=(299,299,3))
    print(type(resnext))