我无法从keras.applications模块导入resnet。

16

我无法导入这个模块

import keras.applications.resnet

模块未找到错误
in () ----> 1 import keras.applications.resnet

ModuleNotFoundError: 没有名为 'keras.applications.resnet' 的模块


keras resnet 链接

8个回答

29
Keras团队目前未在当前模块中包含resnet、resnet_v2和resnext,它们将从Keras 2.2.5开始添加,如此处所述。
为了解决此问题,您可以直接使用keras_applications模块导入所有ResNet、ResNetV2和ResNeXt模型,如下所示。
from keras_applications.resnet import ResNet50

或者如果你只想使用ResNet50

from keras.applications.resnet50 import ResNet50

另外,您也可以按照此处所述的方法从源代码构建。


@SuvigyaVijay 我正在尝试您的方法,但似乎我收到了一个NoneType函数? import keras_applications.resnet_v2 as resenet2 return resenet2.ResNet101V2将返回结果保存到base_model变量中。但如果我调用 base_model(weights='imagenet', include_top=False),我会得到AttributeError: 'NoneType' object has no attribute 'image_data_format' - MasayoMusic
keras_applications doesn't seem to be compatible with eager execution, so I get an error when I try to use this in tf2.0: "module 'tensorflow' has no attribute 'get_default_graph'" - craq
1
我尝试了这个,但是出现了“ModuleNotFoundError: No module named 'keras.applications.resnet50'”的错误。 - Revolucion for Monica

10

尝试使用

from tensorflow.keras.applications.resnet50 import ResNet50

6

在Keras 2.2.4中使用ResNeXt的解决方法可以在这里找到。

ResNeXt50()函数需要4个额外的参数:backend、layers、models和utils。

import keras
from keras_applications.resnext import ResNeXt50

model = ResNeXt50(weights='imagenet',
                  backend=keras.backend,
                  layers=keras.layers,
                  models=keras.models,
                  utils=keras.utils)

1
在Keras中有多个版本的ResNet,您需要指定您想要加载的ResNet的版本,例如您希望加载ResNet50。
使用: from keras.applications import ResNet50 Edit 2 这是您在应用程序上使用 dir() 命令时获得的列表。

['DenseNet121', 'DenseNet169', 'DenseNet201', 'InceptionResNetV2', 'InceptionV3', 'MobileNet', 'MobileNetV2', 'NASNetLarge', 'NASNetMobile', 'ResNet50', 'VGG16', 'VGG19', 'Xception', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'absolute_import', 'backend', 'densenet', 'division', 'inception_resnet_v2', 'inception_v3', 'keras_applications', 'keras_modules_injection', 'layers', 'mobilenet', 'mobilenet_v2', 'models', 'nasnet', 'print_function', 'resnet50', 'utils', 'vgg16', 'vgg19', 'xception'],这里显示的模型可以像这样被加载。这里确实缺少一些像 ResNet101 这样的模型,我看能否想出解决方法。

编辑:证明这也有效

enter image description here

要查看所有可用版本的Resnet模型,请访问https://keras.io/applications/#resnet


resnet modules available inside keras.applications.renset not in keras.applications - Mohamed Thasin ah
@MohamedThasinah,仅因为你知道的方式有效,并不意味着没有其他方法可以实现它,请查看上面答案的编辑以寻找证据。顺便说一下,这是在 Google Colab 中实现的,并且摘要已被截断,但我认为它传达了信息。 - anand_v.singh
我也是,如果你成功导入resnet101,请告诉我 :) - Mohamed Thasin ah
@MohamedThasinah 你使用的Keras版本是多少? - desertnaut
@desertnaut - 我正在使用Keras 2.2.2。 - Mohamed Thasin ah
显示剩余4条评论

0

运行之前

tensorflow.keras.applications.resnet50 import ResNet50 

你需要运行

from tensorflow import keras

0
from keras.applications.resnet
import ResNet101    

tf.keras.backend.clear_session
model=VGG19()
model.summary()


tf.keras.utils.plot_model(model,show_shapes=True)
visualkeras.layered_view(model,legend=True)

1
你的回答可以通过添加更多关于代码的信息以及它如何帮助提问者来改进。 - Tyler2P

0

有一个名为“keras-resnet”的Python包,其中包含ResNet50、ResNet101、ResNet152和许多其他变体的ResNet。(https://pypi.org/project/keras-resnet/

安装也非常简单。只需输入:

pip install keras-resnet

它将安装该模块,然后像这样使用它:

from keras_resnet.models import ResNet50, ResNet101, ResNet152

backbone = ResNet50(inputs=image_input, include_top=False, freeze_bn=True)
C2, C3, C4, C5 = backbone.outputs # this will give you intermediate 
# outputs of four blocks of resnet if you want to merge low and high level features

我正在使用这个模块的骨架,对我来说运行良好!


该项目没有文档。不是很可靠/可信赖。 - Pe Dro

0

检查版本:

pip list | grep Keras

如果已经安装,请卸载并升级:
pip uninstall Keras
pip install Keras==2.3.1

pip uninstall Keras-Applications
pip install Keras-Applications==1.0.8

pip uninstall Keras-Preprocessing
pip install Keras-Preprocessing==1.1.0

我认为最好使用“pip install --upgrade”,因为这些版本很快就会过时。 - jtb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接