首页 > 解决方案 > 如何将预训练的 tensorflow pb 冻结图转换为可修改的 h5 keras 模型?

问题描述

我一直在寻找一种方法来做到这一点,但我找不到答案。我发现的大多数线程都是想要反其道而行之的人。

背景故事:

我正在试验tensorflow/models存储库提供的一些预训练模型。模型保存为 .pb 冻结图。我想通过更改最终层以适应我的应用程序来微调其中一些模型。

因此,我想将模型加载到 jupyter notebook 中作为普通的 keras h5 模型。

我怎样才能做到这一点?你有更好的方法吗?

谢谢。

标签: pythontensorflowkerasdeep-learningcomputer-vision

解决方案


似乎您所要做的就是下载模型文件并将它们存储在一个目录中。调用目录,例如 c:\models。然后加载模型。

model = tf.keras.models.load_model(r'c:\models')
model.summary()  # prints out the model layers
# generate code to modify the model as you typically do for transfer learning
# compile  the changed model
# train the model
# save the trained model as a .h5 file
dir=r'path to the directory you want to save the model to'
model_identifier= 'abcd.h5' # for abcd use whatever  identification you want
save_path=os.path.join(dir, model_identifier)
model.save(save_path)

推荐阅读