python - 如何将预训练的 tensorflow pb 冻结图转换为可修改的 h5 keras 模型?
问题描述
我一直在寻找一种方法来做到这一点,但我找不到答案。我发现的大多数线程都是想要反其道而行之的人。
背景故事:
我正在试验tensorflow/models存储库提供的一些预训练模型。模型保存为 .pb 冻结图。我想通过更改最终层以适应我的应用程序来微调其中一些模型。
因此,我想将模型加载到 jupyter notebook 中作为普通的 keras h5 模型。
我怎样才能做到这一点?你有更好的方法吗?
谢谢。
解决方案
似乎您所要做的就是下载模型文件并将它们存储在一个目录中。调用目录,例如 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)
推荐阅读
- java - 如何在 Apache POI 中创建 XSSFTable
- javascript - 加载克隆的模态(引导程序)
- java - 当方法调用与执行时完全一样时,EasyMock 对象抛出意外的方法调用
- javascript - react-google-maps - 未捕获的 ReferenceError:未定义谷歌
- r - 查找唯一值的第一个示例并返回行号
- php - 访问受保护属性的 Symfony 代码应该抛出错误,但不会
- json - 榆树用数组解码json对象
- javascript - 如何解决脚本库冲突?
- java - Java + 硒
- javascript - 如何在 html.eex 模板中使用 NPM 库中的函数