python - 转换时出现 Coremltools 错误 : "' str' object has no attribute 'decode' "

标签 python keras coremltools

我正在尝试使用 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 。我知道它们不是您正在寻找的答案,但似乎是出路。否则你只能等待更新。

关于python - 转换时出现 Coremltools 错误 : "' str' object has no attribute 'decode' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64767814/

相关文章:

python - Pyspark - 从数据帧创建训练集和测试集

python - 带有 TF 后端的 Keras : get gradient of outputs with respect to inputs

python - tf.data.Dataset 与 tf.keras 的性能

ios - 减小使用 turicreate 创建的半精度 Core ML 模型的大小

python - 转换 keras 模型后 coreml 中的输入形状不正确

python - 小数精度的分数

python - 类型错误 : cannot unpack non-iterable int object

python - 带有 Python 的 jupyter 实验室中函数的参数

tensorflow - 分析 keras 模型时出错