Coremltools error while converting: “'str' object has no attribute 'decode'” [duplicate]

时间秒杀一切 提交于 2021-01-05 06:03:45

问题


I am trying to use coremltools to convert a keras model to mlmodel. I am getting the following code:

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')

This gives me the following error:

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

What could be the problem? According to the error message, the problem is with a keras code, not my code, but I don't know. I am using coremltools 4.0 and python 3.7.9

Thank you


回答1:


it seems to be an issue related to a recent release of version 3.0.0 of h5yp, so either you can use a previous version of h5yp (pip install tensorflow h5py<3.0.0) or you can try to remove .decode('utf-8') from tensorflow/python/keras/saving/hdf5_format.py. I know they are not the answer you are looking for but seems to be the way out. Otherwise you can just wait for an update.



来源:https://stackoverflow.com/questions/64767814/coremltools-error-while-converting-str-object-has-no-attribute-decode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!