AttributeError: module 'keras.engine.topology' has no attribute'load_weights_from_hdf5_group_by_name

本文分享了在配置Mask-Rcnn过程中遇到的问题及解决方案,包括如何修正从.ipynb文件转换为.py文件时出现的错误,以及如何解决因keras版本不匹配导致的AttributeError。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近配置Mask-Rcnn遇到的中间的问题。通常网上的教程一般使用jupyter notebook或者Ipython notebook,还有很多人希望直接把.ipynb转成.py 。在转成.py的demo程序后,往往需要注释一些代码:

    1.将生成的.py程序(以demo.py为例)第32行:get_ipython().run_line_magic('matplotlib','line')   此行注释。因为 get_ipython()只有在使用ipython或者jupyter会用到,生成的demo.py不会用到这个包。

    2.在终端运行 python3 demo.py时会出现这样的错误:AttributeError: module 'keras.engine.topology' has no attribute'load_weights_from_hdf5_group_by_name'  ;

                

          出现这个错误的原因是:keras的版本不对。当我们在配置mask-rcnn的时候,根目录下的requirements.txt里面要求的python的包注明的是“keras>=2.0.8”,但是load_weights_from_hdf5_group_by_name只在keras2.0.8的版本中出现,不会出现在最新的keras版本中,(大家可以看下自己的keras版本,命令如下:(1)python 回车(2)import keras h 回车(3)keras.__version__ 回车),我这里默认安装的是keras2.2.0,版本太高。

          解决方法:1)卸载keras:pip uninstall keras       2)安装2.0.8版本的keras:pip install keras==2.0.8 

          再运行,OK,没问题,可以继续踩坑了偷笑偷笑偷笑

源代码import jieba import numpy as np import matplotlib.pyplot as plt from keras.models import Model from keras.layers import Input, Embedding, Bidirectional, LSTM, Dense, Dropout from keras.preprocessing.text import Tokenizer from keras.preprocessing.sequence import pad_sequences from keras.utils import to_categorical from keras import backend as K from tensorflow import kerasimport warnings from keras.src.api_export import keras_export from keras.src.utils.module_utils import dmtree from keras.src.utils.module_utils import optree if optree.available: from keras.src.tree import optree_impl as tree_impl elif dmtree.available: from keras.src.tree import dmtree_impl as tree_impl else: raise ImportError( "To use Keras, you need to have `optree` installed. " "Install it via `pip install optree`" ) def register_tree_node_class(cls): return tree_impl.register_tree_node_class(cls) @keras_export("keras.tree.MAP_TO_NONE") class MAP_TO_NONE: """Special value for use with `traverse()`.""" pass @keras_export("keras.tree.is_nested") def is_nested(structure): """Checks if a given structure is nested. Examples: >>> keras.tree.is_nested(42) False >>> keras.tree.is_nested({"foo": 42}) True Args: structure: A structure to check. Returns: `True` if a given structure is nested, i.e. is a sequence, a mapping, or a namedtuple, and `False` otherwise. """ return tree_impl.is_nested(structure) @keras_export("keras.tree.traverse") def traverse(func, structure, top_down=True): """Traverses the given nested structure, applying the given function. The traversal is depth-first. If `top_down` is True (default), parents are returned before their children (giving the option to avoid traversing into a sub-tree). Examples: >>> v = [] >>> keras.tree.traverse(v.append, [(1, 2), [3], {"a": 4}], top_down=True) [(1, 2), [3], {'a': 4}] >>> v [[(1
最新发布
03-14
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值