簡體   English   中英

TensorFlow / Keras:如何從Keras的應用程序模塊中獲取缺失的模型(ResNet101,ResNeXt等)?

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

我(最新)的Keras安裝和TensorFlow 1.10 Keras API安裝中缺少許多已記錄的Keras應用程序 我按照建議導入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應用程序GitHub存儲庫復制文件並將其粘貼到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的默認安裝中使用它們? 為什么文檔沒有解釋這一點?

  • 問題原因:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM