How to save Tensorflow 2 Object Detection Model including all weights?(如何保存包含所有权重的TensorFlow 2目标检测模型?)
本文介绍了如何保存包含所有权重的TensorFlow 2目标检测模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用PYTHON中的TensorFlow 2API进行对象检测。到目前为止,这个方法运行得很好。然而,如果我想保存模型,我使用的是exporter_main_v2.py,它导出一个图形(.pb)和一个检查点(checkpoint,ckpt-0.data,ckpt-0.index))。图表不不包括任何权重,我必须始终使用检查点来处理保存的模型。
是否有办法将所有权重保存到Protobuf(.pb)文件中?
以下是我尝试过的内容:
- 保存冻结模型:TF2显然不再支持冻结图形。将冻结包含所有权重的图形的
export_inference_graph.py在TF2下不起作用。 - 与
freeze_graph.py相同:只能使用TF1
推荐答案
您仍然可以使用tf2中的冻结技术,使用compat.v1模块:
在下面的代码片段中,我假设您有一个预先训练好的模型,其权重以TF2方式保存,tf.saved_model.save。
graph = tf.Graph()
with graph.as_default():
sess = tf.compat.v1.Session()
with sess.as_default():
# creating the model/loading it from a TF2 pb file
# (If you have a keras model, you can use
#`tf.keras.models.load_model` instead).
model = tf.saved_model.load("/path/to/model")
# the default signature might be different.
sign = model.signatures["serving_default"]
# if using keras, just use model.outputs
tensor_out_names = [out.name.split(":")[0] for out in sign.outputs]
graphdef = tf.compat.v1.graph_util.convert_variables_to_constants(
sess, graph.as_graph_def(), tensor_out_names
)
# the following is optional, use only if no more training is required
graphdef = tf.compat.v1.graph_util.remove_training_nodes(graphdef)
tf.python.framework.graph_io.write_graph(graphdef, "./", "/path/to/frozengraph", as_text=False)
但是,除非是出于与旧工具的兼容性原因,否则我不会这么做。compat模块可能有一天会被弃用,据我所知,只有一个文件包含图形+权重,而不是拆分它们,不会有很大的值。
这篇关于如何保存包含所有权重的TensorFlow 2目标检测模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:如何保存包含所有权重的TensorFlow 2目标检测模型?
基础教程推荐
猜你喜欢
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
