keras与h5py版本不兼容

问题

Initializing data generator: / |#                                                                                                                                                                                                                   | 0 Elapsed Time: 0:00:00Traceback (most recent call last):
  File "evaluate_crfnet.py", line 109, in <module>
    model = keras.models.load_model(args.model, custom_objects=backbone.custom_objects)
  File "/home/jiangcm/anaconda3/envs/crfnet/lib/python3.7/site-packages/keras/engine/saving.py", line 419, in load_model
    model = _deserialize_model(f, custom_objects, compile)
  File "/home/jiangcm/anaconda3/envs/crfnet/lib/python3.7/site-packages/keras/engine/saving.py", line 224, in _deserialize_model
    model_config = json.loads(model_config.decode('utf-8'))
AttributeError: 'str' object has no attribute 'decode'

分析:

keras与h5py版本不兼容。
查看h5py的版本,发现安装的是3.0.0的版本。h5py的官网说到在3.0.0的版本中,字符串的保存和加载进行了更新。如下:

Storing strings
When creating a new dataset or attribute, Python str or bytes objects will be treated as variable-length strings, marked as UTF-8 and ASCII respectively. Numpy bytes arrays (‘S’ dtypes) make fixed-length strings. You can use string_dtype() to explictly specify any HDF5 string datatype.
When writing data to an existing dataset or attribute, data passed as bytes is written without checking the encoding. Data passed as Python str objects is encoded as either ASCII or UTF-8, based on the HDF5 datatype. In either case, null bytes (’\x00’) in the data will cause an error.

2.10版本中,存储字符串的方式如下:

Strings in HDF5
The Most Important Thing
If you remember nothing else, remember this:
All strings in HDF5 hold encoded text.

可以明显的看到,在2.10版本中,字符串的存储是经过编码的,而在3.0.0的版本中,则不会检查是否经过编码。所以才会出现最初的AttributeError: ‘str’ object has no attribute ‘decode’。

解决方案

降低h5py版本

pip install h5py==2.10
  • 13
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值