将以tensorflow格式保存的模型加载到keras时发生AttributeError

2024-04-25 21:09:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试将保存的模型加载回keras时遇到一些问题。保存模型很好:

In [6]: model.save('savefile', save_format='tf')
INFO:tensorflow:Assets written to: savefile/assets

但是在加载模型时,我得到了以下错误:

In [7]: tf.keras.models.load_model('savefile')
ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-7-ddb252fa0c51>", line 1, in <module>
    tf.keras.models.load_model('lofasz.xx')
  File "/usr/local/lib/python3.5/dist-packages/tensorflow_core/python/keras/saving/save.py", line 190, in load_model
    return saved_model_load.load(filepath, compile)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 105, in load
    model = tf_load.load_internal(path, loader_cls=KerasObjectLoader)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow_core/python/saved_model/load.py", line 599, in load_internal
    export_dir)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 170, in __init__
    super(KerasObjectLoader, self).__init__(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow_core/python/saved_model/load.py", line 122, in __init__
    self._load_all()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 183, in _load_all
    self._finalize_objects()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 394, in _finalize_objects
    self._reconstruct_all_models()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 412, in _reconstruct_all_models
    self._reconstruct_model(model_id, model, layers)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 442, in _reconstruct_model
    model._set_inputs(input_shape)  # pylint: disable=protected-access
  File "/usr/local/lib/python3.5/dist-packages/tensorflow_core/python/keras/engine/training.py", line 2448, in _set_inputs
    inputs = self._set_input_attrs(inputs)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow_core/python/training/tracking/base.py", line 456, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow_core/python/keras/engine/training.py", line 2487, in _set_input_attrs
    input_shape = (None,) + tuple(inputs.shape[1:])
AttributeError: 'list' object has no attribute 'shape'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py", line 1821, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'AttributeError' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/IPython/core/ultratb.py", line 1132, in get_records
    return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
  File "/usr/lib/python3/dist-packages/IPython/core/ultratb.py", line 313, in wrapped
    return f(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/IPython/core/ultratb.py", line 358, in _fixed_getinnerframes
    records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
  File "/usr/lib/python3.5/inspect.py", line 1454, in getinnerframes
    frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
  File "/usr/lib/python3.5/inspect.py", line 1411, in getframeinfo
    filename = getsourcefile(frame) or getfile(frame)
  File "/usr/lib/python3.5/inspect.py", line 671, in getsourcefile
    if getattr(getmodule(object, filename), '__loader__', None) is not None:
  File "/usr/lib/python3.5/inspect.py", line 717, in getmodule
    os.path.realpath(f)] = module.__name__
AttributeError: module has no attribute '__name__'
---------------------------------------------------------------------------

使用tf.saved_model.load加载确实有效:

In [9]: qq=tf.saved_model.load('savefile')

In [10]: qq
Out[10]: <tensorflow.python.saved_model.load.Loader._recreate_base_user_object.<locals>._UserObject at 0x7fdff155feb8>

但我没有找到任何关于如何将其作为keras模型恢复的信息。有什么想法吗


Tags: inpycoremodellibpackagesusrlocal