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

猜你喜欢

转载自blog.csdn.net/xinxiang7/article/details/110221620