简体   繁体   English

如何将预训练的 tensorflow pb 冻结图转换为可修改的 h5 keras 模型?

[英]How to convert a pretrained tensorflow pb frozen graph into a modifiable h5 keras model?

I have been searching for a method to do this for so long, and I can not find an answer.我一直在寻找一种方法来做到这一点,我找不到答案。 Most threads I found are of people wanting to do the opposite.我发现的大多数线程都是想要做相反的事情的人。

Backstory:背景故事:

I am experimenting with some pre-trained models provided by the tensorflow/models repository.我正在试验tensorflow/models存储库提供的一些预训练模型。 The models are saved as .pb frozen graphs.模型保存为 .pb 冻结图。 I want to fine-tune some of these models by changing the final layers to suit my application.我想通过更改最终层以适合我的应用程序来微调其中一些模型。

Hence, I want to load the models inside a jupyter notebook as a normal keras h5 model.因此,我想将模型作为普通的 keras h5 模型加载到 jupyter 笔记本中。

How can I do that?我怎样才能做到这一点? do you have a better way to do so?你有更好的方法吗?

Thanks.谢谢。

seems like all you would have to do is download the model files and store them in a directory.似乎您所要做的就是下载模型文件并将它们存储在一个目录中。 Call the directory for example c:\\models.调用目录,例如 c:\\models。 Then load the model.然后加载模型。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将具有自定义 keras 层(.h5)的 keras 模型冻结到张量流图(.pb)? - How to freeze a keras model with custom keras layers(.h5) to tensorflow graph(.pb)? 如何将 Keras.h5 导出为 tensorflow.pb? - How to export Keras .h5 to tensorflow .pb? 将keras h5转换为tensorflow pb以进行批处理推断 - convert keras h5 to tensorflow pb for batch inference 如何将 .pb 文件转换为 .h5。 (Tensorflow 模型到 keras) - How to convert .pb file to .h5. (Tensorflow model to keras) Convert a Tensorflow model in SavedModel format (.pb file) saved with tf.saved_model.save to a Keras model (.h5 file) - Convert a Tensorflow model in SavedModel format (.pb file) saved with tf.saved_model.save to a Keras model (.h5 file) Tensorflow lite model 推理与 keras h5 model(VGG16 预训练)相比非常慢 - Tensorflow lite model inference is very slow compared to keras h5 model (VGG16 pretrained) 如何将Tensorflow Simple Audio Recognition冷冻图(.pb)转换为Core ML模型? - How to convert Tensorflow Simple Audio Recognition frozen graph(.pb) to Core ML model? 无法将 keras/tensorflow h5/json 转换为 tensorflow pb - Trouble converting keras/tensorflow h5/json into tensorflow pb 如何将 .h5 文件转换为 .pb 文件? - how to convert .h5 file to .pb file? 加载 Tensorflow keras Model (.h5) 时出错 - Error loading Tensorflow keras Model (.h5)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM