keras: AttributeError: 'NoneType' object has no attribute 'image_data_format'

在 https://keras-zh.readthedocs.io/applications/ 给出了keras applications 中的一些预训练模型,部分包导入方式应为:

import keras_applications
mod =keras_applications.resnet.ResNet101(include_top=True,
    weights=None,
    input_shape=(224,224,3),
    )

此时会出现如题错误:

data_format=backend.image_data_format(),
AttributeError: 'NoneType' object has no attribute 'image_data_format'

改正方法,代码添加四行修改为:

mod = keras_applications.resnet.ResNet101(include_top=True,
                                          weights=None,
                                          input_shape=(224, 224, 3),
                                          backend=keras.backend,
                                          layers=keras.layers,
                                          models=keras.models,
                                          utils=keras.utils
                                          )

为什么这莫改,可以参考源码:

./Anaconda3/Lib/site-packages/keras/applications/resnet50.py
.:/Anaconda3/Lib/site-packages/keras/applications/init.py
keras.applications.resnet50在构建实例的时候调用了装饰器keras_modules_injection,给keras_applications相关类增加参数。

你可能感兴趣的:(python与人工睿智)