这篇文章主要介绍了tensorflow 2.0模式下训练的模型转成 tf1.x 版本的pb模型实例,具有很好的参考价值,希望对大家有所帮助。一起跟随想过来看看吧
升级到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版本不存在特别大的区别,我自己的模型是可以直接用的.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
运行成功后, 会在当前目录下生成一个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模型实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。