【问题标题】:TensorFlow/Keras: How to get missing models (ResNet101, ResNeXt, etc.) from Keras' applications module?TensorFlow/Keras:如何从 Keras 的应用程序模块中获取缺失的模型(ResNet101、ResNeXt 等)?
【发布时间】:2019-11-04 22:18:04
【问题描述】:

我的(最新)Keras 安装和 TensorFlow 1.10 Keras API 安装中缺少许多记录在案的 Keras applications。 我按照建议导入 Keras 的应用程序模块并按如下方式使用它:

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

我也试过

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

但在这两种情况下,我都会遇到相同类型的错误:

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

打印help(applications) 产量:

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)

这表明模型最初不存在于我的安装中。为什么不?它们也没有打包在 TensorFlow 的 Keras API 中。

我尝试从Keras applications GitHub repository 复制文件并将它们粘贴到site-packages/keras/applications/,但这会导致以下堆栈跟踪:

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'

关于如何解决这个问题的任何想法?为什么在 Keras 或 TensorFlow 的默认安装中不包含这些并在其中工作?为什么文档没有解释这一点?

【问题讨论】:

  • 我看到您正在删除并重新发布您的问题,您不应该这样做。
  • 是的,我知道这不好,但我和未来遇到同样问题的人确实得到了很好的答案

标签: python tensorflow keras python-module


【解决方案1】:
  • 问题原因:

backend 对象在第 348 行是 None。 我猜你尝试过这样的事情:

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

backend 信息通过 keras_modules_injection 装饰器从 keras.applications 注入到 keras_applications。

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

  • 解决问题的过程:

确保 keras 和 keras 应用程序版本如下:

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

如果不是,请使用升级

>>pip install --upgrade keras keras-applications

将此拉取请求中的更改,https://github.com/keras-team/keras/pull/11203/files 更新为 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))

【讨论】:

  • 非常感谢您的回复!我通过添加必要的行并注释掉执行相同操作的新行来合并拉取请求,但我在from . import keras_modules_injection 行上得到ImportError: cannot import name 'keras_modules_injection'。我假设因为keras_modules_injection 不存在。以及当拉取请求已经与主分支合并时,为什么我需要合并拉取请求(我假设很久以前,因为关联文件的代码现在看起来不同了)。
  • 如果您使用的是主分支,则不需要。如果您使用的是 2.2.4 版本,则需要合并。
  • 请再次检查您的合并。也许有些东西在 __init__.py 中被覆盖了。 keras_modules_injection 存在于github.com/keras-team/keras/blob/2.2.4/keras/applications/…
  • 嗯我不认为我覆盖了任何东西,但我只是从 master 重新安装,现在我更进一步(即使它们具有相同的版本号,所以我真的不明白) .非常感谢!现在我在调用applications.resnext.ResNeXt101() 时得到"tensorflow/python/ops/array_ops.py", line 1732, in placeholder 中的RuntimeError: tf.placeholder() is not compatible with eager execution.,但我想这是一个不相关的问题,不是我有能力解决的问题。
  • 如何更新拉取请求中的更改?我执行了conda install -c anaconda keras-gpu 命令,接下来我应该运行什么?
猜你喜欢
  • 1970-01-01
  • 2019-12-22
  • 2021-10-13
  • 2018-05-18
  • 2017-12-17
  • 1970-01-01
  • 1970-01-01
  • 2017-02-23
  • 2019-06-23
相关资源
最近更新 更多