mask rcnn 加载权重报错
in load_weights_from_hdf5_group
original_keras_version = f.attrs[‘keras_version’].decode(‘utf8’)
AttributeError: ‘str’ object has no attribute ‘decode’
试过其他博客的方法
卸载原来的h5py模块,安装2.10版本
pip install h5py==2.10 -i https://pypi.tuna.tsinghua.edu.cn/simple/
仍然不行,那只有从报错的源码入手了
根据报错提示问题出在
xxx\Lib\site-packages\keras\engine\topology.py
这个文件的3418,3422行。
python3中,编码的时候区分了字符串和二进制
python3在bytes和str两种类型转换,所需要的函数依次是encode(),decode()
这里f.attrs[‘keras_version’]类型我们不知道,因此需要判断是str还是bytes以便使用相应的方法
如果不判断,这个类型一会是str,一会是bytes,这个bug让我找了好久
代码如下
if 'keras_version' in f.attrs:
original_keras_version = f.attrs['keras_version'].encode('utf-8') if isinstance(f.attrs['keras_version'],str) else f.attrs['keras_version'].decode('utf-8')
else:
original_keras_version = '1'
if 'backend' in f.attrs:
original_backend = f.attrs['backend'].encode('utf-8') if isinstance(f.attrs['backend'],str) else f.attrs['backend'].decode('utf-8')
else:
original_backend = None
``
正常运行截图