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

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

问题描述

我有一个经过训练的 Tensorflow 模型和权重向量,它们已分别导出到 protobuf 和权重文件.

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 中进行培训,因为它声称 Keras 的优化器比 Tensorflow 的优化器快 5-10%.另一种方法是使用 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天全站免登陆