如何将训练有素的Tensorflow模型转换为Keras? [英] How can I convert a trained Tensorflow model to Keras?

查看:934
本文介绍了如何将训练有素的Tensorflow模型转换为Keras?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个训练有素的Tensorflow模型和权重向量,已分别导出到protobuf和weights文件中.

I have a trained Tensorflow model and weights vector which have been exported to protobuf and weights files respectively.

如何将它们转换为Keras可以使用的JSON或YAML和HDF5文件?

How can I convert these to JSON or YAML and HDF5 files which can be used by Keras?

我具有Tensorflow模型的代码,因此将tf.Session转换为keras模型并将其保存在代码中也是可以接受的.

I have the code for the Tensorflow model, so it would also be acceptable to convert the tf.Session to a keras model and save that in code.

推荐答案

当前,Tensorflow或Keras中没有直接内置的支持将冻结的模型或检查点文件转换为hdf5格式.

Currently, there is no direct in-built support in Tensorflow or Keras to convert the frozen model or the checkpoint file to hdf5 format.

但是,由于您已经提到拥有Tensorflow模型的代码,因此必须在Keras中重写该模型的代码.然后,您将必须从检查点文件中读取变量的值,然后使用layer.load_weights(weights)方法将其分配给Keras模型.

But since you have mentioned that you have the code of Tensorflow model, you will have to rewrite that model's code in Keras. Then, you will have to read the values of your variables from the checkpoint file and assign it to Keras model using layer.load_weights(weights) method.

除了这种方法之外,我建议您直接在Keras中进行培训,因为它声称 tf.contrib.keras 在Tensorflow中编写代码模块并直接将文件保存为hdf5格式.

More than this methodology, I would suggest to you to do the training directly in Keras as it claimed that Keras' optimizers are 5-10% times faster than Tensorflow's optimizers. Other way is to write your code in Tensorflow with tf.contrib.keras module and save the file directly in hdf5 format.

这篇关于如何将训练有素的Tensorflow模型转换为Keras?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆