转换时出现 Coremltools 错误:“'str' 对象没有属性 'decode'”
Coremltools error while converting: "'str' object has no attribute '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
。我知道它们不是您正在寻找的答案,但似乎是出路。否则你只能等待更新。
我正在尝试使用 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
。我知道它们不是您正在寻找的答案,但似乎是出路。否则你只能等待更新。