转换时发生Coremltools错误:“'str'对象没有属性'decode'”

问题描述

我正在尝试使用coremltools将keras模型转换为mlmodel。 我得到以下代码

import coremltools
import keras




output_labels = list(range(1,43))
your_model = coremltools.converters.keras.convert('my_model.h5',input_names=['image'],output_names=['output'],class_labels=output_labels,image_input_names='image')

#your_model.author = 'your name'
#your_model.short_description = 'Digit Recognition with MNIST'
#your_model.input_description['image'] = 'Takes as input an image'
#your_model.output_description['output'] = 'Prediction of Digit

your_model.save('your_model_name.mlmodel')

这给了我以下错误

Users/admin/PycharmProjects/TSR37/venv/bin/python /Users/admin/PycharmProjects/TSR37/coremlconverter.py
Traceback (most recent call last):
  File "/Users/admin/PycharmProjects/TSR37/coremlconverter.py",line 9,in <module>
    class_labels=output_labels,image_input_names='image')
  File "/Users/admin/PycharmProjects/TSR37/venv/lib/python3.7/site-packages/coremltools/converters/keras/_keras_converter.py",line 830,in convert
    use_float_arraytype=use_float_arraytype,File "/Users/admin/PycharmProjects/TSR37/venv/lib/python3.7/site-packages/coremltools/converters/keras/_keras_converter.py",line 607,in _convert_to_spec
    use_float_arraytype=use_float_arraytype,File "/Users/admin/PycharmProjects/TSR37/venv/lib/python3.7/site-packages/coremltools/converters/keras/_keras2_converter.py",line 353,in _convert
    model = _keras.models.load_model(model,custom_objects=custom_objects)
  File "/Users/admin/PycharmProjects/TSR37/venv/lib/python3.7/site-packages/keras/engine/saving.py",line 419,in load_model
    model = _deserialize_model(f,custom_objects,compile)
  File "/Users/admin/PycharmProjects/TSR37/venv/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'

Process finished with exit code 1

可能是什么问题?根据错误消息,问题出在keras代码上,而不是我的代码上,但我不知道。 我正在使用coremltools 4.0和python 3.7.9

谢谢

解决方法

这似乎与h5yp的3.0.0最新版本有关,因此您可以使用h5yp的先前版本(pip install tensorflow h5py<3.0.0),也可以尝试删除.decode('utf-8')来自tensorflow/python/keras/saving/hdf5_format.py。我知道它们不是您要找的答案,但似乎是出路。否则,您可以等待更新。