【发布时间】:2020-08-25 22:28:26
【问题描述】:
我遇到了与此处描述的问题类似的问题: ValueError: Unknown layer: Functional
import tensorflow as tf
model = tf.keras.models.load_model("model.h5")
抛出:ValueError: Unknown layer: Functional.
我很确定这是因为 h5 文件保存在 TF 2.3.0 中,而我正在尝试在 2.2.0 中加载它。我宁愿不直接使用 tf 2.3.0 进行转换,我希望找到一种方法来手动修复 h5py 文件本身,或者将正确的自定义对象传递给模型加载器。我注意到它似乎只是存储配置文件的额外密钥,例如https://github.com/tensorflow/tensorflow/issues/41929
问题是,我不确定如何手动摆脱 h5 文件中的 Functional 层。具体来说,我试过了:
import h5py
f = h5py.File("model.h5",'r')
print(f['model_weights'].keys())
给出:
<KeysViewHDF5 ['concatenate_1', 'conv1d_3', 'conv1d_4', 'conv1d_5', 'dense_1', 'dropout_4', 'dropout_5', 'dropout_6', 'dropout_7', 'embedding_1', 'global_average_pooling1d_1', 'global_max_pooling1d_1', 'input_2']>
我在任何地方都看不到Functional 层。该文件中存储的模型配置到底在哪里?例如。我正在寻找类似{"class_name": "Functional", "config": {"name": "model", "layers":...}}
问题:有没有办法可以使用h5py 手动编辑 h5 文件以摆脱功能层?
或者,我可以将特定的custom_obects={'Functiona':???} 传递给load_model 函数吗?
我试过{'Functional':tf.keras.models.Model},但它返回('Keyword argument not understood:', 'groups'),因为我认为它试图将模型加载到权重中?
【问题讨论】:
-
我面临同样的问题,显然除了使用相同的 tensorflow 版本之外没有其他解决方案。如果您找到其他解决方案将不胜感激。
标签: tensorflow keras