前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tensorflow 2.0模式下训练的模型转成 tf1.x 版本的pb模型实例

tensorflow 2.0模式下训练的模型转成 tf1.x 版本的pb模型实例

作者头像
砸漏
发布2020-10-21 16:17:29
2.3K0
发布2020-10-21 16:17:29
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

升级到tf 2.0后, 训练的模型想转成1.x版本的.pb模型, 但之前提供的通过ckpt转pb模型的方法都不可用(因为保存的ckpt不再有.meta)文件, 尝试了好久, 终于找到了一个方法可以迂回转到1.x版本的pb模型.

Note: 本方法首先有些要求需要满足:

可以拿的到模型的网络结构定义源码

网络结构里面的所有操作都是通过tf.keras完成的, 不能出现类似tf.nn 的tensorflow自己的操作符

tf2.0下保存的模型是.h5格式的,并且仅保存了weights, 即通过model.save_weights保存的模型.

在tf1.x的环境下, 将tf2.0保存的weights转为pb模型:

如果在tf2.0下保存的模型符合上述的三个定义, 那么这个.h5文件在1.x环境下其实是可以直接用的, 因为都是通过tf.keras高级封装了,2.0版本和1.x版本不存在特别大的区别,我自己的模型是可以直接用的.

代码语言:javascript
复制
import tensorflow as tf
import os
from nets.efficientNet import *
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
# 这个代码网上说需要加上, 如果模型里有dropout , bn层的话, 我测试过加不加结果都一样, 保险起见还是加上吧
tf.keras.backend.set_learning_phase(0)

# 首先是定义你的模型, 这个需要和tf2.0下一毛一样
inputs = tf.keras.Input(shape=(224, 224, 3), name='modelInput')
outputs = yourModel(inputs, training=False)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
model.load_weights('save_weights.h5')
def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
  """
  Freezes the state of a session into a pruned computation graph.

  Creates a new computation graph where variable nodes are replaced by
  constants taking their current value in the session. The new graph will be
  pruned so subgraphs that are not necessary to compute the requested
  outputs are removed.
  @param session The TensorFlow session to be frozen.
  @param keep_var_names A list of variable names that should not be frozen,
             or None to freeze all the variables in the graph.
  @param output_names Names of the relevant graph outputs.
  @param clear_devices Remove the device directives from the graph for better portability.
  @return The frozen graph definition.
  """
  from tensorflow.python.framework.graph_util import convert_variables_to_constants
  graph = session.graph
  with graph.as_default():
    freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
    output_names = output_names or []
    output_names += [v.op.name for v in tf.global_variables()]
    # Graph -  GraphDef ProtoBuf
    input_graph_def = graph.as_graph_def(add_shapes=True)
    if clear_devices:
      for node in input_graph_def.node:
        node.device = ""
    frozen_graph = convert_variables_to_constants(session, input_graph_def,
                           output_names, freeze_var_names)
    return frozen_graph

frozen_graph = freeze_session(tf.keras.backend.get_session(), output_names=[out.op.name for out in model.outputs])
tf.train.write_graph(frozen_graph, "model", "tf_model.pb", as_text=False)

运行成功后, 会在当前目录下生成一个model文件夹, 里面有生成的tf_model.pb文件, 至此, 我们就完成了将tf2.0下训练的模型转到tf1.x下的pb模型, 这样,就可以用这个pb模型做其它推理或者转tvm ncnn等模型转换工作.

这个转换的重点就是通过keras这个中间商来完成, 所以我们定义的模型就必须要满足这个中间商定义的条件

补充知识:tensorflow2.0降级及如何从别的版本升到2.0

代码实践《tensorflow实战GOOGLE深度学习框架》时,由于本机安装的tensorflow为2.0版本与配套书籍代码1.4的API不兼容,只得将tensorflow降级为1.4.0版本使用,降级方法如下

1 pip uninstall tensorflow

2 pip install tensorflow==1.14.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

验证

import tensorflow as tf print(tf.version)

二 从别的版本升级到2.0

自动卸载与其相关包

pip uninstall tensorflow

安装某版本

pip install –no-cache-dir tensorflow==x.xx (此处填写2.0)

验证

以上这篇tensorflow 2.0模式下训练的模型转成 tf1.x 版本的pb模型实例就是小编分享给大家的全部内容了,希望能给大家一个参考。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档